In my last article (see here), we discovered a NodeJS malware that steals the Discord credentials from the client by patching the sources of the Electron client.
Since then, a lot of things have happened. The author deleted the repository, and he apologized for the harm he caused.
Electron is a framework that lets developers build desktop applications using Web technologies like HTML, Javascript, and CSS.
This framework is prevalent and widely used by many typical applications like VSCode, Whatsapp, or Discord.
As the application code is in Javascript, the sources are still there but hidden in plain sight. So what protects us from a malicious user editing the archive containing the sources and tampering it with malware? Well, on Electron level, practically nothing. On the OS level, you could use code signing or installing the application in a secure place:
C:\Program Files\
can only be edited by the application itself, but, by default, there are installed in %APPDATA%
instead, where there is no such protection.Let's start our journey into Electron injections by looking at how the PirateStealer’s injector works.
It first tries to find your Discord installation in your %APPDATA%
directory and then locates all the running processes. Then it downloads the infection payload from a Github repository, stops your Discord, injects the payload, and restarts your Discord client. Let's focus on the payload injection. The payload injection is done by modifying the main Discord Desktop file that you can find in your %LOCALAPPDATA%\Discord\app-<version>\modules\discord_desktop_core-2\discord_desktop_core
directory. The index.js file should normally look like this:
module.exports = require('./core.asar');
After the injection, it will look like this:
const fs=require("fs"),path=require("path"),{BrowserWindow:BrowserWindow,session:session}=require("electron"),querystring=require("querystring"),os=require("os");var webhook="https://discord.com/api/.....
As you can see, the malware has modified the source code of Discord to get its code running inside Discord.
It is now time to look at this injected payload.
I won’t publish the complete code of the payload as the original Github repository got banned, but we can still highlight some clever mechanisms.
First, we can look at how PirateStealer grabs the token from the Discord client. To prepare the code for distribution, the Discord team bundles its code with Webpack. This tool allows bundling many resources into bigger chunks to facilitate the installation procedure.
Webpack also exposes a way for developers to add their own code at runtime.
This feature comes from this Webpack JSONP Loader. As one can see in the highlighted code, this line allows the developer to access the entire Webpack runtime. From there, we have access to all the functions in Discord. From there, we can list every function and search for the getToken
function. [rant: on] Yep, that’s it, the most security-critical function of Discord is in plain sight of everyone. If Discord renamed that function to any other name, it would break every currently existing token grabber. [rant: off]
With this token, PirateStealers runs several calls to the internal Discord API:
We saw what the malware could do with a simple token. This is quite powerful, in my opinion…
But that’s not all the malware does. It also watches your activity in the application:
From what we saw all along with the article, I would recommend to:
Thank you for reading this article. If you have any suggestions or comments, you can address them preferably on Discord (https://discord.gg/FKuAky4K8M).
If you enjoyed this article, please consider leaving a reaction. This kind of article takes a long time to prepare (almost a week alongside my day job), so if you want to support me, you can do it on Buy me a coffee.
First published here