Server Support and Monitoring Services 2

Comprehensive Guide to Nmap: Commands, Use Cases, and FAQs

Table of Contents

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

  1. Download the Installer: Visit the official Nmap download page and download the Windows installer (nmap-<version>-setup.exe).
  2. 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).
  3. Complete Installation: Click “Install” and wait for the process to finish.
  4. Verification:
    • Open Command Prompt.
    • Type nmap --version and press Enter.
    • You should see the installed Nmap version information.

Mac Installation

  1. 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
  2. Alternative Method:
    • Download the Mac binary from the official website.
    • Extract the package and follow the installation instructions.
  3. 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

  1. Ping Scan (Discover Live Hosts):nmap -sn 192.168.1.0/24
    • Description: Sends an ICMP echo request to determine which hosts are up.
  2. List Scan (Displays Targets):nmap -sL 192.168.1.0/24
    • Description: Lists each host in the specified range without sending packets.
  3. TCP Connect Scan:nmap -sT 192.168.1.10
    • Description: Performs a full TCP connection scan.
  4. SYN Scan (Stealth Scan):nmap -sS 192.168.1.10
    • Description: Sends SYN packets and waits for responses without completing the handshake.
  5. Version Detection:nmap -sV 192.168.1.10
    • Description: Detects service versions on open ports.
  6. OS Detection:nmap -O 192.168.1.10
    • Description: Attempts to determine the operating system of the target.
  7. Scanning Specific Ports:nmap -p 22,80,443 192.168.1.10
    • Description: Scans specified ports.
  8. Scanning All Ports:nmap -p- 192.168.1.10
    • Description: Scans all 65535 TCP ports.
  9. Increasing Verbosity:nmap -v 192.168.1.10
    • Description: Provides detailed output.
  10. 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

  1. Adjusting Timing Templates:nmap -T4 192.168.1.10
    • Description: Sets the timing template to speed up scans (-T0 to -T5).
  2. Fragmentation of Packets:nmap -f 192.168.1.10
    • Description: Sends fragmented packets to evade firewalls.
  3. Spoofing IP Address:nmap -S 192.168.1.100 192.168.1.10
    • Description: Spoofs the source IP address.
  4. Decoy Scanning:nmap -D RND:10 192.168.1.10
    • Description: Uses decoy IP addresses to mask the scan.

Output Options

  1. Save Output to a File:nmap -oN output.txt 192.168.1.10
    • Description: Saves the output in normal format.
  2. Save Output in XML Format:nmap -oX output.xml 192.168.1.10
    • Description: Saves the output in XML format.
  3. Save All Formats:nmap -oA output 192.168.1.10
    • Description: Saves in normal, XML, and grepable formats.

Scanning Techniques

  1. UDP Scan:nmap -sU 192.168.1.10
    • Description: Scans for open UDP ports.
  2. ACK Scan:nmap -sA 192.168.1.10
    • Description: Used to map firewall rulesets.
  3. Fin Scan:nmap -sF 192.168.1.10
    • Description: Sends FIN packets to evade basic firewalls.
  4. Idle Scan:nmap -sI zombie_host 192.168.1.10
    • Description: Performs a stealth scan through a “zombie” host.

Service and Version Detection

  1. Aggressive Version Detection:nmap -sV --version-intensity 5 192.168.1.10
    • Description: Increases the intensity of version detection.
  2. Using Specific Scripts:nmap --script http-enum 192.168.1.10
    • Description: Runs the http-enum script to enumerate web server directories.
  3. 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

  1. Scan for Vulnerabilities:nmap --script vuln 192.168.1.10
    • Description: Runs all scripts in the vuln category.
  2. Run Multiple Scripts:nmap --script http-title,ssh-hostkey 192.168.1.10
    • Description: Runs specified scripts.
  3. 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.

#CommandDescriptionWindowsMacLinux
1nmap -sS targetTCP SYN scan (stealth scan)
2nmap -sT targetTCP connect scan
3nmap -sU targetUDP scan
4nmap -sA targetTCP ACK scan
5nmap -sW targetTCP Window scan
6nmap -sM targetTCP Maimon scan
7nmap -sV targetService version detection
8nmap -O targetOS detection
9nmap -A targetAggressive scan options
10nmap -v targetVerbose output
11nmap -Pn targetDisable host discovery (skip ping)
12nmap -p 80 targetScan specific port
13nmap -p 1-100 targetScan range of ports
14nmap -p- targetScan all ports
15nmap -sC targetScan with default scripts
16nmap --script=default,targetRun specific scripts
17nmap -D decoy1,decoy2 targetDecoy scan to mask the source IP
18nmap -f targetFragment packets
19nmap -T0 targetSlowest scan (Paranoid)
20nmap -T5 targetFastest scan (Insane)
21nmap -oN output.txt targetSave output to a file (normal format)
22nmap -oX output.xml targetSave output in XML format
23nmap -oG output.grep targetSave output in grepable format
24nmap --open targetShow only open ports
25nmap --max-retries 2 targetSet maximum number of port scan probe retries
26nmap --host-timeout 60m targetSet maximum time for host scan
27nmap --script-updatedbUpdate script database
28nmap -6 targetScan IPv6 addresses
29nmap -sI zombie_host targetIdle scan using zombie host
30nmap -sY targetSCTP INIT scan
31nmap -sZ targetSCTP COOKIE-ECHO scan
32nmap --script http-enum targetEnumerate directories on web server
33nmap --script smb-os-discovery targetDiscover OS over SMB
34nmap --script ftp-anon targetCheck for anonymous FTP login
35nmap --script ssl-heartbleed targetCheck for Heartbleed vulnerability
36nmap --traceroute targetPerform traceroute to target
37nmap -iL list.txtScan targets from a file
38nmap --exclude target1,target2Exclude hosts from scan
39nmap -S spoofed_ip targetSpoof source IP address
40nmap --script "vuln and safe" targetRun 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.

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

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.

Need Help with IT?

At LayerLogix, we pride ourselves on offering pain-free IT Support and Services. From Networking to Cyber Security, we have solutions to support your business. 

Let us manage and maintain your IT, so you can focus on your core business. For a consultation, call us today at (713) 571-2390.