Chrome浏览器已保存的密码都保存在一个sqlite3数据库文件中,和Cookies数据库在同一个文件夹。
如:C:\Users\Jueee\AppData\Local\Google\Chrome\User Data\Default\Login Data
前面的路径“C:\Users\Jueee\AppData\Local”,我们可以通过读取环境变量中的值来实现。
使用微软自带的 CryptUnprotectData 函数解密数据库中的密码字段,即可还原密码。
CryptUnprotectData 函数可以在 win32crypt 中找到。
【注】为了防止出现读写出错,建议先把数据库临时拷贝到当前目录。
代码如下:
import os,sys import shutil import sqlite3 import win32crypt db_file_path = os.path.join(os.environ['LOCALAPPDATA'],r'Google\Chrome\User Data\Default\Login Data') print(db_file_path) tmp_file = os.path.join(os.path.dirname(sys.executable),'tmp_tmp_tmp') print(tmp_file) if os.path.exists(tmp_file): os.remove(tmp_file) shutil.copyfile(db_file_path,tmp_file) conn = sqlite3.connect(tmp_file) for row in conn.execute('select signon_realm,username_value,password_value from logins'): try: ret = win32crypt.CryptUnprotectData(row[2],None,None,None,0) print('网站:%-50s,用户名:%-20s,密码:%s' % (row[0][:50],row[1],ret[1].decode('gbk'))) except Exception as e: print('获取Chrome密码失败...') raise e conn.close() os.remove(tmp_file)
运行结果:
账户和密码,就全部都通过明文方式展示出来啦。
GitHub 地址:https://github.com/Jueee/05-WebCrawlers/blob/master/14-ChromePassword.py