Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ESC4 Remediations To Be More Flexible #140

Merged
merged 35 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7782a1c
#126 Initial commit
TrimarcJake Jul 20, 2024
0d33043
#126 Wrong function name
TrimarcJake Jul 20, 2024
62b481d
#126 fresh build with new function.
TrimarcJake Jul 20, 2024
941099e
#126 Another more fresher build
TrimarcJake Jul 20, 2024
34f6ffa
#126 We don't need no stinking Mode param
TrimarcJake Jul 20, 2024
f336ab6
#126 right variable names help
TrimarcJake Jul 20, 2024
059d9e4
#126 actually right variable names help
TrimarcJake Jul 20, 2024
9809792
#126 Trying here-strings for Fixes. Will make things easier.
TrimarcJake Jul 20, 2024
e7dceef
#126 ope forgot to build the new script
TrimarcJake Jul 20, 2024
b34d7de
#126 put here-string in the right place
TrimarcJake Jul 20, 2024
d8917d6
#126 messing with here-string
TrimarcJake Jul 20, 2024
56585a4
#126 messing with here-string
TrimarcJake Jul 20, 2024
5bf448c
#126 messing with here-string
TrimarcJake Jul 20, 2024
70f8b12
#126 lets try a fix as a scriptblock
TrimarcJake Jul 20, 2024
b7d0696
#126 lets try a fix as a scriptblock
TrimarcJake Jul 20, 2024
7b03eea
#126 lets try a fix as a scriptblock
TrimarcJake Jul 20, 2024
31a0a88
#126 Mode will be needed, actually.
TrimarcJake Jul 21, 2024
11be563
#126 need to make sure path and IdentityReference are wrapped in quotes.
TrimarcJake Jul 23, 2024
25ed464
#126 First go at re-adding enrollment.
TrimarcJake Jul 23, 2024
9bacaf1
#126 wrong logic in while loop
TrimarcJake Jul 23, 2024
0893d74
#126 beep boop
TrimarcJake Jul 23, 2024
3714314
#126 notin, not in.
TrimarcJake Jul 23, 2024
b5f8a21
#126 moving stuff into the functions that make most sense.
TrimarcJake Jul 23, 2024
569324c
#126 fresh build
TrimarcJake Jul 23, 2024
d312b3c
Merge branch '126-enhance-esc4-remediations' of https://github.com/Tr…
TrimarcJake Jul 23, 2024
6d9607b
#126 fresher build
TrimarcJake Jul 23, 2024
97d95b5
#126 Fresh build with a little clearer question.
TrimarcJake Jul 23, 2024
e9f3c26
#126 proper escaping of stuff is crucial
TrimarcJake Jul 23, 2024
14adff5
#126 Finalizing Re-add of Enroll
TrimarcJake Jul 23, 2024
d94716f
#126 Initial commit of Re-adding Enroll and Auto-Enroll
TrimarcJake Jul 23, 2024
2a585ea
#126 A little better language.
TrimarcJake Jul 23, 2024
41cb019
#126 Ready for PR!
TrimarcJake Jul 23, 2024
4c9dd79
#126 removed some debug stuff
TrimarcJake Jul 23, 2024
05e13d1
Merge branch 'testing' into 126-enhance-esc4-remediations
TrimarcJake Jul 23, 2024
fd28e2a
#126 Need to pass the Mode parameter correctly?
TrimarcJake Jul 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 166 additions & 4 deletions Invoke-Locksmith.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ function Find-ESC4 {
[Parameter(Mandatory = $true)]
$SafeUsers,
[Parameter(Mandatory = $true)]
$SafeObjectTypes
$SafeObjectTypes,
[int]$Mode
)
$ADCSObjects | ForEach-Object {
$Principal = New-Object System.Security.Principal.NTAccount($_.nTSecurityDescriptor.Owner)
Expand Down Expand Up @@ -521,6 +522,11 @@ function Find-ESC4 {
Revert = '[TODO]'
Technique = 'ESC4'
}

if ( $Mode -in @(1, 3, 4) ) {
Update-ESC4Remediation -Issue $Issue
}

$Issue
}
}
Expand Down Expand Up @@ -1495,7 +1501,8 @@ function Invoke-Scans {
# Could split Scans and PromptMe into separate parameter sets.
[Parameter()]
[ValidateSet('Auditing', 'ESC1', 'ESC2', 'ESC3', 'ESC4', 'ESC5', 'ESC6', 'ESC8', 'All', 'PromptMe')]
[array]$Scans = 'All'
[array]$Scans = 'All',
[int]$Mode
)

# Is this needed?
Expand Down Expand Up @@ -1565,7 +1572,7 @@ function Invoke-Scans {
[array]$ESC3 = Find-ESC3Condition1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
[array]$ESC3 += Find-ESC3Condition2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC4)...'
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes -Mode $Mode
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC5)...'
[array]$ESC5 = Find-ESC5 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes
Write-Host 'Identifying Certificate Authorities configured with dangerous flags (ESC6)...'
Expand Down Expand Up @@ -2184,6 +2191,161 @@ function Test-IsRSATInstalled {
$false
}
}
function Update-ESC4Remediation {
<#
.SYNOPSIS
This function asks the user a set of questions to provide the most appropriate remediation for ESC4 issues.

.DESCRIPTION


.PARAMETER Issue


.PARAMETER Mode


.OUTPUTS
This function updates ESC4 remediations customized to the user's needs.

.EXAMPLE
$Target = Get-Target
$ADCSObjects = Get-ADCSObject -Target $Target
$DangerousRights = @('GenericAll', 'WriteProperty', 'WriteOwner', 'WriteDacl')
$SafeOwners = '-512$|-519$|-544$|-18$|-517$|-500$'
$SafeUsers = '-512$|-519$|-544$|-18$|-517$|-500$|-516$|-9$|-526$|-527$|S-1-5-10'
$SafeObjectTypes = '0e10c968-78fb-11d2-90d4-00c04f79dc55|a05b8cc2-17bc-4802-a710-e7c15ab866a2'
$ESC4Issues = Find-ESC4 -ADCSObjects $ADCSObjects -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeUsers $SafeUsers -SafeObjectTypes $SafeObjectTypes
Update-ESC4Remediation -ESC4Issues $ESC4Issues
#>
[CmdletBinding()]
param(
$Issue
)

$Header = "`n[!] ESC4 Issue detected in $($Issue.Name)"
Write-Host $Header -ForegroundColor Yellow
Write-Host $('-' * $Header.Length) -ForegroundColor Yellow
Write-Host "$($Issue.IdentityReference) has $($Issue.ActiveDirectoryRights) rights on this template.`n"
Write-Host 'To provide the most appropriate remediation for this issue, Locksmith will now ask you a few questions.'

$Admin = ''
do {
$Admin = Read-Host "`nDoes $($Issue.IdentityReference) administer and/or maintain the $($Issue.Name) template? [y/n]"
} while ( ($Admin -ne 'y') -and ($Admin -ne 'n') )

if ($Admin -eq 'y') {
$Issue.Issue = "$($Issue.IdentityReference) has $($Issue.ActiveDirectoryRights) rights on this template, but this is expected."
$Issue.Fix = "No immediate remediation required."
}
elseif ($Issue.Issue -match 'GenericAll') {
$RightsToRestore = 0
while ($RightsToRestore -notin 1..5) {
[string]$Question = @"

Does $($Issue.IdentityReference) need to Enroll and/or AutoEnroll in the $($Issue.Name) template?"

1. Enroll
2. AutoEnroll
3. Both
4. Neither
5. Unsure

Enter your selection [1-5]
"@
$RightsToRestore = Read-Host $Question
}

switch ($RightsToRestore) {
1 {
$Issue.Fix = @"
`$Path = 'AD:$($Issue.DistinguishedName)'
`$ACL = Get-Acl -Path `$Path
`$IdentityReference = [System.Security.Principal.NTAccount]::New('$($Issue.IdentityReference)')
`$EnrollGuid = [System.Guid]::New('0e10c968-78fb-11d2-90d4-00c04f79dc55')
`$ExtendedRight = [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight
`$AccessType = [System.Security.AccessControl.AccessControlType]::Allow
`$InheritanceType = [System.DirectoryServices.ActiveDirectorySecurityInheritance]::All
`$NewRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$EnrollGuid, `$InheritanceType
foreach ( `$ace in `$ACL.access ) {
if ( (`$ace.IdentityReference.Value -like '$($Issue.IdentityReference)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) {
`$ACL.RemoveAccessRule(`$ace) | Out-Null
}
}
`$ACL.AddAccessRule(`$NewRule)
Set-Acl -Path `$Path -AclObject `$ACL
"@
}
2 {
$Issue.Fix = @"
`$Path = 'AD:$($Issue.DistinguishedName)'
`$ACL = Get-Acl -Path `$Path
`$IdentityReference = [System.Security.Principal.NTAccount]::New('$($Issue.IdentityReference)')
`$AutoEnrollGuid = [System.Guid]::New('a05b8cc2-17bc-4802-a710-e7c15ab866a2')
`$ExtendedRight = [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight
`$AccessType = [System.Security.AccessControl.AccessControlType]::Allow
`$InheritanceType = [System.DirectoryServices.ActiveDirectorySecurityInheritance]::All
`$AutoEnrollRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$AutoEnrollGuid, `$InheritanceType
foreach ( `$ace in `$ACL.access ) {
if ( (`$ace.IdentityReference.Value -like '$($Issue.IdentityReference)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) {
`$ACL.RemoveAccessRule(`$ace) | Out-Null
}
}
`$ACL.AddAccessRule(`$AutoEnrollRule)
Set-Acl -Path `$Path -AclObject `$ACL
"@
}
3 {
$Issue.Fix = @"
`$Path = 'AD:$($Issue.DistinguishedName)'
`$ACL = Get-Acl -Path `$Path
`$IdentityReference = [System.Security.Principal.NTAccount]::New('$($Issue.IdentityReference)')
`$EnrollGuid = [System.Guid]::New('0e10c968-78fb-11d2-90d4-00c04f79dc55')
`$AutoEnrollGuid = [System.Guid]::New('a05b8cc2-17bc-4802-a710-e7c15ab866a2')
`$ExtendedRight = [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight
`$AccessType = [System.Security.AccessControl.AccessControlType]::Allow
`$InheritanceType = [System.DirectoryServices.ActiveDirectorySecurityInheritance]::All
`$EnrollRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$EnrollGuid, `$InheritanceType
`$AutoEnrollRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$AutoEnrollGuid, `$InheritanceType
foreach ( `$ace in `$ACL.access ) {
if ( (`$ace.IdentityReference.Value -like '$($Issue.IdentityReference)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) {
`$ACL.RemoveAccessRule(`$ace) | Out-Null
}
}
`$ACL.AddAccessRule(`$EnrollRule)
`$ACL.AddAccessRule(`$AutoEnrollRule)
Set-Acl -Path `$Path -AclObject `$ACL
"@
}
4 {
break
}
5 {
$Issue.Fix = @"
`$Path = 'AD:$($Issue.DistinguishedName)'
`$ACL = Get-Acl -Path `$Path
`$IdentityReference = [System.Security.Principal.NTAccount]::New('$($Issue.IdentityReference)')
`$EnrollGuid = [System.Guid]::New('0e10c968-78fb-11d2-90d4-00c04f79dc55')
`$AutoEnrollGuid = [System.Guid]::New('a05b8cc2-17bc-4802-a710-e7c15ab866a2')
`$ExtendedRight = [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight
`$AccessType = [System.Security.AccessControl.AccessControlType]::Allow
`$InheritanceType = [System.DirectoryServices.ActiveDirectorySecurityInheritance]::All
`$EnrollRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$EnrollGuid, `$InheritanceType
`$AutoEnrollRule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule `$IdentityReference, `$ExtendedRight, `$AccessType, `$AutoEnrollGuid, `$InheritanceType
foreach ( `$ace in `$ACL.access ) {
if ( (`$ace.IdentityReference.Value -like '$($Issue.IdentityReference)' ) -and ( `$ace.ActiveDirectoryRights -notmatch '^ExtendedRight$') ) {
`$ACL.RemoveAccessRule(`$ace) | Out-Null
}
}
`$ACL.AddAccessRule(`$EnrollRule)
`$ACL.AddAccessRule(`$AutoEnrollRule)
Set-Acl -Path `$Path -AclObject `$ACL
"@
}
}
}
}

function Invoke-Locksmith {
<#
.SYNOPSIS
Expand Down Expand Up @@ -2364,7 +2526,7 @@ function Invoke-Locksmith {

if ( $Scans ) {
# If the Scans parameter was used, Invoke-Scans with the specified checks.
$Results = Invoke-Scans -Scans $Scans
$Results = Invoke-Scans -Scans $Scans -Mode $Mode
# Re-hydrate the findings arrays from the Results hash table
$AllIssues = $Results['AllIssues']
$AuditingIssues = $Results['AuditingIssues']
Expand Down
8 changes: 7 additions & 1 deletion Private/Find-ESC4.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
[Parameter(Mandatory = $true)]
$SafeUsers,
[Parameter(Mandatory = $true)]
$SafeObjectTypes
$SafeObjectTypes,
[int]$Mode
)
$ADCSObjects | ForEach-Object {
$Principal = New-Object System.Security.Principal.NTAccount($_.nTSecurityDescriptor.Owner)
Expand Down Expand Up @@ -93,6 +94,11 @@
Revert = '[TODO]'
Technique = 'ESC4'
}

if ( $Mode -in @(1,3,4) ) {
Update-ESC4Remediation -Issue $Issue
}

$Issue
}
}
Expand Down
5 changes: 3 additions & 2 deletions Private/Invoke-Scans.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function Invoke-Scans {
# Could split Scans and PromptMe into separate parameter sets.
[Parameter()]
[ValidateSet('Auditing','ESC1','ESC2','ESC3','ESC4','ESC5','ESC6','ESC8','All','PromptMe')]
[array]$Scans = 'All'
[array]$Scans = 'All',
[int]$Mode
)

# Is this needed?
Expand Down Expand Up @@ -114,7 +115,7 @@ function Invoke-Scans {
[array]$ESC3 = Find-ESC3Condition1 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
[array]$ESC3 += Find-ESC3Condition2 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC4)...'
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes
[array]$ESC4 = Find-ESC4 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes -Mode $Mode
Write-Host 'Identifying AD CS template and other objects with poor access control (ESC5)...'
[array]$ESC5 = Find-ESC5 -ADCSObjects $ADCSObjects -SafeUsers $SafeUsers -DangerousRights $DangerousRights -SafeOwners $SafeOwners -SafeObjectTypes $SafeObjectTypes
Write-Host 'Identifying Certificate Authorities configured with dangerous flags (ESC6)...'
Expand Down
Loading
Loading