Skip to content
Neutral Works
Go back

C2 Defense Evasion: Avoiding Shell Execution in Node.js

Tech

Relatively speaking, it usually takes a lot of effort for offensive security professionals when building, debugging, and maintaining their own C/C++/Go/.Net C2 framework. Instead, creating a Node.js C2 implant, coupled with minimal JavaScript obfuscations, could potentially provide a super convenient way to achieve quick wins during red or purple teaming exercises.

However, it’s worth noting that your compiled Node executable may still be a large one. Challenges also arise due to the difficulties of integrating with your existing shellcode loaders or leveraging well-known LOLBins such as ‘regasm.exe’ and ‘regsvr32.exe’ for initial execution.

Despite the pros and cons discussed above, our focus will primarily be on Node’s child_process module which allows us to quickly spawn a child process, granting access to operating system functionalities by executing various system commands. Indeed, this also forms the essence of what a C2 Implant typically requires.

In general, there are four ways of creating a child process in Node spawn(), fork(), exec() and execFile(). Today, we will be mainly comparing exec()and spawn()when building our own C2 Implant below.

By default, the spawn() function executes the specified command directly without creating a shell, contributing to a slightly higher efficiency compared to the exec() function, which does need to create a command prompt first such as cmd.exe.

// Import exec() function from the child_process module
const { exec } = require('child_process');

exec(received_c2_command_string, (err, stdout, stderr) => {
// Do something here based on the stdout
const buffer = Buffer.from(stdout, 'utf-8');
const base64EncodedString = buffer.toString('base64');
}

node.exe (c2 implant) -> cmd.exe -> ping.exe

As can be seen from the screenshot above, if you are familiar with the capabilities of modern EDRs, the initiation of cmd.exe process before creating any subprocess often becomes a strong indicator of malicious activity already, leaving little room for your C2 to remain stealthy during red teaming exercises.

To address this issue from offensive security perspective, it is therefore recommended to use spawn() instead. This function simply helps launch a command in a new process, allowing for the efficient passing of arguments to the specified command.

// Import spawn() function from the child_process module
const { spawn } = require("child_process");

const [cmd, ...args] = received_c2_command_string.split(" ");
const child = spawn(cmd, args);
// Do something here based on the stdout
child.stdout.on("data", data => {
  stdout += data;
});

As per the screenshot below, instead of spawning cmd.exe first, our C2 Implant (node.exe) this time directly executed ping.exe once the command was received from the remote server.

node.exe (c2 implant) -> ping.exe

In some recent EDR evasion tests, coupled with minimal JavaScript obfuscations, it has proven effective in meeting the initial reconnaissance requirement once the payload is successfully executed on a victim’s machine.

Please note that this post does not explore EDR unhooking, which is of course another crucial aspect for maximising the benefits of your defense evasion attempt. But, hey, we still get the idea right? :P

I hope you find this article useful, and thank you for reading.


Related: For more on custom offensive tooling, see Building Your Own Shellcode Stager with Customised Functions. For a complementary approach that reduces tooling overhead altogether, see Reverse SOCKS5 Tunnels for Red Teaming and Adversary Emulation.


Share this post:

Previous Post
Applying Graph Theory to CTI and Threat Hunting
Next Post
Reverse SOCKS5 Tunnels for Red Teaming and Adversary Emulation