# modules/features/pentest/casts.nix — record the terminal, as raw material. # # htbcast new session in the current box: # ~/htb//casts/session__.cast # or, with no box set, a terminal session: # ~/htb/_termsessions/termsession__.cast # htbcast -t force a termsession even when a box is current # htbcast -n foothold name it yourself # htbcast -b sauna record into a box that is not the current one # htbcast ls [box|term] recordings, newest first, with durations # htbcast play [name] replay it in the terminal (default: newest); # idle gaps capped at 2s (-i N, -i 0 = real), -s speed # htbcast txt [name] plain-text transcript -> .txt # htbcast gif [name] animated GIF -> .gif # name: a name, a path (casts/x.cast), a box # (newest of it), or a unique part (01) # # "The current box" is whatever `htbbox new` / `htbbox use` last pointed at # ($STATE/box); after `htbbox clear` recordings go to termsessions instead. # is one past the highest number already in that folder, and each new # session claims its name atomically (mkdir), so any number of terminals can # start htbcast in the same second without overwriting each other. # # Several at once is the normal case: one terminal running a scan, another # working the shell. Each gets its own cast, so the default name carries a # counter and a timestamp rather than needing a flag. # # Why asciinema and not a screen recorder: a .cast is JSON lines of # {time, "o", bytes}, so the whole session is machine-readable. That is the # point -- these are the input an agent reads to write the write-up, and the # `txt` form is what you paste into a prompt. # # IMPORTANT, and the reason `gif` is not the deliverable: the website's # terminal GIFs are generated from a spec, never from a recording # (the website repo's AGENTS.md, "ASCII terminal clips"), because a real session # carries typos, dead waits and a scrollback that disagrees with the prose. # So `htbcast gif` is for previewing and for judging what to cut; the published # clip is still written as script/clips/specs/.json. # # Version note: nixpkgs' asciinema is 3.x and writes asciicast v3. The renderer # is `asciinema-agg` (bin/agg), NOT `pkgs.agg`, which is AntiGrain Geometry, a # C++ graphics library with no agg binary at all. agg 1.9 does read v3 -- this # was checked by recording a cast and rendering it, not assumed. { lib, ... }: let # Hoisted out of the module so perSystem.checks can build the same script the # system installs. A check against a different derivation is not a check. mkHtbcast = pkgs: pkgs.writeShellScriptBin "htbcast" '' set -uo pipefail PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.gnused pkgs.gnugrep pkgs.jq pkgs.asciinema pkgs.asciinema-agg ]}:$PATH STATE="''${XDG_STATE_HOME:-$HOME/.local/state}/htb" ROOT="$HOME/htb" TERMDIR="$ROOT/_termsessions" # "_" so htbbox ls never mistakes it for a box # printf, not a heredoc -- see the note in boxes.nix: an indented # terminator inside a Nix string never terminates. usage() { printf '%s\n' \ 'usage:' \ ' htbcast [-b box | -t] [-n name] start a new session (box, else termsession)' \ ' htbcast ls [box|term] list recordings' \ ' htbcast play [name] [-b box|-t] replay (default: newest)' \ ' -i SECS cap idle gaps (default 2, 0 = real time) -s N speed' \ ' htbcast txt [name] [-b box|-t] plain-text transcript' \ ' htbcast gif [name] [-b box|-t] animated GIF (preview only, see header)' \ ' name: a name, a path (casts/x.cast), a box, or a unique part (01)' } current() { [ -s "$STATE/box" ] && [ -d "$ROOT/$(cat "$STATE/box")" ] && cat "$STATE/box"; } sub="''${1:-rec}" case "$sub" in ls|play|txt|gif) shift ;; rec) [ "$#" -gt 0 ] && shift ;; -h|--help) usage; exit 0 ;; *) sub=rec ;; esac box="" name="" term="" idle=2 speed=1 # `play`/`txt`/`gif` take the name positionally; everything else by flag. case "$sub" in play|txt|gif) name="''${1:-}"; [ "$#" -gt 0 ] && shift ;; ls) case "''${1:-}" in -*|"") ;; term) term=1; shift ;; *) box="$1"; shift ;; esac ;; esac while [ "$#" -gt 0 ]; do case "$1" in -b|--box) box="''${2:-}"; shift 2 ;; -t|--term) term=1; shift ;; -n|--name) name="''${2:-}"; shift 2 ;; -i|--idle) idle="''${2:-}"; shift 2 ;; -s|--speed) speed="''${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "htbcast: unknown option: $1" >&2; usage >&2; exit 2 ;; esac done # Where this session lives: an explicit box, else -t, else the current # box, else a terminal session. explicit="$box$term" if [ -z "$term" ] && [ -z "$box" ]; then box=$(current) || box=""; fi if [ -n "$term" ] || [ -z "$box" ]; then dir="$TERMDIR" prefix="termsession" else case "$box" in *[!A-Za-z0-9_.-]*|.*|_*) echo "htbcast: bad box name: $box" >&2; exit 2 ;; esac [ -d "$ROOT/$box" ] || { echo "htbcast: no such box: $box (htbbox ls)" >&2; exit 2; } dir="$ROOT/$box/casts" prefix="''${box}session" fi mkdir -p "$dir" case "$sub" in rec) if [ -n "$name" ]; then case "$name" in *[!A-Za-z0-9_.-]*|.*) echo "htbcast: bad name: $name" >&2; exit 2 ;; esac [ -e "$dir/$name.cast" ] && { echo "htbcast: $dir/$name.cast exists — pick another -n" >&2; exit 2; } mkdir "$dir/.$name.claim" 2>/dev/null || { echo "htbcast: $name is being recorded already" >&2; exit 2; } else # One past the highest _NN already here (not the file count, so a # deleted recording never makes two sessions share a number). n=0 for c in "$dir"/*_[0-9][0-9]*.cast "$dir"/.*_[0-9][0-9]*.claim; do [ -e "$c" ] || continue k=$(basename "$c"); k=''${k%.*}; k=''${k##*_} case "$k" in ""|*[!0-9]*) continue ;; esac k=$((10#$k)); [ "$k" -gt "$n" ] && n=$k done # mkdir is atomic: if another terminal claimed this number in the # same instant, take the next one. while :; do n=$((n + 1)) name=$(printf '%s_%s_%02d' "$prefix" "$(date +%Y%m%d-%H%M%S)" "$n") [ -e "$dir/$name.cast" ] && continue mkdir "$dir/.$name.claim" 2>/dev/null && break done fi out="$dir/$name.cast" trap 'rmdir "$dir/.$name.claim" 2>/dev/null' EXIT echo "htbcast: recording to $out — exit the shell (or ctrl-d) to stop" # --title so `ls` and the website spec have something to read back. asciinema rec --title "$name" "$out" ;; ls) # One write then exit 0: `htbcast ls | grep -q x` closes the pipe on # its first match and EPIPE would otherwise set a failing status. out="$dir " found="" for c in $(ls -1t "$dir"/*.cast 2>/dev/null); do found=1 # v3 stores each event's gap since the last one, v2 its absolute # time: the length is the sum in v3 and the max in v2. secs=$(jq -rs '(.[0].version // 2) as $v | map(select(type=="array") | .[0]) | if length == 0 then "?" else (if $v >= 3 then add else max end | floor | tostring) + "s" end' "$c" 2>/dev/null || echo "?") out="$out$(printf ' %-44s %-6s %s' "$(basename "$c" .cast)" "$secs" "$(du -h "$c" | cut -f1)") " done [ -n "$found" ] || out="$out no recordings yet — htbcast " printf '%s' "$out" || true exit 0 ;; play|txt|gif) # What `name` may be, tried in this order: # (nothing) | last the newest recording here # a path to a .cast casts/x.cast, ~/htb/pov/casts/x, as tab-completed # a name, .cast optional povsession_20261013-195539_01 # a box pov -> that box's newest recording # a number 01 -> the _01 recording # part of a name 195539 -- if exactly one matches newest() { ls -1t "$1"/*.cast 2>/dev/null | head -1; } c="" if [ -z "$name" ] || [ "$name" = last ]; then c=$(newest "$dir") [ -n "$c" ] || { echo "htbcast: no recordings in $dir (htbcast ls)" >&2; exit 2; } elif [ -f "$name" ] || [ -f "$name.cast" ]; then [ -f "$name" ] && c="$name" || c="$name.cast" else base=''${name##*/}; base=''${base%.cast} if [ -f "$dir/$base.cast" ]; then c="$dir/$base.cast" elif [ -z "$explicit" ] && [ -d "$ROOT/$base/casts" ] && [ -n "$(newest "$ROOT/$base/casts")" ]; then c=$(newest "$ROOT/$base/casts") else # A bare number is the _NN counter first (01 must not match # 20261013), and only then any part of a name (195539). matches="" case "$base" in *[!0-9]*) ;; *) matches=$(ls -1t "$dir"/*.cast 2>/dev/null | grep -E -- "_0*$((10#$base))\.cast$" || true) ;; esac [ -n "$matches" ] || matches=$(ls -1t "$dir"/*.cast 2>/dev/null | grep -F -- "$base" || true) case "$(printf '%s' "$matches" | grep -c .)" in 1) c="$matches" ;; 0) echo "htbcast: no recording matching '$name' in $dir" >&2 htbcast ls ''${box:+-b "$box"} ''${term:+-t} >&2; exit 2 ;; *) echo "htbcast: '$name' matches several recordings — be more specific:" >&2 printf '%s\n' "$matches" | sed 's|.*/| |; s|\.cast$||' >&2; exit 2 ;; esac fi fi [ -s "$c" ] || { echo "htbcast: empty recording: $c" >&2; exit 2; } echo "htbcast: $c" >&2 # txt/gif land next to the cast, wherever it was found. case "$sub" in # Idle gaps capped (-i, default 2s): a scan you waited 20 minutes # on replays as 2 seconds. -i 0 keeps real time. play) case "$idle$speed" in *[!0-9.]*) echo "htbcast: -i/-s take numbers" >&2; exit 2 ;; esac if [ "$idle" = 0 ]; then exec asciinema play -s "$speed" "$c" else exec asciinema play -i "$idle" -s "$speed" "$c"; fi ;; txt) asciinema convert --overwrite --output-format txt "$c" "''${c%.cast}.txt" && echo "''${c%.cast}.txt" ;; gif) agg "$c" "''${c%.cast}.gif" && echo "''${c%.cast}.gif" ;; esac ;; esac ''; in { flake.nixosModules.pentest-casts = { config, pkgs, lib, ... }: { config = lib.mkIf config.daemon.pentest.enable { environment.systemPackages = [ (mkHtbcast pkgs) pkgs.asciinema pkgs.asciinema-agg ]; }; }; # Exercises the whole chain on a real recording: record -> ls -> txt -> gif. # The gif step is the one that matters most: nixpkgs' asciinema is 3.x and # writes asciicast v3, and agg is 1.9. If a future bump breaks that pairing # this check goes red instead of `htbcast gif` failing mid-write-up. perSystem = { pkgs, ... }: { checks.pentest-casts = pkgs.runCommand "pentest-casts-check" { nativeBuildInputs = [ (mkHtbcast pkgs) pkgs.asciinema pkgs.asciinema-agg pkgs.jq ]; # agg rasterises text, so it needs a font to exist. The build sandbox # has no fontconfig at all ("Error: no faces matching font family # options"), while the real system does -- so the font is pinned here # rather than in cast, which should use whatever the host has. # DejaVu Sans Mono is in agg's default family list. FONTCONFIG_FILE = pkgs.makeFontsConf { fontDirectories = [ pkgs.dejavu_fonts ]; }; } '' export HOME=$(mktemp -d) box="$HOME/htb/sauna" mkdir -p "$box/casts" # A recording made the way htbcast makes one. --command works without # an interactive tty, which is why this can run in a derivation. asciinema rec --command 'printf "whoami\nnt authority\\system\n"' \ "$box/casts/foothold.cast" >/dev/null 2>&1 test -s "$box/casts/foothold.cast" || { echo "no cast produced"; exit 1; } grep -q '"version":3' "$box/casts/foothold.cast" \ || { echo "expected an asciicast v3 header"; exit 1; } # ls length of a v3 cast is the sum of its gaps, not the largest gap. printf '%s\n' '{"version":3,"term":{"cols":80,"rows":24}}' '[1.5, "o", "a"]' '[2.5, "o", "b"]' \ > "$box/casts/timing.cast" htbcast ls -b sauna | grep timing | grep -q ' 4s ' \ || { echo "v3 length should be 4s: $(htbcast ls -b sauna | grep timing)"; exit 1; } rm "$box/casts/timing.cast" # ls must name it, and must not be killed by a reader closing the pipe. htbcast ls -b sauna | grep -q foothold || { echo "ls did not list the cast"; exit 1; } htbcast ls -b sauna | head -1 >/dev/null || { echo "ls died on EPIPE"; exit 1; } # txt: the transcript an agent is handed. htbcast txt foothold -b sauna >/dev/null grep -q 'nt authority' "$box/casts/foothold.txt" \ || { echo "transcript missing the session output"; exit 1; } # Every way a recording can be named must find the same file. rm -f "$box/casts/foothold.txt" for spec in foothold foothold.cast "$box/casts/foothold.cast" "$box/casts/foothold" foot last sauna; do rm -f "$box/casts/foothold.txt" if [ "$spec" = sauna ]; then htbcast txt sauna >/dev/null 2>&1 else htbcast txt "$spec" -b sauna >/dev/null 2>&1; fi test -s "$box/casts/foothold.txt" || { echo "name '$spec' did not resolve"; exit 1; } done # Twice in a row: the transcript is derived, so it is regenerated. (cd "$box" && htbcast txt casts/foothold.cast -b sauna >/dev/null 2>&1) \ || { echo "relative path casts/foothold.cast did not resolve"; exit 1; } # A bare number is the _NN counter, not a substring of the date. cp "$box/casts/foothold.cast" "$box/casts/saunasession_20260107-010101_07.cast" for spec in 7 07; do rm -f "$box/casts/saunasession_20260107-010101_07.txt" htbcast txt "$spec" -b sauna >/dev/null 2>&1 test -s "$box/casts/saunasession_20260107-010101_07.txt" \ || { echo "number '$spec' did not pick the _07 recording"; exit 1; } done # No _01 here, so 01 falls back to a substring: it is in the date of # the _07 recording only, never in "foothold". rm -f "$box/casts/saunasession_20260107-010101_07.txt" htbcast txt 01 -b sauna >/dev/null 2>&1 test -s "$box/casts/saunasession_20260107-010101_07.txt" \ || { echo "01 should fall back to the date match"; exit 1; } htbcast txt nosuchcast -b sauna >/dev/null 2>&1 \ && { echo "unknown recording must exit non-zero"; exit 1; } || true # gif: asciinema 3 cast through agg 1.9. htbcast gif foothold -b sauna >/dev/null test -s "$box/casts/foothold.gif" || { echo "agg produced no gif"; exit 1; } head -c6 "$box/casts/foothold.gif" | grep -q GIF89a \ || { echo "output is not a gif"; exit 1; } # A box that does not exist must fail, and say how to list them. # Captured rather than piped: the stdenv builder runs with pipefail, # so cast's intended non-zero exit would fail the pipeline even # when grep matches. msg=$(htbcast ls -b nosuchbox 2>&1 || true) printf '%s' "$msg" | grep -q 'htbbox ls' \ || { echo "unknown box should point at htbbox ls, got: $msg"; exit 1; } htbcast ls -b nosuchbox >/dev/null 2>&1 \ && { echo "unknown box must exit non-zero"; exit 1; } || true echo "record -> ls -> txt -> gif all pass" > $out ''; }; }