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编码最佳实践之总结
Feb 14 Python
Python的Flask框架中集成CKeditor富文本编辑器的教程
Jun 13 Python
详解Python里使用正则表达式的ASCII模式
Nov 02 Python
ActiveMQ:使用Python访问ActiveMQ的方法
Jan 30 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 Python
python简单鼠标自动点击某区域的实例
Jun 25 Python
纯python进行矩阵的相乘运算的方法示例
Jul 17 Python
django 取消csrf限制的实例
Mar 13 Python
keras的backend 设置 tensorflow,theano操作
Jun 30 Python
树莓派4B安装Tensorflow的方法步骤
Jul 16 Python
Python办公自动化之Excel(中)
May 24 Python
python cv2图像质量压缩的算法示例
Jun 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
不用数据库的多用户文件自由上传投票系统(3)
2006/10/09 PHP
PHP函数学习之PHP函数点评
2012/07/05 PHP
PHP+jQuery 注册模块的改进(三):更新到Smarty3.1
2014/10/14 PHP
使用phpstorm和xdebug实现远程调试的方法
2015/12/29 PHP
PHP 对象接口简单实现方法示例
2020/04/13 PHP
jsTree 基于JQuery的排序节点 Bug
2011/07/26 Javascript
使用js简单实现了tree树菜单
2013/11/20 Javascript
两种方法实现在HTML页面加载完毕后运行某个js
2014/06/16 Javascript
jQuery基于ajax实现带动画效果无刷新柱状图投票代码
2015/08/10 Javascript
jquery插件autocomplete用法示例
2016/07/01 Javascript
基于angularJS的表单验证指令介绍
2016/10/21 Javascript
JavaScript函数参数的传递方式详解
2017/03/06 Javascript
详解Nodejs之静态资源处理
2017/06/05 NodeJs
前端主流框架vue学习笔记第二篇
2017/07/26 Javascript
使用apifm-wxapi快速开发小程序过程详解
2019/08/05 Javascript
解决vue addRoutes不生效问题
2020/08/04 Javascript
vue使用video插件vue-video-player的示例
2020/10/03 Javascript
js前端传json后台接收‘‘被转为quot的问题解决
2020/11/12 Javascript
python socket网络编程步骤详解(socket套接字使用)
2013/12/06 Python
将Django框架和遗留的Web应用集成的方法
2015/07/24 Python
python实现将汉字保存成文本的方法
2018/11/16 Python
opencv 获取rtsp流媒体视频的实现方法
2019/08/23 Python
python3应用windows api对后台程序窗口及桌面截图并保存的方法
2019/08/27 Python
英国婚礼商城:Wedding Mall
2019/11/02 全球购物
兼职业务员岗位职责
2014/01/01 职场文书
学校运动会开幕演讲稿
2014/01/04 职场文书
开会迟到检讨书
2014/01/08 职场文书
会计助理岗位职责
2014/02/17 职场文书
大二法英学生职业生涯规划范文
2014/02/27 职场文书
合理化建议书
2015/02/04 职场文书
2015年毕业实习工作总结
2015/05/29 职场文书
高中升旗仪式主持词
2015/07/03 职场文书
养成教育工作总结
2015/08/13 职场文书
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
Python序列化模块JSON与Pickle
2022/06/05 Python
win10电脑老是死机怎么办?win10系统老是死机的解决方法
2022/08/05 数码科技