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对象体系深入分析
Oct 28 Python
举例讲解Python中的死锁、可重入锁和互斥锁
Nov 05 Python
Python中turtle作图示例
Nov 15 Python
Python基于动态规划算法解决01背包问题实例
Dec 06 Python
Python爬虫框架Scrapy实例代码
Mar 04 Python
使用Python画股票的K线图的方法步骤
Jun 28 Python
Python通过cv2读取多个USB摄像头
Aug 28 Python
Python 元组操作总结
Sep 18 Python
python中threading开启关闭线程操作
May 02 Python
Python Opencv实现单目标检测的示例代码
Sep 08 Python
Python基础学习之奇异的GUI对话框
May 27 Python
python基础之文件操作
Oct 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 cookie 作用范围?不要在当前页面使用你的cookie
2009/03/24 PHP
yii操作cookie实例简介
2014/07/09 PHP
php多任务程序实例解析
2014/07/19 PHP
PHP速成大法
2015/01/30 PHP
php实现XSS安全过滤的方法
2015/07/29 PHP
修复ShopNC使用QQ 互联时提示100010 错误
2015/11/08 PHP
Zend Framework教程之Application和Bootstrap用法详解
2016/03/10 PHP
微信公众号OAuth2.0网页授权问题浅析
2017/01/21 PHP
laravel获取不到session的三种解决办法【推荐】
2018/09/16 PHP
JavaScript对象之深度克隆介绍
2014/12/08 Javascript
jQuery中 prop() attr()使用详解
2015/05/19 Javascript
jQuery幻灯片特效代码分享--鼠标滑过按钮时切换(2)
2020/11/18 Javascript
node.js cookie-parser之parser.js
2016/06/06 Javascript
JS继承与闭包及JS实现继承的三种方式
2017/10/15 Javascript
使用JS中的Replace()方法遇到的问题小结
2017/10/20 Javascript
JavaScript事件冒泡机制原理实例解析
2020/01/14 Javascript
python查看微信好友是否删除自己
2016/12/19 Python
python中实现指定时间调用函数示例代码
2017/09/08 Python
Windows 7下Python Web环境搭建图文教程
2018/03/20 Python
Tensorflow实现AlexNet卷积神经网络及运算时间评测
2018/05/24 Python
Django框架模板文件使用及模板文件加载顺序分析
2019/05/23 Python
Python3多线程版TCP端口扫描器
2019/08/31 Python
基于pytorch padding=SAME的解决方式
2020/02/18 Python
Django数据库迁移常见使用方法
2020/11/12 Python
使paramiko库执行命令时在给定的时间强制退出功能的实现
2021/03/03 Python
CSS3实现歌词进度文字颜色填充变化动态效果的思路详解
2020/06/02 HTML / CSS
西班牙最好的在线购买葡萄酒的商店:Vinoseleccion
2019/10/30 全球购物
请说出以下代码输出什么
2013/08/30 面试题
人力资源管理专业自荐书范文
2014/02/10 职场文书
关于读书的演讲稿
2014/05/07 职场文书
艺术学院毕业生求职信
2014/07/09 职场文书
2014年统战工作总结
2014/12/09 职场文书
python中sys模块的介绍与实例
2021/04/17 Python
Redis延迟队列和分布式延迟队列的简答实现
2021/05/13 Redis
Redis中一个String类型引发的惨案
2021/07/25 Redis
SpringBoot+Vue+JWT的前后端分离登录认证详细步骤
2021/09/25 Java/Android