Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 53 ยท Building a Safe Lab

Networking Essentials for Security

Learn the networking ideas every security tester needs: layers, IP addresses, ports, DNS, HTTP, ARP, the TCP handshake and how to decode a packet header from raw bytes.

Almost every attack and every defense in this course travels over a network. A scanner probes ports, a phishing link resolves a domain name, a web exploit rides on HTTP, and a monitoring tool watches packets go by. If you do not know what a port, a handshake or a header is, security tools feel like magic and their output is just noise. With a small, solid model of how networks work, the same output becomes readable evidence.

This lesson is a compact tour of the parts that matter for security work: the layered model, addresses and ports, name lookup, the TCP handshake, and what a packet actually looks like as bytes. You will not send traffic to anyone. Instead, you will decode a packet from raw bytes inside the browser and simulate a handshake in code. The real network stack runs in your operating system and cannot run on this site, so the code here is an honest stand-in: it follows the same rules but touches no network. It builds on your lab plan and the Linux command line skills; for general background see the Linux tutorial.

Layers: why networks are described in stacks

Network software is split into layers. Each layer does one job and relies on the one below it. When you send data, each layer wraps it in its own header (this is called encapsulation); the receiver unwraps the layers in reverse order.

You will meet two models. The OSI model has seven layers and is used as a shared vocabulary ("that is a layer 7 problem"). The TCP/IP model has four layers and describes what the internet really uses.

The layers you will name every day

Link (L2) Moves frames on one local network. MAC addresses live here.
Internet (L3) IP addresses and routing between networks.
Transport (L4) TCP and UDP. Ports live here.
Application (L7) HTTP, DNS, SSH, SMTP.

Security tools are named after the layer they work on.

1 / 2

A firewall that filters by IP address and port works on layers 3 and 4. A web application firewall inspects HTTP and works on layer 7. A switch forwards by MAC address at layer 2. Knowing which layer a control sits on tells you what it can and cannot see.

Addresses and ports

An IPv4 address is 32 bits, written as four numbers from 0 to 255, such as 192.168.1.10. Some ranges are private and are never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Your lab and your home network use these. The /24 after an address (CIDR notation) says how many leading bits identify the network; 192.168.1.0/24 means the first 24 bits are the network and the last 8 bits number up to 254 usable hosts. IPv6 uses 128-bit addresses and the same ideas.

An IP address finds a machine. A port (0 to 65535) finds a program on it. A web server usually listens on 80 (HTTP) or 443 (HTTPS), SSH on 22, DNS on 53. A connection is identified by four values: source address, source port, destination address and destination port. Your browser picks a random high source port for each connection, which is why a server can talk to thousands of clients at once on a single port.

On a local network, machines also have MAC addresses (48-bit hardware identifiers). To send an IP packet to a neighbor, a computer needs the neighbor's MAC address and asks "who has 192.168.1.20?" with the Address Resolution Protocol (ARP). Any host may answer, and the asker believes the reply. That trust is a weakness we will return to in Network Attacks and How to Defend Against Them.

DNS and HTTP in one minute

The Domain Name System turns names into addresses. Your computer asks a resolver for www.example.com; the resolver walks the hierarchy (root, com, example.com) and returns records such as A (IPv4), AAAA (IPv6), MX (mail) and TXT (free text). DNS is mostly unencrypted UDP on port 53, and it leaks information about what you visit, which is why passive reconnaissance relies on it.

HTTP is a text protocol on top of TCP. A request is a start line, headers and an optional body:

TEXT
GET /products?id=7 HTTP/1.1
Host: shop.example
User-Agent: demo
Accept: text/html

The server answers with a status line (200 OK, 404 Not Found, 302 Found), headers and a body. HTTPS is the same conversation wrapped in TLS encryption. A great deal of web security comes down to what is inside those headers and parameters, which is the topic of the web security chapter.

TCP and UDP

UDP is "fire and forget": no connection, no delivery guarantee, low overhead. DNS queries and video calls use it. TCP gives you a reliable ordered stream. Before any data moves, the two sides perform the 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: the client asks, the server agrees, the client confirms.

If nothing listens on the port, the server answers with RST (reset) instead of SYN-ACK. If a firewall silently drops the packet, there is no answer at all. This difference between "open", "closed" and "filtered" is exactly what port scanners observe, as you will see in Port Scanning and Service Discovery Concepts.

The flags live in the TCP header as single bits: FIN, SYN, RST, PSH, ACK, URG. A packet with only SYN set is the start of a connection; SYN plus ACK is the reply.

Decoding a packet by hand

Capture tools such as Wireshark or tcpdump (real tools; you would run them only on networks you own or are authorized to monitor) show packets as bytes and then decode them into fields. The decoding is nothing but reading fixed positions. Here is a stand-in decoder: we write the bytes of an IPv4 header followed by a TCP header as hex, and pull out the fields. The packet is invented for this lesson; nothing was captured.

Node.js Decode an IPv4 + TCP header from bytes

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 node:readline or process.stdin), it gets the next line from this box.

Real and stand-in, honestly: the field positions (version in the first nibble, TTL at byte 8, protocol at byte 9, addresses at bytes 12 and 16, TCP ports in the first four TCP bytes) are the real IPv4 and TCP layouts. The bytes are made up, and a real tool also checks lengths, options and fragmentation. Notice that the packet has only SYN set and destination port 80: it is the first step of a connection to a web server.

A handshake you can step through

Now the state logic. This stand-in models a tiny host with a few listening ports and answers a SYN the way a real TCP stack decides to. There are no sockets: it is a function that mirrors the rule "open port โ†’ SYN-ACK, closed port โ†’ RST, firewalled โ†’ silence".

Node.js Simulate how a host answers a SYN

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 node:readline or process.stdin), it gets the next line from this box.

Real stacks add timeouts, retransmissions and rate limits, but the three outcomes are the same ones you will read in scanner output later.

Practical habits

  • Learn to read a packet capture before you rely on a tool summary. One captured exchange teaches more than a page of theory, and it is safe to capture on your own lab network.
  • Always know which layer you are working at. "The firewall blocks it" means little until you can say which field it matches.
  • Treat the local network as untrusted. ARP, DHCP and plain HTTP have no built-in authentication.
  • Keep lab traffic on an isolated virtual network so a mistake never reaches a real system.

Protocols worth knowing and subnetting

Protocols you will meet constantly

  • ARP (Address Resolution Protocol) maps an IPv4 address to a MAC address on the local segment: "who has 192.168.56.10? tell 192.168.56.1". It has no authentication, so any host can send false replies. That is the basis of ARP spoofing (see network attacks); defenses are dynamic ARP inspection and static entries for critical hosts.
  • SMB (TCP 445) shares files and printers on Windows networks. It is also a frequent target and a frequent source of leaks (service enumeration).
  • FTP (TCP 21 plus data connections) transfers files in cleartext, including the password. Use SFTP or FTPS instead.
  • SSH (TCP 22) is encrypted remote administration and file transfer. Key-based authentication, disabled root login and rate limiting are the baseline. The server's host key fingerprint lets you detect impersonation on the first connection.
  • Telnet (TCP 23) is cleartext remote login; seeing it on a network is a finding.
  • TLS secures HTTP, mail and many other protocols. Its handshake in outline: the client says hello with the versions and ciphers it supports and a random value; the server picks one, sends its certificate and a random value; they exchange key material (modern suites use an ephemeral Diffie-Hellman exchange, so past traffic stays safe even if the server's key later leaks); both derive session keys and confirm with finished messages; then application data flows encrypted. A tester checks the certificate chain, protocol versions and cipher choices, and whether HTTP redirects to HTTPS. See Cryptography for Hackers.

The shape of a packet

Every packet is nested envelopes: an Ethernet frame (MAC addresses) contains an IP packet (source and destination addresses, protocol number, time to live) which contains a TCP segment or UDP datagram (ports; TCP adds sequence numbers and flags such as SYN, ACK, FIN, RST) which contains the application data. Each layer adds a header on the way out and removes it on the way in. When you read a capture in Wireshark you are reading these layers from the outside in.

Subnetting

A subnet is a slice of an address space. In CIDR notation 192.168.56.0/24 means "the first 24 bits are the network, the remaining 8 bits identify hosts". Subnetting matters for security because you design segmentation with it, scope scans by it and write firewall rules in it.

For IPv4, a /n prefix leaves 32 - n host bits, giving 2^(32-n) addresses. Two are reserved in ordinary subnets (the network address and the broadcast address), so the usable host count is 2^(32-n) - 2.

CIDR Netmask Addresses Usable hosts
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/28 255.255.255.240 16 14
/30 255.255.255.252 4 2

Private ranges you will see in labs are 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. The stand-in below calculates the network, broadcast and usable range, and checks whether an address is in scope, which is a useful safeguard before any scan.

Node.js Calculate subnets and check scope

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 node:readline or process.stdin), it gets the next line from this box.

The scope check is more than arithmetic: the most common accidental out-of-scope scan is a mistyped prefix, such as /16 instead of /24, which expands 254 targets into 65,534.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which layer of the TCP/IP model contains port numbers?

    Ports are part of the TCP and UDP headers, which belong to the transport layer. IP addresses are on the internet layer and MAC addresses on the link layer.
  2. 2. A client sends SYN to a port and receives RST. What does that usually mean?

    A reset means the host is reachable but nothing accepts connections on that port (closed). A silent drop with no reply points to filtering.
  3. 3. What does the notation 192.168.1.0/24 describe?

    The number after the slash is the length of the network prefix in bits. A /24 leaves 8 host bits, so 256 addresses (254 usable hosts). The range is private, not public.
  4. 4. Why is ARP a security weakness on a local network?

    ARP has no authentication, so a malicious host can claim another host's address. Later lessons cover how to detect and prevent this.

Summary

  • Networks are layered; each layer wraps data in its own header, and security controls work at specific layers.
  • IP addresses find hosts, ports find programs, and a connection is defined by the four values source/destination address and port.
  • DNS maps names to records; HTTP is a text protocol that carries most web attacks and defenses.
  • The TCP handshake is SYN, SYN-ACK, ACK. Open ports answer, closed ports reset, filtered ports stay silent.
  • Packets are just bytes at fixed positions; decoding them is the foundation of traffic analysis.
  • ARP and plain DNS trust whoever answers, so local networks must be treated as untrusted.

Next: Passive Reconnaissance and OSINT