Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Information Gathering

Nmap, db_nmap and Importing Scan Results

Use Nmap together with Metasploit: run db_nmap, import XML, read host and service tables, choose sensible scan options, and understand how defenders spot scans.

Nmap is the standard network scanner, and Metasploit is built to cooperate with it. The console command db_nmap runs Nmap and stores the results in the current workspace, so hosts and services appear in hosts and services without any copying. You can also import XML produced earlier with db_import.

All scans in this course are aimed at your own lab range. The commands and outputs below are read-only examples. The Python stand-ins parse sample Nmap output so you can learn the format.

Running a scan from the console

TEXT
msf6 > workspace lab-recon
msf6 > db_nmap -sV -T3 10.10.10.20
[*] Nmap: Starting Nmap ( https://nmap.org )
[*] Nmap: Nmap scan report for 10.10.10.20
[*] Nmap: PORT   STATE SERVICE VERSION
[*] Nmap: 22/tcp open  ssh     OpenSSH 8.x
[*] Nmap: 80/tcp open  http    Example web server 2.4
[*] Nmap: Nmap done: 1 IP address (1 host up) scanned in 7.21 seconds
msf6 > services

(Example output; real versions and timings differ.) db_nmap passes your options to Nmap and saves the result. Because Nmap runs from the console, it uses the same working directory and privileges.

Useful options

Option Meaning
-sn Host discovery only; no port scan
-sT TCP connect scan; needs no special privileges
-sS SYN ("half-open") scan; faster, needs raw-socket privileges
-sV Detect service versions
-O Guess the operating system
-p 22,80,443 or -p- Chosen ports, or all 65,535
--top-ports 100 The most common ports
-T0 to -T5 Timing: from very slow to aggressive
-oX file.xml Save XML (for db_import)
--script safe Run safe NSE scripts (read the script's description first)

A calm first scan of a lab host: -sV --top-ports 1000 -T3. Aggressive timing (-T4/-T5) is fine on your own lab, not on production without approval.

Nmap scripts in categories such as intrusive, brute or dos can disrupt systems or guess passwords. Only run categories explicitly allowed in your rules of engagement.

Importing XML

TEXT
msf6 > db_import /home/lab/scans/lab-range.xml
[*] Importing 'Nmap XML' data
[*] Successfully imported /home/lab/scans/lab-range.xml

db_import recognises many scanner formats (Nmap, several vulnerability scanners and more) and fills the host, service and vulnerability tables. Import is useful when the scan ran from another machine or while you were away, and it lets you analyse a client-provided scan without touching the network.

flowchart LR
    N["nmap -oX scan.xml"] --> I["db_import"]
    D["db_nmap"] --> DB[("Workspace database")]
    I --> DB
    DB --> H["hosts / services / vulns"]
    DB --> R["services -R fills RHOSTS"]

Both routes end in the same tables.

Reading scan output in code

Nmap's XML is structured, so you can parse it with a standard library. The stand-in below parses a small invented XML report, using Python's xml.etree, and prints a summary table of open ports. It is the same data db_import reads.

Python Parse an Nmap-style XML report

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.

Closed ports are skipped; only open ports are reported.

Flagging old versions

Recon becomes useful when versions meet a known-bad list. The next stand-in compares service versions to a minimum safe version table (invented numbers) and flags outdated ones, the same logic vulnerability scanners use at a much larger scale.

Python Flag outdated service versions

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.

A version check is only a hint: distributions often backport fixes without changing the version number, so confirm with the vendor's advisory before reporting.

How defenders see scans

  • Firewall and IDS logs show one source touching many ports or hosts in a short time; Nmap's default probes have recognisable patterns.
  • Honeypot ports that nothing legitimate uses alert on any connection.
  • Rate limits and tarpits slow scanners down.
  • Network flow data shows scanning even when hosts keep no logs.

Defenders reading logs after your test should be able to match your scan to your time stamps. That is why you keep a log and, where agreed, tell a contact when you start.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does db_nmap do?

    db_nmap launches Nmap and imports its results into the workspace.
  2. 2. Which Nmap option detects service versions?

    -sV probes open ports to identify product and version.
  3. 3. Which command loads an existing Nmap XML file into the database?

    db_import reads Nmap XML and other scanner formats.
  4. 4. Why is a version comparison not proof of a vulnerability?

    Backported patches mean the visible version may look old while being fixed.

Summary

  • db_nmap runs and stores Nmap scans; db_import loads XML from elsewhere.
  • Start with calm options: -sV, top ports, -T3; avoid intrusive scripts without permission.
  • XML is easy to parse in code; compare versions to advisories to find candidates.
  • Scans are visible to defenders: log your own activity.

Next: Service Enumeration Modules