Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

create a program to extract all the cookies from chrome profiles and store them into…

  1. User

    create a program to extract all the cookies from chrome profiles and store them into cookies.txt file Path of cookies file in chrome are C:\Users<username>\AppData\Local\Google\Chrome\User Data<profile name>\Network\Cookies

  2. Assistant

    To create a program that extracts cookies from Chrome profiles and stores them in a cookies.txt file, you can use Python in combination with the sqlite3 library to read the Chrome SQLite database where cookies are stored and cryptography library to decrypt them. Keep in mind that this method assumes an environment where your script has permission to access these files and decrypt the content.

    Here's a complete step-by-step guide for you:

    1. Install Dependencies: Open your terminal or command prompt and install the necessary dependencies using pip:

      pip install pysqlite3 cryptography
      
    2. Write the Program: You will need to write a Python script that does the following:

      • Finds the location of the cookies files.
      • Decodes encrypted cookies using the system’s encryption key.
      • Saves the extracted cookies into a cookies.txt file.

    Here's the implementation:

    import os
    import sqlite3
    import base64
    import shutil
    import win32crypt  # Only available on Windows platforms
    from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
    from cryptography.hazmat.backends import default_backend
    
    # Get the path of the Chrome cookies database
    def get_chrome_cookies_paths():
        local_app_data = os.getenv('LOCALAPPDATA')
        chrome_path = os.path.join(local_app_data, 'Google', 'Chrome', 'User Data')
        cookie_files = []
        for profile in os.listdir(chrome_path):
            profile_path = os.path.join(chrome_path, profile, 'Network', 'Cookies')
            if os.path.exists(profile_path):
                cookie_files.append(profile_path)
        return cookie_files
    
    # Decrypt the Chrome cookies
    def decrypt_cookie(encrypted_value, key=None):
        try:
            # Try to decrypt as DPAPI structure
            decrypted_cookie = win32crypt.CryptUnprotectData(encrypted_value, None, None, None, 0)[1]
        except:
            # Try to decrypt as AES256 GCM structure
            if encrypted_value[:3] == b'v10':
                iv = encrypted_value[3:15]
                payload = encrypted_value[15:]
                cipher = Cipher(algorithms.AES(key), modes.GCM(iv), backend=default_backend())
                decryptor = cipher.decryptor()
                decrypted_cookie = decryptor.update(payload) + decryptor.finalize()
            else:
                decrypted_cookie = encrypted_value
        return decrypted_cookie
    
    # Extract cookies and write to file
    def extract_and_save_cookies():
        cookie_paths = get_chrome_cookies_paths()
        with open('cookies.txt', 'w') as file:
            for path in cookie_paths:
                conn = sqlite3.connect(path)
                cursor = conn.cursor()
                cursor.execute("SELECT host_key, name, encrypted_value FROM cookies")
                for host_key, name, encrypted_value in cursor.fetchall():
                    decrypted_value = decrypt_cookie(encrypted_value)
                    file.write(f'{host_key}\t{name}\t{decrypted_value.decode("utf-8")}\n')
                conn.close()
    
    if __name__ == "__main__":
        extract_and_save_cookies()
        print("Cookies have been extracted to cookies.txt")
    

    Note:

    • This script works on Windows systems with Chrome installed.
    • You need to install the pywin32 library if you don't have it already, since we're using win32crypt:
      pip install pywin32
      
    • You should have proper privileges to read from the Chrome profile directories.

    Security Warning:

    • Be cautious with how you handle and store extracted cookies, as they can include sensitive information.
    • This script is for educational purposes and personal use only.

    This script will extract all cookies from Chrome profiles and save them into a cookies.txt file, which you can find in the same directory as your Python script.