Nmap

Overview

Nmap (Network Mapper) is a powerful, open-source network scanning tool used for network discovery, security auditing, and vulnerability detection. It's one of the most essential tools in a penetration tester or network administrator's toolkit. Nmap works by sending specially crafted packets to target hosts and analyzing the responses to determine hosts' availability, open ports, services running, versions, and operating systems.

Nmap supports powerful features such as scriptable interactions with the target (via NSE — Nmap Scripting Engine), stealth scanning, version detection, and more. It is widely used in both offensive (reconnaissance, enumeration) and defensive (network auditing, exposure assessment) contexts.

Features

Website

Installation

On Debian/Ubuntu:

bash
sudo apt update
sudo apt install nmap

On macOS (via Homebrew):

bash
brew install nmap

On Windows:

  1. Download from the official download page.
  2. Run the installer. It includes both the CLI and the GUI (Zenmap).

Basic Usage

Host Discovery (Ping Sweep)

bash
nmap -sn 192.168.1.0/24

Port Scanning (Default TCP)

bash
nmap 192.168.1.1

Specific Ports

bash
nmap -p 22,80,443 192.168.1.1

Full TCP Port Scan

bash
nmap -p- 192.168.1.1

Service and Version Detection

bash
nmap -sV 192.168.1.1

Operating System Detection

bash
nmap -O 192.168.1.1

Combine Everything

bash
nmap -A 192.168.1.1

Aggressive Scan on a Subnet

bash
nmap -A 192.168.1.0/24

Nmap Scan Types

Scan Type Command Flag Description
TCP SYN Scan -sS Stealth scan, default for root
TCP Connect Scan -sT Full TCP connection
UDP Scan -sU Scans UDP ports
TCP FIN Scan -sF Bypasses some firewalls
NULL Scan -sN No flags set, evasion technique
Xmas Scan -sX FIN, URG, and PSH flags set
SCTP INIT Scan -sY SCTP INIT scan for SCTP ports

Nmap Scripting Engine (NSE)

Running Default Scripts

bash
nmap -sC 192.168.1.1

Running Specific Script

bash
nmap --script http-title 192.168.1.1

Running a Category of Scripts

bash
nmap --script vuln 192.168.1.1

Combine with Service Detection

bash
nmap -sV --script=vuln 192.168.1.1

Output Options

Output Format Flag Description
Normal -oN Human-readable output
XML -oX Useful for parsing
Grepable -oG Output in grep-friendly format
All formats -oA Output in all three major formats

Example:

bash
nmap -oA scan_results -sV 192.168.1.1

Real-World Usage Scenarios

Reconnaissance in Pentesting

Firewall Evasion

bash
nmap -sS -Pn -D decoy1,decoy2,target 192.168.1.1

Discover Vulnerable Services

bash
nmap --script vuln -sV 192.168.1.1

Scan for Specific Protocols

bash
nmap -sU -p 161 192.168.1.0/24

Common NSE Script Categories

Category Description
auth Authentication bypass checks
broadcast Network broadcast discovery
brute Brute force logins
default Default scripts for quick info
discovery Host and service discovery
exploit Known exploits
external External services (e.g., WHOIS)
malware Malware-related detection
vuln Vulnerability detection

Best Practices

Combine with Other Tools

References