30 Mins

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

Security Operator Note: SYN scans are stealthier than Connect scans because the application layer never creates a full session, avoiding most application-level logs.
Python 3
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')
Probing Mechanics • TCP SYN Stealth Scan (-sS) vs. Full Connect Scan (-sT)
SYN Stealth Scan (-sS) - Half Open
1. Attacker → Target: [SYN]
2. Target → Attacker: [SYN-ACK] (Port Open)
3. Attacker → Target: [RST] (Tears connection down before logging)
Full Connect Scan (-sT) - OS Socket
1. Attacker → Target: [SYN]
2. Target → Attacker: [SYN-ACK]
3. Attacker → Target: [ACK] (Full Handshake Established)
4. Attacker → Target: [RST/FIN] (Logged in App Access Log)

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

Security Operator Note: If you lack sudo/root permissions, your Nmap scan will automatically downgrade to an -sT connect scan.
Text
# 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/StateDescription
-p 80,443Scans specific common ports.
-p-Scans all 65535 ports.
FilteredNo response or ICMP unreachable error received.
Security Operator Note: Always specify port ranges to reduce scan time. Scanning all 65535 ports (-p-) can take significant time depending on network latency.
Python 3
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?

Back