SQL Comes to Nmap: Power and Convenience
Tables Used by nmaplog
At this time, eight tables can be found in the nmaplog database. Relevant tables are:
TARGETS—contains information about the target host, including the target ID, IP address, resolved hostname and OS guessed by Nmap. The hostid field links to the portstat table.
SCANNERS—contains information about the host on which nmapsql is executed. The scan_id field is our link to the portstat table.
RUNLIST—contains the user ID, date and time information about each invocation of Nmap, including the host from which it was run. The user name and user ID are from /etc/passwd. The scanner_id field ties to scanners.scan_id.
PORTSTAT—contains the port scan results. Each port reported by nmaplog is recorded, along with a state (open/close/filtered). Nearly all of the other tables link to this table through the ID fields.
HOSTSSTAT—contains rudimentary statistics about the target host for each run of nmapsql, such as the total ports scanned and number of ports found open.
Each target scanned also is assigned a tag and the information stored in the targets table. As with the runlist, the rows in the targets table are populated in two stages. The first stage captures the IP address and the hostname if resolvable, and the second stage populates the os_guessed column. At this time, the fingerprint information for unrecognized OS is not stored, but it may be in the future. No duplicates ever are created in the targets table. In my experience, the only situation where you might have duplicate IP subnets is when you move from one customer to another. A different database for each customer should be used in such cases.
The target IDs are not used at the moment, but you're able to specify your own target ID for any target on the command line. If the specified ID exists, it is ignored and a system-generated ID is used instead, for the sake of uniqueness. If the target ID value after --target-id on the command line does not exist in the targets table, it is assigned to the IP address of the current target. If the target specification is for multiple systems, the first target has the specified target ID, with the subsequent ones being assigned incremental IDs.
nmapsql logs the date and time of execution, the user who executed Nmap, the host on which Nmap is running and an identification number for the execution. These last two items allow nmapsql to be used in large environments and form the basis of comparison among scans. The runid, or runtime ID, is always unique within that data set. If the target specification remains the same, the runid alone can differentiate the results of two scans. But it's also possible to group results of multiple scans under a single runid using the --run-id command-line option. For instance, consider the following invocation of nmapsql:
$ nmap -A --mysql --runid 100 192.168.10.1/24
This command starts Nmap with the logging functionality enabled by the --mysql option, assigns 100 for the current runid and scans the 192.168.10.1/24 network. If this is the first invocation of nmapsql, this would establish a baseline for the network against which all subsequent runs could be compared. nmapsql also automatically creates an entry for the host on which it's running, in this case 192.168.10.44, and assigns it a scanner_id in the scanners table. Partial console output from Nmap for this run is shown in Listing 1.
Listing 1. Partial Output from Nmap
Starting nmap 3.48 ( http://www.insecure.org/nmap/ ) at 2003-12-14 10:00 SGT Insufficient responses for TCP sequencing (1), OS detection may be less accurate Interesting ports on 192.168.10.0: (The 1656 ports scanned but not shown below are in state: closed) PORT STATE SERVICE VERSION 80/tcp open http? Device type: print server Running: Linksys embedded OS details: Linksys EtherFast print server Interesting ports on wap.hasnains.com (192.168.10.1): (The 1654 ports scanned but not shown below are in state: closed) PORT STATE SERVICE VERSION 21/tcp open ftp? 23/tcp open telnet? 80/tcp open http Device type: broadband router Running: Zyxel ZyNOS OS details: ZyXEL Prestige 700/Netgear MA314 broadband router
The target specification in this example is the entire Class C subnet. nmapsql auto-assigns a unique target ID for each live host in the network and stores additional information in the hoststats table. This table alone can be a poor-man's port scan result comparison tool.
Let's take a quick look at what was logged. To do that, we log in to the MySQL client and connect to the database listed in the nmapsql.rc file. Then we issue the query:
$ mysql nmaplog -p mysql> select target_ip, d, t, port, protocol, -> state, runid from portstat -> order by target_ip, d, t ;
This query would produce the table shown in Listing 2. It provides a nice listing ordered by target IP, date and time. Notice that the runid column has 100 for all the rows as stated on the command line.
Free DevOps eBooks, Videos, and more!
Regardless of where you are in your DevOps process, Linux Journal can help!
We offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, and advice & help from the expert sources like:
- Linux Journal
- High-Availability Storage with HA-LVM
- DNSMasq, the Pint-Sized Super Dæmon!
- Localhost DNS Cache
- Real-Time Rogue Wireless Access Point Detection with the Raspberry Pi
- Days Between Dates: the Counting
- Linux for Astronomers
- You're the Boss with UBOS
- The Usability of GNOME
- Multitenant Sites
- PostgreSQL, the NoSQL Database