最近做的活中需要获取对方浏览器的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