| 1 | #!/bin/sh |
| 2 | # Fuzzes a local HTTP server (no network access required) and checks that |
| 3 | # ffuf's JSON output correctly reports matched and unmatched paths. |
| 4 | set -e |
| 5 | |
| 6 | WORKDIR=$(mktemp -d) |
| 7 | SERVER_PID="" |
| 8 | |
| 9 | cleanup() { |
| 10 | [ -n "$SERVER_PID" ] && kill "$SERVER_PID" 2>/dev/null |
| 11 | rm -rf "$WORKDIR" |
| 12 | } |
| 13 | trap cleanup EXIT |
| 14 | |
| 15 | echo "secret content" > "$WORKDIR/secret.txt" |
| 16 | echo "<html>ok</html>" > "$WORKDIR/index.html" |
| 17 | printf 'index.html\nsecret.txt\ndoesnotexist\n' > "$WORKDIR/wordlist.txt" |
| 18 | |
| 19 | PORT=$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()') |
| 20 | |
| 21 | python3 -m http.server "$PORT" --bind 127.0.0.1 --directory "$WORKDIR" >/dev/null 2>&1 & |
| 22 | SERVER_PID=$! |
| 23 | |
| 24 | python3 - "$PORT" <<'PYEOF' |
| 25 | import socket |
| 26 | import sys |
| 27 | import time |
| 28 | |
| 29 | port = int(sys.argv[1]) |
| 30 | for _ in range(20): |
| 31 | try: |
| 32 | with socket.create_connection(("127.0.0.1", port), timeout=0.5): |
| 33 | sys.exit(0) |
| 34 | except OSError: |
| 35 | time.sleep(0.25) |
| 36 | sys.exit("timed out waiting for local http server") |
| 37 | PYEOF |
| 38 | |
| 39 | ffuf -w "$WORKDIR/wordlist.txt" -u "http://127.0.0.1:$PORT/FUZZ" -mc 200 -of json -o "$WORKDIR/result.json" -s |
| 40 | |
| 41 | python3 - "$WORKDIR/result.json" <<'PYEOF' |
| 42 | import json |
| 43 | import sys |
| 44 | |
| 45 | with open(sys.argv[1]) as fh: |
| 46 | data = json.load(fh) |
| 47 | |
| 48 | found = {r["input"]["FUZZ"] for r in data["results"]} |
| 49 | |
| 50 | assert "index.html" in found, "expected index.html to be matched: %r" % found |
| 51 | assert "secret.txt" in found, "expected secret.txt to be matched: %r" % found |
| 52 | assert "doesnotexist" not in found, "unexpected match for doesnotexist: %r" % found |
| 53 | |
| 54 | print("local-fuzz: %d matches as expected" % len(found)) |
| 55 | PYEOF |