I'm trying to use Chromium cookies in Python, because Chromium encrypts its cookies using AES (with CBC) I need to reverse this.
I can recover the AES key from OS X's Keychain (it's stored in Base 64):
security find-generic-password -w -a Chrome -s Chrome Safe Storage
# From Python:
python -c 'from subprocess import PIPE, Popen; print(Popen(['security', 'find-generic-password', '-w', '-a', 'Chrome', '-s', 'Chrome Safe Storage'], stdout=PIPE).stdout.read().strip())'
Here's the code I have, all I'm missing is decrypting the cookies:
from subprocess import PIPE, Popen
from sqlite3 import dbapi2
def get_encryption_key():
cmd = ['security', 'find-generic-password', '-w', '-a', 'Chrome', '-s', 'Chrome Safe Storage']
return Popen(cmd, stdout=PIPE).stdout.read().strip().decode('base-64')
def get_cookies(database):
key = get_encryption_key()
with dbapi2.connect(database) as conn:
conn.rollback()
rows = conn.cursor().execute('SELECT name, encrypted_value FROM cookies WHERE host_key like ".example.com"')
cookies = {}
for name, enc_val in rows:
val = decrypt(enc_val, key) # magic missing
cookies[name] = val
return cookies
I tried a bunch of things with pyCrypto's AES module but:
- I have no Initialization Vector (IV)
enc_val
is not a multiple of 16 in length
Here are some links that seem useful:
Can you help me figure this out?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…