Chrome 80 如何解码 cookie

Пав*_*дов 7 python cookies google-chrome aes dpapi

我有一个用于打开和解密 Google Chrome cookie 的工作脚本,如下所示:

decrypted = win32crypt.CryptUnprotectData(enctypted_cookie_value, None, None, None, 0)
Run Code Online (Sandbox Code Playgroud)

似乎在更新 80 之后它不再是一个有效的解决方案。

根据这篇博客文章https://blog.nirsoft.net/2020/02/19/tools-update-new-encryption-chrome-chromium-version-80/似乎我需要对本地状态文件中的 encrypted_key 进行 CryptUnprotectData ,而不是使用解密密钥以某种方式解密cookie。

对于第一部分,我得到了我的 encrypted_key

path = r'%LocalAppData%\Google\Chrome\User Data\Local State'
path = os.path.expandvars(path)
with open(path, 'r') as file:
    encrypted_key = json.loads(file.read())['os_crypt']['encrypted_key']
encrypted_key = bytearray(encrypted_key, 'utf-8')
Run Code Online (Sandbox Code Playgroud)

然后我试图解密它

解密密钥 = win32crypt.CryptUnprotectData(加密密钥,无,无,无,0)

并得到了例外:

pywintypes.error: (13, 'CryptProtectData', 'The data is invalid.')
Run Code Online (Sandbox Code Playgroud)

我不知道如何修复它

同样对于加密的第二部分,我似乎应该使用 pycryptodome,类似于以下代码段:

cipher = AES.new(encrypted_key, AES.MODE_GCM, nonce=nonce)
plaintext = cipher.decrypt(data)
Run Code Online (Sandbox Code Playgroud)

但我不知道我应该在哪里获得 nonce 值

有人可以解释一下,如何正确解密 Chrome cookie?

Top*_*aco 17

从 Chrome 80 及更高版本开始,cookie 在 GCM 模式下使用 AES-256 进行加密。应用的密钥使用 DPAPI 加密。此处描述详细信息,部分Chrome v80.0 及更高版本

加密的密钥以DPAPI(ie 0x4450415049)的 ASCII 编码开头,是 Base64 编码的,即密钥必须先进行 Base64 解码,并且必须删除前 5 个字节。之后可以解密win32crypt.CryptUnprotectData。解密返回一个元组,其第二个值包含解密的密钥:

import os
import json
import base64 
import win32crypt
from Crypto.Cipher import AES

path = r'%LocalAppData%\Google\Chrome\User Data\Local State'
path = os.path.expandvars(path)
with open(path, 'r') as file:
    encrypted_key = json.loads(file.read())['os_crypt']['encrypted_key']
encrypted_key = base64.b64decode(encrypted_key)                                       # Base64 decoding
encrypted_key = encrypted_key[5:]                                                     # Remove DPAPI
decrypted_key = win32crypt.CryptUnprotectData(encrypted_key, None, None, None, 0)[1]  # Decrypt key
Run Code Online (Sandbox Code Playgroud)

cookie 的加密是在GCM 模式下使用AES-256执行的。这是经过身份验证的加密,可保证机密性和真实性/完整性。在加密过程中会生成一个身份验证标签,用于在解密过程中进行完整性验证。GCM 模式基于 CTR 模式并使用 IV(随机数)。除了 32 字节的密钥外,解密还需要随机数和认证标签。

的加密数据与的ASCII编码开始v10(即0x763130),接着是12个字节的随机数,实际密文和最后的16个字节的认证标签。可以按如下方式分离各个组件:

data = bytes.fromhex('763130...') # the encrypted cookie
nonce = data[3:3+12]
ciphertext = data[3+12:-16]
tag = data[-16:]
Run Code Online (Sandbox Code Playgroud)

其中data包含加密数据。解密本身是使用PyCryptodome完成的:

cipher = AES.new(decrypted_key, AES.MODE_GCM, nonce=nonce)
plaintext = cipher.decrypt_and_verify(ciphertext, tag) # the decrypted cookie
Run Code Online (Sandbox Code Playgroud)

注意:一般来说,也有存储的cookies是用v80以下的Chrome版本保存的,因此是DPAPI加密的。DPAPI 加密的 cookie 可以通过以下事实来识别:它们以序列开头0x01000000D08C9DDF0115D1118C7A00C04FC297EB这里这里,部分关于 DPAPI。这些 cookie 当然不能像上面描述的那样解密,而是使用 DPAPI 加密 cookie 的前一个程序。以未加密或加密形式查看 cookie 的工具分别是ChromeCookiesViewDB Browser for SQLite

  • 而不是“json.loads(file.read())”,您可以简单地执行“json.load(file)” (2认同)