Nmap Context Deep Dive
This page is the technical reference for the Nmap context adapter (NmapAdapterPass, nmap_adapter@1.0). It covers streaming XML parsing, deterministic DuckDB host/port reconciliation, shadow-asset separation, downstream pass effects, and security controls.
Overview
VulnParse-Pin accepts Nmap XML scan output (--nmap / --nmap-ctx / -nmap) as supplementary attack-surface context. Supplying an Nmap XML file never mutates primary scanner findings; instead, NmapAdapterPass extracts confirmed open ports, OS hints, and NSE CVE references, reconciles them deterministically against .vpp.db assets, and publishes nmap_adapter@1.0 to DerivedContext.
Downstream effects:
- DuckDB asset & shadow-IP enrichment (
v2.1.0):NmapAdapterPassreconciles Nmap hosts into.vpp.dbassets, merges confirmedopen_ports_jsonandoshints, infersinternet_facingfor unmatched hosts (unmatched_shadow) wheninternet_facing IS NULLfrom public/test-net IPs or exposed public service ports, and propagates ports/OS/exposure across co-located multi-scanner shadow-IP assets viaip_to_shadow_aids. - Legacy
ScanResultTopN tiebreak & scoring bonus (Scoring@2.0/TopN@1.0only):port_tiebreak_enabled(defaulttrue) andscoring_port_bonus(default0.0, range0.0–5.0) apply only to the legacy in-memoryScoringPass(Scoring@2.0) andTopNPass(TopN@1.0) passes; thev2.1.0DuckDB passesScoring@3.0(ScoringV3Pass) andTopN@2.0(TopNV2Pass) do not applynmap_port_bonusorport_tiebreak_enabled, consuming reconciledassets.internet_facinginstead.
How it works
Pass lifecycle in v2.1.0
In run_v2_duckdb_pipeline(), NmapAdapterPass runs first:
[NmapAdapterPass (nmap_adapter@1.0)] -> [ACI@2.0] -> [Scoring@3.0] -> [Triage@2.0] -> [Remediation@1.0] -> [TopN@2.0] -> [Summary@2.0]
Streaming Nmap XML parsing
_stream_nmap_hosts() (src/vulnparse_pin/core/passes/Nmap/nmap_adapter_pass.py) reads the file through ctx.pfh.open_for_read(...) and streams <nmaprun> / <host> elements using safe_iterparse() + prune_element() from src/vulnparse_pin/parsers/_xml_security.py in O(1) memory per host:
- Validates root tag is
<nmaprun>(otherwise emitsstatus="invalid_format"andNMAP_CTX_INVALID_FORMAT). - Extracts normalized IPv4/IPv6 addresses, normalized hostnames, OS match names,
openports, and validCVE-YYYY-NNNN+IDs from<script>NSE outputs.
Deterministic DuckDB reconciliation (_reconcile_duckdb_hosts)
When a DuckDB workspace (db + run_id) is active, _reconcile_duckdb_hosts() reconciles Nmap hosts deterministically against assets and asset_aliases:
- Deterministic DB read order: Loads existing assets ordered by
ORDER BY asset_id ASCand aliases ordered byORDER BY asset_id ASC, alias_type ASC, alias_value ASC. - Hostname-before-IP matching precedence: For each Nmap host, candidate keys are checked in order
(*hostnames, *ips, *sorted(host["all_keys"]))againstkey_to_aid, preferring hostname matches before IP matches; if unmatched,AssetContextMatcher.resolve(ip=primary_ip, hostname=primary_host)is consulted. - Existing asset updates & shadow-IP propagation (
ip_to_shadow_aids): Merges newly confirmedopen_ports_json, backfills missinghostname,ip_address, oros, infersinternet_facingforunmatched_shadowassets wheninternet_facing IS NULL(_infer_unmatched_internet_facing), and propagates Nmapopen_ports_json,os, and inferredinternet_facingto co-locatedunmatched_shadowassets sharing the same normalized IP viaip_to_shadow_aids(for example, when multiple scanners target the same IP using different URI/hostname keys). - Shadow-asset separation: Nmap hosts that do not match any primary scan asset or
assets.yamlrule are inserted intoassetswithinventory_status = "unmatched_shadow", andmatched_asset_countexplicitly excludesdb_shadow_aidsso shadow hosts do not inflate join counts. - Atomic DuckDB persistence: Updates and new asset/alias rows are flushed via DuckDB
appender(_stg_nmap_upd,assets,asset_aliases) inside a single transaction.
Downstream consumption
v2.1.0DuckDB pipeline (Scoring@3.0,Triage@2.0,Remediation@1.0,TopN@2.0): Consumes the reconciledassets.internet_facingandassets.open_ports_jsoncolumns persisted byNmapAdapterPass(and surfaced in UI/exports);ScoringV3Pass(Scoring@3.0) andTopNV2Pass(TopN@2.0) do not readasset_open_portsfornmap_port_bonusorport_tiebreak_enabled.- Legacy in-memory
ScanResultpasses (Scoring@2.0/TopN@1.0only):ScoringPasschecks whether a finding’saffected_portappears inasset_open_ports[asset_id]to attach"nmap_open_port": Trueand applynmap_port_bonusif> 0.0, whileTopNPassconsumesasset_open_portswhenport_tiebreak_enabled: true.
Status paths and ledger events
NmapAdapterPass is non-blocking; errors do not drop findings.
| Status | Condition | Ledger reason code |
|---|---|---|
disabled |
--nmap / --nmap-ctx not supplied |
NMAP_CTX_DISABLED |
enabled |
XML parsed and reconciled | NMAP_CTX_ENABLED |
error |
File unreadable or XML/XXE error | NMAP_CTX_FAILED |
invalid_format |
Root XML tag is not nmaprun |
NMAP_CTX_INVALID_FORMAT |
join_rate (matched_asset_count / total_assets, excluding shadow-only Nmap hosts from matched_asset_count) is recorded in the NMAP_CTX_ENABLED ledger event.
Configuration and CLI usage
vpp scan scan.nessus --nmap nmap_results.xml -o results.json
Legacy aliases --nmap-ctx and -nmap remain supported. The file must have a .xml extension.
NOTE
--nmap nmap.xmlvs. positionalvpp scan nmap.xml: Passing an Nmap file via--nmapattaches port/exposure context (NmapAdapterPass) onto primary scanner findings without emitting extra informational port findings. Passing an Nmap file as a positional primary scan input (vpp scan nmap.xml) invokesNmapXMLParser(nmap-xml), which emitsInformationalopen-port Track B exposure findings for port-only XML andHighfindings only for NSE<script>/<hostscript>CVE or"vulnerable"outputs.
In config.yaml:
nmap_ctx:
port_tiebreak_enabled: true
scoring_port_bonus: 0.0
config.schema.json enforces additionalProperties: false and 0.0 <= scoring_port_bonus <= 5.0.
Security considerations
- PFH enforcement:
--nmapinput is validated viactx.pfh.ensure_readable_file(...)(forbid_symlinks_read=Trueby default, optional--enforce-root-read). - Hardened XML parsing (NO
defusedxml):_stream_nmap_hosts()usessafe_iterparse()(src/vulnparse_pin/parsers/_xml_security.py), which enforcesresolve_entities=False,no_network=True,load_dtd=False,dtd_validation=False,huge_tree=False, 1 MiB XML prolog validation (including BOM-less UTF-16LE/BE), and full-subtreeetree.Entityrejection. - CVE sanitization: NSE script CVE tokens must match
^CVE-\d{4}-\d{4,7}$. - Bounded bonus:
scoring_port_bonusis schema-capped at5.0.