濮阳杆衣贸易有限公司

主頁 > 知識庫 > 如何使用Python提取Chrome瀏覽器保存的密碼

如何使用Python提取Chrome瀏覽器保存的密碼

熱門標簽:海外網(wǎng)吧地圖標注注冊 聊城語音外呼系統(tǒng) 打電話機器人營銷 ai電銷機器人的優(yōu)勢 地圖標注自己和別人標注區(qū)別 孝感營銷電話機器人效果怎么樣 騰訊地圖標注沒法顯示 商家地圖標注海報 南陽打電話機器人

由于Chrome會將大量瀏覽數(shù)據(jù)本地保存磁盤中,在本教程中,我們將編寫 Python 代碼來提取 Windows 計算機上 Chrome 中保存的密碼。

首先,讓我們安裝所需的庫:

pip install pycryptodome pypiwin32

打開一個新的 Python 文件,并導入必要的模塊:

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

在直接進入提取 chrome密碼之前,我們需要定義一些有用的函數(shù)來幫助我們在主函數(shù)中。

def get_chrome_datetime(chromedate):
    """從chrome格式的datetime返回一個`datetime.datetime`對象
因為'chromedate'的格式是1601年1月以來的微秒數(shù)"""
    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)

    # 從Base64解碼加密密鑰
    key = base64.b64decode(local_state["os_crypt"]["encrypted_key"])
    # 刪除 DPAPI str
    key = key[5:]
    # 返回最初加密的解密密鑰
    # 使用從當前用戶的登錄憑據(jù)派生的會話密鑰
    # 官方文檔doc: http://timgolden.me.uk/pywin32-docs/win32crypt.html
    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 ""
  • get_chrome_datetime()函數(shù)負責將 chrome 日期格式轉換為人類可讀的日期時間格式。
  • get_encryption_key()函數(shù)提取并解碼用于加密密碼的AES密鑰,這"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\Local State"作為 JSON 文件存儲在路徑中
  • decrypt_password() 將加密密碼和 AES 密鑰作為參數(shù),并返回密碼的解密版本。

下面是main主要功能:

def main():
    # 獲取AES密鑰
    key = get_encryption_key()
    # 本地sqlite Chrome數(shù)據(jù)庫路徑
    db_path = os.path.join(os.environ["USERPROFILE"], "AppData", "Local","Google", "Chrome", "User Data", "default", "Login Data")
    # 將文件復制到其他位置
    # 因為如果chrome當前正在運行,數(shù)據(jù)庫將被鎖定
    filename = "ChromeData.db"
    shutil.copyfile(db_path, filename)
    # 連接數(shù)據(jù)庫
    db = sqlite3.connect(filename)
    cursor = db.cursor()
    # 登錄表中有我們需要的數(shù)據(jù)
    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:
        # 嘗試刪除復制的db文件
        os.remove(filename)
    except:
        pass

首先,我們使用之前定義的get_encryption_key()函數(shù)獲取加密密鑰,然后我們將 sqlite 數(shù)據(jù)庫(位于"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\default\Login Data"保存密碼的位置)復制到當前目錄并連接到它,這是因為原始數(shù)據(jù)庫文件將被鎖定Chrome 當前正在運行。

之后,我們對登錄表進行選擇查詢并遍歷所有登錄行,我們還解密每個密碼date_created并將date_last_used日期時間重新格式化為更易于閱讀的格式。

最后,我們打印憑據(jù)并從當前目錄中刪除數(shù)據(jù)庫副本。

讓我們調(diào)用主函數(shù),完美提取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月以來的微秒數(shù)"""
    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提取Chrome瀏覽器保存的密碼的詳細內(nèi)容,更多關于Python提取Chrome瀏覽器保存的密碼的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • python 獲取谷歌瀏覽器保存的密碼

標簽:揚州 六盤水 撫州 迪慶 楊凌 牡丹江 南寧 聊城

巨人網(wǎng)絡通訊聲明:本文標題《如何使用Python提取Chrome瀏覽器保存的密碼》,本文關鍵詞  如何,使用,Python,提取,Chrome,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何使用Python提取Chrome瀏覽器保存的密碼》相關的同類信息!
  • 本頁收集關于如何使用Python提取Chrome瀏覽器保存的密碼的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    八宿县| 青神县| 西峡县| 潼南县| 万州区| 八宿县| 合江县| 崇阳县| 嘉鱼县| 三门县| 社会| 新宁县| 霍林郭勒市| 临桂县| 三穗县| 饶河县| 忻城县| 临泽县| 宁陵县| 晋宁县| 错那县| 安新县| 衢州市| 旌德县| 永修县| 新安县| 正蓝旗| 三河市| 滦平县| 和静县| 浦县| 邓州市| 土默特左旗| 南靖县| 泰宁县| 平利县| 沾化县| 兴安盟| 彰武县| 高青县| 莫力|