-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathChromiumUtils.cs
93 lines (88 loc) · 3.42 KB
/
ChromiumUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace SharpChromium
{
class ChromiumUtils
{
static byte[] DPAPI_HEADER = UTF8Encoding.UTF8.GetBytes("DPAPI");
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 static string GetBase64EncryptedKey()
{
string localStatePath = Environment.GetEnvironmentVariable("LOCALAPPDATA");
// something weird happened
if (localStatePath == "")
return "";
localStatePath = Path.Combine(localStatePath, "Google\\Chrome\\User Data\\Local State");
if (!File.Exists(localStatePath))
return "";
string localStateData = File.ReadAllText(localStatePath);
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 DPAPIChromeAlgKeyFromRaw(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;
}
}
}