-
Notifications
You must be signed in to change notification settings - Fork 17
/
Invoke-ADeleginator.ps1
209 lines (178 loc) · 7.68 KB
/
Invoke-ADeleginator.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
<#
.SYNOPSIS
A companion tool that uses ADeleg to find insecure trustee and resource delegations in Active Directory.
.DESCRIPTION
ADeleginator finds insecure Active Directory delegations by
1) Running ADeleg.exe and creating a csv report
2) Reads the csv report to find common insecure delegations
3) Creates a report containing only the insecure delegations
.EXAMPLE
Invoke-ADeleginator
.EXAMPLE
Invoke-ADeleginator -PathToADeleg 'C:\Tools\ADeleg.exe'
.EXAMPLE
Invoke-ADeleginator -Server dc01.acme.com -PathToADeleg 'C:\Tools\ADeleg.exe'
#>
function Invoke-ADeleginator {
[CmdletBinding()]
Param(
$PathToADeleg,
$Server
)
function Get-CurrentUserGroups {
([ADSISearcher]"samaccountname=$($env:USERNAME)").Findone().Properties.memberof -replace '^CN=([^,]+).+$','$1'
}
# Create ADeleg csv or json report in the current directory
function Create-ADelegReport{
[CmdletBinding()]
Param(
$PathToADeleg,
$ReportName
)
if ($Server) {
try {
& $PathToADeleg --server $Server --csv "ADelegReport_$(Get-Date -Format ddMMyyyy).csv"
} catch {}
} else {
try {
& $PathToADeleg --csv "ADelegReport_$(Get-Date -Format ddMMyyyy).csv"
} catch {}
}
}
# find insecure trustee delegations
function Find-InsecureTrusteeDelegations{
[CmdletBinding()]
Param(
$ADelegReport,
$UnsafeTrustees,
$UnsafeDelegations
)
foreach ($Entry in $ADelegReport) {
if ($Entry.Trustee -match $UnsafeTrustees -and $Entry.Category -match "Allow" `
-and $Entry.Details -match $UnsafeDelegations) {
$InsecureTrusteeDelegations = [pscustomobject]@{
Trustee = $Entry.Trustee
TrusteeType = $Entry.'Trustee Type'
Resource = $Entry.Resource
Category = $Entry.Category
Delegations = $Entry.Details
}
$InsecureTrusteeDelegations
}
}
}
# find insecure resource delegations
function Find-InsecureResourceDelegations {
[CmdletBinding()]
Param(
$ADelegReport,
$UnsafeTrustees,
$Tier0Resources,
$UnsafeDelegations
)
foreach ($Entry in $ADelegReport) {
if ($Entry.Trustee -match $UnsafeTrustees -and $Entry.Resource -match $Tier0Resources `
-and $Entry.Category -match "Allow" -and $Entry.Details -match $UnsafeDelegations) {
$InsecureResourceDelegations = [pscustomobject]@{
Trustee = $Entry.Trustee
TrusteeType = $Entry.'Trustee Type'
Resource = $Entry.Resource
Category = $Entry.Category
Delegations = $Entry.Details
}
$InsecureResourceDelegations
}
}
}
$UnsafeTrustees = 'Domain Users|Authenticated Users|Everyone'
$Tier0Resources = 'Account Operators|Administrator|Administrators|AdminSDHolder|Backup Operators|Cryptographic Operators|Distributed COM Users|Domain Admins|Domain Controllers|Domain Controllers (OU)|Domain root object|DnsAdmins|Enterprise Admins|GPO linked to Tier Zero container|krbtgt|Print Operators|RODC computer object|Schema Admins|Server Operators|Users (container)'
$UnsafeDelegations = 'owns|write all properties|create child objects|delete child objects|Change the owner|add/delete delegations|delete'
$CurrentUserGroups = Get-CurrentUserGroups
if ($CurrentUserGroups -notmatch $Tier0Resources) {
$UnsafeTrustees += "|" + $CurrentUserGroups
}
$PathToADeleg = '.\ADeleg.exe'
$ReportName = "ADelegReport_$(Get-Date -Format ddMMyyyy).csv"
#ASCII!
Write-Host @"
Go, go ADeleginator!
.'|
| | _ _
| | (_X_)
| | |
``.|_.-"-._
|.-"""-.|
_;.-"""-.;_
_.-' _..-.-.._ '-._
';--.-(_o_I_o_)-.--;'
``. | | | | | | .``
``-\| | | |/-'
| | | |
| \_/ |
_.'; ._._. ;'._
_.-'``; | \ - / | ;'-.
.' : / | | | | \ '.
/ : /__ \ \___/ / __\ : ``.
/ | / '._/_\_.' \ : ``\
/ . ``---;"""""'-----`` . \
/ | |() () | \
/ /| | |\ \
/ / | |() () | \ \
| |
\ \ | ][ | | ][ | / /
\ \ ;=""====='"""'====""==; / /
|/``\ \/ |() () \/ /``\|
|_/.-'; | |``-.\_|
/ | ; : \
|__.| | |.__|
; | |
| : ;
| : |
; | |
; | ;
| : |
| | ;
| | ;
'-._ ; _.-'
``;"--.....--";``
| | | |
| | | |
| | | |
T----T T----T
_..._L____J L____J _..._
.`` "-. ``% | | %`` .-" ``.
/ \ .: :. / \
'-..___|_..=:`` ``-:=.._|___..-'
diddle by VK
____ ___ ____ _ ____ ____ _ _ _ ____ ___ ____ ____
|__| | \ |___ | |___ | __ | |\ | |__| | | | |__/
| | |__/ |___ |___ |___ |__] | | \| | | | |__| | \
by: Spencer Alessi @techspence v0.1
"@
if (Get-Item $PathToADeleg -ErrorAction SilentlyContinue) {
#continue
} else {
Write-Warning "ADeleg not found in the current directory. Download and place ADeleg.exe in the same folder as this script, then run ADeleginator again."
Write-Warning "You can download ADeleg from here: https://github.com/mtth-bfft/adeleg/releases"
break;
}
Write-Host "[i] Running ADeleg and creating $ReportName"
Create-ADelegReport -PathToADeleg $PathToADeleg -ReportName $ReportName
$ADelegReport = Import-Csv -Path $ReportName
Write-Host "[i] Checking for insecure trustee/resource delegations..."
$InsecureTrusteeDelegations = Find-InsecureTrusteeDelegations -ADelegReport $ADelegReport -UnsafeTrustees $UnsafeTrustees -UnsafeDelegations $UnsafeDelegations
$InsecureResourceDelegations = Find-InsecureResourceDelegations -ADelegReport $ADelegReport -UnsafeTrustees $UnsafeTrustees -Tier0Resources $Tier0Resources -UnsafeDelegations $UnsafeDelegations
if ($InsecureTrusteeDelegations) {
Write-Host "[!] Insecure trustee delegations found. Exporting report: ADeleg_InsecureTrusteeDelegationReport_$(Get-Date -Format ddMMyyyy).csv" -ForegroundColor Red
$InsecureTrusteeDelegations | Export-Csv ADeleg_InsecureTrusteeDelegationReport_$(Get-Date -Format ddMMyyyy).csv -NoTypeInformation
} else {
Write-Host "[+] No insecure trustee delegations found. Eureka!" -ForegroundColor Green
}
if ($InsecureResourceDelegations) {
Write-Host "[!] Insecure resource delegations found. Exporting report: ADeleg_InsecureResourceDelegationReport_$(Get-Date -Format ddMMyyyy).csv" -ForegroundColor Red
$InsecureResourceDelegations | Export-Csv ADeleg_InsecureResourceDelegationReport_$(Get-Date -Format ddMMyyyy).csv -NoTypeInformation
} else {
Write-Host "[+] No insecure resource delegations found. Eureka!" -ForegroundColor Green
}
Write-Host "`n`nThank you for using ADeleginator. Godspeed! :O)`n"
}