From Scan Output to a Test Plan
After a day of scanning a tester has dozens of XML files, terminal scrollback, enumeration notes and the recon list. Nobody can test from that. The step that turns it into work is unglamorous: merge everything into one service inventory, compare it with what the client said exists, and derive a test plan: one row per service, the checks it needs, and a priority. Done well, this step is also where the first findings appear (services that shouldn't exist), and it makes the report's coverage section almost write itself.
Parsing Nmap XML
Nmap's XML output (-oX, or -oA for all formats) is the only complete record: the command line, the start time, each host with its addresses and names, each port with state, reason, service name, product, version and extra information, and script output. Normal output is for reading; XML is for programs. The session scans services started on 127.0.0.1 and parses the XML with Python's standard library, inside the sandbox.
python3 -m http.server 8080 --bind 127.0.0.1 >http.log 2>&1 &
openssl s_server -accept 127.0.0.1:8443 -cert $SAMPLES/pki/lab.test.crt -key $SAMPLES/pki/lab.test.key -www -quiet >/dev/null 2>&1 &
for i in $(seq 50); do [ "$(ss -Htln | wc -l)" -ge 2 ] && break; sleep 0.1; done
nmap -sT -sV -p 22,5432,8080,8443 -oX scan.xml 127.0.0.1 >/dev/null
kill %1 %2
cat > inventory.py <<'PY'
import csv, sys
import xml.etree.ElementTree as ET
root = ET.parse("scan.xml").getroot()
print("command:", root.get("args"))
rows = []
for host in root.iter("host"):
addr = host.find("address").get("addr")
for port in host.iter("port"):
state = port.find("state")
svc = port.find("service")
rows.append({
"addr": addr, "port": int(port.get("portid")), "state": state.get("state"),
"reason": state.get("reason"), "service": svc.get("name") if svc is not None else "",
"tunnel": (svc.get("tunnel") or "") if svc is not None else "",
"product": " ".join(filter(None, [svc.get("product"), svc.get("version")])) if svc is not None else "",
"method": svc.get("method") if svc is not None else "",
})
w = csv.DictWriter(sys.stdout, fieldnames=list(rows[0]), lineterminator="\n")
w.writeheader()
w.writerows(rows)
PY
python3 inventory.py | tee inventory.csvcommand: nmap -sT -sV -p 22,5432,8080,8443 -oX scan.xml 127.0.0.1
addr,port,state,reason,service,tunnel,product,method
127.0.0.1,22,closed,conn-refused,ssh,,,table
127.0.0.1,5432,closed,conn-refused,postgresql,,,table
127.0.0.1,8080,open,syn-ack,http,,SimpleHTTPServer 0.6,probed
127.0.0.1,8443,open,syn-ack,http,ssl,OpenSSL s_server -www httpd,probed
Two fields deserve attention. method="probed" means version detection really talked to the service and matched its answers. method="table" means Nmap only looked the port number up in its services table: ports 22 and 5432 are closed, nothing answered, yet the XML still labels them ssh and postgresql. An open port can get a table label too, when no probe matches. An inventory that keeps the method keeps the distinction between identified and assumed.
Comparing with what the client said
The second input is the client's own inventory. Differences in either direction matter:
- found, not documented: unknown services (shadow IT, forgotten test systems, a compromise);
- documented, not found: a firewall in the way, a service down, or an inventory that is wrong;
- found and documented, but different: a version or product mismatch.
The analysis below combines a lab scan inventory (in the CSV shape the session produced) with the client's list and derives the plan. Each service type maps to checks from the earlier lessons; priority comes from exposure and what the service is.
scan = [ # addr, port, service, product, method (lab scan of 10.10.0.0/24, abridged)
("10.10.0.10", 22, "ssh", "OpenSSH 8.9p1 Ubuntu 3ubuntu0.10", "probed"),
("10.10.0.10", 443, "https", "nginx 1.18.0", "probed"),
("10.10.0.20", 21, "ftp", "vsftpd 2.3.4", "probed"),
("10.10.0.20", 22, "ssh", "OpenSSH 4.7p1 Debian 8ubuntu1", "probed"),
("10.10.0.21", 445, "microsoft-ds", "", "table"),
("10.10.0.40", 5432, "postgresql", "PostgreSQL DB 9.6.0 or later", "probed"),
("10.10.0.62", 8080, "http", "Jetty 10.0.18", "probed"),
("10.10.0.77", 8888, "http", "Tornado httpd 6.4", "probed"),
]
documented = {("10.10.0.10", 22), ("10.10.0.10", 443), ("10.10.0.20", 21), ("10.10.0.20", 22),
("10.10.0.21", 445), ("10.10.0.21", 3389), ("10.10.0.40", 5432), ("10.10.0.62", 8080)}
CHECKS = {
"ssh": "auth methods, algorithms (ssh2-enum-algos), version triage",
"https": "TLS config, certificate, web test per WSTG (separate track)",
"http": "authentication on admin UI, default credentials (only if RoE allows), version triage",
"ftp": "anonymous login, cleartext credentials, version triage + safe validation",
"microsoft-ds": "signing, SMBv1, anonymous listing, share permissions with test account",
"postgresql": "reachable from which zones, auth (pg_hba), version triage",
}
PRIORITY = {"ftp": 1, "http": 1, "microsoft-ds": 1, "postgresql": 2, "ssh": 2, "https": 2}
found = {(a, p) for a, p, *_ in scan}
print("UNDOCUMENTED:", ", ".join(f"{a}:{p}" for a, p in sorted(found - documented)) or "none")
print("NOT FOUND: ", ", ".join(f"{a}:{p}" for a, p in sorted(documented - found)) or "none")
print()
plan = []
for addr, port, svc, product, method in scan:
prio = 0 if (addr, port) not in documented else PRIORITY[svc]
note = "identify first (port-table guess)" if method == "table" else product
plan.append((prio, addr, port, svc, note, CHECKS[svc]))
for prio, addr, port, svc, note, checks in sorted(plan):
print(f"P{prio} {addr}:{port:<5} {svc:<13} {note:<34} {checks}")UNDOCUMENTED: 10.10.0.77:8888
NOT FOUND: 10.10.0.21:3389
P0 10.10.0.77:8888 http Tornado httpd 6.4 authentication on admin UI, default credentials (only if RoE allows), version triage
P1 10.10.0.20:21 ftp vsftpd 2.3.4 anonymous login, cleartext credentials, version triage + safe validation
P1 10.10.0.21:445 microsoft-ds identify first (port-table guess) signing, SMBv1, anonymous listing, share permissions with test account
P1 10.10.0.62:8080 http Jetty 10.0.18 authentication on admin UI, default credentials (only if RoE allows), version triage
P2 10.10.0.10:22 ssh OpenSSH 8.9p1 Ubuntu 3ubuntu0.10 auth methods, algorithms (ssh2-enum-algos), version triage
P2 10.10.0.10:443 https nginx 1.18.0 TLS config, certificate, web test per WSTG (separate track)
P2 10.10.0.20:22 ssh OpenSSH 4.7p1 Debian 8ubuntu1 auth methods, algorithms (ssh2-enum-algos), version triage
P2 10.10.0.40:5432 postgresql PostgreSQL DB 9.6.0 or later reachable from which zones, auth (pg_hba), version triage
The plan starts with P0, the undocumented Tornado web service on 10.10.0.77:8888. Before anything else, ask the client what it is: it may be a data-science notebook server someone started on a workstation (a common source of unauthenticated code execution), or something worse. Then the P1 services: the legacy FTP with a version to validate, the file server's SMB, the CI server. files01:3389 was documented but not found: either RDP is filtered from the tester's position (good, if intended) or the documentation is stale, and both are worth one line in the report.
How the defender uses this
The same pipeline, run on a schedule by the defenders, is continuous discovery: scan, parse, compare with the inventory, alert on differences. A new service on a workstation is the kind of change that should create a ticket within a day, not wait for the annual test. The test report's "undocumented services" list is a direct input to that process.
How gaps are prevented
Keep one machine-readable inventory (a CMDB, or simply a version-controlled file) that deployments update; give every service an owner; compare it with scan results automatically; and treat "not in the inventory" as a finding category of its own.
Pitfalls
- Merging by name instead of address and port. Names change and several names point at one address; key the inventory by
(address, protocol, port)and keep names as attributes. - Dropping closed and filtered results. "Documented but filtered" is information; keep state and reason.
- Trusting table guesses. A service label with
method="table"is a port number, not an identification. - A plan without negative results. Every row ends as "finding", "tested, nothing found" or "not tested, reason", which becomes the coverage appendix.
Practice
- Extend
inventory.pyto print only open ports and to add a columnconfidence(highifmethod == "probed"and a product is present, elselow). - The client says
10.10.0.77is "a data scientist's laptop" on the office network. Is it in scope, and what do you do with the P0 row?
Answers
- Skip rows whose
stateisn'topen, and add"confidence": "high" if svc is not None and svc.get("method") == "probed" and svc.get("product") else "low"to the row dict. - Its address is inside the scope range, but a personal device may not be covered by the client's authorization (it may not even be the client's property): ask the client lead in writing. Meanwhile record it as an undocumented service. If it is in scope, check whether the notebook server requires a token (Jupyter-style servers without authentication allow anyone to run code, a critical finding) without running any code.
Test yourself
What does method="table" on a service in Nmap XML mean?
- The service was probed and identified
- The name comes only from Nmap's port-number table; nothing was identified
- The service is a database table
- The port is closed
The scan finds a service the client's inventory doesn't list. What is it in the test plan?
- Ignore it
- Top priority: ask the client what it is and add it to the report as undocumented
- A false positive
- Out of scope by definition
A documented RDP service is not found by the scan. What should the report say?
- Nothing
- That it was documented but not reachable from the tester's position (filtered or down), so it was not tested
- That RDP is secure
- That the documentation is fake
What should key a service inventory?
- Host name
- Address, protocol and port
- Product name
- Scan date