Skip to content

Instantly share code, notes, and snippets.

@Ari24-cb24
Last active June 6, 2022 09:37
Show Gist options
  • Save Ari24-cb24/730c26a6a388ef79dd05d394f7636a54 to your computer and use it in GitHub Desktop.
Save Ari24-cb24/730c26a6a388ef79dd05d394f7636a54 to your computer and use it in GitHub Desktop.
Discord Javascript & CSS Injector
PORT=31337
APPLICATION_ID=1337

Set up

Make sure to start Discord like this

C:\Users\user\AppData\Local\DiscordCanary\app-1.0.37>DiscordCanary.exe --remote-debugging-port=31337

How this works

Discord is a webapplication ported to desktop by using electron. Thus discord uses chrome and chrome comes with a built-in devtool tab. This script communicates with websockets to the remote-debugging-port to discord. Its able to execute Javascript leading to injections of all sorts and kinds

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()
console.log("Custom JavaScript injected");
let head = document.getElementsByTagName("head")[0];
let test = document.getElementsByClassName("python-discord-injector");
if (test.length > 0) {
head.removeChild(test);
}
let style_elem = document.createElement("style");
style_elem.classList.add("python-discord-injector");
style_elem.innerHTML = `
.theme-dark {
--header-primary: rgba(255, 255, 255, 0.719);
--text-normal: #c4c4c4f1;
}
span.mention {
color: rgb(255, 0, 0);
background-color: rgb(100, 0, 0);
}
}`;
head.appendChild(style_elem);
.theme-dark {
--header-primary: rgba(255, 255, 255, 0.719);
--text-normal: #c4c4c4f1;
}
span.mention {
color: rgb(255, 0, 0);
background-color: rgb(100, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment