Scope enforcement as code
One out-of-scope request from an unattended script can end your access to a program. Write the program’s scope once into a plain text file, and make every stage that sends traffic pass its target list through one filter that reads that file. Wrap noisy tools so they refuse out-of-scope targets on their own. Before the first unattended night, print exactly what each active stage would touch and read it.
Why scope belongs in code
Section titled “Why scope belongs in code”On a manual run you see every target before you hit it. Unattended, nobody looks. Enumeration returns vendor CNAMEs, wildcards catch a SaaS help desk, redirects lead off-site, and a typo in a script turns example.com into the whole internet. The rules of engagement do not change because you were asleep. You are fully responsible for what your scripts touch.
So scope is enforced by code, in three layers:
- One canonical scope file per program, the only source of truth.
- A filter that every list passes through before any active stage.
- Guarded wrappers around tools that can send heavy traffic, so they check scope even when you call them by hand.
The canonical scope file
Section titled “The canonical scope file”The recon workflow used scope/roots.txt for root domains. The pipeline needs more shapes than roots, so it uses scope/in-scope.txt with three kinds of line, and keeps scope/out-of-scope.txt in the same format.
# Copied from the program policy on 2026-11-01. Policy URL in policy.env.*.example.com # any subdomain of example.com (not example.com itself)example.com # the apex, listed on its ownapi.example.net # one exact host198.51.100.0/24 # an IPv4 range the policy lists# Out-of-scope always wins over in-scope.blog.example.com # hosted by a blogging SaaS, excluded in the policy*.corp.example.com # internal corporate apps, excludedhelp.example.com # third-party help desk# programs/example/scope/policy.env: rate limits and settings the pipeline must obeyPOLICY_URL="https://platform.example/programs/example" # where you copied the scope fromRATE=10 # max requests per second, from the policyCONCURRENCY=5MAX_TARGETS=300 # cap on hosts any heavy stage may touch per runNMAP_RATE=50 # packets per second for the safe-nmap wrapperNOTIFY_ID=triageNUCLEI_TAGS=exposure,misconfig,panel,takeoverAUTOMATED_SCANNING=yes # set to no if the policy bans it; active stages must check thisRules for the file:
- Wildcard semantics are explicit.
*.example.commeans subdomains only. If the apex is in scope, list it. Programs differ on this; when the policy is unclear, ask, and until then leave the apex out. - One source of truth. No stage keeps its own copy or its own list of domains. If you change scope, you change one file.
- Comments say where it came from. The date and the policy URL let you audit later.
- IPv6 ranges are not handled by the simple filter below. If a program lists them, filter IPv6 by hand or with a tool that understands IPv6 CIDRs, such as
mapcidr(check the README). policy.envis sourced as shell code by the pipeline. It is your own file, but treat it like code: no commands, only assignments.
The filter
Section titled “The filter”The filter reads hosts, host:port pairs or URLs on standard input and prints only the lines whose host is in scope and not out of scope. It is the same awk program the worked pipeline embeds, saved as a standalone command so wrappers and your own one-liners can use it.
#!/usr/bin/env bash# ~/pipeline/bin/inscope: filter stdin to in-scope lines for $PROGRAM. Fails closed.set -uo pipefailP="${PIPE_ROOT:-$HOME/pipeline}/programs/${PROGRAM:?set PROGRAM}"IN="$P/scope/in-scope.txt"; OUT="$P/scope/out-of-scope.txt"grep -qvE '^[[:space:]]*(#|$)' "$IN" 2>/dev/null || { echo "inscope: $IN missing or empty" >&2; exit 3; }[[ -f "$OUT" ]] || { echo "inscope: $OUT missing" >&2; exit 3; }awk -v IN="$IN" -v OUT="$OUT" ' function host(s) { sub(/^[A-Za-z]+:\/\//, "", s); sub(/[\/?#].*$/, "", s); sub(/:[0-9]+$/, "", s); return tolower(s) } function n(ip, a) { split(ip, a, "."); return ((a[1] * 256 + a[2]) * 256 + a[3]) * 256 + a[4] } function load(f, k, line, p) { while ((getline line < f) > 0) { sub(/#.*/, "", line); gsub(/[ \t\r]/, "", line); line = tolower(line) if (line == "") continue if (line ~ /\//) { split(line, p, "/"); nc[k]++; cb[k, nc[k]] = n(p[1]); cs[k, nc[k]] = 2 ^ (32 - p[2]) } else if (line ~ /^\*\./) { ns[k]++; suf[k, ns[k]] = substr(line, 2) } else ex[k, line] = 1 } } function hit(h, k, i, s, b) { if ((k, h) in ex) return 1 for (i = 1; i <= ns[k]; i++) { s = suf[k, i] if (length(h) > length(s) && substr(h, length(h) - length(s) + 1) == s) return 1 } if (h ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) for (i = 1; i <= nc[k]; i++) { b = cb[k, i] - cb[k, i] % cs[k, i]; if (n(h) >= b && n(h) < b + cs[k, i]) return 1 } return 0 } BEGIN { load(IN, "in"); load(OUT, "out") } { h = host($1); if (!hit(h, "out") && hit(h, "in")) print }'How it works, in plain words:
host()strips a scheme, a path or query, and a port, then lowercases.https://API.example.com:8443/v1?x=1becomesapi.example.com.load()sorts each scope line into one of three tables: exact hosts, wildcard suffixes (*.example.combecomes.example.com), and CIDR ranges stored as a start number and a size.hit()checks exact match, then “ends with the suffix and is longer than it”, then, for IPv4 literals only, whether the address falls inside a range.- A line passes only if it does not hit out-of-scope and does hit in-scope.
Keep this awk. grep -vf out-of-scope.txt reads that file as regular expressions. An empty line matches every target, and the pattern corp.example.com also drops notcorp.example.com.
The regexes avoid {n} repetition on purpose: mawk, Debian’s default awk, did not support it until recent versions.
# Install the filter and test it on a few lines you know the answer tochmod +x ~/pipeline/bin/inscope && printf '%s\n' https://dev.example.com/x blog.example.com 198.51.100.7 203.0.113.9 vendor.example.org | PROGRAM=example ~/pipeline/bin/inscope# Expected output with the example scope abovehttps://dev.example.com/x198.51.100.7https://dev.example.com/x matched *.example.com. 198.51.100.7 falls in 198.51.100.0/24. blog.example.com is out of scope, so it is dropped even though the name would otherwise match. 203.0.113.9 and vendor.example.org matched no rule. Passing this filter means the string fits the files. It does not mean the host answers, and it does not mean the company owns the address.
Where the filter runs
Section titled “Where the filter runs”Every intermediate file passes through it before an active stage, even when the previous stage “should” have produced only in-scope data. Filtering twice is cheap. Trusting a tool’s output is how vendor hosts get scanned.
| Stage output | Why it can leave scope | Filter before |
|---|---|---|
| Passive enumeration | Data sources return unrelated names, and a wildcard root can cover a vendor CNAME | Resolution |
| Resolution | Some resolvers return junk; CNAME chains point at vendors | Probing |
| Probing | Redirects to other domains if you follow them | Scoring and scanning |
| Scoring queue | Built from probe output | Nuclei |
| Nuclei findings | Templates can follow redirects | Verification re-run |
Names, IPs and the CIDR gray zone
Section titled “Names, IPs and the CIDR gray zone”A host name that matches a wildcard is in scope by name. An IP literal is in scope if it falls in a listed range. Two edge cases break simple scope filters. First, a hostname can miss every domain rule yet resolve into an in-scope IP range. Second, a wildcard-matched hostname can resolve to a shared cloud or CDN IP the company does not own.
# Names that resolve into an in-scope CIDR but do not match a name rule: review by hand, never auto-includednsx -l candidates.txt -a -resp -silent -json | jq -r '.host as $h | .a[]? | "\($h) \(.)"' | while read -r h ip; do [[ -z "$(echo "$h" | PROGRAM=example inscope)" && -n "$(echo "$ip" | PROGRAM=example inscope)" ]] && echo "$h $ip"; done| Case | What the filter does | What you do |
|---|---|---|
| Name matches a wildcard, resolves anywhere | Passes | Check CNAMEs for vendors in your morning review; the rules of engagement guide warns about this |
| Name matches nothing, IP in a listed range | Drops the name; keeps the IP if you probe by IP | Ask the program before testing the name |
| IP literal in a listed range | Passes | In scope by IP |
| Name matches a wildcard, it is a takeover candidate | Passes | Follow the program’s takeover rules; phase 7 |
status.example.com can pass *.example.com while its CNAME is example.statuspage.io, which is Atlassian. Scanning that name tests Atlassian. Check CNAMEs in the morning review and add vendor hosts to out-of-scope.txt.
Guard rails for noisy stages
Section titled “Guard rails for noisy stages”A wrapper is a small script with the same name as the tool, earlier in your PATH, that checks scope and then calls the real tool. It protects you from yourself on a tired evening as much as from the pipeline.
#!/usr/bin/env bash# ~/pipeline/bin/safe-nmap: refuse to scan anything not in scope, cap rate. Heavy active.set -uo pipefail[[ -n "${PROGRAM:-}" ]] || { echo "safe-nmap: set PROGRAM" >&2; exit 3; }targets=$(mktemp); trap 'rm -f "$targets"' EXITprintf '%s\n' "$@" | grep -v '^-' | PROGRAM="$PROGRAM" inscope >"$targets" || exit 3asked=$(printf '%s\n' "$@" | grep -vc '^-'); kept=$(wc -l <"$targets")[[ "$kept" -gt 0 && "$kept" -eq "$asked" ]] || { echo "safe-nmap: $((asked - kept)) target(s) out of scope, refusing all" >&2; exit 3; }source "${PIPE_ROOT:-$HOME/pipeline}/programs/$PROGRAM/scope/policy.env" || exit 3exec nmap --max-rate "${NMAP_RATE:-50}" -iL "$targets" $(printf '%s\n' "$@" | grep '^-')#!/usr/bin/env bash# ~/pipeline/bin/safe-ffuf: check the -u host against scope and force a rate. Heavy active.set -uo pipefailurl=""; args=("$@")for ((i = 0; i < ${#args[@]}; i++)); do [[ "${args[i]}" == -u ]] && url="${args[i+1]:-}"; done[[ -n "$url" ]] || { echo "safe-ffuf: -u is required" >&2; exit 2; }[[ -n "$(printf '%s\n' "${url//FUZZ/x}" | PROGRAM="${PROGRAM:?set PROGRAM}" inscope)" ]] || { echo "safe-ffuf: $url is out of scope" >&2; exit 3; }source "${PIPE_ROOT:-$HOME/pipeline}/programs/$PROGRAM/scope/policy.env"exec ffuf "$@" -rate "${RATE:?RATE missing in policy.env}"Design choices worth copying:
- All or nothing. If one of five targets is out of scope, refuse all five. A partial scan hides the mistake.
- The wrapper sets the rate, read from
policy.env, so a forgotten flag cannot mean “tool default”. Nmap’s--max-rateand ffuf’s-rateare long-standing flags; confirm on your version with-h. FUZZin the host (vhost fuzzing) is replaced before the check, so the check sees a real host name. Vhost fuzzing against an in-scope IP still sends many Host headers; keep it manual.- Wrappers do not replace reading output. They stop the obvious mistakes.
When scope changes
Section titled “When scope changes”Programs add assets, remove them, and move things out of scope after an incident. Your pipeline has history from the old scope.
-
Update the files. Edit
in-scope.txtandout-of-scope.txt, change the date comment, and commit with a message that names the policy change. -
Audit history against the new scope. Anything you have stored that is now out of scope must not be tested, and leads on it should leave the queue.
Terminal window # Hosts you have seen that the new scope no longer allowscomm -23 <(sort -u state/hosts.txt) <(PROGRAM=example inscope < state/hosts.txt | sort -u)Terminal window # Evidence folders whose finding URL is now out of scopefor d in evidence/*/; do u=$(sed -n 's/^url=//p' "$d/meta.txt"); [[ -z "$(echo "$u" | PROGRAM=example inscope)" ]] && echo "$d $u"; done -
Decide about open leads. A finding discovered while the asset was in scope may still be reportable; the policy or the program decides, not you. Ask before testing further.
-
Watch for additions. New in-scope assets have the fewest testers. Seed them into the next run and expect a burst of new hosts in the digest.
-
Rerun the pre-flight below before the next unattended run.
Pre-flight: what will each stage touch
Section titled “Pre-flight: what will each stage touch”Before the first unattended night, and after any scope change, print exactly what each active stage would send traffic to, and read it. This takes ten minutes and is the single best protection against an embarrassing email from a program.
#!/usr/bin/env bash# ~/pipeline/bin/preflight: show what each active stage would touch for $PROGRAM. Passive: sends nothing.set -uo pipefailP="${PIPE_ROOT:-$HOME/pipeline}/programs/${PROGRAM:?set PROGRAM}"; C="$P/current"source "$P/scope/policy.env" || exit 3echo "== policy: RATE=$RATE CONCURRENCY=${CONCURRENCY:-?} MAX_TARGETS=${MAX_TARGETS:-?} AUTOMATED_SCANNING=${AUTOMATED_SCANNING:-unset}"echo "== enumeration roots (passive)"; sed -nE 's/^[[:space:]]*\*\.([^[:space:]#]+).*/\1/p' "$P/scope/in-scope.txt"echo "== resolution would query: $(PROGRAM=$PROGRAM inscope <"$P/state/hosts.txt" | wc -l) names"echo "== probe would touch: $(PROGRAM=$PROGRAM inscope <"$C/resolved.txt" | wc -l) hosts at $RATE req/s"echo "== nuclei would touch (top ${MAX_TARGETS:-300} by score):"cut -f2 "$C/queue.tsv" | head -n "${MAX_TARGETS:-300}" | PROGRAM=$PROGRAM inscope | awk -F/ '{print $3}' | sort | uniq -c | sort -rn | head -40echo "== rejected by the filter in the last run (should be vendors, not your targets):"comm -23 <(sort -u "$C/resolved.txt") <(PROGRAM=$PROGRAM inscope <"$C/resolved.txt" | sort -u) | head -20# Run it by hand and read every linePROGRAM=example ~/pipeline/bin/preflight | lessWhat to look for:
- Hosts in the Nuclei list that belong to a vendor, a parent company or a different product.
- A rejected list that contains hosts you thought were in scope: your scope file is too narrow, or the policy is unclear.
AUTOMATED_SCANNING=unsetornowith active stages enabled. Stop and fix that first.- A host count far larger than last week. Something in enumeration changed; understand it before you scan it.
Next, rank the hosts that survive the filter with Scoring and triage engines.
Open local graph around this page
Linked from here
Recon AtlasFor authorized testing only. Your scope is the map edge.