SkillPilotAI

Network attacks and defense

The common network attacks (ARP spoofing / MITM, DNS poisoning, SYN flood, DDoS and port scans), seen only from the defender's side: how each one works, how to spot it in logs and conntrack, and which defense stops it.

96.1 Why is it important?

  • Many core protocols (ARP, plain DNS, TCP handshake) were designed without verification, so they can be abused.
  • Defenders must recognise the pattern of each attack to raise the right alert and pick the right fix.
  • Interviews often ask: how does a SYN flood work, what are SYN cookies, IDS vs IPS.

96.2 Attacks at a glance

AttackHow it worksDefense
ARP spoofing / MITMFake ARP reply: gateway IP is-at attacker MAC; traffic flows through the attackerDynamic ARP Inspection, static ARP for gateway, HTTPS everywhere
DNS poisoningForged replies with a guessed query ID put a fake IP in the resolver cacheRandom source port + query ID, DNSSEC, encrypted DNS
SYN floodThousands of SYNs from fake IPs fill the half-open SYN queueSYN cookies (net.ipv4.tcp_syncookies)
DDoSBotnet of thousands of devices: volumetric, protocol or application attackISP scrubbing, CDN, anycast
Port scanOne IP sends SYN to many ports to find open onesIDS alert, IPS block

96.3 ARP spoofing and MITM

ARP spoofing leads to man-in-the-middle
  1. 1.PC 192.168.116.20 has the gateway 192.168.116.1 MAC in its ARP table

  2. 2.Attacker .66 sends a fake ARP reply: 192.168.116.1 is-at 02:42:c0:a8:74:42

  3. 3.ARP has no verification: the PC overwrites the gateway entry

  4. 4.PC traffic now goes to the attacker, who reads it and forwards it to the gateway

  5. 5.Defense: DAI on the switch, static gateway ARP, HTTPS; alert when 2 IPs share 1 MAC

96.4 DNS poisoning

  • The resolver forwards 'A example.com?' to the authoritative server.
  • At the same time the attacker floods the resolver with forged replies (spoofed source, guessed ID, A 203.0.113.66).
  • If one forged reply matches before the real answer, the resolver caches it and every user goes to the fake site until the TTL expires.
  • Defense: random source port + query ID (much harder to guess), DNSSEC signatures, encrypted DNS.

96.5 SYN flood and SYN cookies

  • Normal handshake: SYN -> SYN-ACK -> ACK; for each SYN the server keeps a half-open entry in its SYN queue.
  • The attacker sends thousands of SYNs from fake source IPs; their ACK never comes, the queue fills, real users' SYNs are dropped.
  • SYN cookies: when the queue is full the server saves nothing and encodes the connection details in the SYN-ACK sequence number.
  • A real client's ACK returns that number, so the server rebuilds the connection; fake IPs cost nothing. On Linux net.ipv4.tcp_syncookies is on by default.
SYN flood vs SYN cookies
  1. 1.Attacker sends SYN x300 from fake 198.51.100.x to server :80

  2. 2.Server replies SYN-ACK to fake IPs; no ACK ever returns

  3. 3.SYN queue full: kernel logs 'Possible SYN flooding ... Sending cookies'

  4. 4.Real user's SYN gets a SYN-ACK carrying a cookie (no queue entry)

  5. 5.User's ACK returns the cookie: connection built, site stays up

96.6 DDoS

DDoS typeWhat it fillsExample
VolumetricLink bandwidth10 Gbps junk on a 1 Gbps office link
ProtocolServer / firewall tablesSYN flood
ApplicationApp resourcesMillions of HTTP requests
  • A botnet is a group of hacked cameras, routers, phones and PCs; every bot has a different IP, so blocking one IP does nothing.
  • A volumetric attack fills the link before your firewall, so the fix must be upstream: ISP scrubbing, CDN, anycast.

96.7 Port scan detection, IDS and IPS

PointIDSIPS
PositionWatches a copy (switch mirror port / TAP)Inline in the traffic path
ActionOnly raises alertsDrops packets, can block the IP for a while
RiskDoes not stop the attackFalse positives block real traffic
ToolsSuricata, Snort, ZeekSuricata, Snort (inline mode)
  • Port scan pattern: one source touches ~100 different ports within seconds.
  • Both use signature-based and anomaly-based detection; new IPS rules are first tested in IDS mode.

96.8 Practical: baseline and spotting a SYN flood

Lab: namespaces in one Docker container with a web server 192.168.116.10, a normal user and a lab attacker; the 198.51.100.0 documentation range is blackholed at the lab gateway.

Terminal
sysctl net.ipv4.tcp_syncookiesss -ltnss -tnH state syn-recv | wc -lcurl -s -m 3 http://192.168.116.10/
Output
srv$ sysctl net.ipv4.tcp_syncookiesnet.ipv4.tcp_syncookies = 1srv$ ss -ltnState  Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0      64           0.0.0.0:80        0.0.0.0:*srv$ ss -tnH state syn-recv | wc -l0pc$ curl -s -m 3 http://192.168.116.10/hello from srv
Terminal
python3 /tmp/flood.pytcpdump -lnn -i eth0 -c 5 'tcp[13] == 2'ss -tnH state syn-recv | wc -lconntrack -L -p tcp --state SYN_RECV | wc -lnstat -az TcpExtSyncookiesSentdmesg | grep -i "SYN flooding"curl -s -m 3 http://192.168.116.10/
Output
atk$ python3 /tmp/flood.pysent 300 SYN packetssrv$ tcpdump -lnn -i eth0 -c 5 'tcp[13] == 2'IP 198.51.100.89.37061 > 192.168.116.10.80: Flags [S]IP 198.51.100.122.44928 > 192.168.116.10.80: Flags [S]IP 198.51.100.52.32516 > 192.168.116.10.80: Flags [S]5 packets capturedsrv$ ss -tnH state syn-recv | wc -l32srv$ conntrack -L -p tcp --state SYN_RECV | wc -lconntrack v1.4.8 (conntrack-tools): 300 flow entries have been shown.300srv$ nstat -az TcpExtSyncookiesSent#kernelTcpExtSyncookiesSent            235                0.0srv$ dmesg | grep -i "SYN flooding"[200744.581343] TCP: request_sock_TCP: Possible SYN flooding on port 0.0.0.0:80. Sending cookies.pc$ curl -s -m 3 http://192.168.116.10/hello from srv
  • tcp[13] == 2 captures packets with only the SYN flag; every source IP is different.
  • ss shows only 32 half-open entries (queue limit reached, then cookies); conntrack shows all 300 in SYN_RECV.
  • nstat: 235 cookies sent; dmesg 'Possible SYN flooding' is the simplest monitoring alert.
  • The real user's curl still works during the flood: SYN cookies saved it.

96.9 Practical: port scan detection

Terminal
tcpdump -lnn -i eth0 'tcp[13] == 2' > scan.txtnmap -sS -n -Pn -p 1-100 192.168.116.10grep -c 192.168.116.66 scan.txtawk 'NF{print $5}' scan.txt | sort -u | wc -l
Output
atk$ nmap -sS -n -Pn -p 1-100 192.168.116.10Not shown: 99 closed tcp ports (reset)PORT   STATE SERVICE80/tcp open  httpNmap done: 1 IP address (1 host up) scanned in 0.50 secondssrv$ grep -c 192.168.116.66 scan.txt100srv$ awk 'NF{print $5}' scan.txt | sort -u | wc -l100
  • 100 SYNs from one source to 100 different ports in under a second: exactly what an IDS port-scan rule catches.
  • Scan only your own network or with written permission.

96.10 Common mistakes

  • Thinking a strong firewall stops DDoS: a 10 Gbps flood fills a 1 Gbps link before the firewall even sees it.
  • Blocking single IPs during a DDoS: botnets have thousands.
  • Turning off tcp_syncookies: the server loses its main SYN flood defense.
  • Deploying new IPS rules straight to inline mode: false positives can block real users.

96.11 Interview tips

  • Many IPs + one port = SYN flood / DDoS; one IP + many ports = port scan.
  • SYN cookies encode connection state in the SYN-ACK sequence number, so the queue cannot be exhausted.
  • IDS watches a copy and alerts; IPS is inline and drops, with false-positive risk; both use signature + anomaly detection.
  • ARP spoofing defense: DAI + HTTPS; DNS poisoning defense: random port/ID + DNSSEC.
  • Volumetric DDoS needs upstream help: scrubbing, CDN, anycast.