Skip to content

M4 — migrate -sU and the six flag scans onto the group scan engine - #55

Merged
kj299 merged 3 commits into
masterfrom
claude/m4-group-udp-flag
Jul 25, 2026
Merged

kj299 merged 3 commits into
masterfrom
claude/m4-group-udp-flag

Conversation

@kj299

@kj299 kj299 commented Jul 25, 2026

Copy link
Copy Markdown
Owner

Backlog #2, second sub-slice. Every raw scan now runs on the single multi-host engine introduced for -sS in #54 — and the three near-identical single-host drivers it subsumes are deleted rather than left beside it.

Net −476 lines while adding two scan families to the engine and more test coverage.

What lands

sys::group — two new RawScanKind impls join SynKind:

  • UdpKind (-sU). Its BPF also admits ICMP (an error is addressed to our source address, carrying no port of ours in its own header), and a port that never answers is open|filtered rather than filtered.
  • FlagKind (-sA/-sW/-sM/-sF/-sN/-sX) — one impl for all six, parametrized by ScanType, which fixes both the probe's flag combination and how a RST-or-silence reads.

core::udpscan — the matcher is now a pure function of the frame. UdpReply gains src_ip: the sender for a direct datagram, or the destination of the quoted probe for an ICMP error. UdpMatchCtx.target is gone; from_target becomes "the ICMP sender is the host the quoted probe was addressed to", computed from the packet alone.

That is what lets one matcher serve a whole host group — and it is stricter than the C, which compares the ICMP source against the host whose probe list is being searched. A host that quotes a probe we sent to a different host can no longer have its error counted as an authoritative closed for that other host; the reply is attributed to the host we actually probed, and (sender ≠ quoted destination) reads as filtered. Same verdicts on well-formed traffic; visible only under spoofed or misdirected ICMP. Ledgered as udpscan-icmp-attributed-to-quoted-destination.

core::flagscan — FlagReply gains src_ip (the reply's IPv4 source).

sys::{synscan,udpscan,flagscan} shrink to thin entry points that pick the per-scan keys and delegate to group_scan_targets. The CLI is unchanged.

Verified as root (--features pcap)

All four e2e suites pass, including the on-the-wire differential gates for SYN, UDP and flag probes — which now exercise the group engine directly rather than a driver that no longer exists.

$ nmap-rs -sU -Pn -p 39926,36583 127.0.0.1 127.0.0.2     # 39926 bound on .1 only
127.0.0.1  39926/udp open|filtered  ;  Not shown: 1 closed udp port (port-unreach)
127.0.0.2  Not shown: 2 closed udp ports (port-unreach)

$ nmap-rs -sA -Pn -p 50879 127.0.0.1 127.0.0.2
both hosts: Not shown: 1 unfiltered tcp port (reset)

The bound UDP port reads open|filtered only on .1 and closed on .2 — per-host demux through one shared capture.

Gates

fmt + clippy (default and all-features) on pinned 1.97.0 · 310 core tests · miri clean · unsafe-audit 11/11 documented, 0 undocumented (no new unsafe) · cargo deny clean · fuzz smoke match_udp 7.5M / match_flag 2.6M / match_syn 7.7M runs, no crashes.

The retired drivers' unit tests were ported onto the engine, plus new multi-host demux tests for both scan families (including an ICMP error relayed by a router landing on the probed host, not the router).

Ledger

Three *-single-host-first-slice divergences resolved; new group-scan-one-engine-for-every-raw-scan and udpscan-icmp-attributed-to-quoted-destination.

Remaining sub-slices of backlog #2

  1. UDP protocol payloads (payload.cc) — shrinks the open|filtered bucket.
  2. SYN/flag ICMP back-fill — core::udpscan::embedded_probe already returns the quoted destination, which is exactly what attributes an error to the right host in a group scan.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w


Generated by Claude Code

claude added 3 commits July 25, 2026 02:54
Backlog #2, second sub-slice. Every raw scan now runs on the single
multi-host engine introduced for -sS, and the three near-identical
single-host drivers it subsumes are deleted rather than left beside it.

sys::group
  - UdpKind (-sU) and FlagKind (-sA/-sW/-sM/-sF/-sN/-sX) implement
    RawScanKind, joining SynKind. UdpKind's BPF also admits ICMP (an
    error is addressed to our source address, carrying no port of ours);
    its no-response default is open|filtered rather than filtered.
  - The retired drivers' unit tests are ported onto the engine, plus new
    multi-host demux tests for both scan families.

core::udpscan — the matcher is now a pure function of the frame
  - UdpReply gains src_ip: the sender for a direct datagram, or the
    destination of the quoted probe for an ICMP error. UdpMatchCtx.target
    is gone; from_target becomes "the ICMP sender is the host the quoted
    probe was addressed to", computed from the packet alone.
  - This is what lets one matcher serve a whole host group, and it is
    stricter than the C: a host that quotes a probe we sent to a
    different host can no longer have its error counted as an
    authoritative "closed" for that other host. Ledgered as
    udpscan-icmp-attributed-to-quoted-destination.

core::flagscan — FlagReply gains src_ip (the reply's IPv4 source).

sys::{synscan,udpscan,flagscan} shrink to thin entry points that pick the
per-scan keys and delegate to group_scan_targets; the CLI is unchanged.

Gates: fmt + clippy (default and all-features) on pinned 1.97.0; 310 core
tests; miri clean; unsafe-audit 11/11 documented, 0 undocumented (no new
unsafe); cargo deny clean; fuzz smoke match_udp 7.5M / match_flag 2.6M /
match_syn 7.7M runs, no crashes. Privileged as root: all four e2e suites
pass, including the on-the-wire differential gates for SYN, UDP and flag
probes, now exercising the group engine directly. Live multi-host CLI runs
verified for -sU and -sA against 127.0.0.1 + 127.0.0.2.

Ledger: three single-host-first-slice divergences resolved; new entry
group-scan-one-engine-for-every-raw-scan.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
The three entry-point modules only use IpAddr/TimingTemplate/ScanType
inside their #[cfg(feature = "pcap")] functions, so a default-feature
build saw them as unused imports and CI's -D warnings rejected it.

Caught by CI, not locally: I had linted only --all-features, which turns
pcap on. Both configurations are now verified.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
@kj299
kj299 marked this pull request as ready for review July 25, 2026 11:26
@kj299
kj299 merged commit 2d77858 into master Jul 25, 2026
12 checks passed
kj299 added a commit that referenced this pull request Jul 25, 2026
Backlog #2's last sub-slice. An ICMP destination-unreachable (type 3) or
time-exceeded (type 11) quotes the packet that provoked it, so it can be
matched back to the probe it answers. The UDP scan used this from the
start; core::icmp_quote generalizes the quote parser to TCP and shares it
with -sS and all six flag scans.

This matters most for -sF/-sN/-sX/-sM, whose no-response default is
open|filtered: without ICMP matching those scans could never report a port
as filtered at all.

core::icmp_quote (new)
  - parse_icmp_error yields the three addresses that are NOT interchangeable:
    the sender (may be a router), the quoted source (must be us), and the
    quoted destination (the host the verdict belongs to). from_target is
    then just sender == quoted_dst.
  - icmp_to_reason ports portreasons.cc, including that codes 9/10/13 are
    three distinct reasons in nmap, not one "admin-prohibited".
  - Also now the single home of ipv4_offset, which all three matchers had
    their own copy of — closing the "DRY the scan matchers" follow-up.

core::synscan
  - match_tcp_icmp_error, shared with the flag scans since the quote rules
    are identical. The quote contains our original packet, so unlike a RST
    (which reflects no sequence of ours) the encoded sequence IS present and
    is verified, as the C compares th_seq to probe->tcpseq().
  - SynReply/FlagReply/UdpReply now carry the reason they matched on, so a
    filtered port reports the specific code rather than "no-response".

sys::group
  - MatchParams groups the per-scan match constants and carries our source
    address; the SYN and flag BPF filters now also admit ICMP, without which
    the errors never reach the matcher.

Two findings from reading the C, one of which corrects this port:

1. The #55 ledger claimed attributing an ICMP error to the quoted
   destination was "stricter than the C". That was wrong: the C does the
   same (findHost(&encaps_hdr.dst), then from_target against the outer
   source). Corrected in DIVERGENCES.md — it is a structural note, not a
   hardening. The ledger ships as release notes, so accuracy matters.

2. A real gap in our own UDP path: the C requires the quoted packet's
   source to be our address ("If it didn't come from us, we don't care")
   and we never checked it. All three scans now enforce it, which is why
   every match context carries our_ip.

Gates: fmt + clippy (default and all-features) on 1.97.0; 335 core tests;
miri clean; unsafe-audit 11/11 documented, 0 undocumented; fuzz match_syn
5.0M / match_udp 5.7M / match_flag 1.8M / services_probes_parse 290k runs,
no crashes; all four privileged e2e suites pass as root.

Ledger: synscan-icmp-match-deferred and flagscan-icmp-match-deferred
resolved; udpscan-icmp-attributed-to-quoted-destination corrected; new
icmp-quote-requires-our-source, icmp-quote-verifies-our-sequence,
icmp-reason-fidelity, icmp-bpf-widened-for-tcp-scans.


Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w

Co-authored-by: Claude <[email protected]>
kj299 pushed a commit that referenced this pull request Nov 14, 2026
…habit

Every step of nmap's IPv6-scans milestone ran its sabotage from a script
written for the occasion. None bounded a run: one mutation made its test
loop forever, was killed by hand after 96 s, and was then printed as
CAUGHT with no failing test (LESSONS #54). Two ledger entries named, as
their pins, tests that passed with the fix reverted (LESSONS #55).

sabotage.py takes a JSON list of mutations. It requires the replaced span
to occur once, restores from the saved bytes and bumps the mtime, kills a
run at its timeout and calls that INCONCLUSIVE, counts only a failing
test named in the output, and with `expect` only the test the ledger
names (WRONG-TEST otherwise). It refuses to start over a backup a killed
run left behind. Self-tested; run against nmap-rs in a separate worktree.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JfEzHqD7a7TKDuUs5GwZ2w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants