浏览器保存密码获取与解密

最近遇到edge打开就崩溃的问题,想换成chrome,但是edge里面的书签和密码还未导出,再查询后得出以下办法。

文件位置

密钥位置

C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Local State

加密的密码位置

C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default\Login Data

书签位置

C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default\Bookmarks

cookies位置

C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default\Network\Cookies

history位置

C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default\History

获取密钥

打开Local State文件并获取密钥,该文件是一个json文件

import json
with open('Local State', 'r', encoding='utf-8') as f:
    localState = json.loads(f.read())
print(localState['os_crypt']['encrypted_key'])

image

密码获取

打开Login Data文件并获取加密的密码,该文件是一个sqlite数据库

import sqlite3
conn = sqlite3.connect('Login Data')
cur = conn.cursor()
cur.execute("select origin_url, username_value, password_value from logins")
res = cur.fetchall()
for i in range(len(res)):
    print(f'Url: {res[i][0]}')
    print(f'Username: {res[i][1]}')
    print(f'Cipher Text: {res[i][2]}')

image

密码解密

因为得到的密码是密文,因此需要解密后才能得到正确的密码

import base64
from Crypto.Cipher import AES
from win32crypt import CryptUnprotectData

secretKey = base64.b64decode(localState['os_crypt']['encrypted_key'])
secretKey = secretKey[5:]
secretKey = CryptUnprotectData(secretKey, None, None, None, 0)[1]

def decrypt(cipherText):
    initialisationVector = cipherText[3:15]  # 从密文中提取初始化向量
    encryptedPassword = cipherText[15:-16]  # 从密文中提取加密密码
    cipher = AES.new(secretKey, AES.MODE_GCM, initialisationVector)  # 构造AES算法解密密码
    decryptedPass = cipher.decrypt(encryptedPassword)
    decryptedPass = decryptedPass.decode()
    return decryptedPass

print(decrypt(res[38][2]))

image

完整代码

解密后并保存为xlsx文件,使用前需要先将Local State和Local Data文件复制到脚本目录

import json, sqlite3, base64, pandas as pd
from Crypto.Cipher import AES
from win32crypt import CryptUnprotectData

with open('Local State', 'r', encoding='utf-8') as f:
    localState = json.loads(f.read())
secretKey = base64.b64decode(localState['os_crypt']['encrypted_key'])
secretKey = secretKey[5:]
secretKey = CryptUnprotectData(secretKey, None, None, None, 0)[1]

def decrypt(cipherText):
    initialisationVector = cipherText[3:15]  # 从密文中提取初始化向量
    encryptedPassword = cipherText[15:-16]  # 从密文中提取加密密码
    cipher = AES.new(secretKey, AES.MODE_GCM, initialisationVector)  # 构造AES算法解密密码
    decryptedPass = cipher.decrypt(encryptedPassword)
    decryptedPass = decryptedPass.decode()
    return decryptedPass

conn = sqlite3.connect('Login Data')
cur = conn.cursor()
cur.execute("select origin_url, username_value, password_value from logins")
res = cur.fetchall()
data = { 'url': [], 'username': [], 'password': []}
for i in range(len(res)):
    data['url'].append(res[i][0])
    data['username'].append(res[i][1])
    data['password'].append(decrypt(res[i][2]))
data = pd.DataFrame(data)
data.to_excel('LoginData.xlsx', index=None)
posted @ 2024-12-02 22:23  wstong  阅读(21)  评论(0编辑  收藏  举报