TExcelTAD
Home › Network Security: Discovery, Segmentation, Firewalls, VPN & IDS › Nmap Inventories, Output Formats and Baselines

Nmap Inventories, Output Formats and Baselines

A scan you only read on the screen is a scan you will redo. Assessments and continuous monitoring both need scan results as data: saved with the exact command and time, parsed by programs, compared with the last run and with the approved list of services. Nmap writes several output formats for exactly this. This lesson turns scans into an inventory and an inventory into a baseline whose differences are findings.

Scope of every session below: services the session starts on 127.0.0.1 in the sandbox. The same commands against the lab network appear as transcripts.

Output formats

Option Format Use
-oN file normal text, as on screen the report appendix, humans
-oX file XML with every detail (command line, times, states, reasons, service versions, script output) programs, the only complete format
-oG file "grepable": one line per host quick shell pipelines (deprecated, but still common)
-oA base all three at once: base.nmap, base.xml, base.gnmap the default for assessments

Always keep the XML. It records the command line and the start time, which is the evidence that a scan stayed in scope and window.

A scan saved and parsed

The session starts three services, scans them into all three formats, and then reads the XML with Python's standard xml.etree module, the way an inventory script would.

bash · try it yourself
python3 -m http.server 8000 --bind 127.0.0.1 >http.log 2>&1 &
ncat -lk 127.0.0.1 5432 >/dev/null 2>&1 &
ncat -lk 127.0.0.1 2323 >/dev/null 2>&1 &
for i in $(seq 50); do [ "$(ss -Htln | wc -l)" -ge 3 ] && break; sleep 0.1; done
nmap -sT -p 22,2323,5432,8000 -oA scan1 127.0.0.1 >/dev/null
ls scan1.*
echo "--- grepable line:"
grep Ports scan1.gnmap
echo "--- parsed from XML:"
python3 - <<'PY'
import xml.etree.ElementTree as ET
root = ET.parse("scan1.xml").getroot()
print("args:", root.get("args"))
for host in root.iter("host"):
    addr = host.find("address").get("addr")
    for p in host.iter("port"):
        st = p.find("state")
        svc = p.find("service")
        print(f"{addr} {p.get('portid')}/{p.get('protocol')} {st.get('state'):7} reason={st.get('reason'):13} {svc.get('name') if svc is not None else '-'}")
PY
kill %1 %2 %3
scan1.gnmap
scan1.nmap
scan1.xml
--- grepable line:
Host: 127.0.0.1 (localhost)	Ports: 22/closed/tcp//ssh///, 2323/open/tcp//3d-nfsd///, 5432/open/tcp//postgresql///, 8000/open/tcp//http-alt///
--- parsed from XML:
args: nmap -sT -p 22,2323,5432,8000 -oA scan1 127.0.0.1
127.0.0.1 22/tcp closed  reason=conn-refused  ssh
127.0.0.1 2323/tcp open    reason=syn-ack       3d-nfsd
127.0.0.1 5432/tcp open    reason=syn-ack       postgresql
127.0.0.1 8000/tcp open    reason=syn-ack       http-alt

Baselines: what changed?

An inventory is useful when it is compared. The approved baseline for a host is a list of ports and the reason they exist; a new scan is compared with it, and with the previous scan. The session takes two scans a moment apart, with one service stopped and a new one started in between, and diffs them.

bash · try it yourself
python3 -m http.server 8000 --bind 127.0.0.1 >http.log 2>&1 &
ncat -lk 127.0.0.1 2323 >/dev/null 2>&1 & TELNETLIKE=$!
for i in $(seq 50); do [ "$(ss -Htln | wc -l)" -ge 2 ] && break; sleep 0.1; done
nmap -sT -p 2323,4444,8000 -oX before.xml 127.0.0.1 >/dev/null
kill $TELNETLIKE
ncat -lk 127.0.0.1 4444 >/dev/null 2>&1 &
for i in $(seq 50); do ss -Htln | grep -q ':4444' && break; sleep 0.1; done
nmap -sT -p 2323,4444,8000 -oX after.xml 127.0.0.1 >/dev/null
python3 - <<'PY'
import xml.etree.ElementTree as ET
def open_ports(f):
    return {p.get("portid") + "/" + p.get("protocol")
            for p in ET.parse(f).getroot().iter("port") if p.find("state").get("state") == "open"}
approved = {"8000/tcp"}
before, after = open_ports("before.xml"), open_ports("after.xml")
print("before:", sorted(before)); print("after: ", sorted(after))
print("newly open:   ", sorted(after - before))
print("no longer open:", sorted(before - after))
print("open but not approved:", sorted(after - approved))
PY
kill %1 %3 2>/dev/null; wait 2>/dev/null
before: ['2323/tcp', '8000/tcp']
after:  ['4444/tcp', '8000/tcp']
newly open:    ['4444/tcp']
no longer open: ['2323/tcp']
open but not approved: ['4444/tcp']

4444/tcp is new and unapproved: that is the alert. On a real network the same diff runs every night from a scanner host, and each difference becomes a ticket: either the baseline is updated (with a reason and an owner) or the service is removed. Nmap ships ndiff for comparing two XML files; the few lines of Python above do the same for open ports and add the comparison with the approved list.

The lab network, as a transcript

An authorised internal scan of the lab network (lab session, scope 10.10.0.0/24 minus the leased printer, source 10.10.0.99, window 2026-02-03 18:00–22:00 UTC, --max-rate 100):

console
assessor@kali:~$ sudo nmap -sS -sV --top-ports 1000 --max-rate 100 --exclude 10.10.0.40 -oA lab-int-0203 10.10.0.0/24
Starting Nmap 7.94SVN ( https://nmap.org ) at 2026-02-03 18:04 UTC
Nmap scan report for fw.lab.test (10.10.0.1)
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 9.6p1 Ubuntu 3ubuntu13.14 (Ubuntu Linux; protocol 2.0)
53/tcp open  domain  dnsmasq 2.90
Nmap scan report for www.lab.test (10.10.0.10)
PORT    STATE SERVICE  VERSION
22/tcp  open  ssh      OpenSSH 9.6p1 Ubuntu 3ubuntu13.14 (Ubuntu Linux; protocol 2.0)
80/tcp  open  http     nginx 1.24.0 (Ubuntu)
443/tcp open  ssl/http nginx 1.24.0 (Ubuntu)
Nmap scan report for mail.lab.test (10.10.0.25)
PORT    STATE SERVICE VERSION
25/tcp  open  smtp    Postfix smtpd
143/tcp open  imap    Dovecot imapd
587/tcp open  smtp    Postfix smtpd
Nmap scan report for db.lab.test (10.10.0.30)
PORT     STATE SERVICE    VERSION
22/tcp   open  ssh        OpenSSH 8.2p1 Ubuntu 4ubuntu0.5 (Ubuntu Linux; protocol 2.0)
5432/tcp open  postgresql PostgreSQL DB 12.0 - 12.9
Nmap scan report for files.lab.test (10.10.0.60)
PORT    STATE SERVICE     VERSION
21/tcp  open  ftp         vsftpd 3.0.3
445/tcp open  netbios-ssn Samba smbd 4.6.2
Nmap scan report for 10.10.0.77
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 9.2p1 Debian 2+deb12u2 (protocol 2.0)
80/tcp open  http    lighttpd 1.4.69
Nmap done: 255 IP addresses (8 hosts up) scanned in 2873.42 seconds

(The transcript is shortened: closed-port summaries and the workstations, which showed no open ports, are left out.) This scan is the raw material for the whole "Common network weaknesses" section: the database reachable from every host, FTP on the file server, an old SSH on the database, an unknown Raspberry Pi with a web interface.

What one scan can't see

An inventory scan is a view from one place at one time. It misses hosts that were off or asleep (laptops, the handhelds of a night shift), services that only listen on IPv6 (scan with -6 against addresses from the neighbour tables, since a /64 can't be swept), UDP services unless you scan for them, and anything a firewall between the scanner and the host hides. Record those limits in the scan's notes and fill the gaps from the passive sources of Asset Inventory, from scans in other zones, and from host agents.

How continuous scanning supports detection

  • Nightly or weekly scans diffed against the baseline catch new listeners within a day: a developer's test server, a backdoor, an accidentally exposed management port.
  • External scans from outside the perimeter (an Internet-facing view) and internal scans from each zone show what each position can reach, which is how segmentation is verified.
  • Results go to the inventory and the SIEM, where a new open port on a sensitive host is an event.

Pitfalls

  • Saving only -oN: you lose the reasons, the timing and machine-readable versions.
  • Comparing scans made with different options or from different source addresses: the difference is the scan, not the network.
  • Letting the baseline grow without reasons; "approved" must mean someone owns the service.
  • Parsing -oG for anything serious: it has no field for script output and is officially deprecated.

Practice

1. Which XML element and attribute tell you *why* Nmap decided a port's state?

<state state="open" reason="syn-ack" .../> inside each <port>: the reason attribute (syn-ack, conn-refused, no-response, ...).

2. Extend the diff to report ports whose service name changed between the scans.

Build a dictionary port → service name for each file (p.find("service").get("name")), then report ports present in both where the names differ. A changed name on the same port is worth a look: a different program now listens there.

3. In the lab transcript, list three findings for the report.

PostgreSQL on 10.10.0.30 reachable from the scanner's position (every internal host); FTP (cleartext) on the file server; an unknown host 10.10.0.77 with SSH and a web server; older OpenSSH 8.2p1 on the database server; DNS (53/tcp) answering on the firewall.

Test yourself

Which Nmap output format is the only complete one, including the command line and reasons?

  1. -oN
  2. -oG
  3. -oX
  4. -oS

In the baseline session, which port was reported as open but not approved?

  1. 8000/tcp
  2. 2323/tcp
  3. 4444/tcp
  4. 22/tcp

Why compare only scans made with the same options from the same source?

  1. Nmap refuses otherwise
  2. Otherwise differences may come from the scan setup, not from the network
  3. XML files differ in format
  4. To save disk space