Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 36 ยท Reporting and Automation

Parsing Nmap XML Output

Read Nmap XML with Python's ElementTree: extract hosts, open ports, services and script output, build an inventory table and export CSV and JSON.

The XML file written by -oX (or by -oA) is the best starting point for any report, dashboard or database. It has a stable structure, it escapes special characters properly and it contains every detail Nmap collected. In this lesson you learn the structure and read it with Python's built-in xml.etree.ElementTree. The same ideas work in any language that has an XML library.

The sample XML below is invented, uses documentation addresses and was written by hand in the shape Nmap uses. Real files have more attributes, but the elements you need are the same. You can run the code unchanged on files from your own authorised scans.

The structure

flowchart TD
    R["nmaprun<br/>(args, start, version)"] --> H["host (one per target)"]
    R --> S["runstats<br/>(elapsed, hosts up)"]
    H --> ST["status state=up"]
    H --> AD["address addr, addrtype"]
    H --> HN["hostnames"]
    H --> PO["ports"]
    H --> OS["os / osmatch"]
    PO --> P["port protocol, portid"]
    P --> PS["state state, reason"]
    P --> SV["service name, product, version"]
    P --> SC["script id, output"]

The main elements of Nmap XML. A host can have several address elements (IPv4 and MAC).

Important details:

  • Each <host> has <status>, one or more <address> elements, optional <hostnames>, <ports>, <os> and <hostscript>.
  • <port protocol="tcp" portid="22"> contains <state>, optionally <service> and <script> elements.
  • Ports that are not shown because they are closed or filtered are summarised in <extraports>.
  • Script results appear as <script id="http-title" output="..."/>, and structured ones contain nested <table> and <elem> elements.

Reading hosts and ports

Python Extract an inventory from XML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two defensive coding habits are visible: find(...) can return None, so check before calling .get, and attributes such as product may be missing, so use .get(name, default).

Building tables, CSV and JSON

Once the data is in Python structures, exporting is easy. The next example flattens the XML to one row per open port, prints a CSV and a JSON document, and counts services.

Python Flatten to CSV and JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Parsing safely

  • Untrusted XML: XML you wrote yourself is fine, but files from other parties can contain tricks (entity expansion). Use the defusedxml package in production code to parse files you did not create. It has the same interface as ElementTree.
  • Large files: a scan of thousands of hosts can be big. ET.iterparse processes the file element by element and frees memory.
  • Different versions: Nmap adds new elements over time. Read what you need and ignore the rest, instead of failing on unknown tags.
  • IPv6 and MAC addresses: a host can have several <address> elements, with addrtype values ipv4, ipv6 and mac. Pick by addrtype, not by position.
Python Handle several address types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Turning XML into HTML

You do not have to write code for a quick report: xsltproc scan.xml -o report.html applies the stylesheet that comes with Nmap (nmap.xsl) and creates a readable page. For recurring reports you can still use Python to produce Markdown or HTML from the extracted rows, which is what you will do in Project: A Scan Report.

Terminal
# Example commands (read-only)
nmap -sV -oX scan.xml 192.0.2.0/25
xsltproc scan.xml -o report.html

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which XML element holds one scanned target?

    Each target is a host element. nmaprun is the root, and port elements live inside host/ports.
  2. 2. Why use .get('product', '') rather than .get('product')?

    Service details vary, so attributes such as product or version may be absent. A default keeps the code robust.
  3. 3. Which is the safe way to parse XML files that come from someone else?

    Untrusted XML can contain entity-expansion tricks. defusedxml provides the same API with protections.
  4. 4. How do you pick a host's IPv4 address reliably?

    A host can have IPv4, IPv6 and MAC addresses in any order, so select by addrtype.

Summary

  • Nmap XML has one host per target, with address, ports/port, state, service and script elements.
  • ElementTree reads it in a few lines; check for missing elements and attributes.
  • Flatten to rows to export CSV or JSON and answer questions such as "which hosts run old OpenSSH".
  • Parse untrusted XML with defusedxml, and select addresses by addrtype.

Next: Automating Scans and Comparing Results