|
import websocket |
|
import json |
|
import requests |
|
import os |
|
import logging |
|
import subprocess |
|
|
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
class DataManager: |
|
def __init__(self, port): |
|
self.port = port |
|
self.application_id = int(os.environ["APPLICATION_ID"]) |
|
|
|
def prepare_payload(self, method="Runtime.evaluate", **params): |
|
logging.info("Preparing Payload") |
|
return {"id": self.application_id, "method": method, "params": params} |
|
|
|
def retrieve_websocket_debugger_url(self): |
|
logging.info("Retrieving Websocket Debugger URL") |
|
logging.info(f"Requesting http://localhost:{self.port}/json") |
|
|
|
try: |
|
r = requests.get(f"http://localhost:{self.port}/json") |
|
except requests.exceptions.ConnectionError: |
|
logging.critical("There is not Debugger Information Data present. Try to restart the application and check your port again (.env file)!") |
|
return exit(0) |
|
|
|
try: |
|
data = r.json() |
|
except json.JSONDecodeError: |
|
logging.critical("Debugging Information seems to be corrupted. Please try again or try to restart the application and check your port again (.env file)!") |
|
return exit(0) |
|
|
|
if not data: |
|
logging.critical("Couldn't find debugger information. Try to restart the application and check your port again (.env file)!") |
|
return exit(0) |
|
|
|
data = data[-1] |
|
|
|
logging.info("Websocket Debugger URL is: " + data["webSocketDebuggerUrl"]) |
|
|
|
return data["webSocketDebuggerUrl"] |
|
|
|
|
|
class DiscordInjector: |
|
def __init__(self): |
|
self.__port = os.environ["PORT"] |
|
self.__dm = DataManager(self.__port) |
|
self.__debugger_url = self.__dm.retrieve_websocket_debugger_url() |
|
|
|
logging.info("Setting up Websocket Connection") |
|
self.__ws = websocket.create_connection(self.__debugger_url) |
|
logging.info("Connected to Websocket") |
|
|
|
def inject_javascript(self, javascript): |
|
logging.info("Preparing Javascript Oneliner") |
|
|
|
payload = self.__dm.prepare_payload(expression=javascript) |
|
self.__inject_payload(payload) |
|
|
|
def inject_javascript_file(self, path): |
|
logging.info("Reading Javascript File") |
|
|
|
with open(path, "r") as fh: |
|
data = fh.read() |
|
|
|
self.inject_javascript(data) |
|
|
|
def __inject_payload(self, payload): |
|
logging.info("Injecting Payload") |
|
self.__ws.send(json.dumps(payload)) |
|
|
|
def test_injection(self): |
|
logging.info("Testing Injections") |
|
self.inject_javascript("alert('Hello from DiscordCSSInjector made with Python!');") |
|
|
|
|
|
if __name__ == '__main__': |
|
# C:\Users\user\AppData\Local\DiscordCanary\app-1.0.37>DiscordCanary.exe --remote-debugging-port=31337 |
|
# You have to run discord like the above in order to get chrome devtools to work |
|
injector = DiscordInjector() |
|
injector.inject_javascript_file("./test_script.js") |
|
# injector.inject_javascript("console.log('1');") |
|
# injector.test_injection() |