Advanced Scanning Options for Network Assessment
Now that we understand the basics of Nmap and how to scan multiple targets, let's explore some advanced scanning options that are particularly useful for cybersecurity assessments.
Port Selection and Scan Types
By default, Nmap scans the most common 1000 TCP ports. However, you can customize which ports to scan.
Scanning Specific Ports
To scan specific ports:
nmap -p 22,80,443 localhost
This command scans only ports 22, 80, and 443.
To scan a range of ports:
nmap -p 20-25 localhost
This scans ports 20 through 25.
To scan all 65535 TCP ports:
nmap -p- localhost
Note that scanning all ports takes significantly longer.
Using Different Scan Types
Nmap supports various scan techniques. The default is a TCP SYN scan (-sS
), but this requires root privileges. Without root privileges, Nmap uses a TCP connect scan (-sT
).
Let's try a TCP connect scan explicitly:
nmap -sT localhost
For a more stealthy scan (requires root privileges):
sudo nmap -sS localhost
To scan UDP ports (which is often overlooked but important for security):
sudo nmap -sU -p 53,161,162 localhost
This scans UDP ports 53 (DNS), 161 and 162 (SNMP).
OS Detection and Version Scanning
For more comprehensive information, let's combine OS detection and service version scanning:
sudo nmap -sS -O -sV localhost
The -O
option attempts to identify the operating system of the target. This provides valuable information for security assessments, as different operating systems may have different vulnerabilities.
Let's break down what each option does:
-sS
: Performs a SYN scan (requires root)
-O
: Attempts to identify the target's operating system
-sV
: Probes open ports to determine service/version info
The output will include detailed information about the operating system and service versions running on the target.
Using Nmap Scripts
Nmap includes a powerful Nmap Scripting Engine (NSE) that can perform a wide range of tasks, from advanced service detection to vulnerability scanning.
Let's run a basic script that checks for commonly exposed services:
nmap --script=default localhost
For a more security-focused scan:
nmap --script=vuln localhost
This runs vulnerability detection scripts against the target, which can identify common security issues.
When scanning multiple targets, optimizing scan performance becomes crucial. We've already seen the -T
option, but there are more granular controls available.
nmap -T4 --max-rtt-timeout 200ms --min-rate 1000 127.0.0.1/24
This command:
- Uses the "aggressive" timing template (
-T4
)
- Sets the maximum round-trip timeout to 200ms
- Sets a minimum rate of 1000 packets per second
These settings can significantly speed up scans of multiple hosts, though they may be less reliable on congested or high-latency networks.
Combining Everything for a Comprehensive Scan
Let's combine multiple techniques for a comprehensive scan of our local network:
sudo nmap -sS -sV -O -p 1-1000 --script=default -T4 -oA comprehensive_scan 127.0.0.1
This command performs:
- A SYN scan
- Service version detection
- OS detection
- Scans ports 1-1000
- Runs default scripts
- Uses aggressive timing
- Saves results in all formats with the prefix "comprehensive_scan"
Let's examine the results:
cat comprehensive_scan.nmap
This comprehensive scan provides a wealth of information about the target system, including open ports, running services, service versions, and potential vulnerabilities.
Ethical Considerations and Best Practices
Before concluding, it's important to emphasize that Nmap should only be used on networks where you have explicit permission to scan. Unauthorized scanning can be illegal and may be perceived as a hostile action.
Some best practices to follow:
- Always obtain permission before scanning any network or system
- Use less aggressive scans when possible to minimize network impact
- Be aware of the potential for false positives and false negatives
- Document your scanning activities for reference and accountability
- Respect privacy and confidentiality of the information discovered