我正在尝试从我的Chrome浏览器获取会话。 我可以在开发人员工具中看到 2 个 cookie 文件。 但这不方便用户从浏览器获取cookie值,我想在代码中做到这一点。 所以我使用以下代码来获取 Chrome 默认配置文件 cookie sqlite DB:
string local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = @"GoogleChromeUser DataDefaultCookies";
path = Path.Combine(local, path);
接下来我创建SQLite连接和请求
var cmd = new SQLiteCommand("SELECT encrypted_value, name FROM cookies WHERE host_key = 'my_host_ip'", con);
然后我读了结果
byte[] encryptedCookie = (byte[])r.GetValue(r.GetOrdinal("encrypted_value"));
并尝试解密它:
var decodedData = ProtectedData.Unprotect(encryptedCookie, null, DataProtectionScope.CurrentUser);
var plainText = Encoding.ASCII.GetString(decodedData);
在这里我得到了例外
System.Security.Cryptography.CryptographicException
我知道我必须在启动浏览器的同一用户帐户(在同一台机器上(下解密cookie内容,并且参数DataProtectionScope.CurrentUser
用于此
我在调试器中看到 63 个字节(在encryptedCookie
数组中(,我也在 SQLite DB BLOB 字段中看到这个字节。 但是Unprotect
方法会引发System.Security.Cryptography.CryptographicException: Invalid data
错误。
我的代码在办公室的 5 台不同的 PC(Win10、Win7(上运行良好,但在我的开发人员 PC(Win10、VS2019(上不起作用。
我认为问题出在我的 Windows 设置或其他地方,而不是我的代码。 那么我做错了什么?
有趣的说明 - 我发现PowerShell脚本做同样的事情(通过Add-Type -AssemblyName System.Security
( - 获取cookie并解密它。 此脚本在 5 台办公室 PC 上也可以正常工作,但在我的 PC 上不起作用。
我的 Windows 安装是新的,我没有 AV 软件。 我们连接到同一个企业域,并且具有相同的安全设置。
UPD 1一点点体验:
- 从Chrome浏览器获取cookie值(32个字符,JSESSIONID(
- 创建一个简单的应用,使用
CurrentUser
保护范围保护此值。 现在我有一个 178 字节的数组(结果 #1( - 使用 a( https://sqliteonline.com/和 b( DataBase.Net 桌面应用程序查看 Chrome 的 cookie 数据库。 这两种方法给了我相同的结果:只有 63 字节的加密 cookie 数据(结果 #2(。我还可以使用
System.Data.SQLite
因此,结果在长度或内容上不相等 结果 #1 != 结果 #2
看起来像Chrome的cookie值受不同范围的保护(也许是管理员帐户?(,但是我在Chrome进程的任务管理器中看到我的用户帐户名称
附言我使用 .net 4.7.2
UPD 2我在铬源中找到了这种方法
bool OSCrypt::DecryptString(const std::string& ciphertext,
std::string* plaintext) {
if (!base::StartsWith(ciphertext, kEncryptionVersionPrefix,
base::CompareCase::SENSITIVE))
return DecryptStringWithDPAPI(ciphertext, plaintext);
crypto::Aead aead(crypto::Aead::AES_256_GCM);
auto key = GetEncryptionKeyInternal();
aead.Init(&key);
// Obtain the nonce.
std::string nonce =
ciphertext.substr(sizeof(kEncryptionVersionPrefix) - 1, kNonceLength);
// Strip off the versioning prefix before decrypting.
std::string raw_ciphertext =
ciphertext.substr(kNonceLength + (sizeof(kEncryptionVersionPrefix) - 1));
return aead.Open(raw_ciphertext, nonce, std::string(), plaintext);
}
所以 DPAPI 仅在 BLOB 不是以v10
个字符开头时才使用。 但我的 cookie BLOB 以v10
个字符开头,并且根据代码,使用了另一种加密算法,但我不明白为什么。
我终于想通了。 根据Chromium的消息来源,有两种方法用于解密cookie值。
- 如果 Cookie 值以
v10
个字符开头,则我们使用AES_256_GCM
- 否则,使用
DPAPI
对于第一种方法,我们需要key和nonce.key位于Google Chrome文件中,nonce位于加密的cookie值中。
我仍然不清楚 - 什么决定了使用哪种方法
对于正在寻找代码的人,我正在扩展Cerberus的答案。从Chrome 80版本开始,Cookie使用AES256-GCM算法进行加密,AES加密密钥使用DPAPI加密系统进行加密,加密密钥存储在"本地状态"文件中。
byte[] encryptedData=<data stored in cookie file>
string encKey = File.ReadAllText(localAppDataPath + @"GoogleChromeUser DataLocal State");
encKey = JObject.Parse(encKey)["os_crypt"]["encrypted_key"].ToString();
var decodedKey = System.Security.Cryptography.ProtectedData.Unprotect(Convert.FromBase64String(encKey).Skip(5).ToArray(), null, System.Security.Cryptography.DataProtectionScope.LocalMachine);
_cookie = _decryptWithKey(encryptedData, decodedKey, 3);
密钥大小为 256 位。加密的消息格式为,支付负载('v12'(+随机数(12字节(+密文
private string _decryptWithKey(byte[] message, byte[] key, int nonSecretPayloadLength)
{
const int KEY_BIT_SIZE = 256;
const int MAC_BIT_SIZE = 128;
const int NONCE_BIT_SIZE = 96;
if (key == null || key.Length != KEY_BIT_SIZE / 8)
throw new ArgumentException(String.Format("Key needs to be {0} bit!", KEY_BIT_SIZE), "key");
if (message == null || message.Length == 0)
throw new ArgumentException("Message required!", "message");
using (var cipherStream = new MemoryStream(message))
using (var cipherReader = new BinaryReader(cipherStream))
{
var nonSecretPayload = cipherReader.ReadBytes(nonSecretPayloadLength);
var nonce = cipherReader.ReadBytes(NONCE_BIT_SIZE / 8);
var cipher = new GcmBlockCipher(new AesEngine());
var parameters = new AeadParameters(new KeyParameter(key), MAC_BIT_SIZE, nonce);
cipher.Init(false, parameters);
var cipherText = cipherReader.ReadBytes(message.Length);
var plainText = new byte[cipher.GetOutputSize(cipherText.Length)];
try
{
var len = cipher.ProcessBytes(cipherText, 0, cipherText.Length, plainText, 0);
cipher.DoFinal(plainText, len);
}
catch (InvalidCipherTextException)
{
return null;
}
return Encoding.Default.GetString(plainText);
}
}
所需软件包
1( Newtonsoft JSON .net
2(充气城堡加密包