In this step, we're going to learn about the various output formats that Nmap offers. Output formats are crucial in cybersecurity because they allow us to present the scan results in different ways, depending on our needs. For instance, some formats are easy for humans to read, while others are designed for machines to parse, which is useful when integrating with other tools.
Nmap supports several output formats, each with its own unique characteristics and use - cases:
- Normal Output (default): This is a human - readable format. It presents the scan results in a way that is easy for us to understand at a glance, making it great for quick manual analysis.
- XML Output (
-oX
): XML stands for Extensible Markup Language. It's a structured format, which means the data is organized in a hierarchical way. This makes it easy for programs and scripts to parse the data, and it's commonly used when integrating Nmap with other security tools.
- Grepable Output (
-oG
): This is a line - based format. It's designed to be easily processed with Unix tools like grep, awk, and cut. This format is useful when you want to quickly extract specific information from the scan results.
- Script Kiddie Output (
-oS
): This format is similar to the normal output, but it includes ASCII art. However, it's rarely used in practical scenarios.
- All Formats (
-oA
): This option allows you to save the scan results in normal, XML, and grepable formats simultaneously. It's a convenient way to have all types of outputs available at once.
Let's explore these formats by scanning your local web server again.
XML, or Extensible Markup Language, is a structured format that is widely used for data storage and exchange. Its hierarchical structure makes it easy for programs to parse the data, which is why it's commonly used for integration with other security tools.
-
Run an Nmap scan and save the output in XML format:
nmap -p 8080 localhost -oX /home/labex/project/scan_results.xml
In this command, -p 8080
specifies that we're scanning port 8080, localhost
is the target we're scanning (our local machine), and -oX
tells Nmap to save the output in XML format to the specified file path.
-
View the XML output:
cat /home/labex/project/scan_results.xml
The cat
command is used to display the contents of a file. When you run this command, you'll see XML - formatted output that looks something like this (abbreviated):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nmaprun>
<nmaprun scanner="nmap" args="nmap -p 8080 localhost -oX /home/labex/project/scan_results.xml" ...>
<scaninfo type="connect" protocol="tcp" .../>
<verbose level="0"/>
<debugging level="0"/>
<host>
<status state="up" reason="conn - refused" reason_ttl="0"/>
<address addr="127.0.0.1" addrtype="ipv4"/>
<hostnames>
<hostname name="localhost" type="user"/>
<hostname name="localhost" type="PTR"/>
</hostnames>
<ports>
<port protocol="tcp" portid="8080">
<state state="open" reason="syn - ack" reason_ttl="0"/>
<service name="http - proxy" method="table" conf="3"/>
</port>
</ports>
<times srtt="97" rttvar="5000" to="100000"/>
</host>
<runstats>...</runstats>
</nmaprun>
-
The XML format includes detailed information organized in a hierarchical structure. Let's extract the specific port information:
grep -A5 "<port " /home/labex/project/scan_results.xml > /home/labex/project/port_details.txt
The grep
command is used to search for a specific pattern in a file. -A5
means to display 5 lines after the line that matches the pattern. We're searching for lines that contain <port
in the XML file and saving the results to a new file called port_details.txt
.
-
View the extracted port details:
cat /home/labex/project/port_details.txt
Running this command will show you the port information section from the XML.
Grepable output is specifically designed to be easily processed with tools like grep, awk, and cut. These Unix tools are very powerful for text processing and can help us quickly extract the information we need from the scan results.
-
Run an Nmap scan and save the output in grepable format:
nmap -p 8080 localhost -oG /home/labex/project/scan_results.grep
Here, -oG
tells Nmap to save the output in grepable format to the specified file.
-
View the grepable output:
cat /home/labex/project/scan_results.grep
The output will look similar to this:
## Nmap 7.80 scan initiated Wed Nov 8 12:40:00 2023 as: nmap -p 8080 localhost -oG /home/labex/project/scan_results.grep
Host: 127.0.0.1 (localhost) Status: Up
Host: 127.0.0.1 (localhost) Ports: 8080/open/tcp//http - proxy///
## Nmap done at Wed Nov 8 12:40:00 2023 -- 1 IP address (1 host up) scanned in 0.05 seconds
-
The grepable format puts all host information on a single line, making it easy to use with text processing tools. For example, you can extract just the line containing port information:
grep "Ports:" /home/labex/project/scan_results.grep > /home/labex/project/ports_info.txt
This command searches for lines containing the word "Ports:" in the grepable output file and saves the matching line to a new file called ports_info.txt
.
-
View the extracted ports information:
cat /home/labex/project/ports_info.txt
Nmap provides a convenient option to save the output in multiple formats at once using the -oA
option. This can save you time if you need different types of outputs for different purposes.
-
Run an Nmap scan and save the output in all formats:
nmap -p 8080 localhost -oA /home/labex/project/all_formats
This command scans port 8080 on the local machine and saves the results in normal, XML, and grepable formats.
-
Check the files created:
ls -la /home/labex/project/all_formats.*
The ls -la
command lists all files in a directory with detailed information. When you run this command, you should see three files:
all_formats.nmap
(normal output)
all_formats.xml
(XML output)
all_formats.gnmap
(grepable output)
Now you understand the different output formats that Nmap offers and how to use them. Each format has its own advantages:
- Normal output is easy for humans to read
- XML output is structured for machine parsing and integration with other tools
- Grepable output is designed for quick analysis with Unix text processing tools
In the next step, you will learn how to analyze these outputs in more detail.