2. TCP SYN Stealth & Full Connect Port Scanning
TCP SYN Scan (-sS): The Stealth Approach
The TCP SYN scan is the default and most popular scan in Nmap. Often called half-open scanning, it avoids completing the TCP three-way handshake, making it harder for simple logging systems to detect connection attempts. The scanner sends a SYN packet to a port. If a SYN/ACK is received, the port is open; if an RST is received, it is closed; if no response is received, it is likely filtered by a firewall.
To perform this manually in Python, one would use raw sockets (requiring root privileges). The process involves building a custom TCP header with the SYN flag set and injecting it into the network stack.
Real-world Nmap syntax: nmap -sS -p 80 192.168.1.1
import socket import struct def create_syn_packet(target_ip, port): # Simplified mock structure for a TCP SYN packet src_port = 12345 seq = 0 ack = 0 offset_res = 80 flags = 0x02 # SYN flag window = 1024 checksum = 0 urg_ptr = 0 return struct.pack('!HHIIBBHHH', src_port, port, seq, ack, offset_res, flags, window, checksum, urg_ptr) print(f'SYN packet structure prepared for port: 80')
TCP Connect Scan (-sT): The Standard Approach
The TCP Connect scan uses the OS system call 'connect()' to establish a full three-way handshake with the target port. This is the default scan type for non-privileged users who cannot construct raw packets.
Because it completes the handshake (SYN, SYN/ACK, ACK), it is easily logged by target servers. This provides high reliability but low stealth, making it useful in environments where root privileges are unavailable or when aiming for high accuracy in identifying open ports without triggering packet filtering nuances.
Real-world Nmap syntax: nmap -sT -p 80 192.168.1.1
# Using standard nc (netcat) to perform a full connect scan nc -zv 192.168.1.1 80
Port States and Selection Logic
Nmap identifies three primary states: Open (application accepting connections), Closed (port reachable but no application listening), and Filtered (a firewall or ACL blocks access). Understanding these states is vital for analyzing network topology.
| Flag/State | Description |
|---|---|
| -p 80,443 | Scans specific common ports. |
| -p- | Scans all 65535 ports. |
| Filtered | No response or ICMP unreachable error received. |
def check_port_range(start, end): ports = list(range(start, end + 1)) print(f'Scanner configured to sweep {len(ports)} ports.') check_port_range(1, 1024)
Knowledge Check
Ready to test your understanding of 2. TCP SYN Stealth & Full Connect Port Scanning?