Introduction
Context Explanation
The “Temptation” challenge is a web application security exercise focusing on template injection vulnerabilities in Python web applications.
Directive
The goal is to retrieve a flag by exploiting vulnerabilities in the web application.
Solution
Analyzing source code
First, we need to retrieve the application’s source code. The webpage contains a hidden comment suggesting to look at /?source
.
|
|
However, directly accessing this path doesn’t reveal anything.
After testing different inputs, we discover that adding any parameter value (e.g., /?source=anything
) reveals the source code.
|
|
The source code analysis reveals several key points:
- The application uses the
web.py
framework - The flag is stored in
/tmp/flag.txt
- A form asks for a “temptation” input
- Multiple security checks are implemented:
|
|
|
|
The interesting part is the template string evaluation using web.template.Template()
with an f-string, which is vulnerable to Python code injection.
|
|
Exploitation
The vulnerability lies in the template processing. While the application prevents using the word flag
directly and requires the final output to be FLAG
, we can bypass these restrictions using template injection.
- First, we confirm the vulnerability using a time-based payload:
|
|
Access to the flag
- After confirming the RCE capability, we craft a payload to exfiltrate the flag:
|
|
This payload:
- Uses
os.system()
to execute shell commands - Reads the flag file using
cat
with a wildcard to avoid using “flag” in the payload - Base64 encodes the content to ensure safe transmission
- Exfiltrates the data using
curl
to a RequestBin endpoint
- Decoding the received base64 string:
|
|
Tips & Tricks
- When dealing with template injection, remember that f-strings in template contexts can be particularly dangerous
- Base64 encoding is useful for exfiltrating data while avoiding special character issues
- RequestBin (or similar services) are valuable tools for data exfiltration in CTF challenges
- Using wildcards (
f*.txt
) can help bypass word blacklists - Always check source code comments, they often contain valuable hints