-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdomain_audit.ps1
4921 lines (4063 loc) · 184 KB
/
domain_audit.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
<#
Author Jony Schats - 0xjs
#>
# PLEASE EDIT THESE VARIABLES BEFORE YOU RUN!
$script:PowerView_Path = "$PSScriptRoot\import\PowerView.ps1"
$script:Powerupsql_Path = "$PSScriptRoot\import\PowerUpSQL.ps1"
$script:PowerMad_Path = "$PSScriptRoot\import\Powermad.ps1"
$script:BloodHound_Path = "$PSScriptRoot\import\Sharphound.ps1"
$script:Portscan_Path = "$PSScriptRoot\import\Invoke-Portscan.ps1"
$script:Impacket_Path = "$PSScriptRoot\import\impacket"
$script:GpRegisteryPolicy_Path = "$PSScriptRoot\import\GPRegistryPolicy\GPRegistryPolicy.psd1"
$script:CME_Path = "$PSScriptRoot\import\cme"
$script:LdapRelayScan_Path = "$PSScriptRoot\import\LdapRelayScan\LdapRelayScan.py"
# Variables
$script:CredentialStatus = ''
$script:OutputDirectory_Path = ''
$script:Findings_Path = ''
$script:Data_Path = ''
$script:Checks_Path = ''
$script:OutputDirectoryCreated = ''
$script:Creds = ''
$script:DefaultGroupNamesCreated = ''
# Check and Import dependancies
if (-not(Test-Path -Path $PowerView_Path)) {
Write-Host -ForegroundColor Red "$PowerView_Path Not found on the system"
Write-Host -ForegroundColor Red "Exiting script most functions use PowerView"
break
}
else {
Import-Module -Force -Name $PowerView_Path -WarningAction silentlycontinue
if (-not(Get-Command Get-Domainuser -Erroraction silentlycontinue)) {
Write-Host -ForegroundColor Red "Powerview didn't import correctly"
break
}
}
if (-not(Test-Path -Path $BloodHound_Path)) {
Write-Host -ForegroundColor Red "$BloodHound_Path doesn't exist. Please check the file and path variables in the script."
Write-Host -ForegroundColor Red "Won't be able to collect BloodHound data"
Write-Host " "
}
else {
Import-Module -Force -Name $BloodHound_Path -WarningAction silentlycontinue
if (-not(Get-Command Invoke-BloodHound -Erroraction silentlycontinue)) {
Write-Host -ForegroundColor Red "Bloodhound didn't import correctly"
}
}
if (-not(Test-Path -Path $GpRegisteryPolicy_Path)) {
Write-Host -ForegroundColor Red "$GpRegisteryPolicy_Path doesn't exist. Please check the file and path variables in the script."
Write-Host -ForegroundColor Red "Won't be able to parse LAPS policy"
Write-Host " "
}
else {
Import-Module -Force -Name $GpRegisteryPolicy_Path -WarningAction silentlycontinue
if (-not(Get-Command Parse-PolFile -Erroraction silentlycontinue)) {
Write-Host -ForegroundColor Red "GPRegistryPolicy didn't import correctly"
}
}
if (-not(Test-Path -Path $PowerMad_Path)) {
Write-Host -ForegroundColor Red "$PowerMad_Path doesn't exist. Please check the file and path variables in the script."
Write-Host -ForegroundColor Red "Won't be able to check ADIDNS"
Write-Host " "
}
else {
Import-Module -Force -Name $PowerMad_Path -WarningAction silentlycontinue
if (-not(Get-Command Get-ADIDNSPermission -Erroraction silentlycontinue)) {
Write-Host -ForegroundColor Red "PowerMad didn't import correctly"
}
}
if (-not(Test-Path -Path $Portscan_Path)) {
Write-Host -ForegroundColor Red "$Portscan_Path doesn't exist. Please check the file and path variables in the script."
Write-Host -ForegroundColor Red "Won't be able to scan for open ports and enumerate further"
Write-Host " "
}
else {
Import-Module -Force -Name $Portscan_Path -WarningAction silentlycontinue
if (-not(Get-Command Invoke-Portscan -Erroraction silentlycontinue)) {
Write-Host -ForegroundColor Red "PowerMad didn't import correctly"
}
}
if (-not(Test-Path -Path $Impacket_Path\examples\GetUserSPNs.py)) {
Write-Host -ForegroundColor Red "$Impacket_Path\examples\GetUserSPNs.py doesn't exist. Please check installation."
Write-Host -ForegroundColor Red "Won't be able to parse Kerberoast, AS-REPRoast or check for the printspooler service"
Write-Host " "
}
if (-not(Test-Path -Path $LdapRelayScan_Path)) {
Write-Host -ForegroundColor Red "$LdapRelayScan_Path doesn't exist. Please check installation."
Write-Host -ForegroundColor Red "Won't be able to check for LDAPS signing and binding"
Write-Host " "
}
$CheckPython = (python -V)
if (-not($CheckPython -Match "Python")) {
Write-Host -ForegroundColor Red "Python doesn't exist. Please check installation."
Write-Host -ForegroundColor Red "Won't be able to do any of the SMB or share checks"
}
if (-not(Test-Path -Path $CME_Path)) {
Write-Host -ForegroundColor Red "$CME_Path doesn't exist."
Write-Host -ForegroundColor Red "Won't be able to do any of the SMB or share checks"
}
# Check for sysvol and netlogon keys hardened unc paths
$HardenedUncRegKeys = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\NetworkProvider\HardenedPaths
if (-not($HardenedUncRegKeys -match "\\\\\*\\NETLOGON")){
Write-Host -ForegroundColor Red "Hardened UNC Path NetLogon not allowed, run the following command:"
Write-Host -ForegroundColor Red 'reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\NetworkProvider\HardenedPaths /v "\\*\NETLOGON" /d "RequireMutualAuthentication=0" /t REG_SZ /f'
Write-Host " "
}
if (-not($HardenedUncRegKeys -match "\\\\\*\\SYSVOL")){
Write-Host -ForegroundColor Red "Hardened UNC Path NetLogon not allowed, run the following command:"
Write-Host -ForegroundColor Red 'reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\NetworkProvider\HardenedPaths /v "\\*\SYSVOL" /d "RequireMutualAuthentication=0" /t REG_SZ /f'
Write-Host " "
}
Function Invoke-ADCheckAll {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: All
Optional Dependencies: None
.DESCRIPTION
Runs all domain audit checks
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.PARAMETER OutputDirectory
Specifies the path to use for the output directory, defaults to the current directory.
.PARAMETER SkipBloodHound
If specified skips the BloodHound enumeration
.PARAMETER SkipRoasting
If specified skips the kerberoasting and AS-REP roasting with Impacket.
.PARAMETER SkipEmptyPasswordGuess
If specified skips authenticating with a empty password for the users with PASSWD_NOTREQD attribute.
.EXAMPLE
Invoke-ADCheckAll -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!'
Start ADChecks with all modules
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter a IP of a domain controller here, e.g. 10.0.0.1")]
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory=$true,HelpMessage="Enter the username to connect with")]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true,HelpMessage="Enter the password of the user")]
[ValidateNotNullOrEmpty()]
[string]$Password,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$OutputDirectory,
[Parameter(Mandatory = $false)]
[Switch]
$SkipBloodHound,
[Parameter(Mandatory = $false)]
[Switch]
$SkipRoasting,
[Parameter(Mandatory = $false)]
[Switch]
$SkipEmptyPasswordGuess
)
Write-Verbose "[++] Executing Invoke-ChangeDNS"
Invoke-ChangeDNS -Domain $Domain -Server $Server
Write-Verbose "[++] Executing Test-ADAuthentication"
Test-ADAuthentication -Domain $Domain -Server $Server -User $User -Password $Password | Out-Null
if ($CredentialStatus -eq $false) {
Write-Host -ForegroundColor Red "[-] Exiting, please provide a valid set of credentials"
Invoke-EmptyDNS
break
}
if ($User -ne $Creds.Username) {
Create-CredentialObject -User $User -Password $Password -Domain $Domain
if ($PSBoundParameters['OutputDirectory']) {
New-OutputDirectory -Domain $Domain -OutputDirectory $OutputDirectory
}
else {
New-OutputDirectory -Domain $Domain
}
Write-Host " "
Invoke-WriteExplanation
if ($SkipBloodHound){
Invoke-ADEnum -Domain $Domain -Server $Server -User $User -Password $Password -SkipBloodHound
}
else {
Invoke-ADEnum -Domain $Domain -Server $Server -User $User -Password $Password
}
Invoke-ADEnumTrust -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADEnumAzure -Domain $Domain -Server $Server -User $User -Password $Password
Write-Host "---------- EXECUTING CHECKS ----------"
Write-Host "[+] Executing in another window because runas is required"
Write-Host -ForegroundColor Yellow "[+] Please manually supply the Password $Password"
"--- Running SQL checks in new window ---"
runas /noprofile /env /netonly /user:$Domain\$User "powershell.exe -Exec bypass -NoExit Import-Module $PSCommandPath; Set-Variable Findings_Path -Value $OutputDirectory_Path\findings; Set-Variable Data_Path -Value $OutputDirectory_Path\data; Set-Variable Checks_Path -Value $OutputDirectory_Path\checks; Set-Variable OutputDirectoryCreated -Value $OutputDirectoryCreated; Invoke-ADCheckSQL -Domain $Domain -Server $Server -User $User -Password '$Password' -SkipPrompt"
Write-Host " "
Invoke-ADCheckDomainFunctionalLevel -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPasspol -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckFineGrainedPasswordPolicy -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPasspolKerberos -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckLAPS -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckDescription -Domain $Domain -Server $Server -User $User -Password $Password
if ($SkipRoasting){
Invoke-ADCheckRoasting -Domain $Domain -Server $Server -User $User -Password $Password -SkipRoasting
}
else {
Invoke-ADCheckRoasting -Domain $Domain -Server $Server -User $User -Password $Password
}
Invoke-ADCheckDelegation -Domain $Domain -Server $Server -User $User -Password $Password
if ($SkipEmptyPasswordGuess){
Invoke-ADCheckUserAttributes -Domain $Domain -Server $Server -User $User -Password $Password -SkipEmptyPasswordGuess
}
else {
Invoke-ADCheckUserAttributes -Domain $Domain -Server $Server -User $User -Password $Password
}
Invoke-ADCheckOutdatedComputers -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckInactiveObjects -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPrivilegedObjects -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckDomainJoin -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckADIDNS -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPreWindows2000Group -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPre-Windows2000Computers -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckPrintspoolerDC -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckLDAP -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckExchange -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckCS -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckSysvolPassword -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckNetlogonPassword -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADGetIPInfo -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADGetPortInfo -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckSMB -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckWebclient -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-ADCheckAccess -Domain $Domain -Server $Server -User $User -Password $Password
Invoke-EmptyDNS
}
}
Function Create-CredentialObject {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Creates a credential object using the username and password supplied.
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.EXAMPLE
Create-CredentialObject -User '0xjs' -Password 'Password01!' -Domain Contoso.com
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter the username to connect with")]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true,HelpMessage="Enter the password of the user")]
[ValidateNotNullOrEmpty()]
[string]$Password
)
$Domain_User = $Domain + "\" + $User
Write-Verbose "[+] Function Create-CredentialObject"
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$script:Creds = New-Object System.Management.Automation.PSCredential($Domain_User, $SecurePassword)
Write-Verbose "[+] Created credential object with username $User"
}
Function Invoke-ChangeDNS {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Checks if powershell is running as admin and then it changes the DNS for each interface to the one of the domain controller because some checks will fail from a non-domain joined machine perspective if the DC isn't set as the DNS server. It will also add the domain name to the host file, since its required for some checks (impacket).
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER Domain
Specifies the domain to place with the Server in the hosts file e.g. contoso.com.
.EXAMPLE
Invoke-ChangeDNS -Server '10.0.0.1' -Domain contoso.com
Change DNS Server to 10.0.0.1 and write 10.0.0.1 contoso.com to the hosts file
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter a IP of a domain controller here, e.g. 10.0.0.1")]
[ValidateNotNullOrEmpty()]
[string]$Server
)
Write-Verbose "[+] Function Invoke-ChangeDNS"
#Check if running as administrator and if yes then change dns and hostfile!
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)){
Write-Host "[+] Running as administrator, changing DNS to $Server and adding $Server $Domain to host file "
Write-Verbose "[+] Running as administrator"
# Set DNS for adapter
Write-Verbose "[+] Changing DNS for each adapter to DC IP $Server"
$Array = Get-DnsClientServerAddress
foreach($element in $Array)
{
Set-DnsClientServerAddress -InterfaceIndex $($element).InterfaceIndex -ServerAddresses $Server -ErrorAction silentlycontinue | Out-Null
}
#Change host file
$hostfile_path = "C:\Windows\System32\drivers\etc\hosts"
$content_hostfile = Get-Content $hostfile_path
$hostline = "$Server $Domain"
if ($content_hostfile -match "$hostline"){
Write-Verbose "[+] $Domain is already in \etc\hosts"
}
else {
Write-Verbose "[+] Writing Domainname $Domain and DC IP $Server to $hostfile_path"
Add-Content -Path $hostfile_path -Value "`r`n$hostline"
}
}
else {
Write-Host -ForegroundColor Red "[-] Not running as administrator, please manually set hostfile for the domainname and DNS to the DC"
$confirmation = Read-Host "Did you set the entry in hostfile and changed DN? y/n"
if ($confirmation -eq 'n') {
exit
}
}
}
Function Invoke-EmptyDNS {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Checks if powershell is running as admin and then reset the DNS for each interface.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER Domain
Specifies the domain to place with the Server in the hosts file e.g. contoso.com.
.EXAMPLE
Invoke-EmptyDNS
#>
#Parameters
[CmdletBinding()]
Param()
Write-Verbose "[+] Function Invoke-EmptyDNS"
#Check if running as administrator and if yes then change dns and hostfile!
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)){
Write-Host "[+] Running as administrator, Clearing DNS"
Write-Verbose "[+] Running as administrator"
# Set DNS for adapter
Write-Verbose "[+] Clearing DNS for each adapter"
$interfaces = Get-DnsClientServerAddress
foreach($interface in $interfaces){
Set-DnsClientServerAddress -InterfaceIndex $interface.InterfaceIndex -ResetServerAddresses
}
}
}
Function New-OutputDirectory {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Creates a output directory with the Domainname-Date format and subdirectores for the tool output. Either in the current directory or specified directories.
.PARAMETER Domain
Specifies the domain name to use for the directory name.
.PARAMETER OutputDirectory
Specifies the path to use for the output directory, defaults to the current directory.
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$OutputDirectory
)
Write-Verbose "[+] Function New-OutputDirectory"
#Create a folder with the current date to save data
$date = (get-date).ToString('yyyy-MM-dd')
if ($PSBoundParameters['OutputDirectory']) {
Write-Verbose "[+] Setting directory path to $OutputDirectory$domain-$date"
$script:OutputDirectory_Path = "$OutputDirectory$domain-$date"
}
else {
$OutputDirectory = (get-location)
Write-Verbose "[+] Defaulting to $OutputDirectory\$domain-$date"
$script:OutputDirectory_Path = "$OutputDirectory\$domain-$date"
}
$script:Findings_Path = "$OutputDirectory_Path\findings"
$script:Data_Path = "$OutputDirectory_Path\data"
$script:Checks_Path = "$OutputDirectory_Path\checks"
if (Test-Path -Path $OutputDirectory_Path) {
}
else {
New-Item -ItemType Directory -Path "$OutputDirectory_Path" -ErrorAction SilentlyContinue | Out-Null
Write-Verbose "[+] Created directory $OutputDirectory_Path"
}
if (Test-Path -Path $Checks_Path) {
}
else {
New-Item -ItemType Directory -Path "$Checks_Path" -ErrorAction SilentlyContinue | Out-Null
Write-Verbose "[+] Created subdirectory $Checks_Path"
}
if (Test-Path -Path $Data_Path) {
}
else {
New-Item -ItemType Directory -Path "$Data_Path" -ErrorAction SilentlyContinue | Out-Null
Write-Verbose "[+] Created subdirectory $Data_Path"
}
if (Test-Path -Path $Findings_Path) {
}
else {
New-Item -ItemType Directory -Path "$Findings_Path" -ErrorAction SilentlyContinue | Out-Null
Write-Verbose "[+] Created subdirectory $Findings_Path"
}
$script:OutputDirectoryCreated = $true
Write-Host "[+] Output will be written in $OutputDirectory_Path"
}
Function Test-ADAuthentication {
<#
.SYNOPSIS
Author: itpro-tips.com
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Tests a set of credentials against the DC.
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.EXAMPLE
Test-ADAuthentication -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!'
[+] AD Authentication for contoso.com\0xjs succeeded!
.EXAMPLE
Test-ADAuthentication -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password ''
[-] AD Authentication for contoso.com\0xjs failed
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter a IP of a domain controller here, e.g. 10.0.0.1")]
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory=$true,HelpMessage="Enter the username to connect with")]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true,HelpMessage="Enter the password of the user")]
[ValidateNotNullOrEmpty()]
[string]$Password
)
Write-Verbose "[+] Function Test-ADAuthentication"
Write-Verbose "[+] Testing credentials $Domain\$User and $Password against $Server"
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$contextType = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$argumentList = New-Object -TypeName "System.Collections.ArrayList"
$null = $argumentList.Add($contextType)
$null = $argumentList.Add($Domain)
if($null -ne $Server){
$argumentList.Add($Server) | Out-Null
}
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $argumentList -ErrorAction SilentlyContinue
if ($null -eq $principalContext) {
Write-Verbose "[+] Failed authentication to $Server for $Domain\$User"
Write-Host -ForegroundColor Red "[-] AD Authentication for $Domain\$User failed"
$script:CredentialStatus = $false
}
if ($principalContext.ValidateCredentials($User, $Password)) {
Write-Host -ForegroundColor Green "[+] AD Authentication for $Domain\$User succeeded!"
$script:CredentialStatus = $true
}
else {
Write-Verbose "[+] Failed authentication to $Server for $Domain\$User"
Write-Host -ForegroundColor Red "[-] AD Authentication for $Domain\$User failed"
$script:CredentialStatus = $false
}
}
Function Invoke-ADGetGroupNames {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Calculate the default SIDS used for queries since group names are different across languages.
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.PARAMETER OutputDirectory
Specifies the path to use for the output directory, defaults to the current directory.
.EXAMPLE
Invoke-ADCheckPrivilegedObjects -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!'
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter a IP of a domain controller here, e.g. 10.0.0.1")]
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory=$true,HelpMessage="Enter the username to connect with")]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true,HelpMessage="Enter the password of the user")]
[ValidateNotNullOrEmpty()]
[string]$Password,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$OutputDirectory
)
if ($OutputDirectoryCreated -ne $true) {
if ($PSBoundParameters['OutputDirectory']) {
New-OutputDirectory -Domain $Domain -OutputDirectory $OutputDirectory
}
else {
New-OutputDirectory -Domain $Domain
}
}
if ($User -ne $Creds.Username) {
Create-CredentialObject -User $User -Password $Password -Domain $Domain
}
# Get domain sid and create domain admin and enterprise admin sid
# https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/81d92bba-d22b-4a8c-908a-554ab29148ab
$script:DomainSid = Get-DomainSID -Domain $Domain -Server $Server -Credential $Creds
$script:DominAdminSid = $DomainSid + '-512'
$script:DomainAdminGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $DominAdminSid).samaccountname
$script:EnterpriseAdminSid = $DomainSid + '-519'
$script:EnterpriseAdminGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $EnterpriseAdminSid).samaccountname
$script:CertPublisherSid = $DomainSid + '-517'
$script:CertPublisherGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $CertPublisherSid).samaccountname
$script:SchemaAdminsSid = $DomainSid + '-518'
$script:SchemaAdminsGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $SchemaAdminsSid).samaccountname
$script:ProtectedUsersSid = $DomainSid + '-525'
$script:ProtectedUsersGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $ProtectedUsersSid).samaccountname
$script:GroupPolicyCreatorOwnersSid = $DomainSid + '-520'
$script:GroupPolicyCreatorOwnersGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $GroupPolicyCreatorOwnersSid).samaccountname
$script:KeyAdminsSid = $DomainSid + '-526'
$script:KeyAdminsGroupName = (Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds $KeyAdminsSid).samaccountname
$script:AccountOperatorsSid = "S-1-5-32-548"
$script:BackopOperatorSid = "S-1-5-32-551"
$script:PrintOperatorSid = "S-1-5-32-550"
$script:RemoteManagementUsersSid = "S-1-5-32-580"
$script:HyperVAdminsSid = "S-1-5-32-578"
$script:AdministratorsSid = "S-1-5-32-544"
$script:DefaultGroupNamesCreated = $true
}
Function Invoke-WriteExplanation {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Writes the explanation of how the data structure and the output of the tool works.
#>
Write-Host "---------- DATA EXPLAINED ----------"
Write-Host "- All data is written to $OutputDirectory_Path\"
Write-Host "- In this folder are three subfolders"
Write-Host "- files in \findings\ are findings that should be reported"
Write-Host "- files in \checks\ needs to be checked"
Write-Host "- files in \data\ is raw data"
Write-Host " "
Write-Host "---------- COLORS EXPLAINED ----------"
Write-Host "White is informational text"
Write-Host -ForegroundColor DarkGreen "Green means check has passed"
Write-Host -ForegroundColor Yellow "Yellow means manually check the data"
Write-Host -ForegroundColor Red "Red means finding"
Write-Host " "
}
Function Invoke-ADEnum {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Enumerates basic Active Directory stuff like users, groups, computers etc. and saves usefull info in CSV and .txt formats.
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.PARAMETER OutputDirectory
Specifies the path to use for the output directory, defaults to the current directory.
.PARAMETER SkipBloodHound
If specified skips the BloodHound enumeration
.EXAMPLE
Invoke-ADEnum -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!'
Execute all enumeration steps
.EXAMPLE
Invoke-ADEnum -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!' -OutputDirectory C:\temp\
Execute all basic enumeration steps and save output in C:\temp\
.EXAMPLE
Invoke-ADEnum -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!' -SkipBloodHound
Execute all basic enumeration steps but skip BloudHound
#>
#Parameters
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage="Enter a domain name here, e.g. contoso.com")]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory=$true,HelpMessage="Enter a IP of a domain controller here, e.g. 10.0.0.1")]
[ValidateNotNullOrEmpty()]
[string]$Server,
[Parameter(Mandatory=$true,HelpMessage="Enter the username to connect with")]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true,HelpMessage="Enter the password of the user")]
[ValidateNotNullOrEmpty()]
[string]$Password,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$OutputDirectory,
[Parameter(Mandatory = $false)]
[Switch]
$SkipBloodHound
)
if ($OutputDirectoryCreated -ne $true) {
if ($PSBoundParameters['OutputDirectory']) {
New-OutputDirectory -Domain $Domain -OutputDirectory $OutputDirectory
}
else {
New-OutputDirectory -Domain $Domain
}
}
if ($User -ne $Creds.Username) {
Create-CredentialObject -User $User -Password $Password -Domain $Domain
}
if ($DefaultGroupNamesCreated -ne $true) {
Invoke-ADGetGroupNames -User $User -Password $Password -Domain $Domain -Server $Server
}
Write-Host "---------- GATHERING DATA ----------"
Write-Host "[+] Gathering data of all Users, Groups, Computerobject, GPO's, OU's, DC's and saving it to csv"
if (-Not $PSBoundParameters['SkipBloodHound']) {
Write-Host "[+] Gathering BloodHound data all, session and ACL in seperate PowerShell session in background"
Invoke-Expression "cmd /c start powershell -WindowStyle hidden -Command {Import-Module $script:BloodHound_Path; Invoke-BloodHound -CollectionMethod all -Domain $Domain -DomainController $Server -LdapUsername $User -LdapPassword '$Password' -OutputDirectory $Data_Path; Invoke-BloodHound -CollectionMethod session -Domain $Domain -DomainController $Server -LdapUsername $User -LdapPassword '$Password' -OutputDirectory $Data_Path; Invoke-BloodHound -CollectionMethod acl -Domain $Domain -DomainController $Server -LdapUsername $User -LdapPassword '$Password' -OutputDirectory $Data_Path}"
}
Write-Verbose "[+] Gathering data of domain object"
$DomainData = Get-Domain -Domain $Domain -Credential $Creds
Write-Verbose "[+] Gathering data of all Users"
Get-DomainUser -Domain $Domain -Server $Server -Credential $Creds | Select-Object samaccountname, description, mail, serviceprincipalname, msds-allowedtodelegateto, useraccountcontrol, lastlogon, pwdlastset | Export-Csv $Data_Path\data_users.csv
Write-Verbose "[+] Gathering data of all Groups"
Get-DomainGroup -Domain $Domain -Server $Server -Credential $Creds | Export-Csv $Data_Path\data_groups.csv
Write-Verbose "[+] Gathering data of all Computerobjects"
Get-DomainComputer -Domain $Domain -Server $Server -Credential $Creds | Export-Csv $Data_Path\data_computers.csv
Write-Verbose "[+] Gathering data of all GPO's"
Get-DomainGPO -Domain $Domain -Server $Server -Credential $Creds | Export-Csv $Data_Path\data_gpo.csv
Write-Verbose "[+] Gathering data of all OU's"
Get-DomainOU -Domain $Domain -Server $Server -Credential $Creds | Export-Csv $Data_Path\data_ou.csv
Write-Verbose "[+] Gathering data of all domain controllers"
Get-DomainController -Domain $Domain -Server $Server -Credential $Creds | Export-Csv $Data_Path\data_domaincontrollers.csv
#Get the amount of users, groups, computers etc
$usercount = Import-Csv $Data_Path\data_users.csv | Measure-Object | Select-Object -expand Count
$usercountenabled = Import-Csv $Data_Path\data_users.csv | Where-Object -Property useraccountcontrol -NotMatch "ACCOUNTDISABLE" | Measure-Object | Select-Object -expand Count
$groupcount = Import-Csv $Data_Path\data_groups.csv | Measure-Object | Select-Object -expand Count
$computercount = Import-Csv $Data_Path\data_computers.csv | Measure-Object | Select-Object -expand Count
$gpocount = Import-Csv $Data_Path\data_gpo.csv | Measure-Object | Select-Object -expand Count
$oucount = Import-Csv $Data_Path\data_ou.csv | Measure-Object | Select-Object -expand Count
$dccount = Import-Csv $Data_Path\data_domaincontrollers.csv | Measure-Object | Select-Object -expand Count
Write-Host " "
Write-Host "---------- BASIC ENUMERATION ----------"
Write-Host "[W] Saving a list of all users to $Data_Path\list_users.txt"
Import-Csv $Data_Path\data_users.csv | Select-Object -ExpandProperty samaccountname | Sort-Object -Property samaccountname | Out-File $Data_Path\list_users.txt
Write-Host "[W] Saving a list of all enabled users to $Data_Path\list_users_enabled.txt"
Import-Csv $Data_Path\data_users.csv | Where-Object -Property useraccountcontrol -NotMatch "ACCOUNTDISABLE" | Select-Object -ExpandProperty samaccountname | Sort-Object -Property samaccountname | Out-File $Data_Path\list_users_enabled.txt
$file = "$Data_Path\list_administrators.txt"
Write-Host "[W] Saving a list of all administrators to $file"
$data = Get-DomainGroupMember -Domain $Domain -Server $Server -Credential $Creds $DomainAdminGroupName -Recurse | Get-DomainUser -Domain $Domain -Server $Server -Credential $Creds | Select-Object samaccountname | Format-Table -Autosize
$data += Get-DomainGroupMember -Domain $Domain -Server $Server -Credential $Creds $EnterpriseAdminGroupName -Recurse | Get-DomainUser -Domain $Domain -Server $Server -Credential $Creds | Select-Object samaccountname | Format-Table -Autosize
$data += Get-DomainGroupMember -Domain $Domain -Server $Server -Credential $Creds $AdministratorsSid -Recurse | Get-DomainUser -Domain $Domain -Server $Server -Credential $Creds | Select-Object samaccountname | Format-Table -Autosize
$data | Out-File $file
$data = Get-Content $file
$data = $data | Sort-Object -Unique
$data = $data -replace 'samaccountname', '' -replace '--', '' -replace 'serviceprincipalname', '' #remove strings
$data = $data.Trim() | ? {$_.trim() -ne "" } #Remove spaces and white lines
$data = $data | Sort-Object -Unique
$data | Out-File $file
$file = "$Data_Path\list_privileged_users.txt"
Write-Host "[W] Saving a list of all privileged users to $file"
$data = Get-DomainGroup -AdminCount -Domain $Domain -Server $Server -Credential $Creds | Get-DomainGroupMember -Domain $Domain -Server $Server -Credential $Creds -Recurse -ErrorAction silentlycontinue -WarningAction silentlycontinue | Get-DomainUser -Domain $Domain -Server $Server -Credential $Creds | Select-Object samaccountname | Sort-object samaccountname -Unique
$privusercount = $data | Measure-object | Select-Object -expand Count
$data.samaccountname | Out-File $file
Write-Host "[W] Saving a list of all groups to $Data_Path\list_groups.txt"
Import-Csv $Data_Path\data_groups.csv | Select-Object samaccountname | Sort-Object -Property samaccountname | Out-File $Data_Path\list_groups.txt
Write-Host "[W] Saving a list of all computerobjects to $Data_Path\list_computers.txt"
Import-Csv $Data_Path\data_computers.csv | Select-Object dnshostname | Sort-Object -Property dnshostname | Out-File $Data_Path\list_computers.txt
Write-Host " "
# Check if the amount of admins is more then 5% of all users
$data = Get-Content $data_path\list_administrators.txt | sort-object -Unique
$admincount = $data | Measure-object | Select-Object -expand Count
$file = "$findings_path\large_amount_of_administrators.txt"
$percentage = ($admincount / $usercountenabled ) * 100
$percentage_admins = [math]::Round($percentage,2)
$thresholdpercentage = 5
# Defining domain functional levels
$DomainMode = @{
0 = "Windows 2000 native"
1 = "Windows 2003 interim"
2 = "Windows 2003"
3 = "Windows 2008"
4 = "Windows 2008 R2"
5 = "Windows 2012"
6 = "Windows 2012 R2"
7 = "Windows 2016"
8 = "TBD"
}
$DomainFunctionalLevel = $DomainMode[$DomainData.DomainModeLevel]
Write-Host "---------- DOMAIN INFORMATION ----------"
Write-Host "The domain functional level is: $DomainFunctionalLevel"
Write-Host "In the domain $Domain there are:"
Write-Host "- $usercount users and $usercountenabled enabled users"
Write-Host "- $groupcount groups"
Write-Host "- $computercount computers"
Write-Host "- $oucount OU's"
Write-Host "- $gpocount GPO's"
Write-Host "- $admincount Administrators"
Write-Host "- $privusercount Privileged users"
Write-Host "- $dccount Domain Controllers"
Write-Host " "
# Check if the amount of admins is more then 5% of all users
Write-Host "---Checking if amount of admins is more then 5% of all users---"
if ($percentage_admins -lt $thresholdpercentage){
Write-Host -ForegroundColor DarkGreen "[+] There are only $admincount administrators, which is $percentage_admins% of all users"
}
else {
$count = $data | Measure-Object | Select-Object -expand Count
Write-Host -ForegroundColor Red "[-] There are $admincount administrators, which is $percentage_admins% of all users"
Write-Host "[W] Writing to $file"
$data | Out-File $file
}
Write-Host " "
}
Function Invoke-ADCheckDomainFunctionalLevel {
<#
.SYNOPSIS
Author: Jony Schats - 0xjs
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Checks the functional level for the domain
.PARAMETER Domain
Specifies the domain to use for the query and creating outputdirectory.
.PARAMETER Server
Specifies an Active Directory server IP to bind to, e.g. 10.0.0.1
.PARAMETER User
Specifies the username to use for the query.
.PARAMETER Password
Specifies the Password in combination with the username to use for the query.
.PARAMETER OutputDirectory
Specifies the path to use for the output directory, defaults to the current directory.
.EXAMPLE
Invoke-ADCheckFunctionalLevel -Domain 'contoso.com' -Server 'dc1.contoso.com' -User '0xjs' -Password 'Password01!'
Enumerate trusts for contoso.com