3. UDP & Advanced Stealth Scans (NULL, FIN, XMAS)
UDP Port Scanning (-sU)
UDP is a connectionless protocol, meaning there is no handshake. When an Nmap UDP scan (-sU) sends a datagram to a port, it waits for a response. If the port is open, the application might return data, or Nmap receives nothing (which Nmap interprets as open|filtered).
If the port is closed, the target OS should send an ICMP 'Port Unreachable' error (Type 3, Code 3). This is the primary mechanism for detecting closed ports. Below is a command to initiate a UDP scan and a Python script to emulate a socket listener.
| Response | Nmap Interpretation |
|---|---|
| No Response | Open or Filtered |
| ICMP Port Unreachable | Closed |
nmap -sU -p 53,161,123 192.168.1.1
Stealth Scans: NULL, FIN, and XMAS
Stealth scans exploit RFC 793, which states that for any non-SYN packet sent to a closed port, the target must send an RST packet. If the port is open, the target should ignore the packet. NULL (no flags), FIN (FIN flag), and XMAS (FIN, PSH, and URG flags) use these flags to probe ports without establishing a full handshake.
These scans are highly effective against systems that strictly follow RFC 793, but often fail against Windows systems because Microsoft's implementation sends RST packets for every packet, regardless of the port status.
nmap -sN 192.168.1.1 # NULL Scan nmap -sF 192.168.1.1 # FIN Scan nmap -sX 192.168.1.1 # XMAS Scan
TCP ACK Scanning (-sA)
The ACK scan is not used to find open ports but to map firewall rule sets. It determines if a port is filtered by sending an ACK packet. If the port returns an RST, the port is unfiltered. If it sends back an ICMP error or no response, the port is filtered.
Below is a Python demonstration using the Scapy-style raw socket creation pattern to understand header construction.
import socket import struct def create_ack_packet(src_ip, dst_ip, dst_port): # Simplified TCP header construction # Flags: 0x10 is ACK tcp_header = struct.pack('!HHIIBBHHH', 12345, dst_port, 0, 0, 5, 0x10, 5840, 0, 0) print(f'Constructed TCP ACK header for port {dst_port}') return tcp_header create_ack_packet('192.168.1.5', '192.168.1.1', 80)
Knowledge Check
Ready to test your understanding of 3. UDP & Advanced Stealth Scans (NULL, FIN, XMAS)?