简体   繁体   English

我可以将Cookie从python导出到Chrome吗?

[英]Can I export cookies from python to Chrome?

Here's what I'm trying to do. 这就是我想要做的。 I want to read the cookies from Chrome's sqlite file, use them on python and then save them again to Chrome's sqlite file since they could have been updated while using them with python. 我想从Chrome的sqlite文件中读取cookie,在python上使用它们,然后再次将它们保存到Chrome的sqlite文件中,因为在与python一起使用时可能已经进行了更新。

This is more or less the code I have to test this, I can extract them and use them correctly but I can't manage to update them on sqlite and keep using them on the browser. 这或多或少是我必须测试的代码,我可以提取它们并正确使用它们,但是我无法在sqlite上更新它们并继续在浏览器中使用它们。

I'm not even sure if I'm using the correct types or if I'm passing the correct type to the UPDATE, should I do anything to newEncrypted before passing it to sqlite? 我什至不确定我使用的是正确的类型还是将正确的类型传递给UPDATE,在传递给sqlite之前我应该​​对newEncrypted做任何事情吗?

  • encrypted_value's type() is buffer 加密值的type()是缓冲区
  • decrypted's type() is unicode 解密的type()是unicode
  • newEncrypted's type() is str newEncrypted的type()是str

     import shutil, cookielib, sqlite3, win32crypt def decrypt(result): cookies = [] for host_key,name,value,expires_utc,encrypted_value in result: #print "Adding cookie" if value == '': print encrypted_value decrypted = win32crypt.CryptUnprotectData(encrypted_value, None, None, None, 0)[1].decode('utf-8') else: decrypted = value cookies.append(cookielib.Cookie(None, name, decrypted, '80', True, host_key, True, False, '/', True, False, (int(expires_utc)/1000000)-11644473600, False, None, None, None, False)) return cookies cookie_file = "C:/Users/Daviid/AppData/Local/Google/Chrome/User Data/Default/Cookies" conn = sqlite3.connect(cookie_file) cursor = conn.cursor() sql = 'select host_key,name,value,expires_utc,encrypted_value from cookies where host_key = ".google.com" or host_key = "www.google.com"' cursor.execute(sql) result = cursor.fetchall()[0] cookies = decrypt(result) ### # Use Cookies in urllib, Requests, whatever... # cookies get updated ### updatedCookie = cookies[0] updatedCookie.value = "NewValue" newEncrypted = win32crypt.CryptProtectData(updatedCookie.value, '', None, None, None, 0) cursor.execute("UPDATE cookies SET encrypted_value=? WHERE host_key = ? AND name = ?",(newEncrypted, updatedCookie.domain, updatedCookie.name)) cursor.close() conn.commit() conn.close() 

sqlite documentation page: [Python]: sqlite3 - DB-API 2.0 interface for SQLite databases . sqlite文档页面: [Python]:sqlite3-SQLite数据库的DB-API 2.0接口

code.py : code.py

import sys
import sqlite3
import win32crypt


COOKIE_FILE = "ChromeCookies.db"
HOST_KEYS = ".google.com", "www.google.com"


def decrypt_cookies(query_result):
    cookies = []
    for host_key, name, value, expires_utc, encrypted in query_result:
        if encrypted:
            decrypted_descr, decrypted = win32crypt.CryptUnprotectData(encrypted, None, None, None, 0)
        else:
            decrypted_descr, decrypted = u"", u""
        cookies.append((host_key, name, expires_utc, decrypted, decrypted_descr, value))
        # Idx:              0       1        2           3            4            5
    return cookies


def init_db(file_name=COOKIE_FILE):
    conn = sqlite3.connect(file_name)
    return conn, conn.cursor()


def shutdown_db(cursor, connection, commit=False):
    cursor.close()
    if commit:
        connection.commit()
    connection.close()


def get_cookies(cursor, host_keys=HOST_KEYS):
    query_tmpl = "SELECT host_key, name, value, expires_utc, encrypted_value FROM cookies WHERE host_key IN {:}"
    cursor.execute(query_tmpl.format(host_keys))
    result = cursor.fetchall()
    cookies = decrypt_cookies(result)
    return cookies


def format_long_text(text, max_len=50):
    length = len(text)
    if length <= max_len:
        format_string = "({:d})[{:s}]"
    else:
        format_string =  "({:d})[{:s}...]"
    return format_string.format(length, text[:max_len])


def print_cookie(idx, cookie_tuple, print_data=False):
    print("\nIdx: {:d}\nHost key: {:s}\nName: {:s}\nExpires: {:d}".format(idx, *(cookie_tuple[:3])))
    if print_data:
         print("Decrypted: {:s}".format(format_long_text(cookie_tuple[3])))
         print("Decrypted description: {:s}".format(format_long_text(cookie_tuple[4])))
         print("Data: {:s}".format(format_long_text(cookie_tuple[5])))


def update_cookie(cursor, cookie_tuple):
    decrypted_length = len(cookie_tuple[3])
    encrypted = win32crypt.CryptProtectData(cookie_tuple[3][:decrypted_length // 2], cookie_tuple[4], None, None, None, 0)
    cursor.execute("UPDATE cookies SET encrypted_value = ? WHERE host_key = ? AND name = ?", (buffer(encrypted), cookie_tuple[0], cookie_tuple[1]))


def main():
    conn, cursor = init_db()
    cookies = get_cookies(cursor)
    cookie_idx = 8 % len(cookies) # For simplicity's sake choose an index that references a cookie with an unique `host_key` and `name`
    for idx, cookie_tuple in enumerate(cookies):
        print_cookie(idx, cookie_tuple)
    updated_cookie = cookies[cookie_idx]
    print_cookie(cookie_idx, updated_cookie, print_data=True)
    update_cookie(cursor, updated_cookie)
    shutdown_db(cursor, conn, commit=True)

    print("\nReopening DB...\n")

    conn, cursor = init_db()
    cookies = get_cookies(cursor)
    print("Cookies: {:d}".format(len(cookies)))
    #for idx, cookie_tuple in enumerate(cookies):
    #    print_cookie(idx, cookie_tuple)
    updated_cookie = cookies[cookie_idx]
    print_cookie(cookie_idx, updated_cookie, print_data=True)
    shutdown_db(cursor, conn)


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

Notes : 注意事项

  • I split the code in functions to be more modular (reusable and parameterized) 我将函数中的代码拆分为更具模块化(可重用和参数化)
  • I don't have cookielib installed, so I removed all the parts that reference it, and using tuples to store cookies data instead (I know that dict or collections.namedtuple would have been nicer, but I didn't want to add more code, as this part won't be kept anyway) 我没有安装cookielib ,所以我删除了所有引用它的部分,而是使用元组来存储cookie数据(我知道dictcollections.namedtuple会更好,但是我不想添加更多代码,因为无论如何都不会保留此部分)
  • No error handling 无错误处理
  • Some of the functions are for printing purposes only 一些功能仅用于打印目的
  • As a test, I chose one of the cookies and I only keep the 1 st half of its (encrypted) data. 作为测试,我选择饼干之一,我只保留第1一半的(加密)的数据。 It will become invalid of course, but it's just for demo purposes only 当然,它将变为无效,但仅用于演示目的
  • The key is buffer(encrypted) (in update_cookie ), since the cookies.encrypted_value column is a BLOB 密钥是buffer(encrypted) (在update_cookie ),因为cookies.encrypted_value列是BLOB
  • It's Python2 compatible only (noticed #print "Adding cookie" in the question), because in Python3 strings are unicode (and buffer is not present) 仅与 Python2兼容(在问题中注意到#print "Adding cookie" ),因为在Python3中,字符串是unicode(并且不存在buffer

Output : 输出

 (py27x64_test) e:\\Work\\Dev\\StackOverflow\\q050886719>"e:\\Work\\Dev\\VEnvs\\py27x64_test\\Scripts\\python.exe" code.py Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 Idx: 0 Host key: .google.com Name: 1P_JAR Expires: 13176376602000000 Idx: 1 Host key: .google.com Name: AID Expires: 13194604800101431 Idx: 2 Host key: .google.com Name: APISID Expires: 13236602265411651 Idx: 3 Host key: .google.com Name: CGIC Expires: 13189118241681530 Idx: 4 Host key: .google.com Name: CGIC Expires: 13189118241681619 Idx: 5 Host key: .google.com Name: CONSENT Expires: 13791196798240053 Idx: 6 Host key: .google.com Name: GMAIL_RTT Expires: 0 Idx: 7 Host key: .google.com Name: HSID Expires: 13236602265411610 Idx: 8 Host key: .google.com Name: NID Expires: 13189336603638418 Idx: 9 Host key: .google.com Name: SAPISID Expires: 13236602265411673 Idx: 10 Host key: .google.com Name: SID Expires: 13236602265411550 Idx: 11 Host key: .google.com Name: SIDCC Expires: 13181563951325551 Idx: 12 Host key: .google.com Name: SNID Expires: 13189336552795314 Idx: 13 Host key: .google.com Name: SSID Expires: 13236602265411631 Idx: 14 Host key: .google.com Name: TAID Expires: 13174995823101361 Idx: 15 Host key: www.google.com Name: GAPS Expires: 13229834000111924 Idx: 8 Host key: .google.com Name: NID Expires: 13189336603638418 Decrypted: (254)[132=tqLTXZ4dOSPbstdv6oktg9VxbNX3LpwLKGpXzpMwnyVTis...] Decrypted description: (0)[] Data: (0)[] Reopening DB... Cookies: 16 Idx: 8 Host key: .google.com Name: NID Expires: 13189336603638418 Decrypted: (127)[132=tqLTXZ4dOSPbstdv6oktg9VxbNX3LpwLKGpXzpMwnyVTis...] Decrypted description: (0)[] Data: (0)[] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM