0x00 Preface
---
In the previous article 'Node.js in Penetration Testing: Implementation of a Downloader', code for implementing a Downloader using Node.js was open-sourced, briefly analyzing its exploitation approach in penetration testing.
Node.js syntax is simple and easy to understand, making Node.js code also relatively easy to analyze.
To increase the difficulty of analyzing Node.js code, my idea is to utilize a feature of Node.js to encapsulate the payload in the form of a C++ addon.
This not only increases the difficulty of analyzing the Node.js code but also allows the payload to be implemented in C++ code. Existing C++ code can be used with minor modifications, reducing the cost of secondary development.
0x01 Introduction
---
This article will cover the following topics:
- Introduction to C++ Addons
- Setting up the Development Environment for C++ Addons
- Example of C++ Addon Code
- Exploitation Approach
- Defense Recommendations
0x02 Introduction to C++ Addons
---
Node.js C++ addons are dynamically linked libraries written in C++ that can be loaded into Node.js using the require() function. By utilizing the APIs provided by V8, they enable mutual calls between JavaScript and C++, bridging the interface between the two languages.
Official Documentation:
https://nodejs.org/api/addons.html
Usage Example:
- After successfully compiling a C++ addon that exports a method named: hello
- The code to call the exported method from the C++ addon in Node.js is as follows:
const addon = require('./addon.node'); |
- Execute the code
node.exe test.js |
0x03 Setting Up the Development Environment for C++ Addons
---
1. Windows Development Environment
Test system: Win7sp1 x64
The following tools need to be installed:
- .NET Framework 4.5.1 or higher
- Python 2.7
- Visual Studio 2015 or higher
The specific setup process is as follows:
1. Install .NET Framework 4.5.1
https://www.microsoft.com/en-US/download/details.aspx?id=5842
2. Download Node.js
https://nodejs.org/en/download/
3. Use Windows-Build-Tools to automatically install dependency tools
https://github.com/felixrieseberg/windows-build-tools
cd c:\ |
If installation fails, you can choose to manually install the following tools:
- Python 2.7
- Visual Studio 2015 or later
4. Install node-gyp
https://github.com/nodejs/node-gyp
npm install -g node-gyp |
2. Linux development environment
wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz |
Note:
You need to add an environment variable to specify the location of node (export PATH=/root/node-v10.15.3-linux-x64/bin:$PATH), otherwise npm install will fail with the error: /usr/bin/env: 'node': No such file or directory
Example:
- hello.cc:
#include |
- binding.gyp
{ |
- Compile via node-gyp to generate plugins
node-gyp configure |
Note:
Can be combined into a single command:
node-gyp configure build |
Node.js supports cross-compilation. For specific parameter details, refer to:
https://www.npmjs.com/package/node-pre-gyp
Command to generate plugins for Windows 64-bit system under Linux is as follows:
node-gyp configure build --target_arch=x64 --target_platform=win32 |
0x04 C++ Plugin Code Example
---
During development, it's best to avoid conditional statements like if, as direct use may cause compilation errors
1. Release file
#include |
2. Execute command:
#include |
3. Execute shellcode
Generate shellcode:
msfvenom -p windows/x64/exec CMD=calc.exe -f c |
Load and execute shellcode:
#include |
The compiled plugin has been uploaded to GitHub at the following address:
An open-source project
The export method for the above plugin code is 'hello', and the invocation method is as follows:
const addon = require('./addon.node'); |
0x05 Exploitation Approach
---
1. Loaded by a third-party trusted program
Reference:
https://bbs.pediy.com/thread-249573.htm
t.exe->node.exe->main.js
main.js and addon.node are placed in the same directory, the content of main.js is as follows:
const addon = require('./addon.node'); |
addon.node is in DLL format, making it impossible to directly obtain the payload, increasing the cost of static analysis
0x06 Defense Recommendations
---
Monitor the behavior of child processes (node.exe) of t.exe, and intercept if suspicious behavior is detected, revoking trust in the certificate
0x07 Summary
---
This article introduces the usage of C++ addons in Node.js, which can be used to increase the difficulty of analyzing Node.js code, and finally shares three payload writing methods.