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


Posted in Python onJanuary 06, 2021

由于谷歌浏览器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)

以上就是python 获取谷歌浏览器保存的密码的详细内容,更多关于python 获取浏览器密码的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python抓取京东图书评论数据
Aug 31 Python
python求众数问题实例
Sep 26 Python
python类继承与子类实例初始化用法分析
Apr 17 Python
windows下ipython的安装与使用详解
Oct 20 Python
python抓取网站的图片并下载到本地的方法
May 22 Python
python实现自动发送邮件
Jun 20 Python
python中ASCII码字符与int之间的转换方法
Jul 09 Python
python 读取文本文件的行数据,文件.splitlines()的方法
Jul 12 Python
解决python3 urllib 链接中有中文的问题
Jul 16 Python
python输入中文的实例方法
Sep 14 Python
Python使用paramiko连接远程服务器执行Shell命令的实现
Mar 04 Python
Python Matplotlib库实现画局部图
Nov 17 Python
python实现PolynomialFeatures多项式的方法
Jan 06 #Python
pytorch中index_select()的用法详解
Jan 06 #Python
Python之京东商品秒杀的实现示例
Jan 06 #Python
Python实现小黑屋游戏的完整实例
Jan 06 #Python
Jupyter Notebook 安装配置与使用详解
Jan 06 #Python
在Ubuntu中安装并配置Pycharm教程的实现方法
Jan 06 #Python
python requests库的使用
Jan 06 #Python
You might like
用PHP与XML联手进行网站编程代码实例
2008/07/10 PHP
PHP新手NOTICE错误常见解决方法
2011/12/07 PHP
PHP设计模式之原型模式定义与用法详解
2018/04/03 PHP
laravel5 Eloquent 实现事务方式
2019/10/21 PHP
jquery控制listbox中项的移动并排序
2009/11/12 Javascript
讨论javascript(一)工厂方式 js面象对象的定义方法
2009/12/15 Javascript
基于Jquery的文字自动截取(提供源代码)
2011/08/09 Javascript
javascript与有限状态机详解
2014/05/08 Javascript
JS实现弹性漂浮效果的广告代码
2015/09/02 Javascript
微信企业号开发之微信考勤Cookies的使用
2015/09/11 Javascript
JS中artdialog弹出框控件之提交表单思路详解
2016/04/18 Javascript
Bootstrap弹出带合法性检查的登录框实例代码【推荐】
2016/06/23 Javascript
JS常见算法详解
2017/02/28 Javascript
jquery实现tab选项卡切换效果(悬停、下方横线动画位移)
2017/05/05 jQuery
使用Angular CLI生成 Angular 5项目教程详解
2018/03/18 Javascript
vue 遮罩层阻止默认滚动事件操作
2020/07/28 Javascript
python操作xml文件示例
2014/04/07 Python
wxPython中listbox用法实例详解
2015/06/01 Python
Python实现二叉树结构与进行二叉树遍历的方法详解
2016/05/24 Python
Python实现Windows和Linux之间互相传输文件(文件夹)的方法
2017/05/08 Python
Python中实现最小二乘法思路及实现代码
2018/01/04 Python
PyCharm设置SSH远程调试的方法
2018/07/17 Python
Python中应该使用%还是format来格式化字符串
2018/09/25 Python
python3的url编码和解码,自定义gbk、utf-8的例子
2019/08/22 Python
python使用Geany编辑器配置方法
2020/02/21 Python
python实现学生成绩测评系统
2020/06/22 Python
Python colormap库的安装和使用详情
2020/10/06 Python
大学生职业生涯设计书
2014/01/02 职场文书
初二学习计划书范文
2014/04/27 职场文书
学党史心得体会
2014/09/05 职场文书
青年文明号申报材料
2014/12/23 职场文书
综合素质评价个性与发展自我评价
2015/03/06 职场文书
2015年教导处教学工作总结
2015/07/22 职场文书
MongoDB使用场景总结
2022/02/24 MongoDB
Mysql使用全文索引(FullText index)的实例代码
2022/04/03 MySQL
如何设置多台电脑共享打印机?多台电脑共享打印机的方法
2022/04/08 数码科技