2021最新获取cookie

最近做的活中需要获取对方浏览器的cookie信息,在网上找了一下,发现了比较新版本chrome获取cookie的方法:

  • 这是我在早期看到的,那时候还不是收费模式,现在需要付费才能看,现在再我这里观看免费的哦,相关代码如下:

废话不多说,开启省流量模式,上代码:


# -*- coding:'utf-8' -*-
import time,traceback,sqlite3,os,json,base64

from win32crypt import CryptUnprotectData
from cryptography.hazmat.primitives.ciphers.aead import AESGCM 

def decryptString(key,data):
    nonce,cipherbytes=data[3:15],data[15:]
    aesgcm=AESGCM(key)
    plainbytes=aesgcm.decrypt(nonce,cipherbytes,None)
    plaintext=plainbytes.decode('utf-8')

    return plaintext

def getKey():
    LocalState = os.environ['LOCALAPPDATA'] + r'\Google\Chrome\User Data\Local State'
    with open(LocalState, '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)
    encrypted_key=encrypted_key_with_header[5:]
    key = CryptUnprotectData(encrypted_key,None,None,None,0)[1]
    return key


def getChromeCookie(hostsUrl):
    cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies" 
    if not os.path.exists(cookiepath):
        raise Exception('Cookies file not exist!')

    sql = f"select name,encrypted_value from cookies where host_key = '.{hostsUrl}'"
  
    try:
        conn = sqlite3.connect(cookiepath)
        conn.text_factory =  bytes 
        res=conn.execute(sql).fetchall()
        conn.close()
    except Exception as e:
        print(e)
    key = getKey()
    cookieList=[]
    for row in res:
        cookieList.append( f'{str(row[0],encoding = "utf-8")}={decryptString(key, row[1])}')
    cookie = ';'.join(cookieList)
    return cookie


print(getChromeCookie('baidu.com'))

直接在getChromeCookie中放你需要获取网站的名称就行,环境是python3
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值