Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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" \
Expand Down
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
```
36 changes: 36 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -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 ;;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve script-check failures in the all command

When running ./check.sh all, calling scripts on the left side of && disables Bash's errexit behavior throughout that function. Consequently, failures from shellcheck or tests/scripts.sh are ignored, the final echo reports success, and the image checks proceed; if those pass, the overall command exits successfully despite failed script checks. This was reproduced with a failing shellcheck stub. Invoke scripts outside the conditional list or explicitly propagate failures from both checks.

Useful? React with 👍 / 👎.

*) echo "usage: ./check.sh [scripts|image|all]" >&2; exit 2 ;;
esac
12 changes: 10 additions & 2 deletions player-account.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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=<name> [shared=<folder>] [password_hash=<file>] sh player-account.sh
# account=<name> [shared=<folder>] [shared_squashed=1] [password_hash=<file>] sh player-account.sh
set -e
: "${account:?the account to make}"
shared=${shared:-}
shared_squashed=${shared_squashed:-}
password_hash=${password_hash:-}

owner=0
Expand Down Expand Up @@ -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
Expand Down
Loading