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中线程问题的简单讲解
Apr 03 Python
仅用50行Python代码实现一个简单的代理服务器
Apr 08 Python
python创建临时文件夹的方法
Jul 06 Python
python实现斐波那契数列的方法示例
Jan 12 Python
Python生成器以及应用实例解析
Feb 08 Python
Python Matplotlib实现三维数据的散点图绘制
Mar 19 Python
python定时按日期备份MySQL数据并压缩
Apr 19 Python
Django外键(ForeignKey)操作以及related_name的作用详解
Jul 29 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
Jan 02 Python
Python脚本去除文件的只读性操作
Mar 05 Python
Python中的datetime包与time包包和模块详情
Feb 28 Python
在 Python 中利用 Pool 进行多线程
Apr 24 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的字符串用法小结
2010/06/08 PHP
ThinkPHP惯例配置文件详解
2014/07/14 PHP
php实现的递归提成方案实例
2015/11/14 PHP
php实现微信发红包
2015/12/05 PHP
简单谈谈 php 文件锁
2017/02/19 PHP
PHP fclose函数用法总结
2019/02/15 PHP
this[] 指的是什么内容 讨论
2007/03/24 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
js加减乘除丢失精度问题解决方法
2014/05/16 Javascript
javascript中的正则表达式使用指南
2015/03/01 Javascript
JavaScript之AOP编程实例
2015/07/17 Javascript
酷炫jQuery全屏3D焦点图动画效果
2016/03/22 Javascript
[原创]jQuery常用的4种加载方式分析
2016/07/25 Javascript
Javascript 对cookie操作详解及实例
2016/12/29 Javascript
Vue实现typeahead组件功能(非常靠谱)
2017/08/26 Javascript
微信小程序 页面跳转事件绑定的实例详解
2017/09/20 Javascript
vue+iview写个弹框的示例代码
2017/12/05 Javascript
微信小程序使用image组件显示图片的方法【附源码下载】
2017/12/08 Javascript
js循环map 获取所有的key和value的实现代码(json)
2018/05/09 Javascript
[04:49]期待西雅图之战 2016国际邀请赛中国区预选赛WINGS战队赛后采访
2016/06/29 DOTA
python 排列组合之itertools
2013/03/20 Python
跨平台python异步回调机制实现和使用方法
2013/11/26 Python
Python的Bottle框架中实现最基本的get和post的方法的教程
2015/04/30 Python
pycharm通过ssh连接远程服务器教程
2020/02/12 Python
python torch.utils.data.DataLoader使用方法
2020/04/02 Python
python异常处理之try finally不报错的原因
2020/05/18 Python
python virtualenv虚拟环境配置与使用教程详解
2020/07/13 Python
python实现移动木板小游戏
2020/10/09 Python
三星新西兰官网:Samsung新西兰
2019/03/05 全球购物
校园报刊亭创业计划书
2014/01/02 职场文书
物流专业大学的自我评价
2014/01/11 职场文书
同学会邀请书大全
2014/01/12 职场文书
电工工作职责范本
2014/02/22 职场文书
承诺保证书格式
2015/02/28 职场文书
golang 实现时间戳和时间的转化
2021/05/07 Golang
CSS3实现指纹特效代码
2022/03/17 HTML / CSS