Nmap Tutorial
Nmap Tutorial
Auditing
Abstract
Nmap (Network Mapper) stands as a crucial open-source tool for
network discovery, security auditing, and vulnerability assessment. This
tutorial aims to empower network administrators, security professionals,
and ethical hackers with a comprehensive understanding of Nmap's
functionalities. Beginning with installation guidance across multiple
platforms, it proceeds to explore scanning techniques, including SYN
scans, TCP connect scans, and UDP scans.
The tutorial emphasizes best practices for output handling and analysis,
showcasing the integration of Nmap with other tools for streamlined
security workflows. By the conclusion, readers will be equipped to
conduct thorough network scans, identify vulnerabilities, and fortify
network security effectively.
Installation
Linux
# RHEL/CentOS/Fedora
sudo yum install nmap
---------------------------------------------------------------------------------------
Windows
For Windows systems, visit the official Nmap website
(https://nmap.org/download.html) and download the latest version of the
Windows installer. Run the installer and follow the prompts to complete
the installation process.
Alternatively, you can install Nmap on Windows using a package
manager like Chocolatey. Open an elevated Command Prompt or
PowerShell window and run the following command:
--------------------------------------------------------------------------------------
choco install nmap
--------------------------------------------------------------------------------------
This will download and install the latest version of Nmap from the
Chocolatey repository.
After installation, you can run Nmap from the Command Prompt or
PowerShell by simply typing `nmap` followed by the desired options
and targets.
macOS
On macOS, Nmap can be installed using the Homebrew package
manager:
---------------------------------------------------------------------------------------
-brew install nmap
Basic Nmap Scan
After successful installation, you can begin with a basic Nmap scan to
discover active hosts on your network and gather information about
open ports and services. Open a terminal or command prompt and run
the following command:
---------------------------------------------------------------------------------------
nmap <target>
---------------------------------------------------------------------------------------
Replace `<target>` with the IP address, hostname, or network range you
want to scan. For example:
---------------------------------------------------------------------------------------
nmap 192.168.1.1 # Scan a single IP address
nmap 192.168.1.0/24 # Scan a subnet using CIDR notation
nmap example.com # Scan a domain name
---------------------------------------------------------------------------------------
This command will perform a default TCP connect scan and display
information about open ports, services, and other relevant details.
Scan Types
- SYN Scan (-sS): A stealthy scan that determines open ports by sending
SYN packets and analyzing the responses. This is the default scan type.
- TCP Connect Scan (-sT): A comprehensive scan that establishes full
TCP connections with the target system to determine open ports. This is
the default scan type when running Nmap without root privileges.
- UDP Scan (-sU): Scans for open UDP ports on the target system.
- ACK Scan (-sA): Determines whether ports are filtered or unfiltered by
sending ACK packets.
- Window Scan (-sW): Attempts to determine open ports by analyzing
the TCP Window field in the response packets.
- Maimon Scan (-sM): A stealthy scan that uses FIN/ACK packets to
determine open ports.
To specify a scan type, use the corresponding option. For example:
-----------------------------------------------------------------------------------
nmap -sS 192.168.1.1 # SYN scan
nmap -sU 192.168.1.1 # UDP scan
-------------------------------------------------------------------------------------
Port Specification
By default, Nmap scans the most common 1000 ports. However, you
can specify the ports or port ranges you want to scan using the `-p`
option:
---------------------------------------------------------------------------------------
nmap -p 22,80,443 192.168.1.1 # Scan specific ports
nmap -p 1-65535 192.168.1.1 # Scan all ports
nmap -p U:53,T:21-25,80 192.168.1.1 # Scan TCP and UDP ports
nmap -p- 192.168.1.1 # Scan all ports (same as 1-65535)
---------------------------------------------------------------------------------------
nmap -p http,https 192.168.1.1 # Scan HTTP and HTTPS ports
---------------------------------------------------------------------------------------
Service and Version Detection
This can provide valuable information about the services, their versions,
and potential vulnerabilities. You can adjust the intensity level of
version detection using the `--version-intensity` option:
---------------------------------------------------------------------------------------
nmap -sV --version-intensity 8 192.168.1.1 # Higher intensity (0-9)
---------------------------------------------------------------------------------------
Nmap can also attempt to identify the operating system running on the
target system using TCP/IP stack fingerprinting. Enable OS detection
with the `-O` option:
---------------------------------------------------------------------------------------
nmap -O 192.168.1.1
---------------------------------------------------------------------------------------
This feature can help you understand the target environment and
potential vulnerabilities associated with specific operating systems.
For example:
---------------------------------------------------------------------------------------
nmap -T4 192.168.1.1 # Aggressive scan
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
nmap --script=banner 192.168.1.1 # Scan with the "banner" script
---------------------------------------------------------------------------------------
You can also use wildcards to run multiple scripts:
---------------------------------------------------------------------------------------
nmap --script=http* 192.168.1.1 # Run all HTTP-related scripts
---------------------------------------------------------------------------------------
To run the default set of scripts considered useful for discovery and safe,
use the `-sC` or `--script=default` option:
---------------------------------------------------------------------------------------
nmap -sC 192.168.1.1
nmap --script=default 192.168.1.1
---------------------------------------------------------------------------------------
Some useful NSE script examples include:
2. .Decoys (-D)
The decoy option sends scans from spoofed IP addresses, making it
appear as if the scan is coming from multiple sources. This can help
evade security systems that filter based on the source IP address.
-----------------------------------------------------------------------------
nmap -D 192.168.1.101,192.168.1.102,192.168.1.103 192.168.1.1
-----------------------------------------------------------------------------
3. Source Port (-g)
This option specifies the source port number to use for the scan. Some
firewalls and IDS may be configured to allow or block specific port
numbers.
-----------------------------------------------------------------------------
nmap -g 53 192.168.1.1 # Scan using source port 53 (DNS)
-----------------------------------------------------------------------------
Nmap provides several options for handling and formatting scan output:
- Normal Output (-oN): Saves output in a normal human-readable
format.
- XML Output (-oX): Saves output in XML format.
- Grepable Output (-oG): Saves output in a format suitable for grep.
- All Formats (-oA): Saves output in all major formats (normal, XML,
and grepable).
- Append Output (--append-output): Appends a scan to a previous scan
file.
- Verbosity (-v/-vv): Increases the verbosity level of the output.
- Debugging (-d/-dd): Increases the debugging level of the output.
- Reason (--reason): Displays the reason a port is in a particular state.
- Open Only (--open): Only shows open (or possibly open) ports.
- Packet Trace (--packet-trace): Shows all packets sent and received.
Example output handling commands:
---------------------------------------------------------------------------------------
nmap -p80 -sV -oG - --open 192.168.1.1/24 | grep open # Scan for web
servers and grep open hosts
nmap -iR 10 -n -oX out.xml | grep "Nmap" | cut -d " " -f5 > live-hosts.txt
# Generate live host list
ndiff scan1.xml scan2.xml # Compare Nmap XML output files
xsltproc nmap.xml -o nmap.html # Convert Nmap XML to HTML
---------------------------------------------------------------------------------------
Scripting and Automation
When using Nmap for network scanning and security auditing, it's
essential to follow best practices and security considerations:
Nmap is a powerful and versatile tool that plays a crucial role in network
security and vulnerability assessment. By leveraging its extensive
capabilities, including port scanning, service and version detection,
operating system fingerprinting, scripting, and evasion techniques, you
can gain valuable insights into your network infrastructure and identify
potential security risks.
This tutorial has provided a comprehensive overview of Nmap's
functionalities, equipping you with the knowledge and skills to
effectively utilize this tool in your network security efforts. Whether you
are a network administrator, security professional, or ethical hacker,
mastering Nmap will enhance your ability to protect systems, discover
vulnerabilities, and maintain a secure network environment.