Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 31 ยท Scanning and Enumeration Concepts

TCP/IP Essentials for Security

Understand the TCP/IP basics that every scanner and analyzer relies on: layers, the three-way handshake, ports, flags, ICMP and NAT, with a header-parsing example.

Security tools do not work by magic. A port scanner sends specific packets and reads specific replies. A packet analyzer decodes bytes into fields. A firewall decides based on a few header values. If you know what is inside a packet, the output of Nmap or Wireshark stops being mysterious. This lesson covers the parts of TCP/IP you need for the rest of the track: layers, addresses and ports, the TCP handshake and its flags, ICMP, and NAT. A stand-in parses real header bytes in Python.

If you want the general networking background first, see Networking Basics. Here we focus on what matters for scanning and defense.

Layers in one picture

Data is wrapped layer by layer on its way out and unwrapped on its way in.

Four layers, four jobs

Link Ethernet or Wi-Fi frames, MAC addresses.
Internet IP addresses and routing.
Transport TCP or UDP, ports.
Application HTTP, DNS, SSH, SMTP.

Each layer adds its own header around the data from above.

1 / 2

An Ethernet frame carries an IP packet; the IP packet carries a TCP segment or UDP datagram; that carries your application data. A scanner mostly lives at the transport layer, an analyzer shows all of them.

IP addresses and ports

An IPv4 address has 32 bits, written as four numbers like 192.0.2.10. The ranges 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private: they are not routed on the public internet. A port is a 16-bit number (0 to 65535) that selects a service on a host. Port 22 is conventionally SSH, 80 HTTP, 443 HTTPS, 53 DNS. These are conventions, not rules. A service can run on any port, which is why service enumeration exists.

A socket is the combination of an address and a port. A TCP connection is identified by four values: source address, source port, destination address, destination port.

TCP: the handshake and the flags

TCP provides a reliable byte stream. Before data flows, both sides agree on starting sequence numbers with a three-way handshake.

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: SYN (seq=x)
    S->>C: SYN + ACK (seq=y, ack=x+1)
    C->>S: ACK (ack=y+1)
    Note over C,S: Connection established

The handshake: three packets, then data can flow.

Flags are single bits in the TCP header:

Flag Name Role
SYN Synchronize Starts a connection
ACK Acknowledge Confirms received data
FIN Finish Orderly close
RST Reset Abort, or "nothing is listening here"
PSH Push Deliver data now
URG Urgent Rarely used

Connections end either politely (FIN, ACK, FIN, ACK) or abruptly with RST.

How a scanner infers state

When a client sends a SYN to a port, the reply tells the story:

  • SYN+ACK: something is listening, the port is open.
  • RST (usually RST+ACK): the host is up but nothing listens, the port is closed.
  • No reply at all: a firewall probably dropped the packet, or the host is down.

That three-way split is the basis of the next lesson, How Port Scanning Works.

UDP and ICMP

UDP has no handshake and no delivery guarantee. A UDP datagram is just fired off. If nothing listens, the target host may answer with an ICMP message. Many UDP services only respond to a correctly formatted request, so silence is ambiguous.

ICMP carries control and error messages and has no ports. Familiar types:

  • Type 8 / type 0: echo request / echo reply (what ping uses).
  • Type 3: destination unreachable, with codes such as "port unreachable" (3) and "administratively prohibited" (13).
  • Type 11: time exceeded, which traceroute relies on.

Blocking all ICMP breaks things like path MTU discovery. Defenders usually filter selectively rather than drop it all.

NAT in brief

Network Address Translation lets many private hosts share one public address. The router rewrites source addresses and ports on the way out and keeps a table to reverse the change on replies. For security this means two things: hosts behind NAT are not directly reachable from outside unless a rule forwards a port, and a scan from the internet sees the router, not the machines behind it. NAT is not a firewall, though. It is a side effect of address sharing that happens to block unsolicited inbound traffic.

Stand-in: parse header bytes

Real analyzers read raw bytes and unpack fields at fixed offsets. The first example does that for a hand-built IPv4 header and TCP header using Python's struct module. The bytes are invented (documentation addresses, no real traffic).

Python Decode an IPv4 + TCP header

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second example classifies what a scanner would conclude from different replies. It reuses the flag decoding and applies the rules from earlier in the lesson.

Python What does this reply mean?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Remember what is real here and what is not: the byte layout and flag meanings are exactly what the protocols define. The "replies" are made up, because a browser sandbox cannot send raw packets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A SYN probe to a port gets a SYN+ACK back. What does the scanner conclude?

    SYN+ACK is the second step of the handshake. A service accepted the connection attempt, so the port is open.
  2. 2. Which reply normally means that the host is up but nothing listens on that TCP port?

    A reset tells the sender there is no listener. Silence usually means a firewall dropped the probe.
  3. 3. What identifies one TCP connection?

    The four-value tuple is unique per connection, which is why many clients can talk to port 443 at once.
  4. 4. Why is NAT not a substitute for a firewall?

    NAT is address sharing. It does not inspect or filter traffic on purpose, and rules can forward ports back to internal hosts.

Summary

  • Data is wrapped in layers: link, internet (IP), transport (TCP/UDP) and application.
  • A port selects a service; a TCP connection is identified by two addresses and two ports.
  • The handshake is SYN, SYN+ACK, ACK. Flags such as SYN, ACK, FIN and RST drive scanner conclusions.
  • Open, closed and filtered are inferred from SYN+ACK, RST or silence.
  • ICMP carries errors and diagnostics; NAT hides internal hosts but is not a firewall.
  • The Python examples parse invented bytes; they are stand-ins for a real analyzer.

Next: How Port Scanning Works