XSS Template Injection and Remote Code Execution
Node.js allows you to run JavaScript outside of the browser. Due to its streamlined, fast, and cross-platform nature, Node.js can greatly simplify projects by unifying the stack. Although Node.js is not a web server, it allows servers (things that can be programmed in JavaScript) to exist in environments outside of the actual web client.
As Node.js becomes more and more popular, its security issues are the focus of hackers and security researchers.
Template engine
Template engine allows (website) programs to achieve interface and data separation, business code and logic code separation, which greatly improves development efficiency, good design also makes code reuse easier.
Pug is a robust, flexible, and feature-rich HTML template engine developed specifically for the Node.js platform. Pug was renamed from Jade. Pug writes code by indentation (representing the nested relationship between tags). During the compile process, there is no need to consider whether the tag is closed or not.
This is the official website of pug, you can take a look at its syntax: https://www.pugjs.cn/api/getting-started.html.
Template injection
The server level accepts user input as part of the web application template, allowing the underlying template to be modified and the template engine to execute malicious content inserted by the user during rendering.
This can be used maliciously in wikis, WSYWIG, or email templates. This rarely happens inadvertently, so it is often misinterpreted as just XSS.
PUG
First, you can download a target drone
1 | sudo docker pull registry.cn-shanghai.aliyuncs.com/yhskc/chatsys:latest |
After running, you can choose one of the PUG XSS types for actual combat. There are five types in total, and each type prompts what the template in the background code is.
You can also download PUG template directly to see what the complete background template code is? I just posted it here.
1 | doctype html |
We can refer to this official doc to see how this template syntax is used: https://www.pugjs.cn/language/interpolation.html
String embedding, translation
This is the name3 exercise, that is, ‘script.var user3 = #{name3};’, the official doc says:
The code inside #{and} will also be evaluated, escaped, and finally embedded in the rendered output of the template. It can be any correct JavaScript expression, you are free to play around with it. Pug is smart enough to figure out where the end of the embedded expression is, so you don’t have to worry about} in the expression, and you don’t need extra escaping. If you need to represent a #{text, you can escape it or generate it with the embedding function (yes, this is very metaprogramming).
Then we enter a piece of’alert (1) ', then this piece of code can be successfully executed
String embedded, not translated
This is an exercise for name2, that is, here’No results found for! {name2} ', where name2 is untranslated and can be injected with any code.
RCE (Remote Code Execution)
The program does not detect input properly, allowing attackers to execute unexpected code or system commands. This allows attackers to obtain Webshell and gain control rights over the host. It must be looked for in every intrusion and web application penetration test.
In the Node.js project, JavaScript is used as the development language. If there is a template injection vulnerability, it will often be upgraded to RCE (Remote Command Execution).
Principle
To get shell execution, we have to find the right function to execute in Node/JavaScript.
In JavaScript, there is a special object called Global Object, which and all its properties can be accessed anywhere in the program, namely global variables.
In browser JavaScript, usually window is a global object, while the global object in Node.js is global, and all global variables (except global itself) are properties of the global object.
We will identify the root node of our own global object, and then proceed to determine which modules and functions we can access. We ** In Pug, the “=” character allows us to output JavaScript results. **
Shell execution
You want to eventually import child_process using the Require function to run operating system commands.
In the drone container we just downloaded, select DirectMessage.
Then try to send a normal request based on the usage page, and use Burp to fetch the request.
1 | GET /ti?user=123&comment=123&link=123 HTTP/1.1 |
This is the returned raw.
1 | HTTP/1.1 200 OK |
This is the request caught by burp. You can see that the content of our user is rendered by the pug in the background. We try to execute a piece of code here to make the user ‘= 8 * 8’. Here, we need to use the encoder of burp to perform url encoding and then put it in.
After trying, we found that it can indeed be executed and returned 64, which means that this code has an xss injection vulnerability, where we can execute any code we want.
Note that the following code is written in the original form, and you need to code it first when injecting it.
1 | each val,index in global |
After this code is injected, each attribute of the global variable will be output as a p tag, and we can obtain all the attributes.
Then gradually search, we will find’global.process.mainModule.require ', which is the require statement we used, and then we can construct such a code
1 | var x = global.process.mainModule.require |
In this way, when we access the account.txt file again, we can get all the passwds.
How to quickly find code vulnerabilities in template engines
You can use tplmap to find vulnerabilities, similar to sqlmap.