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中unittest用法实例
Sep 25 Python
浅析python中的分片与截断序列
Aug 09 Python
学习python中matplotlib绘图设置坐标轴刻度、文本
Feb 07 Python
python3+PyQt5使用数据库窗口视图
Apr 24 Python
解决Python 中英文混输格式对齐的问题
Jul 16 Python
pycharm恢复默认设置或者是替换pycharm的解释器实例
Oct 29 Python
Python使用itchat模块实现群聊转发,自动回复功能示例
Aug 26 Python
python set集合使用方法解析
Nov 05 Python
关于Python 中的时间处理包datetime和arrow的方法详解
Mar 19 Python
PyPDF2读取PDF文件内容保存到本地TXT实例
May 12 Python
Python自动化之UnitTest框架实战记录
Sep 08 Python
Python爬虫逆向分析某云音乐加密参数的实例分析
Dec 04 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缓存技术的使用说明
2011/08/06 PHP
php中使用getimagesize获取图片、flash等文件的尺寸信息实例
2014/04/29 PHP
php smarty truncate UTF8乱码问题解决办法
2014/06/13 PHP
WordPress中对访客评论功能的一些优化方法
2015/11/24 PHP
PHP简单获取上月、本月、近15天、近30天的方法示例
2017/07/03 PHP
php微信公众号开发之秒杀
2018/10/20 PHP
php实现JWT(json web token)鉴权实例详解
2019/11/05 PHP
javascript查找字符串中出现最多的字符和次数的小例子
2013/10/29 Javascript
浅谈JSON中stringify 函数、toJosn函数和parse函数
2015/01/26 Javascript
PHP 数组current和next用法分享
2015/03/05 Javascript
jQuery中常用的遍历函数用法实例总结
2015/09/01 Javascript
深入探讨javascript函数式编程
2015/10/11 Javascript
微信支付 JS API支付接口详解
2016/07/11 Javascript
AngularJS ng-controller 指令简单实例
2016/08/01 Javascript
JQuery实现列表中复选框全选反选功能封装(推荐)
2016/11/24 Javascript
angularjs之$timeout指令详解
2017/06/13 Javascript
js弹性势能动画之抛物线运动实例详解
2017/07/27 Javascript
webpack 2.x配置reactjs基本开发环境详解
2017/08/08 Javascript
微信小程序 input表单与redio及下拉列表的使用实例
2017/09/20 Javascript
解决在Vue中使用axios用form表单出现的问题
2019/10/30 Javascript
Javascript如何实现双指控制图片功能
2020/02/25 Javascript
JavaScript装箱及拆箱boxing及unBoxing用法解析
2020/06/15 Javascript
JS JQuery获取data-*属性值方法解析
2020/09/01 jQuery
详解python中的模块及包导入
2019/08/30 Python
Python帮你微信头像任意添加装饰别再@微信官方了
2019/09/25 Python
在IE6系列等老式浏览器中使用HTML5的新标签实现方案
2012/12/25 HTML / CSS
HTML5之SVG 2D入门1—SVG(可缩放矢量图形)概述
2013/01/30 HTML / CSS
萨克斯第五大道英国:Saks Fifth Avenue英国
2019/04/01 全球购物
软件配置管理有什么好处
2015/04/15 面试题
《一个小村庄的故事》教学反思
2014/04/13 职场文书
团日活动总结书格式
2014/05/08 职场文书
给校长的建议书100字
2014/05/16 职场文书
中学生打架检讨书
2014/10/13 职场文书
2014年药品销售工作总结
2014/12/16 职场文书
交通安全教育主题班会
2015/08/12 职场文书
导游词之南京夫子庙
2019/12/09 职场文书