As a red teamer or penetration tester, one of the most important skills you can possess is scanning and enumerating a target network. It’s the digital equivalent of casing a bank: you need to see the cameras, the guards, and the open windows before you make your move. Reconnaissance is not a box to check off before the “real” hacking begins - it is the foundation of every successful operation.
In this deep dive, we will move beyond the basic nmap -sC -sV and explore the tactical application of network scanning in a professional engagement. We’ll cover TCP/IP fundamentals that underpin scan behavior, advanced NSE usage, firewall evasion, modern Rust-based tooling, and detailed service enumeration strategies for common protocols.
1. TCP/IP Fundamentals: Why Scans Work
Before we can understand how scanning tools detect services, we need to understand the underlying protocols.
The TCP Three-Way Handshake
TCP (Transmission Control Protocol) establishes a reliable, ordered connection between two hosts using a three-way handshake:
- SYN: The client sends a SYN (synchronize) packet to the server, requesting a connection.
- SYN-ACK: If the port is open and listening, the server responds with SYN-ACK (synchronize-acknowledge).
- ACK: The client sends an ACK (acknowledge), and the connection is established.
If the port is closed, the server responds with an RST (reset) packet instead of SYN-ACK. If the port is filtered by a firewall, the packet is simply dropped, and the client receives nothing (or an ICMP unreachable message).
UDP: No Handshake
UDP (User Datagram Protocol) is connectionless. There is no handshake. You send a packet, and you hope it arrives. If a UDP port is open, the service may or may not respond. If it’s closed, the host typically sends an ICMP “Port Unreachable” message. If it’s filtered, you get nothing.
This makes UDP scanning inherently slower and less reliable than TCP scanning.
ICMP: The Control Layer
ICMP (Internet Control Message Protocol) is used for diagnostic and error messages. Key ICMP types include:
- Type 0 (Echo Reply): Response to a ping.
- Type 3 (Destination Unreachable): Indicates a closed port or network issue.
- Type 8 (Echo Request): A ping.
- Type 11 (Time Exceeded): Used by
traceroute.
Many scans use ICMP for host discovery before port scanning.
2. Nmap: The Heart of Reconnaissance
Nmap (Network Mapper) is the undisputed king of network scanning tools, maintained by Gordon Lyon (Fyodor) since 1997. While most people know the basic flags, a red teamer uses its advanced features to extract maximum intelligence while managing their forensic footprint.
Host Discovery (Ping Scans)
Before scanning ports, Nmap determines which hosts are alive. By default, it uses a combination of techniques:
| |
[!TIP] On internal networks where ICMP is often blocked, use
-PSwith ports likely to be open (22, 80, 445) for more reliable host discovery.
TCP Scan Types
SYN Scan (-sS):
The default and most common scan. Also called “half-open” or “stealth” scan.
- Nmap sends a SYN packet.
- If the port is open, the target responds with SYN-ACK.
- Nmap sends RST to tear down the connection before it completes.
Because the TCP handshake is never finished, this scan is less likely to be logged by applications (which only see completed connections). However, modern firewalls and IDS/IPS systems do log SYN scans.
| |
TCP Connect Scan (-sT):
Completes the full three-way handshake. Used when you don’t have root/admin privileges (SYN scan requires raw sockets).
| |
ACK Scan (-sA):
Sends ACK packets instead of SYN. Doesn’t determine if a port is open or closed - it determines if a port is filtered by a stateful firewall. Firewalls that track connection state will drop unsolicited ACK packets.
| |
FIN, Xmas, and Null Scans (-sF, -sX, -sN):
These scans send packets with unusual flag combinations (FIN only, FIN-PSH-URG, or no flags). According to RFC 793, a closed port should respond with RST, while an open port should ignore the packet. In practice, these scans are noisy on modern systems and often unreliable, but they can sometimes bypass older stateless firewalls.
Idle Scan (-sI):
The ultimate stealth scan. It uses a “zombie” host on the network (a host with a predictable IP ID sequence, like an old printer) to scan the target on your behalf. Your IP address never sends a packet directly to the target port.
| |
This is complex but extremely valuable for stealthy reconnaissance in high-security environments.
UDP Scan (-sU)
UDP scanning is critical because many important services run on UDP: DNS (53), SNMP (161), NTP (123), TFTP (69), DHCP (67/68), and more. However, UDP scanning is painfully slow because:
- Open ports often don’t respond, so Nmap must wait for a timeout.
- Many systems rate-limit ICMP “Port Unreachable” messages.
| |
[!TIP] Never scan all 65,535 UDP ports unless you have hours to spare. Target the top 100 or use a custom list of security-relevant UDP ports.
Version Detection (-sV)
Once ports are identified, Nmap can probe them to determine the running service and version.
| |
Operating System Detection (-O)
Nmap analyzes TCP/IP stack behavior (TTL values, window sizes, TCP options) to fingerprint the target’s operating system.
| |
Combining Flags: The Classics
| |
3. Weaponizing the Nmap Scripting Engine (NSE)
The NSE is what transforms Nmap from a port scanner into a vulnerability scanner and enumeration platform. Scripts are written in Lua and located in /usr/share/nmap/scripts/ on most Linux systems.
Script Categories
Nmap scripts are organized into categories:
auth: Authentication-related scripts.broadcast: Discover hosts via broadcast.brute: Brute-force credential attacks.default: Safe scripts run with-sC.discovery: Service discovery and enumeration.dos: Denial of service (use with extreme caution).exploit: Attempt to exploit vulnerabilities.external: Scripts that query external services.fuzzer: Fuzzing scripts.intrusive: Scripts likely to crash services or be detected.malware: Detect malware infections.safe: Scripts unlikely to crash services.version: Version detection helpers.vuln: Vulnerability detection.
Running Scripts
| |
Targeted Enumeration Examples
SMB Enumeration (Port 445):
| |
HTTP Enumeration (Ports 80, 443):
| |
SSH (Port 22):
| |
LDAP (Port 389):
| |
Script Arguments
Many scripts accept arguments to customize their behavior:
| |
Updating the Script Database
| |
Writing Your Own NSE Scripts
Sometimes you need to check for a specific indicator (like a specific favicon hash, banner, or HTTP response).
| |
4. Stealth and Evasion: Staying Below the Threshold
Modern EDRs, IDSs, and SIEM solutions are very good at spotting Nmap’s default patterns. As a red teamer, you must learn to fly under the radar.
Timing Templates (-T)
Nmap’s timing templates control the speed and aggressiveness of scans.
| Template | Name | Description |
|---|---|---|
-T0 | Paranoid | Sends one packet every 5 minutes. For extremely high-security targets. |
-T1 | Sneaky | One packet every 15 seconds. Still very slow but more practical. |
-T2 | Polite | One packet every 0.4 seconds. Avoids overloading fragile systems. |
-T3 | Normal | Default timing. Balances speed and stealth. |
-T4 | Aggressive | Faster, assumes a fast and reliable network. |
-T5 | Insane | Maximum speed, high packet loss expected. For CTF, not real engagements. |
For real engagements, consider -T2 or custom timing controls:
| |
Packet Manipulation
Decoys (-D):
Spoof your scan from multiple IP addresses. The target sees traffic from 10 IPs; one is real, nine are fake. This makes attribution difficult.
| |
[!WARNING] Decoys must appear to be valid hosts on the network, or they will be obviously fake. Do not use decoys across the internet - they are typically only effective on LANs.
Source Port Spoofing (-g or --source-port):
Many poorly configured firewalls allow traffic from “trusted” source ports like DNS (53) or HTTP (80).
| |
IP Fragmentation (-f):
Split TCP headers into tiny 8-byte fragments. This can bypass simple packet filters that look for specific flag combinations but don’t reassemble fragments.
| |
MTU Control (--mtu):
Specify a custom Maximum Transmission Unit for fragmentation.
| |
Randomize Host Order (--randomize-hosts):
When scanning a subnet, randomize the order to avoid sequential detection.
| |
Spoof MAC Address (--spoof-mac):
Useful on LANs to impersonate a different device type.
| |
5. Output Formats and Reporting
Nmap supports multiple output formats for different use cases.
| |
Parsing Nmap Output
The XML output can be parsed with tools like xsltproc or Python’s python-nmap library.
| |
6. Modern High-Speed Alternatives
While Nmap is accurate and feature-rich, its single-threaded-per-host architecture makes it slow for large networks. Modern alternatives sacrifice some accuracy for speed.
RustScan: The Modern Speed Demon
Written in Rust, RustScan finds open ports in seconds by:
- Adjusting the system’s
ulimitto allow thousands of concurrent connections. - Using asynchronous I/O for massively parallel port scanning.
- Piping the discovered open ports to Nmap for deep analysis.
| |
RustScan can complete a full 65k port scan of a single host in under 10 seconds on a fast network.
Masscan: Internet-Scale Scanning
Masscan uses its own custom TCP/IP stack (bypassing the OS kernel) to transmit millions of packets per second. It is entirely stateless - it does not track connections.
| |
[!WARNING] Masscan at high rates will overwhelm consumer-grade routers and may violate network usage policies. Use on VPSes with dedicated network interfaces or in lab environments, though you may violate the terms of service of your VPS provider.
Naabu: Another Fast Scanner
From ProjectDiscovery, Naabu is a Go-based port scanner designed for speed.
| |
7. Detailed Service Enumeration Strategies
Once ports are identified as open, the real work begins. Each service type requires a specific enumeration approach.
SMB (Ports 139, 445)
SMB is a goldmine for internal networks - shares, usernames, password policies, and often critical vulnerabilities.
| |
SNMP (Port 161 UDP)
SNMP often leaks process lists, installed software, network interfaces (dual-homed hosts!), and even credentials (SNMPv1/v2 use community strings in cleartext).
| |
NFS (Port 2049)
Network File System exports often have weak permissions. Look for no_root_squash which allows root access.
| |
LDAP (Port 389)
LDAP servers (Active Directory, OpenLDAP) can leak the entire directory structure.
| |
DNS (Port 53)
DNS misconfigurations can reveal internal hostnames and network structure.
| |
SMTP (Port 25)
SMTP can be used to enumerate valid usernames via VRFY and EXPN commands.
| |
8. Organizing Your Reconnaissance
On a real engagement, you will scan many hosts and services. Organization is critical.
Directory Structure
| |
Tools for Aggregation
- Zenmap: Nmap’s GUI; can aggregate and compare scans.
- Nessus/OpenVAS: Vulnerability scanners with built-in scanning.
- Faraday: Collaborative penetration testing IDE.
- Reconftw: Automated reconnaissance framework.
Conclusion
Network scanning is not a “fire and forget” task. It is a methodical process of uncovering the target’s attack surface while managing your own forensic footprint. By understanding TCP/IP fundamentals, mastering Nmap’s advanced features and NSE scripting, implementing stealth timing and packet manipulation, and leveraging high-speed tools like RustScan and Masscan, you transform from a script kiddie into a surgical operator.
The network is the terrain. Know it better than the defenders do. Every open port is a potential door. Every service version is a potential CVE. Every misconfiguration is a potential foothold.
Happy hunting!