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 相关文章推荐
如何搜索查找并解决Django相关的问题
Jun 30 Python
Python中使用bidict模块双向字典结构的奇技淫巧
Jul 12 Python
python3.X 抓取火车票信息【修正版】
Jun 19 Python
Python3 pip3 list 出现 DEPRECATION 警告的解决方法
Feb 16 Python
Python Datetime模块和Calendar模块用法实例分析
Apr 15 Python
树莓派用python中的OpenCV输出USB摄像头画面
Jun 22 Python
python 列表转为字典的两个小方法(小结)
Jun 28 Python
对python3中的RE(正则表达式)-详细总结
Jul 23 Python
python笔记_将循环内容在一行输出的方法
Aug 08 Python
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
Oct 14 Python
Django怎么在admin后台注册数据库表
Nov 14 Python
python selenium 获取接口数据的实现
Dec 07 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获取网站域名和地址的代码
2008/08/17 PHP
PHP 数组教程 定义数组
2009/10/23 PHP
Laravel 5框架学习之路由、控制器和视图简介
2015/04/07 PHP
PHP数据库编程之MySQL优化策略概述
2017/08/16 PHP
Jquery下:nth-child(an+b)的使用注意
2011/05/28 Javascript
从URL中提取参数与将对象转换为URL查询参数的实现代码
2012/01/12 Javascript
js确定对象类型方法
2012/03/30 Javascript
jquery实现点击TreeView文本父节点展开/折叠子节点
2013/01/10 Javascript
Js 获取Gridview选中行的内容操作步骤
2013/02/05 Javascript
jquery.qrcode在线生成二维码使用示例
2013/08/21 Javascript
jquery中的ajax方法怎样通过JSONP进行远程调用
2014/05/04 Javascript
JavaScript及jquey实现多个数组的合并操作
2014/09/06 Javascript
jquery操作 iframe的方法
2014/12/03 Javascript
浅谈Unicode与JavaScript的发展史
2015/01/19 Javascript
网页从弹窗页面单选框传值至父页面代码分享
2015/09/29 Javascript
js简单网速测试方法完整实例
2015/12/15 Javascript
jQuery实现百叶窗焦点图动画效果代码分享(附源码下载)
2016/03/14 Javascript
JS检测移动端横竖屏的代码
2016/05/30 Javascript
AngularJs Scope详解及示例代码
2016/09/01 Javascript
node.js缺少mysql模块运行报错的解决方法
2016/11/13 Javascript
基于JavaScript实现熔岩灯效果导航菜单
2017/01/04 Javascript
详解Webpack-dev-server的proxy用法
2018/09/08 Javascript
基于vue-cli3+typescript的tsx开发模板搭建过程分享
2020/02/28 Javascript
vue.js实现双击放大预览功能
2020/06/23 Javascript
[49:35]KG vs SECRET 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
Python实现的简单hangman游戏实例
2015/06/28 Python
SQLite3中文编码 Python的实现
2017/01/11 Python
python在文本开头插入一行的实例
2018/05/02 Python
浅析Python数据处理
2018/05/02 Python
解决sublime+python3无法输出中文的问题
2018/12/12 Python
python实现暗通道去雾算法的示例
2020/09/27 Python
5 个强大的HTML5 API 函数推荐
2014/11/19 HTML / CSS
The North Face北面荷兰官网:美国著名户外品牌
2019/10/16 全球购物
PHP笔试题
2012/02/22 面试题
护理专业本科生自荐信
2013/10/01 职场文书
班主任与学生安全责任书
2014/07/25 职场文书