Comprehensive Guide to Nmap: Commands, Use Cases, and FAQs
Last Updated: October 2023
Introduction to Nmap
What is Nmap?
Nmap, short for Network Mapper, is a free and open-source utility used for network discovery and security auditing. It is an essential tool for network administrators and security professionals to:
- Discover hosts and services on a computer network.
- Create a detailed map of the network.
- Perform security assessments and audits.
Key Features
- Host Discovery: Identify active devices on a network.
- Port Scanning: Enumerate open ports and services.
- Service and Version Detection: Determine the application name and version number of services.
- Operating System Detection: Identify the operating system and hardware characteristics.
- Nmap Scripting Engine (NSE): Automate tasks using scripts for vulnerability detection, backdoor detection, and more.
- Flexible Output: Generate reports in various formats like plain text, XML, and HTML.
Installing Nmap on Windows, Mac, and Linux
Windows Installation
- Download the Installer: Visit the official Nmap download page and download the Windows installer (
nmap-<version>-setup.exe
). - Run the Installer: Double-click the downloaded file and follow the installation wizard.
- Accept the license agreement.
- Choose the destination folder.
- Select components to install (Nmap, Zenmap GUI, Ncat, Ndiff, and Nping).
- Complete Installation: Click “Install” and wait for the process to finish.
- Verification:
- Open Command Prompt.
- Type
nmap --version
and press Enter. - You should see the installed Nmap version information.
Mac Installation
- Using Homebrew:
- Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Update Homebrew:
brew update
- Install Nmap:
brew install nmap
- Install Homebrew if not already installed:
- Alternative Method:
- Download the Mac binary from the official website.
- Extract the package and follow the installation instructions.
- Verification:
- Open Terminal.
- Type
nmap --version
and press Enter.
Linux Installation
- Ubuntu/Debian:
sudo apt-get update sudo apt-get install nmap
- Fedora:
sudo dnf install nmap
- CentOS/RHEL:
sudo yum install nmap
- Arch Linux:
sudo pacman -S nmap
- Verification:
- Open Terminal.
- Type
nmap --version
and press Enter.
Understanding Nmap Basics
Before diving into commands, it’s crucial to understand how Nmap works and its core components.
Scanning Techniques
- TCP Connect Scan (
-sT
): Establishes a full TCP connection with the target. - SYN Scan (
-sS
): Sends SYN packets and analyzes responses (stealthy scan). - UDP Scan (
-sU
): Scans for open UDP ports. - Ping Scan (
-sn
): Checks if hosts are up without scanning ports. - Aggressive Scan (
-A
): Enables OS detection, version detection, script scanning, and traceroute.
Target Specification
Nmap allows various ways to specify targets:
- Single IP:
192.168.1.1
- Range of IPs:
192.168.1.1-100
- CIDR Notation:
192.168.1.0/24
- Hostname:
example.com
- List of targets from a file:
-iL targets.txt
Port Specification
- Single Port:
-p 80
- Multiple Ports:
-p 80,443,22
- Port Range:
-p 1-1000
- All Ports:
-p-
Basic Nmap Commands
These commands are fundamental and useful for beginners.
Syntax
nmap [Scan Type(s)] [Options] {target specification}
Examples
- Ping Scan (Discover Live Hosts):
nmap -sn 192.168.1.0/24
- Description: Sends an ICMP echo request to determine which hosts are up.
- List Scan (Displays Targets):
nmap -sL 192.168.1.0/24
- Description: Lists each host in the specified range without sending packets.
- TCP Connect Scan:
nmap -sT 192.168.1.10
- Description: Performs a full TCP connection scan.
- SYN Scan (Stealth Scan):
nmap -sS 192.168.1.10
- Description: Sends SYN packets and waits for responses without completing the handshake.
- Version Detection:
nmap -sV 192.168.1.10
- Description: Detects service versions on open ports.
- OS Detection:
nmap -O 192.168.1.10
- Description: Attempts to determine the operating system of the target.
- Scanning Specific Ports:
nmap -p 22,80,443 192.168.1.10
- Description: Scans specified ports.
- Scanning All Ports:
nmap -p- 192.168.1.10
- Description: Scans all 65535 TCP ports.
- Increasing Verbosity:
nmap -v 192.168.1.10
- Description: Provides detailed output.
- Aggressive Scan:
nmap -A 192.168.1.10
- Description: Enables OS detection, version detection, script scanning, and traceroute.
Advanced Nmap Commands
For users looking to leverage Nmap’s full potential.
Timing and Performance Options
- Adjusting Timing Templates:
nmap -T4 192.168.1.10
- Description: Sets the timing template to speed up scans (
-T0
to-T5
).
- Description: Sets the timing template to speed up scans (
- Fragmentation of Packets:
nmap -f 192.168.1.10
- Description: Sends fragmented packets to evade firewalls.
- Spoofing IP Address:
nmap -S 192.168.1.100 192.168.1.10
- Description: Spoofs the source IP address.
- Decoy Scanning:
nmap -D RND:10 192.168.1.10
- Description: Uses decoy IP addresses to mask the scan.
Output Options
- Save Output to a File:
nmap -oN output.txt 192.168.1.10
- Description: Saves the output in normal format.
- Save Output in XML Format:
nmap -oX output.xml 192.168.1.10
- Description: Saves the output in XML format.
- Save All Formats:
nmap -oA output 192.168.1.10
- Description: Saves in normal, XML, and grepable formats.
Scanning Techniques
- UDP Scan:
nmap -sU 192.168.1.10
- Description: Scans for open UDP ports.
- ACK Scan:
nmap -sA 192.168.1.10
- Description: Used to map firewall rulesets.
- Fin Scan:
nmap -sF 192.168.1.10
- Description: Sends FIN packets to evade basic firewalls.
- Idle Scan:
nmap -sI zombie_host 192.168.1.10
- Description: Performs a stealth scan through a “zombie” host.
Service and Version Detection
- Aggressive Version Detection:
nmap -sV --version-intensity 5 192.168.1.10
- Description: Increases the intensity of version detection.
- Using Specific Scripts:
nmap --script http-enum 192.168.1.10
- Description: Runs the
http-enum
script to enumerate web server directories.
- Description: Runs the
- Exclude Hosts from Scan:
nmap 192.168.1.0/24 --exclude 192.168.1.5
- Description: Excludes specified hosts from the scan.
Nmap Scripting Engine (NSE)
The Nmap Scripting Engine allows users to write and use scripts to automate a wide variety of networking tasks.
Categories of NSE Scripts
- Auth: Scripts related to authentication.
- Broadcast: Discover hosts by broadcasting on the network.
- Brute: Perform brute-force password auditing.
- Discovery: Discover more about the network and hosts.
- Dos: Denial of Service testing.
- Exploit: Vulnerability exploitation.
- External: Rely on third-party databases.
- Fuzzer: Fuzz testing services.
- Intrusive: Likely to disrupt the target.
- Malware: Detect malware infections.
- Safe: Unlikely to disrupt the target.
- Version: Extended version detection.
- Vuln: Check for specific vulnerabilities.
Using NSE Scripts
- Scan for Vulnerabilities:
nmap --script vuln 192.168.1.10
- Description: Runs all scripts in the
vuln
category.
- Description: Runs all scripts in the
- Run Multiple Scripts:
nmap --script http-title,ssh-hostkey 192.168.1.10
- Description: Runs specified scripts.
- Using Script Arguments:
nmap --script http-brute --script-args userdb=users.txt,passdb=passes.txt 192.168.1.10
- Description: Performs brute-force password auditing on HTTP auth.
Nmap Use Cases
Network Discovery
- Objective: Identify active hosts and map network topology.
- Command:
nmap -sn 10.0.0.0/24
- Explanation: Performs a ping scan to discover live hosts without scanning ports.
Security Audits
- Objective: Check for open ports and vulnerabilities.
- Command:
nmap -sV --script vuln 10.0.0.5
- Explanation: Detects services and runs vulnerability scripts.
Firewall and IDS Evasion
- Objective: Test firewall configurations and evade detection.
- Command:
nmap -Pn -f -D decoy1,decoy2,decoy3 10.0.0.5
- Explanation: Disables ping, fragments packets, and uses decoys.
Compliance Testing
- Objective: Ensure systems meet security compliance standards.
- Command:
nmap -p- --script vuln,compliance 10.0.0.5
- Explanation: Scans all ports and checks for compliance-related vulnerabilities.
Performance Optimization
- Objective: Scan large networks efficiently.
- Command:
nmap -T4 -A -v 10.0.0.0/16
- Explanation: Uses a faster timing template with aggressive options and verbose output.
Top 40 Nmap Commands Compared Across Platforms
Below is a chart listing the top 40 Nmap commands, compatible across Windows, Mac, and Linux platforms. Nmap commands are generally consistent across these operating systems.
# | Command | Description | Windows | Mac | Linux |
---|---|---|---|---|---|
1 | nmap -sS target | TCP SYN scan (stealth scan) | ✔ | ✔ | ✔ |
2 | nmap -sT target | TCP connect scan | ✔ | ✔ | ✔ |
3 | nmap -sU target | UDP scan | ✔ | ✔ | ✔ |
4 | nmap -sA target | TCP ACK scan | ✔ | ✔ | ✔ |
5 | nmap -sW target | TCP Window scan | ✔ | ✔ | ✔ |
6 | nmap -sM target | TCP Maimon scan | ✔ | ✔ | ✔ |
7 | nmap -sV target | Service version detection | ✔ | ✔ | ✔ |
8 | nmap -O target | OS detection | ✔ | ✔ | ✔ |
9 | nmap -A target | Aggressive scan options | ✔ | ✔ | ✔ |
10 | nmap -v target | Verbose output | ✔ | ✔ | ✔ |
11 | nmap -Pn target | Disable host discovery (skip ping) | ✔ | ✔ | ✔ |
12 | nmap -p 80 target | Scan specific port | ✔ | ✔ | ✔ |
13 | nmap -p 1-100 target | Scan range of ports | ✔ | ✔ | ✔ |
14 | nmap -p- target | Scan all ports | ✔ | ✔ | ✔ |
15 | nmap -sC target | Scan with default scripts | ✔ | ✔ | ✔ |
16 | nmap --script=default,target | Run specific scripts | ✔ | ✔ | ✔ |
17 | nmap -D decoy1,decoy2 target | Decoy scan to mask the source IP | ✔ | ✔ | ✔ |
18 | nmap -f target | Fragment packets | ✔ | ✔ | ✔ |
19 | nmap -T0 target | Slowest scan (Paranoid) | ✔ | ✔ | ✔ |
20 | nmap -T5 target | Fastest scan (Insane) | ✔ | ✔ | ✔ |
21 | nmap -oN output.txt target | Save output to a file (normal format) | ✔ | ✔ | ✔ |
22 | nmap -oX output.xml target | Save output in XML format | ✔ | ✔ | ✔ |
23 | nmap -oG output.grep target | Save output in grepable format | ✔ | ✔ | ✔ |
24 | nmap --open target | Show only open ports | ✔ | ✔ | ✔ |
25 | nmap --max-retries 2 target | Set maximum number of port scan probe retries | ✔ | ✔ | ✔ |
26 | nmap --host-timeout 60m target | Set maximum time for host scan | ✔ | ✔ | ✔ |
27 | nmap --script-updatedb | Update script database | ✔ | ✔ | ✔ |
28 | nmap -6 target | Scan IPv6 addresses | ✔ | ✔ | ✔ |
29 | nmap -sI zombie_host target | Idle scan using zombie host | ✔ | ✔ | ✔ |
30 | nmap -sY target | SCTP INIT scan | ✔ | ✔ | ✔ |
31 | nmap -sZ target | SCTP COOKIE-ECHO scan | ✔ | ✔ | ✔ |
32 | nmap --script http-enum target | Enumerate directories on web server | ✔ | ✔ | ✔ |
33 | nmap --script smb-os-discovery target | Discover OS over SMB | ✔ | ✔ | ✔ |
34 | nmap --script ftp-anon target | Check for anonymous FTP login | ✔ | ✔ | ✔ |
35 | nmap --script ssl-heartbleed target | Check for Heartbleed vulnerability | ✔ | ✔ | ✔ |
36 | nmap --traceroute target | Perform traceroute to target | ✔ | ✔ | ✔ |
37 | nmap -iL list.txt | Scan targets from a file | ✔ | ✔ | ✔ |
38 | nmap --exclude target1,target2 | Exclude hosts from scan | ✔ | ✔ | ✔ |
39 | nmap -S spoofed_ip target | Spoof source IP address | ✔ | ✔ | ✔ |
40 | nmap --script "vuln and safe" target | Run safe vulnerability scripts | ✔ | ✔ | ✔ |
Note: All commands listed are compatible across Windows, Mac, and Linux, provided Nmap is properly installed and executed with appropriate permissions (e.g., administrative or root privileges when required).
Frequently Asked Questions (FAQs)
1. What is Nmap used for?
Nmap is used for network discovery, management, and security auditing. It helps in discovering hosts, open ports, services, and potential vulnerabilities on a network.
2. Is Nmap legal to use?
Yes, Nmap is legal to use for authorized network scanning and security auditing. Scanning networks or systems without permission is illegal and unethical. Always obtain proper authorization before performing scans.
3. How do I update Nmap to the latest version?
- Windows: Download and run the latest installer from the official Nmap website.
- Mac: Use Homebrew:
brew update brew upgrade nmap
- Linux:
- Ubuntu/Debian:
sudo apt-get update sudo apt-get install nmap
- Alternatively, compile from source:
wget https://nmap.org/dist/nmap-<version>.tar.bz2 tar xvjf nmap-<version>.tar.bz2 cd nmap-<version> ./configure make sudo make install
- Ubuntu/Debian:
4. Can Nmap detect firewalls and intrusion detection systems (IDS)?
Yes, Nmap can help identify the presence of firewalls and IDS by analyzing responses or lack thereof. Techniques like ACK scans (-sA
), FIN scans (-sF
), and using decoys can provide insights into firewall rules and IDS behaviors.
5. What is the Nmap Scripting Engine (NSE)?
The Nmap Scripting Engine allows users to write and use scripts to automate a wide variety of networking tasks, including advanced service detection, vulnerability detection, and more. It significantly extends Nmap’s capabilities.
6. How can I perform a stealth scan?
You can perform a stealth scan using the SYN scan (-sS
), which sends SYN packets and analyzes the response without completing the TCP handshake.
7. How do I scan all ports on a target?
Use the -p-
option to scan all 65535 TCP ports:
nmap -p- target
8. Can Nmap scan UDP ports?
Yes, Nmap can scan UDP ports using the -sU
option. Note that UDP scans can be slower and less reliable due to the nature of the protocol.
9. How do I save Nmap scan results to a file?
Use the output options:
- Normal output:
-oN output.txt
- XML output:
-oX output.xml
- Grepable output:
-oG output.grep
- All formats:
-oA output
10. What are the timing templates, and how do they affect scans?
Timing templates (-T0
to -T5
) control the speed and aggressiveness of scans. Lower numbers are slower and more cautious, while higher numbers are faster but more likely to be detected and less reliable on congested networks.
Conclusion
Nmap is an indispensable tool for network administrators and security professionals. Its powerful features and versatility make it suitable for tasks ranging from simple network discovery to complex security auditing and compliance testing. By understanding and utilizing the wide array of commands and options available, users can effectively manage and secure their networks across Windows, Mac, and Linux platforms.
Whether you’re performing basic scans or leveraging advanced scripting capabilities, Nmap provides the flexibility and power needed to handle various network scenarios. Always remember to use Nmap responsibly and ethically, ensuring you have proper authorization before scanning any networks or systems.
For more detailed information and updates, visit the official Nmap documentation and the Nmap Reference Guide.