|
import os |
|
import json |
|
import base64 |
|
import sqlite3 |
|
import shutil |
|
import win32crypt |
|
from Crypto.Cipher import AES |
|
|
|
class ChromeCookie: |
|
""" |
|
https://thepythoncode.com/code/extract-chrome-cookies-python |
|
""" |
|
def __init__(self, path, domain=None): |
|
self.path = path |
|
self.domain = domain |
|
self.cookies = self.get() |
|
|
|
def get_encryption_key(self): |
|
local_state_path = os.path.join(self.path, 'Local State') |
|
with open(local_state_path, "r", encoding="utf-8") as f: |
|
local_state = f.read() |
|
local_state = json.loads(local_state) |
|
key = base64.b64decode(local_state["os_crypt"]["encrypted_key"]) |
|
key = key[5:] |
|
return win32crypt.CryptUnprotectData(key, None, None, None, 0)[1] |
|
|
|
def decrypt_data(self, data, key): |
|
try: |
|
iv = data[3:15] |
|
data = data[15:] |
|
cipher = AES.new(key, AES.MODE_GCM, iv) |
|
return cipher.decrypt(data)[:-16].decode() |
|
except: |
|
try: |
|
return str(win32crypt.CryptUnprotectData(data, None, None, None, 0)[1]) |
|
except: |
|
return "" |
|
|
|
def get(self): |
|
db_path = os.path.join(self.path, 'Default/Network/Cookies') |
|
filename = "Cookies.db" |
|
if not os.path.isfile(filename): |
|
shutil.copyfile(db_path, filename) |
|
db = sqlite3.connect(filename) |
|
db.text_factory = lambda b: b.decode(errors="ignore") |
|
cursor = db.cursor() |
|
where = self.domain if self.domain else '' |
|
print(self.domain) |
|
cursor.execute(f""" |
|
SELECT |
|
host_key, |
|
name, |
|
value, |
|
path, |
|
is_secure, |
|
is_httponly, |
|
creation_utc, |
|
last_access_utc, |
|
expires_utc, |
|
encrypted_value |
|
FROM cookies |
|
WHERE host_key like '{where}' |
|
""") |
|
key = self.get_encryption_key() |
|
cookies = [] |
|
for host_key, name, value, path, is_secure, is_httponly, creation_utc, last_access_utc, expires_utc, encrypted_value in cursor.fetchall(): |
|
if not value: |
|
decrypted_value = self.decrypt_data(encrypted_value, key) |
|
else: |
|
decrypted_value = value |
|
cookie = { |
|
'name': name, |
|
'value': decrypted_value, |
|
'domain': host_key, |
|
'path': path, |
|
'expires': expires_utc, |
|
'secure': is_secure, |
|
'httponly': is_httponly, |
|
'last_access': last_access_utc |
|
} |
|
cookies.append(cookie) |
|
db.close() |
|
return cookies |
|
|
|
def json(self): |
|
return json.dumps(self.cookies, indent=4) |
|
|
|
def string(self): |
|
return '; '.join([f"{cookie['name']}={cookie['value']}" for cookie in self.cookies]) |
|
|
|
|
|
if __name__ == "__main__": |
|
cookies = ChromeCookie('D:/Users/WIN11/Documents/Shoplisy/Akun Shopeeku/chrome/', domain='.shopee.co.id') |
|
print(cookies.string()) |
|
print(cookies.json()) |