python 获取谷歌浏览器保存的密码

作者:rm-rf* 时间:2022-05-21 21:39:49 

由于谷歌浏览器80以后版本采用了新的加密方式,所以记录在这里


# -*- coding:utf-8 -*-
import os
import json
import base64
import sqlite3
from win32crypt import CryptUnprotectData
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

#  pip install pywin32
#  pip install cryptography
#  文档:https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_win.cc?q=OSCrypt&ss=chromium

class Chrome:
 def __init__(self):
   self.local_state = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data\Local State'
   self.cookie_path = os.environ['LOCALAPPDATA'] + r"\Google\Chrome\User Data\Default\Login Data"

def get_key(self):
   with open(self.local_state, 'r', encoding='utf-8') as f:
     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]
   return key_

@staticmethod
 def decrypt_string(key, secret, salt=None):
   """
   解密
   """
   # 去掉'v10'
   nonce, cipher_bytes = secret[3:15], secret[15:]
   aes_gcm = AESGCM(key)
   return aes_gcm.decrypt(nonce, cipher_bytes, salt).decode('utf-8')

@staticmethod
 def encrypt_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")
   return prefix + nonce + aes_gcm.encrypt(nonce, cipher_bytes, salt)

def get_password(self, host):
   sql = f"select username_value,password_value from logins where signon_realm ='{host}';"
   with sqlite3.connect(self.cookie_path) as conn:
     cu = conn.cursor()
     res = cu.execute(sql).fetchall()
     cu.close()
     result = []
     key = self.get_key()

for name, encrypted_value in res:

if encrypted_value[0:3] == b'v10' or encrypted_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})
     return result

def set_password(self, host, username, password):
   key = self.get_key()
   encrypt_secret = self.encrypt_string(key, password)
   sq = f"""update logins set password_value=x'{encrypt_secret.hex()}' where signon_realm ='{host}' and username_value='{username}';"""
   with sqlite3.connect(self.cookie_path) as conn:
     cu = conn.cursor()
     cu.execute(sq)
     conn.commit()

if __name__ == '__main__':
 a = Chrome()
 aa = a.get_password("https://baidu.com")
 print(aa)

来源:https://www.cnblogs.com/lyalong/tag/python/default.html?page=2

标签:python,谷歌,浏览器,密码
0
投稿

猜你喜欢

  • python中内置库csv的使用及说明

    2022-02-22 07:31:54
  • asp如何获知文件最后的修改日期和时间?

    2009-11-24 20:49:00
  • python避免死锁方法实例分析

    2023-08-04 04:32:59
  • python 教程实现 turtle海龟绘图

    2022-03-19 10:45:35
  • python深度学习之多标签分类器及pytorch实现源码

    2022-09-26 01:09:12
  • python爬取微信公众号文章图片并转为PDF

    2021-02-02 06:53:31
  • python sklearn常用分类算法模型的调用

    2021-06-18 11:42:25
  • Python 聊聊socket中的listen()参数(数字)到底代表什么

    2022-10-17 00:49:25
  • 将django项目部署到centos的踩坑实战

    2021-05-14 06:00:22
  • python使用正则表达式提取网页URL的方法

    2023-10-09 16:27:18
  • Python常用工具之音频调整音量

    2023-10-29 03:15:33
  • Design IT. (8),一匹“更快的马”

    2009-02-11 12:19:00
  • Python-opencv实现红绿两色识别操作

    2021-05-04 18:35:51
  • Visual Studio Code搭建django项目的方法步骤

    2022-11-28 22:04:00
  • 简单代码屏蔽超级链接虚线框

    2008-02-03 11:34:00
  • Python实现Tracert追踪TTL值的方法详解

    2023-06-24 13:08:14
  • python3.7.2 tkinter entry框限定输入数字的操作

    2021-02-22 14:40:36
  • ASP分页函数

    2009-07-06 12:41:00
  • 在Python的Tornado框架中实现简单的在线代理的教程

    2021-12-31 08:51:39
  • python利用opencv调用摄像头实现目标检测

    2023-01-07 10:16:18
  • asp之家 网络编程 m.aspxhome.com