A comprehensive guide to Nmap, covering port scanning, scan types, the Nmap Scripting Engine (NSE), and firewall evasion techniques for penetration testing and network reconnaissance.
When it comes to penetration testing, knowledge is power. Proper enumeration is crucial before any exploitation attempt.
Port scanning is the first step to map out a network:
Key points:
Tool of choice: Nmap, the industry standard for port scanning and service discovery.
Nmap is primarily run from the terminal:
nmap [options] [target]
nmap -h
man nmap
Examples of useful switches:
-p [ports]
β Specify ports-sV
β Detect service versions-O
β OS detection-A
β Aggressive scan (OS + version + script scan + traceroute)Nmap supports multiple scan types:
-sT
)-sS
)-sU
)Less common scans:
-sN
)-sF
)-sX
)Performs a full TCP three-way handshake with each port:
Example:
sudo nmap -sT [target]
Pros:
Cons:
Also known as Half-open / Stealth scans:
Example:
sudo nmap -sS [target]
Advantages:
open|filtered
Example:
sudo nmap -sU --top-ports 20 [target]
β Note: UDP scans are very slow (~20 mins for 1000 ports).
-sN
) β Sends packet with no flags-sF
) β Sends packet with FIN flag only-sX
) β Sends packet with PSH, URG, FIN flagsBehavior:
open|filtered
)Examples:
sudo nmap -sN [target]
sudo nmap -sF [target]
sudo nmap -sX [target]
Use to discover active hosts on a network:
nmap -sn 192.168.0.1-254
nmap -sn 192.168.0.0/24
-sn
β Skip port scanning, only host discoveryThe NSE uses Lua scripts to extend Nmapβs functionality.
Categories:
safe
β Non-intrusiveintrusive
β May affect targetvuln
β Vulnerability scanningexploit
β Exploit vulnerabilitiesauth
β Authentication bypassbrute
β Brute-force credentialsdiscovery
β Additional information gatheringRun scripts by category:
nmap --script=vuln [target]
Run multiple scripts:
nmap --script=smb-enum-users,smb-enum-shares [target]
Use script arguments:
nmap -p 80 --script http-put --script-args http-put.url='/dav/shell.php',http-put.file='./shell.php'
Check script help:
nmap --script-help [script-name]
Scripts are stored in:
/usr/share/nmap/scripts
Search locally:
grep "ftp" /usr/share/nmap/scripts/script.db
ls -l /usr/share/nmap/scripts/*ftp*
Install new scripts:
sudo wget -O /usr/share/nmap/scripts/[script-name].nse https://svn.nmap.org/nmap/scripts/[script-name].nse
sudo nmap --script-updatedb
Bypass ICMP-blocking firewalls:
nmap -Pn [target]
Other useful switches:
-f
β Fragment packets--mtu [size]
β Custom packet size (multiple of 8)--scan-delay [ms]
β Delay between packets (evade IDS/timing triggers)--badsum
β Send packets with invalid checksums (test firewalls)Nmap is a powerful reconnaissance tool when used properly, forming the foundation for any penetration test or security audit.
###Learn More Nmap Live Host Discovery