Technical Analysis of "Explotación y prevención de SSTI" (Server-Side Template Injection)
Overview:
This presentation by Artssec provides a comprehensive overview of Server-Side Template Injection (SSTI), detailing its exploitation and prevention methods. SSTI occurs when user input is directly embedded into a server-side template, allowing attackers to execute arbitrary code on the server.
Key Technical Details:
Understanding SSTI:
- Template Engines:
- Template engines are used to dynamically generate HTML or other text output.
-
Common engines include Jinja2 (Python), Twig (PHP), and Freemarker (Java).
-
Vulnerability Mechanism:
- SSTI arises when user input is concatenated directly into the template without proper sanitization or escaping.
- Example:
{{ user_input }}
in a Jinja2 template.
Exploitation Techniques:
- Basic Payloads:
- Injection can be simple, such as
{{ 7*7 }}
which outputs49
if the engine evaluates it directly. -
Testing involves injecting non-malicious payloads that demonstrate execution, such as
{{%20}}
in Jinja2. -
Advanced Payloads:
- Exploitation often involves accessing built-in methods or sensitive variables.
- In Jinja2, accessing
config
orself.__globals__.__builtins__.eval
can lead to arbitrary code execution.
Example payload in Jinja2:
jinja
{{ config.items() }}
- Escalating the Attack:
- Attackers may use the template engine to read server files, execute commands, or pivot to further systems.
Python exploitation via os
module:
jinja
{{ ''.__class__.mro()[1].__subclasses__()[407]("cat /etc/passwd", shell=True, stdout=-1).communicate() }}
Real-World Examples:
- Case Studies:
-
The presentation references real-world cases where SSTI led to significant breaches, demonstrating the impact of these vulnerabilities.
-
Common Platforms:
- Applications in various programming languages are at risk, especially those using template engines with improper input handling.
Prevention and Mitigation:
- Input Sanitization:
- Ensure user inputs are properly sanitized before being included in templates.
-
Utilize templating functions that automatically escape user input.
-
Context-Aware Escaping:
- Implement context-aware escaping, where the nature of the input context determines the escaping strategy.
-
Example for HTML context: converting
<
to<
. -
Least Privilege:
- Follow the principle of least privilege for the processes that handle templates.
-
Restrict filesystem access and execution rights for the template engine process.
-
Security Measures:
- Use static analysis tools to detect potential SSTI vulnerabilities during development.
- Implement Content Security Policy (CSP) to reduce the impact of a successful injection.
Detection:
- Automated Testing:
- Use automated security scanners to detect SSTI vulnerabilities during the development cycle.
- Manual Testing:
- Penetration testers should craft and inject specific payloads to identify injection points.
- Monitoring:
- Implement logging and monitoring to detect exploitation attempts against the template engine.
Conclusion:
The presentation effectively outlines the high risk associated with SSTI vulnerabilities and underscores the importance of robust input handling and sanitization practices. By illustrating both basic and advanced exploitation techniques, it provides a clear roadmap for security professionals to understand the mechanics of SSTI and implement effective prevention and mitigation strategies.
Additional Resources: - For further reading and advanced detection tools, refer to online repositories and security forums specializing in web application security and template engine exploitation techniques.
Source:
For complete details, visit the original slides here.