python 获取谷歌浏览器保存的密码
由于谷歌浏览器80以后版本采用了新的加密方式,所以记录在这里
#-*-coding:utf-8-*-
importos
importjson
importbase64
importsqlite3
fromwin32cryptimportCryptUnprotectData
fromcryptography.hazmat.primitives.ciphers.aeadimportAESGCM
#pipinstallpywin32
#pipinstallcryptography
#文档:https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_win.cc?q=OSCrypt&ss=chromium
classChrome:
def__init__(self):
self.local_state=os.environ['LOCALAPPDATA']+r'\Google\Chrome\UserData\LocalState'
self.cookie_path=os.environ['LOCALAPPDATA']+r"\Google\Chrome\UserData\Default\LoginData"
defget_key(self):
withopen(self.local_state,'r',encoding='utf-8')asf:
base64_encrypted_key=json.load(f)['os_crypt']['encrypted_key']
encrypted_key_with_header=base64.b64decode(base64_encrypted_key)
#去掉开头的DPAPI
encrypted_key=encrypted_key_with_header[5:]
key_=CryptUnprotectData(encrypted_key,None,None,None,0)[1]
returnkey_
@staticmethod
defdecrypt_string(key,secret,salt=None):
"""
解密
"""
#去掉'v10'
nonce,cipher_bytes=secret[3:15],secret[15:]
aes_gcm=AESGCM(key)
returnaes_gcm.decrypt(nonce,cipher_bytes,salt).decode('utf-8')
@staticmethod
defencrypt_string(key,data,salt=None):
"""
加密
"""
aes_gcm=AESGCM(key)
prefix="v10".encode("utf-8")
#随机生成12位字符串,拼接"v10"共15位
nonce=os.urandom(12)
cipher_bytes=data.encode("utf-8")
returnprefix+nonce+aes_gcm.encrypt(nonce,cipher_bytes,salt)
defget_password(self,host):
sql=f"selectusername_value,password_valuefromloginswheresignon_realm='{host}';"
withsqlite3.connect(self.cookie_path)asconn:
cu=conn.cursor()
res=cu.execute(sql).fetchall()
cu.close()
result=[]
key=self.get_key()
forname,encrypted_valueinres:
ifencrypted_value[0:3]==b'v10'orencrypted_value[0:3]==b'v11':
password=self.decrypt_string(key,encrypted_value)
else:
password=CryptUnprotectData(encrypted_value)[1].decode()
result.append({'user_name':name,'password':password})
returnresult
defset_password(self,host,username,password):
key=self.get_key()
encrypt_secret=self.encrypt_string(key,password)
sq=f"""updateloginssetpassword_value=x'{encrypt_secret.hex()}'wheresignon_realm='{host}'andusername_value='{username}';"""
withsqlite3.connect(self.cookie_path)asconn:
cu=conn.cursor()
cu.execute(sq)
conn.commit()
if__name__=='__main__':
a=Chrome()
aa=a.get_password("https://baidu.com")
print(aa)
以上就是python获取谷歌浏览器保存的密码的详细内容,更多关于python获取浏览器密码的资料请关注毛票票其它相关文章!