Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Threats and Models

Packet Analysis for Defenders

Learn how defenders capture and read packets with tcpdump, Wireshark and BPF filters, then summarise flows and flag clear-text credentials in Python.

Logs tell you what a system decided to record. Packets tell you what actually crossed the wire. When an alert is vague ("unusual traffic from host 10.0.4.17") the packet capture is the ground truth: who talked to whom, on which ports, with what flags, and, if it is unencrypted, with what content. Packet analysis is therefore one of the most useful skills in network defence, used for troubleshooting, for incident response and for verifying that your own controls work.

This lesson introduces the standard tools as read-only examples, explains capture filters, and then lets you analyse a fabricated capture in Python: summarising conversations, finding clear-text credentials, and spotting suspicious DNS. Only capture traffic on networks you own or are authorised to monitor, and treat captures as sensitive data.

The tools

tcpdump is the classic command-line capture tool. Wireshark is the graphical analyser, with tshark as its command-line sibling. Both read and write the pcap / pcapng file formats. A capture needs privileges, because the network interface is put in a mode that delivers packets not addressed to your machine (on switched networks you still only see your own traffic and broadcasts unless the switch mirrors ports to you).

Example commands (read-only, shown so you recognise them; example output is representative, not from a real host):

Terminal
# Capture 100 packets on interface eth0, no name lookups, write to a file
tcpdump -i eth0 -n -c 100 -w sample.pcap

# Read it back and show only DNS
tcpdump -n -r sample.pcap 'udp port 53'

# Show TCP SYNs that are not part of an established connection
tcpdump -n -r sample.pcap 'tcp[tcpflags] & (tcp-syn|tcp-ack) == tcp-syn'
TEXT
12:00:01.104213 IP 192.168.1.20.51514 > 203.0.113.10.443: Flags [S], seq 1000, win 65535, length 0
12:00:01.121877 IP 203.0.113.10.443 > 192.168.1.20.51514: Flags [S.], seq 7000, ack 1001, win 65535, length 0
12:00:01.121930 IP 192.168.1.20.51514 > 203.0.113.10.443: Flags [.], ack 1, win 65535, length 0

That is the three-way handshake: [S] (SYN), [S.] (SYN+ACK), [.] (ACK).

Capture filters (BPF)

Capturing everything on a busy link is wasteful and risky, so tools accept a Berkeley Packet Filter (BPF) expression that discards unwanted packets early. The syntax is small and worth learning:

Filter Meaning
host 203.0.113.10 Traffic to or from that address
net 192.168.1.0/24 Traffic involving a subnet
port 443 Source or destination port 443
tcp, udp, icmp Protocol
src host A and dst port 22 Combine with and, or, not
tcp[tcpflags] & tcp-syn != 0 Packets with the SYN flag set

Wireshark additionally has display filters (different syntax, applied after capture) such as http.request.method == "POST" or dns.qry.name contains "example".

What to look at first

  1. Conversations: who talks to whom, how much, for how long. Statistics views in Wireshark (Conversations, Endpoints) give this immediately.
  2. Protocol hierarchy: what share of traffic is HTTPS, DNS, SMB, unknown? A large share of an odd protocol is a lead.
  3. Handshakes and resets: many resets suggest scanning or a broken service; many unanswered SYNs suggest a flood or a firewall drop.
  4. Timing: regular beacons every 60 seconds to one address can be malware calling home.
  5. Clear-text content: credentials, session tokens and personal data that should not be unencrypted.

Summarising a capture in Python

Packet tools compute flow summaries; you can do the same with a few lines. The data below is a fabricated list of packets (time, source, destination, destination port, flags, length), the sort of table you would export from a capture.

Python Summarise conversations and find top talkers

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.

Real command and control traffic usually adds random jitter to avoid exactly this check, so analysts use statistics (low variance in gaps) rather than exact equality. The principle, however, is what matters: automation has regular rhythm; humans do not.

Finding clear-text secrets

Anything sent over an unencrypted protocol can be read from a capture. This is the strongest practical argument for TLS everywhere. The program below scans fabricated HTTP payloads for typical sensitive fields. Using it on real captures from your own network is a good audit; the right fix is to retire the clear-text service, not to hide the evidence.

Python Flag credentials sent in clear text

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.

Notice that Basic authentication is only Base64 encoded, not encrypted; anyone can decode it. The script hides the secret values in its own output, which is good practice when sharing findings.

Suspicious DNS in a capture

DNS is allowed through almost every firewall, so attackers abuse it to smuggle data out. Signs include very long hostnames, unusual record types, or a flood of queries for random-looking subdomains of one domain. A simple check flags long, high-entropy labels.

Python Flag suspicious DNS query names

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 thresholds are a starting point, not a verdict. Content delivery networks and some software legitimately use long random labels, which is why such detections need an allow-list and a human to check.

Practical habits

  • Capture the minimum: filter by host and port, and limit the time window.
  • Protect captures: they can contain passwords and personal data. Store them encrypted, share them only on need, delete them after the investigation.
  • Record context: write down the capture point, time, filter and tool version.
  • Know what you cannot see: encrypted traffic shows only metadata (addresses, ports, sizes, timing), which is still useful for flows and beacons.
  • Verify, then act: a packet pattern is evidence, not proof; correlate with logs and hosts.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In tcpdump output, what does the flag [S.] represent?

    [S] is SYN alone; [S.] means SYN with ACK, the server's reply in the three-way handshake.
  2. 2. What is a BPF filter used for?

    BPF expressions such as 'host 203.0.113.10 and port 443' tell the capture tool which packets to keep and which to discard.
  3. 3. A host contacts one rare port every exactly 60 seconds. What might this indicate?

    Regular timing is a sign of automation. Malware command and control often beacons, although jitter is added in practice.
  4. 4. Why is HTTP Basic authentication over plain HTTP unsafe?

    Base64 is an encoding, not encryption. Use it only inside TLS, or better, use stronger authentication.
  5. 5. What is a sensible way to handle a packet capture from a production network?

    Captures can contain passwords and personal data, so they are sensitive and need minimal collection, secure storage and a deletion plan.

Summary

  • Packets show what really crossed the wire; tcpdump captures, Wireshark and tshark analyse.
  • BPF capture filters limit what is kept; display filters narrow what you view.
  • Start with conversations, protocol mix, handshakes and resets, timing and clear-text content.
  • Python can summarise flows, find beacons, flag clear-text secrets and spot odd DNS names.
  • Treat captures as sensitive: capture little, protect it and delete it.

Next: Firewall Types and How They Work