About nmap
Nmap (Network Mapper) is a free and open-source utility for network discovery and security auditing. Originally written by Gordon Lyon (Fyodor), nmap has become the de facto standard for network reconnaissance and is used by system administrators, security professionals, and network engineers worldwide.
Primary uses: Network inventory, managing service upgrade schedules, monitoring host or service uptime, discovering vulnerabilities, mapping network topology, identifying open ports, detecting operating systems and applications, and performing security assessments. Nmap uses raw IP packets to determine available hosts, services, operating systems, packet filters/firewalls, and numerous other characteristics.
Key capabilities: Host discovery, port scanning, version detection, OS detection, scriptable interaction with the target (NSE - Nmap Scripting Engine), and flexible output formats for integration with other tools.
Detailed Examples
1Basic Host Discovery (Ping Scan)
Determine which hosts are up on a network without performing a port scan.
-sn flag performs a "ping scan" (previously called -sP). This discovers which hosts are online without scanning ports. By default, nmap sends an ICMP echo request, a TCP SYN to port 443, a TCP ACK to port 80, and an ICMP timestamp request. When run with root privileges (sudo), nmap also uses ARP requests on local networks, which is faster and more reliable. The output shows four hosts are up out of 256 addresses scanned. For three hosts, MAC addresses are shown (meaning they're on the same local network), while 192.168.1.100 shows no MAC (likely the scanning host itself). Latency times indicate network responsiveness.
Scan Type: Host Discovery
Techniques used: ICMP echo request, TCP SYN (443), TCP ACK (80), ICMP timestamp, ARP request (local networks) Requires root: For full functionality, yes Typical use case: Initial network reconnaissance, inventory management2Basic Port Scan - Common Ports
Scan the most commonly used ports on a single target.
3SYN Stealth Scan with Specific Ports
Perform a stealthy half-open scan on specific ports.
-sS flag performs a TCP SYN scan (also called "half-open" scan), which is the default when running as root. This sends SYN packets and analyzes responses without completing the TCP handshake, making it stealthier than connect scans. The -p flag specifies exactly which ports to scan (FTP, SSH, Telnet, SMTP, HTTP, HTTPS, and RDP). The results show three different port states: open (22, 3389) - accepting connections; closed (21, 25, 80, 443) - no service listening but port accessible; filtered (23) - firewall or packet filter blocking access, so nmap can't determine if it's open or closed. Port 3389 being open suggests this is a Windows machine with Remote Desktop enabled. The VMware MAC address indicates this is a virtual machine.
Scan Type: TCP SYN (Stealth Scan)
How it works: Sends SYN, receives SYN/ACK (open) or RST (closed), sends RST to abort Requires root: Yes (raw socket access) Detection difficulty: Medium (logged by firewalls, not usually by applications)4Service Version Detection
Identify specific versions of services running on open ports.
-sV flag enables version detection, which probes open ports to determine the exact service and version. This is significantly more intensive than a basic port scan. Nmap connects to each open port and sends various probes to elicit responses that reveal version information. The results show detailed service information: OpenSSH version 8.9p1 on Ubuntu, Apache 2.4.52 web server, and MySQL 8.0.35. This level of detail is crucial for security auditing because specific versions may have known vulnerabilities. The "Service Info" line provides OS detection based on service fingerprints. Note the scan took 6.78 seconds compared to 0.18 seconds for the basic scan - version detection is much slower because it must interact with each service.
5Operating System Detection
Identify the operating system and version of the target host.
-O flag enables OS detection using TCP/IP stack fingerprinting. Nmap sends a series of TCP and UDP packets to the target and examines the responses. Different operating systems implement TCP/IP stacks slightly differently (in terms of window sizes, options, TTL values, etc.), creating unique "fingerprints." The results indicate this is likely Microsoft Windows 10 or Windows Server 2016, with CPE (Common Platform Enumeration) identifiers for precise system identification. The "Network Distance: 1 hop" indicates the target is on the same local network. OS detection requires at least one open and one closed port to be effective. The confidence level isn't 100% because multiple OS versions can have similar fingerprints.
Scan Type: OS Fingerprinting
Technique: Analyzes TCP/IP stack behavior, sequence prediction, ICMP responses Requirements: Root access, at least one open and one closed port Accuracy: Generally 85-95% with good conditions6Aggressive Scan (Combined Techniques)
Comprehensive scan with OS detection, version detection, script scanning, and traceroute.
-A flag enables aggressive scanning, which combines OS detection (-O), version detection (-sV), script scanning (--script=default), and traceroute (--traceroute). The -T4 flag sets the timing template to "aggressive" for faster scanning. The results are comprehensive: NSE (Nmap Scripting Engine) scripts gathered SSH host keys, HTTP server headers and titles, SSL certificate information, and MySQL server capabilities. The scripts ran automatically and provided valuable reconnaissance data. The SSL certificate shows the server's validity period (2024-2026) and confirms the hostname. The traceroute shows only one hop, confirming local network placement. This scan took 18.93 seconds because it's running multiple detection methods and numerous scripts.
7UDP Port Scan
Scan for open UDP ports, which are often overlooked but critical for security.
-sU flag performs UDP scanning, which is fundamentally different from TCP scanning. UDP is a connectionless protocol, so there's no handshake to complete or refuse. Nmap sends UDP packets and waits for responses. An open port means the service responded; closed means an ICMP port unreachable message was received; open|filtered means no response was received, which could mean the port is open but the service doesn't respond to empty probes, or a firewall is dropping packets. This router shows typical UDP services: DNS (53), DHCP server (67), DHCP client (68), NTP time sync (123), and SNMP network management (161). UDP scanning is notoriously slow because of rate limiting on ICMP unreachable messages and the need to wait for timeouts.
8Scan All TCP Ports
Comprehensive scan of all 65,535 TCP ports to find services on non-standard ports.
-p- flag (equivalent to -p 1-65535) scans all 65,535 possible TCP ports instead of just the default 1,000. The -v flag provides verbose output, showing discovered ports in real-time as the scan progresses. The -T4 timing template speeds up the scan. This comprehensive scan discovered services running on non-standard ports that a default scan would miss: port 8080 (alternate HTTP), 8443 (alternate HTTPS), 9000 (often used for development services), and 33060 (MySQL X Protocol). The scan took 42.35 seconds to check all 65,535 ports. Without -T4, this could take several minutes. In security assessments, administrators sometimes move services to non-standard ports for "security through obscurity," but a full port scan reveals them.
9NSE Script Scan for Vulnerabilities
Use Nmap Scripting Engine to check for common vulnerabilities.
--script=vuln option runs all NSE scripts in the "vuln" category, which check for known vulnerabilities. NSE (Nmap Scripting Engine) is a powerful Lua-based scripting framework that extends nmap's capabilities. In this scan, scripts tested for XSS vulnerabilities, CSRF, enumerated web directories (finding /admin/ and /backup/ directories), and discovered an SSL POODLE vulnerability (CVE-2014-3566) on port 443. The POODLE finding is significant - it's a real vulnerability in SSL 3.0 that could allow attackers to decrypt secure connections. The script provides the CVE identifier, description, disclosure date, and references for further research. The /backup/ directory with directory listing is also a security concern. This scan took 47.82 seconds because vulnerability scripts are thorough and make multiple requests.
10Saving Scan Results in Multiple Formats
Export scan results in various formats for documentation and integration with other tools.
-oA flag outputs results in all three major formats simultaneously with the specified basename "network_scan_20251128". This creates three files: .nmap (normal human-readable format, same as -oN), .gnmap (grepable format for parsing with command-line tools, same as -oG), and .xml (XML format for importing into other tools like Metasploit, Nessus, or custom parsers, same as -oX). The XML format is particularly valuable for integrating nmap results into vulnerability management systems, automated reporting tools, or security information and event management (SIEM) platforms. The scan took over 8 minutes (485 seconds) because it performed an aggressive scan (-A) on an entire /24 network (256 addresses). Using date-stamped filenames (20251128) is a best practice for tracking scan history and comparing results over time.
Output Format Best Practices
- -oN (normal): Human-readable, good for reports and review
- -oG (grepable): Easy parsing with grep, awk, sed
- -oX (XML): Tool integration, programmatic analysis
- -oA (all): Creates all three, safest choice
- Append mode: Use --append-output to add to existing files
- Resume: Use --resume filename to continue interrupted scans
Scan Techniques Reference
| Scan Type | Flag | Description | Requires Root |
|---|---|---|---|
| TCP SYN Scan | -sS |
Half-open scan, sends SYN, doesn't complete handshake (default with root) | Yes |
| TCP Connect Scan | -sT |
Full TCP connection, uses system connect() call (default without root) | No |
| UDP Scan | -sU |
Scans UDP ports by sending UDP packets | Yes (recommended) |
| TCP ACK Scan | -sA |
Maps firewall rules, determines filtered vs. unfiltered ports | Yes |
| TCP Window Scan | -sW |
Like ACK but examines TCP window field for open port detection | Yes |
| TCP Maimon Scan | -sM |
Sends FIN/ACK probe, exploits BSD-derived TCP implementation detail | Yes |
| TCP NULL Scan | -sN |
Sends packet with no flags set | Yes |
| TCP FIN Scan | -sF |
Sends packet with only FIN flag set | Yes |
| TCP Xmas Scan | -sX |
Sends packet with FIN, PSH, and URG flags (lights up like a Christmas tree) | Yes |
| Ping Scan | -sn |
Host discovery only, no port scanning | No (limited), Yes (full) |
| IP Protocol Scan | -sO |
Determines which IP protocols are supported (TCP, UDP, ICMP, etc.) | Yes |
| FTP Bounce Scan | -b |
Uses FTP server to scan other hosts (rarely works now) | No |
Timing and Performance Options
| Template | Name | Description | Use Case |
|---|---|---|---|
-T0 |
Paranoid | Extremely slow, one port at a time, 5-minute delays | IDS evasion (historical, rarely effective now) |
-T1 |
Sneaky | Very slow, 15-second delays between probes | IDS evasion attempts |
-T2 |
Polite | Slows down to use less bandwidth and target resources | Avoiding network congestion, respecting target |
-T3 |
Normal | Default timing, balances speed and accuracy | General purpose scanning (default) |
-T4 |
Aggressive | Speeds up scans on fast/reliable networks | Fast local network scans, modern networks |
-T5 |
Insane | Extremely fast, may sacrifice accuracy | Very fast networks, when speed matters more than accuracy |
Port Specification Options
| Option | Example | Description |
|---|---|---|
-p <port> |
-p 22 |
Scan a single port |
-p <port1,port2> |
-p 22,80,443 |
Scan specific ports (comma-separated) |
-p <start-end> |
-p 1-1024 |
Scan a range of ports |
-p- |
-p- |
Scan all 65,535 ports (1-65535) |
-p U:<ports>,T:<ports> |
-p U:53,T:80,443 |
Specify UDP and TCP ports separately |
--top-ports <n> |
--top-ports 100 |
Scan the N most common ports |
--port-ratio <ratio> |
--port-ratio 0.1 |
Scan ports with ratio or higher in nmap-services |
-F |
-F |
Fast scan (scans only 100 most common ports) |
Host Discovery Options
| Option | Description |
|---|---|
-sn |
Ping scan only (no port scan), previously -sP |
-Pn |
Skip host discovery, treat all hosts as online |
-PS <ports> |
TCP SYN discovery to given ports (default: 80) |
-PA <ports> |
TCP ACK discovery to given ports (default: 80) |
-PU <ports> |
UDP discovery to given ports (default: 40125) |
-PE |
ICMP echo request discovery (traditional ping) |
-PP |
ICMP timestamp request discovery |
-PM |
ICMP address mask request discovery |
-PR |
ARP discovery on local network (default for local) |
-n |
Never do DNS resolution (speeds up scans) |
-R |
Always do DNS resolution (even for offline hosts) |
Nmap Scripting Engine (NSE) Categories
--script=<category> or individual scripts with --script=<scriptname>.
| Category | Description | Risk Level |
|---|---|---|
auth |
Scripts dealing with authentication (bypassing, brute force) | Medium-High |
broadcast |
Discover hosts via broadcast (DHCP, DNS-SD, etc.) | Low |
brute |
Brute force attack scripts (passwords, keys) | High |
default |
Scripts that run with -sC or -A, safe and useful | Low |
discovery |
Active information gathering (SNMP, uptime, etc.) | Low-Medium |
dos |
Scripts that may cause denial of service | Very High |
exploit |
Scripts that actively exploit vulnerabilities | Very High |
external |
Scripts that send data to external resources | Medium |
fuzzer |
Fuzzing scripts that send unexpected input | High |
intrusive |
Scripts that are very noisy or risky | High |
malware |
Scripts that check for malware/backdoors | Low |
safe |
Scripts unlikely to crash services or be intrusive | Very Low |
version |
Scripts used for advanced version detection | Low |
vuln |
Scripts that check for specific vulnerabilities | Medium-High |
Best Practices for Network Scanning
Legal and Ethical Guidelines
- Get explicit written permission: Before scanning any network, obtain clear written authorization from the network owner or authorized representative.
- Define scope clearly: Document exactly which IP addresses, networks, and time windows are approved for scanning.
- Notify relevant parties: Inform IT security teams, NOC staff, and other stakeholders before conducting scans that might trigger alerts.
- Use test networks: Practice and develop skills on your own networks, virtual lab environments, or explicitly authorized test ranges like scanme.nmap.org.
- Document everything: Keep detailed logs of what you scanned, when, and what authorization you had.
- Respect system resources: Don't overwhelm target systems with aggressive scans that could cause performance issues or outages.
Technical Best Practices
- Start with discovery: Begin with host discovery (-sn) to identify live hosts before port scanning.
- Scan progressively: Start with quick scans (top ports), then expand to comprehensive scans based on findings.
- Use appropriate timing: -T4 for local networks, -T3 for internet, -T2 for avoiding detection or reducing load.
- Save all results: Always use -oA to save results in multiple formats for analysis and documentation.
- Scan UDP services: Don't forget UDP (-sU) - many critical services use UDP and are often overlooked.
- Version detection matters: Use -sV to identify exact service versions for vulnerability assessment.
- Leverage NSE: Use appropriate NSE scripts for deeper reconnaissance and vulnerability detection.
- Scan from multiple locations: For security assessments, scan from both internal and external perspectives.
- Regular scanning schedule: Establish regular scanning schedules for continuous security monitoring.
- Compare results over time: Use tools like ndiff to compare scan results and identify changes in network infrastructure.
Security Assessment Workflow
- Phase 1 - Discovery: Identify live hosts (nmap -sn network/24)
- Phase 2 - Quick Scan: Fast port scan (nmap -T4 --top-ports 1000 targets)
- Phase 3 - Comprehensive: Full port scan on interesting hosts (nmap -p- targets)
- Phase 4 - Service Detection: Identify versions (nmap -sV -p <discovered ports> targets)
- Phase 5 - OS Detection: Identify operating systems (nmap -O targets)
- Phase 6 - Vulnerability Scanning: Run vuln scripts (nmap --script=vuln targets)
- Phase 7 - Deep Dive: Run specific NSE scripts based on services found
- Phase 8 - Documentation: Compile results, analyze findings, create remediation plan
Common Use Cases
Quick Network Inventory
Web Server Security Audit
Database Server Check
Firewall Rule Testing
Troubleshooting and Common Issues
Related Tools and Integration
- ndiff: Compare nmap scans to identify changes over time - included with nmap
- Zenmap: Official GUI for nmap with network topology mapping and result comparison
- ncat: Netcat replacement with SSL, IPv6, SOCKS, and connection brokering
- nping: Packet generation and response analysis tool, like ping on steroids
- Masscan: Ultra-fast port scanner for scanning the entire internet (aggressive alternative to nmap)
- Nessus/OpenVAS: Vulnerability scanners that can import nmap XML results
- Metasploit: Penetration testing framework with nmap database integration (db_nmap)
- Wireshark: Packet analyzer useful for understanding what nmap is doing on the wire
- SIEM platforms: Many can ingest nmap XML for asset discovery and vulnerability correlation
Additional Resources
- Official Documentation: https://nmap.org/book/ (Nmap Network Scanning by Gordon Lyon)
- NSE Script Documentation: https://nmap.org/nsedoc/
- Nmap Mailing Lists: dev@nmap.org and nmap-hackers for technical discussions
- Legal Test Target: scanme.nmap.org (explicitly authorized for testing)
- Nmap GitHub: https://github.com/nmap/nmap (source code, issue tracking)
- Man Page: man nmap (comprehensive reference always available locally)