Your scope check should parse targets, not compare strings

2026-11-21来源 dev.toEnglish

On an authorized engagement, the signed scope is usually a short list of CIDRs. The targets people type into tools are not: 203.0.113.0-100 copied from a client email, 10.0.0.* from someone's notes, a hostname that resolved to something else last week. If "in scope" is a string comparison or a note in the report, those are exactly the inputs that slip through.

Here is the check we ended up with, in plain Python with only the standard library.

The rules

  1. Store scope as CIDRs and nothing else. Default to private space (RFC 1918 plus loopback); a public range is added deliberately, from the rules of engagement.
  2. Parse every target into a network before comparing.
  3. Dash ranges (a.b.c.10-40) may only vary the last octet, and both ends must fall inside the same authorized block. A CIDR is a contiguous run of addresses, so if both ends are in it, everything between them is too.
  4. Wildcards only at the end. 10.0.0.* is a /24; 10.*.30.40 is 256 scattered addresses and gets rejected.
  5. Anything that doesn't parse is out of scope. No "probably fine".

The code

import ipaddress DEFAULT_SCOPE = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8"] def parse_target(t: str): """Return an ip_network, a (first, last) tuple for dash ranges, or None if ambiguous.""" t = t.strip() if "-" in t and t.count(".") == 3 and "/" not in t: base, _, last = t.rpartition(".") a, _, b = last.partition("-") try: return (ipaddress.ip_address(f"{base}.{a}"), ipaddress.ip_address(f"{base}.{b}")) except ValueError: return None if "*" in t: octets = t.split(".") if "*" not in octets: return None first = octets.index("*") if any(o != "*" for o in octets[first:]): return None # 10.*.30.40 is not a contiguous block while len(octets) < 4: octets.append("*") fixed = octets[:first] if not fixed: return None cidr = ".".join(fixed + ["0"] * (4 - first)) + f"/{8 * first}" try: return ipaddress.ip_network(cidr) except ValueError: return None try: return ipaddress.ip_network(t, strict=False) except ValueError: return None # hostnames, typos, anything else: deny def in_scope(target: str, scope: list[str]) -> bool: nets = [ipaddress.ip_network(r, strict=False) for r in scope] parsed = parse_target(target) if parsed is None: return False if isinstance(parsed, tuple): lo, hi = parsed return any(lo in n and hi in n for n in nets) # both ends in the SAME block return any(parsed.subnet_of(n) for n in nets if n.version == parsed.version) 

With a signed scope of 203.0.113.0/26 on top of the private defaults:

203.0.113.10 True 203.0.113.0-100 False # .64-.100 are outside the /26 203.0.113.0/25 False # the /25 is twice the authorized block 192.168.1.* True 10.*.30.40 False # not contiguous example.com False # doesn't parse: deny 192.168.1.10-50 True 

Where to call it

At every action, not once at the start of the engagement. Hosts get added halfway through: a new subnet shows up in a scan, someone pastes a target from chat. If the scan, the web recon and the login testing each re-check the target, a late addition can't skip the gate.

What it doesn't cover

  • Hostnames. Resolve them, check every resulting address, and re-check later: DNS changes during an engagement.
  • IPv6 dash ranges and wildcards. The sketch above handles IPv6 CIDRs only.
  • Reading the rules of engagement. This turns "I think it was in scope" into a check; it doesn't decide what the client authorized.

This is how scope works in GRIMZ, the engagement console we build at NO-DO, where every active action also needs a human confirmation. If you handle client scopes written as hostnames or dash ranges differently, we'd like to hear how.

原文:https://www.a8q9.cn/en/article/your-scope-check-should-parse-targets-not-compare-strings-01debd.html

相关文章