diff --git a/.github/workflows/image.yml b/.github/workflows/image.yml index 5a99cb6..af7d346 100644 --- a/.github/workflows/image.yml +++ b/.github/workflows/image.yml @@ -8,7 +8,9 @@ name: Attack box image # never reaches players. The launcher doesn't follow `latest`: it pins the image by digest, with # this repository's files as they were at the commit that published it. Once a publish is out, the # pushed digest is pulled and smoke-tested again on both architectures, then the `pin` job opens a -# pull request into the launcher's dev with both. +# pull request into the launcher's dev with both. Each architecture's image carries BuildKit's SBOM +# and provenance attestations, and the published index a GitHub build provenance attestation +# (README.md, "Verify the image"). on: pull_request: push: @@ -33,18 +35,8 @@ jobs: timeout-minutes: 10 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - # The VM set-up's parts are fragments of one script (vm/kali.sh defines `$keep`): each - # composition the launcher makes must parse, with the player's account between the toolset - # and the desktop. - - name: The VM set-up parses - run: | - set -euo pipefail - for parts in "vm/kali.sh vm/toolset.sh" "vm/kali.sh vm/toolset.sh vm/desktop.sh" "vm/kali.sh vm/desktop.sh"; do - # shellcheck disable=SC2086 - { cat $parts; } | sh -n - done - - name: shellcheck - run: shellcheck player-account.sh smoke-test.sh + # The VM set-up's compositions parse, and shellcheck (check.sh, as a contributor runs it). + - run: ./check.sh scripts smoke: strategy: @@ -61,11 +53,9 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 # The image is large (~14 GB unpacked): Docker keeps its data on the runner's scratch disk. - uses: ./.github/actions/scratch-disk-docker - # No registry, no secrets: a plain local build, so pull requests from forks run it too. - - name: Build - run: docker build --pull -t attack-box:smoke . - - name: Smoke test - run: ./smoke-test.sh attack-box:smoke + # No registry, no secrets: a plain local build and its smoke test (check.sh), so pull + # requests from forks run it too. + - run: ./check.sh image build: needs: [scripts, smoke] @@ -93,6 +83,10 @@ jobs: context: . platforms: ${{ matrix.platform }} outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=true + # BuildKit's attestations, pushed with the image: an SPDX SBOM of its packages and the + # full SLSA provenance of its build (the repository has no build secrets to leak). + sbom: true + provenance: mode=max - run: | set -euo pipefail mkdir -p /tmp/digests @@ -108,6 +102,10 @@ jobs: needs: build runs-on: ubuntu-24.04 timeout-minutes: 20 + permissions: + contents: read + id-token: write # sign the provenance attestation (Sigstore, keyless) + attestations: write outputs: digest: ${{ steps.digest.outputs.digest }} steps: @@ -139,6 +137,13 @@ jobs: [[ $digest =~ ^sha256:[0-9a-f]{64}$ ]] || { echo "::error::unexpected digest '$digest'"; exit 1; } echo "Published $IMAGE@$digest" echo "digest=$digest" >> "$GITHUB_OUTPUT" + # The published index's build provenance, signed through Sigstore, kept by GitHub and pushed + # to Docker Hub beside the image: `gh attestation verify oci://...`. + - uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-name: index.docker.io/${{ env.IMAGE }} + subject-digest: ${{ steps.digest.outputs.digest }} + push-to-registry: true # The pushed image itself, by digest, on both architectures: the smoke job tested a local build. verify: @@ -169,7 +174,7 @@ jobs: timeout-minutes: 15 env: LAUNCHER: Cyber-Courses/Cyber-CTF-Launcher - DIR: src-tauri/attack-box + PIN: src-tauri/attack-box.json steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -187,22 +192,25 @@ jobs: PINNED: ${{ env.IMAGE }}@${{ needs.publish.outputs.digest }} run: | set -euo pipefail - file="$DIR/image.json" - if [ "$(jq -er .image "$file")" = "$PINNED" ]; then - echo "The launcher already pins $PINNED" + if [ "$(jq -er .image "$PIN")" = "$PINNED" ] && [ "$(jq -er .commit "$PIN")" = "$GITHUB_SHA" ]; then + echo "The launcher already pins $PINNED with the files at $GITHUB_SHA" exit 0 fi - # The old pin joins `previous`, so a player's saved old default moves to the new one. - jq --arg new "$PINNED" '.previous = ([.image] + .previous | map(select(. != $new)) | unique) | .image = $new' "$file" > "$RUNNER_TEMP/image.json" - mv "$RUNNER_TEMP/image.json" "$file" - cp ../attack-box/player-account.sh "$DIR/player-account.sh" - mkdir -p "$DIR/vm" - cp ../attack-box/vm/kali.sh ../attack-box/vm/toolset.sh ../attack-box/vm/desktop.sh "$DIR/vm/" + # The launcher keeps no copy of the scripts: it downloads them at this commit and checks + # each against its SHA-256 here. + files="$(cd ../attack-box && for f in player-account.sh vm/kali.sh vm/toolset.sh vm/desktop.sh; do + printf '%s\t%s\n' "$f" "$(sha256sum "$f" | cut -d' ' -f1)"; done | jq -Rn '[inputs | split("\t") | {(.[0]): .[1]}] | add')" + # An image that changed: the old one joins `previous`, so a player's saved old default + # moves to the new one. + jq --arg new "$PINNED" --arg commit "$GITHUB_SHA" --argjson files "$files" \ + '(if .image != $new then .previous = ([.image] + .previous | map(select(. != $new)) | unique) else . end) + | .image = $new | .commit = $commit | .files = $files' "$PIN" > "$RUNNER_TEMP/attack-box.json" + mv "$RUNNER_TEMP/attack-box.json" "$PIN" branch="attack-box-pin-${PINNED: -12}" git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git checkout -q -b "$branch" - git add "$DIR" + git add "$PIN" git commit -qm "Attack box: pin ${PINNED#*@}" -m "Published and smoke-tested on amd64 and arm64 by $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID, with the attack box's files at $GITHUB_SHA." git push -q origin "$branch" gh pr create --repo "$LAUNCHER" --base dev --head "$branch" \ diff --git a/README.md b/README.md index 444d8aa..adc32d6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,9 @@ The launcher assembles one `sh` script from these parts, in this order: 1. `vm/kali.sh`: on a Debian VM (where Kali has no image for the computer's CPU), Kali's archive in place of Debian's, its keyring checked against Kali's two archive keys before apt trusts it, keeping the VM's own kernel. It defines `$keep`, which the parts after it use. -2. `vm/toolset.sh`: Kali's headless toolset, unless the box has it already. +2. `vm/toolset.sh`: nothing where the box has Kali's toolset (Kali's own box, amd64); elsewhere (a + Debian VM made Kali, on Arm) a small set, the same for every lab: Kali's core and the common web, + network and password tools. The player installs more with `sudo apt install`. 3. The player's account: `player-account.sh`, run by the launcher with the box's shared folder and password file. 4. `vm/desktop.sh`, for a box with a desktop: XFCE, `player` signed in by LightDM, no screen lock. @@ -25,21 +27,40 @@ The launcher assembles one `sh` script from these parts, in this order: - The account is `player` (`ARG PLAYER=player`), with a home, passwordless sudo and a password of the box's own; the launcher opens its shell as that account. `smoke-test.sh` checks it. -- The launcher pins this repository's output: the image by its digest, and these files as they - were at the commit that published it. A publish opens a pull request into the launcher's `dev` - with both (`.github/workflows/image.yml`, its `pin` job). +- The launcher pins this repository's output in its `src-tauri/attack-box.json`: the image by its + digest, the commit that published it, and each script's SHA-256 there. It keeps no copy of the + scripts: it downloads them at that commit and refuses one whose hash differs. A publish opens a + pull request into the launcher's `dev` with the new pin (`.github/workflows/image.yml`, its `pin` + job). +- The scripts' behaviour is tested here (`tests/scripts.sh`, run by `./check.sh`); the launcher + tests only how it puts them together. ## Building and testing ```sh -docker build --pull -t attack-box:local . -./smoke-test.sh attack-box:local +./check.sh # the scripts: the VM set-up's compositions parse, shellcheck +./check.sh image # the image built locally and smoke-tested +./check.sh all # both ``` -The workflow builds and smoke-tests every pull request on amd64 and arm64; a push to `main`, the +The workflow runs the same command: it checks the scripts, and builds and smoke-tests every pull request on amd64 and arm64; a push to `main`, the weekly run or a manual run publishes, only once both pass, then pulls the published digest and smoke-tests it again before pinning it in the launcher. It needs the Docker Hub organization's credentials (`DOCKER_ORGA`, `DOCKER_OAT`) and, for the pin, `LAUNCHER_TOKEN`: a token that can push a branch to the launcher's repository and open a pull request there. + +## Verify the image + +Each architecture's image carries BuildKit's attestations (an SPDX SBOM of its packages and the +SLSA provenance of its build), and the published multi-arch index a build provenance attestation +signed through Sigstore and kept by GitHub. With the digest the launcher pins: + +```sh +PINNED=cyberctf/attack-box@sha256:... +docker buildx imagetools inspect "$PINNED" --format '{{ json .SBOM }}' # the SBOM +docker buildx imagetools inspect "$PINNED" --format '{{ json .Provenance }}' # BuildKit's provenance +gh attestation verify "oci://index.docker.io/$PINNED" --repo CyberCTF/attack-box \ + --signer-workflow CyberCTF/attack-box/.github/workflows/image.yml # built by image.yml here +``` diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..3f6eb3f --- /dev/null +++ b/check.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# The attack box's check, the command CI runs (.github/workflows/image.yml) and a contributor runs: +# +# ./check.sh the scripts: the VM set-up's compositions parse, shellcheck passes, and +# their tests (tests/scripts.sh) pass +# ./check.sh image the image built locally (no registry, no secrets) and smoke-tested +# ./check.sh all both +# +# The VM set-up's parts are fragments of one script (vm/kali.sh defines `$keep`): each composition +# the launcher makes must parse, with the player's account between the toolset and the desktop. +set -euo pipefail +cd "$(dirname "$0")" + +scripts() { + local parts + for parts in "vm/kali.sh vm/toolset.sh" "vm/kali.sh vm/toolset.sh vm/desktop.sh" "vm/kali.sh vm/desktop.sh"; do + # shellcheck disable=SC2086 # the list is the files, split on purpose + if ! cat $parts | sh -n; then echo "check: the VM set-up doesn't parse as $parts" >&2; return 1; fi + done + shellcheck check.sh player-account.sh smoke-test.sh tests/scripts.sh + tests/scripts.sh + echo "check: scripts ok" +} + +image() { + docker build --pull -t attack-box:smoke . + ./smoke-test.sh attack-box:smoke + echo "check: image ok" +} + +case "${1:-scripts}" in + scripts) scripts ;; + image) image ;; + all) scripts && image ;; + *) echo "usage: ./check.sh [scripts|image|all]" >&2; exit 2 ;; +esac diff --git a/player-account.sh b/player-account.sh index 5d3e40f..b19bbe6 100644 --- a/player-account.sh +++ b/player-account.sh @@ -3,6 +3,11 @@ # login shell and sudo without a password. Where the shared folder ("$shared", mounted from the # computer running the lab) belongs to a user the box doesn't have, the account takes that user's # uid, and it joins the folder's group: the player writes there as that computer's user does. +# Where the folder's mount writes as its owner whoever writes ("$shared_squashed" is 1), the folder's +# group is the account's own (primary) group instead: QEMU's virtiofsd on Linux runs unprivileged, +# every account of the box squashed to the computer's user, and refuses a write made through a +# supplementary group (it would have to take that group itself, which only a privileged virtiofsd +# can), while the box still checks the folder's permissions, so the account needs the group. # Where the launcher gave the account a password, "$password_hash" is a root-only file holding its # crypt(3) hash: handed to chpasswd on its standard input, so neither the password nor its hash is # ever on a command line. The screen locker, a login, su and SSH take that password; sudo asks none. @@ -11,10 +16,11 @@ # may predate the account, or the folder's owner may differ from the image's), and once when a VM # box is set up. The launcher names the account (`attack_box::PLAYER`) and the folder. # -# account= [shared=] [password_hash=] sh player-account.sh +# account= [shared=] [shared_squashed=1] [password_hash=] sh player-account.sh set -e : "${account:?the account to make}" shared=${shared:-} +shared_squashed=${shared_squashed:-} password_hash=${password_hash:-} owner=0 @@ -43,7 +49,9 @@ fi if [ "$group" != 0 ]; then getent group "$group" >/dev/null || groupadd --gid "$group" shared || : name=$(getent group "$group" | cut -d: -f1) - if [ -n "$name" ]; then + if [ -n "$name" ] && [ "$shared_squashed" = 1 ]; then + usermod -g "$name" "$account" || echo "player-account: $shared's group isn't $account's own" >&2 + elif [ -n "$name" ]; then usermod -a -G "$name" "$account" || echo "player-account: $account isn't in $shared's group" >&2 fi fi diff --git a/tests/scripts.sh b/tests/scripts.sh new file mode 100755 index 0000000..d2941f2 --- /dev/null +++ b/tests/scripts.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2016 # the scripts' own text, its `$` literal +# The scripts' behaviour, on stand-ins for the tools they call (no root, no VM): what the launcher +# relies on when it downloads them at its pinned commit. Run by ./check.sh. Moved from the +# launcher's own tests (src-tauri/src/runtime/attack_box/tests.rs) when it stopped keeping a copy. +set -euo pipefail +cd "$(dirname "$0")/.." + +failed=0 +fail() { echo "FAIL: $*" >&2; failed=1; } +ok() { echo "ok: $*"; } +contains() { case "$1" in *"$2"*) return 0 ;; *) return 1 ;; esac; } +# The position of $2 in $1 (its first), -1 when absent. +at() { local rest="${1%%"$2"*}"; if [ "$rest" = "$1" ]; then echo -1; else echo "${#rest}"; fi; } + +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +# Stand-in programs in a folder of their own, first on PATH. +stub() { mkdir -p "$1"; printf '%s\n' "$3" > "$1/$2"; chmod 755 "$1/$2"; } + +# --- vm/kali.sh: Kali's archive keyring checked before apt trusts it -------------------------- +kali="$(cat vm/kali.sh)" +for key in 827C8569F2518CC677FECA1AED65462EC8D5E4C5 44C6513A8E4FB3D30875F758ED444FF07D8D0BF6; do + contains "$kali" "$key" || fail "kali.sh: Kali's archive key $key" +done +download=$(at "$kali" "archive.kali.org/archive-keyring.gpg") +checked=$(at "$kali" '[ "$k" = "44C6513A8E4FB3D30875F758ED444FF07D8D0BF6 827C8569F2518CC677FECA1AED65462EC8D5E4C5" ]') +refused=$(at "$kali" "Nothing was installed") +trusted=$(at "$kali" "/usr/share/keyrings/kali-archive-keyring.gpg") +used=$(at "$kali" "signed-by=") +if [ "$download" -ge 0 ] && [ "$download" -lt "$checked" ] && [ "$checked" -lt "$trusted" ] && [ "$refused" -lt "$trusted" ] && [ "$trusted" -lt "$used" ]; then + ok "kali.sh checks the keyring before apt trusts it" +else + fail "kali.sh: downloaded ($download) < checked ($checked) and refused ($refused) < trusted ($trusted) < used ($used)" +fi +if ! contains "$kali" '-o "$d/key"' || contains "$kali" "-o /usr/share/keyrings"; then fail "kali.sh: the keyring is downloaded into the private folder"; fi + +# --- vm/toolset.sh: nothing on Kali's own box, a small fixed set elsewhere ------------------- +toolset_calls() { # $1: dpkg-query's answer for the toolset + local dir="$tmp/toolset-$RANDOM" + stub "$dir" dpkg-query "#!/bin/sh +printf '%s' '$1'" + stub "$dir" apt-get "#!/bin/sh +echo \"\$*\" >> '$dir/calls'" + { echo "keep=''"; cat vm/toolset.sh; } | PATH="$dir:/usr/bin:/bin" sh + cat "$dir/calls" 2>/dev/null || true +} +if [ -z "$(toolset_calls 'install ok installed')" ]; then ok "a box with the toolset takes nothing from the archive"; else fail "toolset.sh: a box with the toolset called apt-get"; fi +got="$(toolset_calls '')" +first="$(printf '%s\n' "$got" | head -1)" +install="$(printf '%s\n' "$got" | sed -n 2p)" +if [ "$first" = update ] && contains "$install" 'install -y kali-linux-core nmap' && contains "$install" ' sqlmap ' && ! contains "$install" kali-linux-headless; then + ok "a box without Kali's toolset (Debian made Kali, on Arm) is given the small set, not all of it" +else + fail "toolset.sh: without the toolset: got [$got]" +fi + +# --- player-account.sh: the account, its sudo, its password, the shared folder's group ------- +account="$(cat player-account.sh)" +if contains "$account" '{ printf '"'"'%s:'"'"' "$account"; cat "$password_hash"; } | chpasswd -e'; then ok "the password's hash goes to chpasswd on its standard input, never a command line"; else fail "player-account.sh: the hash read from its file into chpasswd -e"; fi +if ! contains "$account" "NOPASSWD: ALL" || ! contains "$account" "--create-home"; then fail "player-account.sh: a home and sudo with no password"; fi +account_calls() { # $1: shared_squashed's value; a shared folder owned by uid/gid 1000 (`vagrant`) + local dir="$tmp/account-$RANDOM" + mkdir -p "$dir/shared" "$dir/sudoers.d" + local record="#!/bin/sh +echo \"\$(basename \"\$0\") \$*\" >> '$dir/calls'" + stub "$dir/bin" stat "#!/bin/sh +echo 1000" + stub "$dir/bin" getent "#!/bin/sh +case \"\$1 \$2\" in + 'passwd 1000') echo 'vagrant:x:1000:1000::/home/vagrant:/bin/sh' ;; + 'group 1000') echo 'vagrant:x:1000:' ;; + *) exit 2 ;; +esac" + stub "$dir/bin" id "#!/bin/sh +exit 1" + for t in useradd usermod groupadd; do stub "$dir/bin" "$t" "$record"; done + # The sudo rule goes to the test's folder, not the computer's. + sed "s|/etc/sudoers.d|$dir/sudoers.d|g" player-account.sh > "$dir/player-account.sh" + PATH="$dir/bin:/usr/bin:/bin" account=player shared="$dir/shared" shared_squashed="$1" sh "$dir/player-account.sh" + [ -f "$dir/sudoers.d/player" ] || fail "player-account.sh: its sudo rule" + cat "$dir/calls" +} +shell=/bin/sh; [ -x /bin/bash ] && shell=/bin/bash +got="$(account_calls '')" +want="useradd --create-home --shell $shell player"$'\n'"usermod -a -G vagrant player" +if [ "$got" = "$want" ]; then ok "the player joins the shared folder's group"; else fail "player-account.sh: not squashed: got [$got], want [$want]"; fi +# Regression: on QEMU under Linux the player couldn't write in the shared folder through a +# supplementary group, which the unprivileged virtiofsd refuses. +got="$(account_calls 1)" +want="useradd --create-home --shell $shell player"$'\n'"usermod -g vagrant player" +if [ "$got" = "$want" ]; then ok "where the folder is squashed, its group is the player's own"; else fail "player-account.sh: squashed: got [$got], want [$want]"; fi + +# --- vm/desktop.sh: XFCE, the player signed in, the screen never locked ---------------------- +desktop="$(cat vm/desktop.sh)" +for want in "install -y \$keep kali-desktop-xfce lightdm" "autologin-user=player" "systemctl set-default graphical.target" \ + "xserver-command=X -s 0 -dpms" "for locker in light-locker xfce4-screensaver" "Hidden=true" "xset s off -dpms" \ + "off xfce4-screensaver /lock/enabled" "off xfce4-power-manager /xfce4-power-manager/lock-screen-suspend-hibernate" \ + "off xfce4-session /shutdown/LockScreen" "/etc/xdg/autostart/cyberctf-no-screen-lock.desktop" "/home/player/.config/autostart"; do + contains "$desktop" "$want" || fail "desktop.sh: $want" +done +# --- nothing accepted on the player's behalf --------------------------------------------------- +# No switch that accepts a licence, an agreement or terms for the player (comments aside): where a +# vendor asks, its own window shows it and the player accepts it there. Moved from the launcher's +# consent scan (src-tauri/src/testing/source_scan/consent.rs). +accepts='--accept-license|--accept-licence|--accept-package-agreements|--accept-source-agreements|--accept-eula|--accept-terms|--agree-tos|-license accept|accepted-license|license select true|accept_eula|accepteula|/eula' +for f in player-account.sh vm/*.sh Dockerfile bashrc.cyberctf; do + found="$(grep -vE '^[[:space:]]*#' "$f" | grep -inE -e "$accepts" || true)" + if [ -n "$found" ]; then fail "$f accepts for the player: $found"; fi +done + +if [ "$failed" = 0 ]; then ok "the scripts behave as the launcher relies on"; fi + +exit "$failed" diff --git a/vm/toolset.sh b/vm/toolset.sh index 100bedf..ac19482 100644 --- a/vm/toolset.sh +++ b/vm/toolset.sh @@ -1,4 +1,13 @@ +# Kali's tools. Kali's own box (amd64) ships its whole toolset (kali-linux-default): nothing is +# installed there. Elsewhere, a Debian VM made Kali by kali.sh (on Arm, where Kali has no box), +# gets a small set that serves every lab, the same for each so it hints at no lab's solution: +# Kali's core and the common web, network and password tools. The player installs more with +# `sudo apt install`. +tools='kali-linux-core nmap curl wget netcat-traditional socat dnsutils whois tcpdump jq git vim + sqlmap gobuster ffuf dirb nikto whatweb hydra john wordlists smbclient impacket-scripts + python3-requests' if ! dpkg-query -W -f='${Status}' kali-linux-headless 2>/dev/null | grep -q 'ok installed'; then apt-get update - apt-get install -y $keep kali-linux-headless + # shellcheck disable=SC2086 # $keep and $tools are lists of words + apt-get install -y $keep $tools fi