Skip to content

Agent: ingest_scan_output for nmap XML files (worker, additive) - #214

Merged
BaaRai merged 1 commit into
mainfrom
feat/scan-ingest-core
Nov 13, 2026
Merged

BaaRai merged 1 commit into
mainfrom
feat/scan-ingest-core

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 13, 2026

Copy link
Copy Markdown
Owner

What it does

Adds ingest_scan_output, a worker tool that ingests an nmap XML file already written to disk, so the model never reads the raw scanner output.

The worker runs nmap with -oX <file>, then calls ingest_scan_output with format: "nmap-xml" and the file path. The tool parses the file deterministically and writes without the LLM:

  • assets through the existing insert_assets handler: one ip asset per host with its open ports, one service asset per open port, and an HTTP service asset with URL and port for http/https services. Only type="user" hostnames are bound to the IP; PTR names stay in the fact detail.
  • one record_fact fact per host (source tool_output) listing its open ports.

The model receives only a compact summary: counters (hosts, ports, urls, events, skipped, truncated, assets_written, facts_written, facts_existing) and at most 20 notable items per type (hosts, ports, urls). Neither the raw content nor a full dump is returned.

Additive

  • The agent decides when to call the tool. Bash output is never parsed automatically.
  • No existing behaviour changes, and no existing tool schema changes. The only edits to existing files are the worker registration next to ingest_bbot and adding the tool to the target gate's noTargetTools list, as ingest_bbot is, because its input names a file, not a target.
  • No new dependency (encoding/xml, encoding/json).

Scope of this PR

  • nmap XML only. nuclei JSONL, httpx JSONL and ffuf JSON come in PR2, together with the shared 64 KiB-per-line JSONL reader taken from the bbot ingester.
  • Worker only, like ingest_bbot (registered in WorkerTools()). The tool is seeded into the tools catalog through BuiltinToolSeeds(), as ingest_bbot is.

Limits and safety

  • Path confinement: path is resolved under the run's working directory (ToolContext.WorkingDir, the directory Bash runs in). A path with a .. element, an absolute path outside it, and a symlink, file or directory, that resolves outside it are refused (filepath.EvalSymlinks + filepath.Rel). The tool needs the ToolContext, which writeExpTool does not pass on, so it is built with the same actool.Spec as writeTool inside the same needExploration guard.
  • File size cap: 256 MiB, checked with stat and enforced again with io.LimitReader. Only regular files are read.
  • Line cap: not applicable to nmap XML. The file is streamed with xml.Decoder, one <host> element at a time, never read whole into memory. The 64 KiB-per-line cap for the JSONL formats comes with PR2.
  • Bounded summary: at most 20 notable items per type, with counters reported separately. A host's fact summary lists at most 50 ports, then "and N more".
  • Target text is data: banners, products, versions, CPEs, hostnames and OS names go only into the fact detail, as one Go-quoted (strconv.Quote) compact JSON string. Summaries and notable items carry only validated typed fields: IP, port/protocol, service-name token, URL.
  • Truncated file: the complete hosts are kept and the result returns truncated: true. A file that is not nmap XML is refused. Parser errors are not echoed, because they can quote the file.
  • Re-ingestion: assets go through the existing upserts; a fact whose source, summary and detail match an existing fact in the exploration is counted in facts_existing, not written again.

Tests

  • Unit tests: counters, assets and facts from a hand-written fixture with instruction-shaped banner text; truncated file; non-XML file refused; 20-item notable cap with 30 hosts; path confinement (.., absolute outside path, file symlink and directory symlink leaving the directory, directory, missing file, over-cap sparse file, no working directory).
  • Postgres test (modelled on bbot_ingest_tool_pg_test.go): real task, insert_assets and record_fact, asset rows and facts checked, re-ingestion keeps counters with no new asset row or fact, symlink escape refused through the tool.
  • go build ./..., go vet ./agent, go test -count=1 ./agent, go test -count=1 -race ./agent, go test -count=1 ./intercept, gofmt -l on changed files.

Limitations

  • TOCTOU: there is a window between the path check (EvalSymlinks + Rel) and os.Open. A process in the working directory could swap a path component for a symlink in between. Hardening (for example O_NOFOLLOW or a SameFile check after opening) is not in this PR.
  • Fixtures are hand-written from the documented nmap -oX format. Nothing was tested against real scanner output.

A worker can write nmap output with -oX and call ingest_scan_output with
the format and path. The file is parsed deterministically: assets go
through the insert_assets handler, each host becomes one tool_output
fact listing its open ports, and the model gets only counters and at
most 20 notable items per type.

- path is resolved under the run working directory; "..", an absolute
  path outside it and a symlink leading outside are refused; 256 MiB cap
- the XML is streamed host by host with encoding/xml; a file cut short
  keeps the complete hosts and returns truncated: true
- target text (banners, products, hostnames) is kept only as a
  Go-quoted string in the fact detail
- re-ingesting a file upserts the same assets and skips identical facts

Worker-only, like ingest_bbot. nuclei, httpx and ffuf follow separately.
@BaaRai
BaaRai merged commit a9b57d0 into main Nov 13, 2026
4 checks passed
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.

1 participant