Chapter 9: Packet Analysis and Protocol Investigation

Learning Objectives

Section 1: PCAP Analysis and File Extraction

A PCAP (Packet CAPture) file is a binary file containing timestamped network frames. Two formats are common: the original libpcap format and PCAPNG (Wireshark's default), which supports multiple interfaces and extended metadata.

Capture Tools

ToolRolePrimary Use Case
tcpdumpCLI capture and filterHeadless servers, scripted capture, IR triage
WiresharkGUI capture and analysisDeep inspection, stream reassembly, file extraction
tsharkCLI version of WiresharkScripted analysis, automation, SIEM integration
NetworkMinerPassive analysisArtifact extraction, host profiling

Common tcpdump Capture Commands

# Capture all traffic on eth0, rotate every 100MB
tcpdump -i eth0 -w /evidence/capture-%Y%m%d-%H%M%S.pcap -C 100 -Z analyst

# Capture only traffic to/from a suspected C2 server
tcpdump -i eth0 host 203.0.113.55 -w /evidence/c2-traffic.pcap

# Capture DNS and HTTP for exfiltration analysis
tcpdump -i eth0 'port 53 or port 80' -w /evidence/dns-http.pcap

TCP Stream Reassembly in Wireshark

TCP is stream-oriented — application data is segmented across multiple packets. Wireshark's reassembly engine merges segments in sequence number order.

Following a TCP Stream: Right-click any packet → Follow > TCP Stream. Switch to Raw or Hex mode for binary data. A binary starting with MZ bytes is a Windows PE executable.

Exporting HTTP Objects

For HTTP traffic: File > Export Objects > HTTP lists every reassembled HTTP object — HTML pages, images, executables, ZIP archives. This is far more efficient than manual stream following.

Encrypted Traffic Limitations

ApproachRequirementNotes
Pre-master secret logBrowser key logging pre-configuredSet SSLKEYLOGFILE env variable
Server private keyAccess to TLS private keyOnly works for non-PFS cipher suites
Endpoint inspectionEDR/agent on the hostCaptures data before encryption
JA3/JA3S fingerprintingMetadata onlyIdentifies TLS client/server fingerprints
PCAP Incident Response Triage Workflow
Live Capture / PCAP Received Apply Triage Filters: IPs · Ports · Protocols Encrypted? No Yes Identify Suspicious Streams Unusual ports · Beaconing Obtain Key Material SSLKEYLOGFILE · JA3 Server Private Key · EDR Stream Type TCP HTTP Follow TCP Stream File → Export Objects → HTTP Extract Artifacts EXEs · Docs · Credentials Hash + Sandbox SHA-256 · VirusTotal Correlate with Threat Intel

Key Points — Section 1

Pre-Check — Section 1

1. What are the magic bytes (hex) that identify a Windows PE executable at the start of a TCP stream payload?

2. Which Wireshark menu path lets you extract all HTTP objects (files, images, scripts) from a PCAP in one step?

Section 2: Intrusion Key Elements in Packet Data

The 5-Tuple

Every network session is uniquely identified by its 5-tuple:

ElementDescriptionExample
Source IPOriginating host address192.168.1.105
Destination IPTarget host address203.0.113.55
Source PortOriginating application port54231 (ephemeral)
Destination PortTarget service port443 (HTTPS)
ProtocolLayer 4 protocolTCP (6)

Port/Service Anomalies

ScenarioWhat to Look For
Non-standard port for common serviceHTTP on port 8888, SSH on port 2222
Legitimate port, wrong protocolIRC traffic on port 443, DNS tunneling on port 53
High ephemeral-range destination portPossible reverse shell or C2 callback
Sequential port scanning patternSYN packets to consecutive destination ports

Payload Inspection — File Magic Bytes

Magic Bytes (Hex)File Type
4D 5AWindows PE executable (EXE/DLL)
50 4B 03 04ZIP archive
25 50 44 46PDF document
FF D8 FFJPEG image
89 50 4E 47PNG image

Protocol Anomalies

Key Points — Section 2

Pre-Check — Section 2

3. An analyst sees an ICMP Echo Request with a 1,400-byte payload. What does this most likely indicate?

4. The five elements of a network session 5-tuple are: src IP, dst IP, src port, dst port, and ___?

Section 3: Protocol Header Analysis

Ethernet and ARP

FieldSizeIntrusion Relevance
Destination MAC6 bytesBroadcast (FF:FF:FF:FF:FF:FF) used in ARP scans
Source MAC6 bytesSpoofed MACs indicate MAC flooding or evasion
EtherType2 bytes0x0800=IPv4; 0x86DD=IPv6; 0x0806=ARP
VLAN Tag (802.1Q)4 bytes (optional)Double-tagging (0x8100) = VLAN hopping attack

ARP Spoofing: An attacker broadcasts gratuitous ARP replies claiming a victim IP maps to the attacker's MAC. Detection: multiple ARP replies for the same IP from different MACs in rapid succession.

IPv4 TTL Baseline

OSDefault TTL
Linux/Unix64
Windows128
Cisco IOS255
Solaris255

A packet arriving with TTL=64 claiming to be from a Windows machine is suspicious — it may be spoofed or the OS fingerprint is masked.

TCP Flag Analysis

Flag PatternLegitimate UseMalicious Use
SYN onlyConnection initiationSYN flood (DoS), SYN scan (Nmap -sS)
SYN + FINNever validStealth scan — evade stateful FW
No flags (NULL)Never validNULL scan (Nmap -sN)
FIN onlyNever valid in isolationFIN scan (Nmap -sF)
URG+PSH+FIN (Xmas)Never validXmas scan (Nmap -sX)
RST floodError conditionDoS against TCP sessions
ACK onlyEstablished sessionACK scan for firewall mapping
# SYN flood detection
tcp.flags.syn == 1 && tcp.flags.ack == 0

# Stealth scan (invalid SYN+FIN)
tcp.flags.syn == 1 && tcp.flags.fin == 1

# NULL scan
tcp.flags == 0x000

# Xmas scan
tcp.flags == 0x029
TCP Flag Combinations: Legitimate vs. Malicious
Incoming TCP Packet Flag Check SYN only SYN flood / Port Scan tcp.flags.syn==1 && ack==0 SYN + ACK Normal handshake reply VALID — expected SYN + FIN NEVER VALID Stealth scan — evades FW No Flags (NULL) Nmap -sN scan Xmas (URG+PSH +FIN) Nmap -sX · NEVER VALID FIN only Nmap -sF scan NEVER VALID alone RST Flood TCP session DoS attack Teardown all connections ACK only Firewall rule mapping ACK scan technique Valid / Expected Malicious / Attack Suspicious / Recon

Key Points — Section 3

Pre-Check — Section 3

5. A packet arrives claiming to originate from a Windows system, but its IP TTL is 64. What does this indicate?

6. Which TCP flag combination is used by Nmap's Xmas scan?

Section 4: Application Protocol Analysis

DNS Tunneling Detection

DNS (UDP/TCP port 53) is almost universally allowed through firewalls, making it a favored channel for data exfiltration and C2. Data is encoded into DNS query subdomain labels:

aGVsbG8gd29ybGQ.attacker-c2.com
dGhpcyBpcyBleGZpbA.attacker-c2.com
IndicatorNormal DNSDNS Tunneling
Query name length< 30 charactersOften > 50 characters
Query frequencyLow, irregularHigh, regular (beaconing)
Record typesA, AAAA, MX, CNAMETXT, NULL, heavy TXT responses
Unique subdomainsFew per domainHundreds of unique subdomains
Query namesHuman-readableRandom/encoded strings
# Long DNS query names (key heuristic for tunneling/DGA)
dns.qry.name.len > 36

# TXT record queries (commonly abused for tunneling)
dns.qry.type == 16

HTTP Header Analysis

HeaderLegitimate ValueSuspicious Value
User-AgentBrowser string (Mozilla/5.0...)Missing, empty, or tool signature (sqlmap, Nikto)
HostTarget domain nameIP address directly, or missing entirely
RefererPrevious page URLMissing when expected, URL injection attempts
X-Forwarded-ForLegitimate proxy chainSpoofed IPs, multiple forged headers

SMTP Exfiltration Extraction

# Step 1: Filter SMTP traffic
smtp

# Step 2: Follow TCP Stream, find Base64 attachment block
# Step 3: Decode the attachment
base64 -d attachment.b64 > attachment.bin
file attachment.bin

C2 Beaconing Pattern

DNS Tunneling: Data Exfiltration Flow
Compromised Host 10.1.2.50 Malware running Corporate DNS Resolver Forwards queries upstream Port 53 ← always open Attacker Auth NS c2.evildomain.xyz Decodes exfil data Sends C2 commands Query: aGVsbG8g d29ybGQ.c2.evil... Recursive fwd → attacker NS TXT reply C2 command enc. DNS Response encoded command Query: dGhpcyBp cyBleGZpbA... Recursive fwd chunk #2 Next C2 cmd in TXT record DNS Response C2 instructions Detection Heuristics: dns.qry.name.len > 36 | Query rate: 847 queries/30min | Single domain: c2.evildomain.xyz Subdomain entropy: HIGH | Record type: TXT | Decode subdomains → file contents revealed

Key Points — Section 4

Pre-Check — Section 4

7. Which Wireshark filter is the primary heuristic for detecting DNS tunneling based on query name length?

8. In HTTP traffic, what does a missing or empty User-Agent header typically indicate?

Section 5: Regular Expressions for Security Analysis

Core Regex Syntax

MetacharacterMeaningExampleMatches
.Any single charactera.cabc, a1c, a_c
*Zero or more of precedingab*cac, abc, abbc
+One or more of precedingab+cabc, abbc (not ac)
?Zero or one of precedingcolou?rcolor, colour
^Start of string/line^GETLines starting with GET
$End of string/line\.php$Strings ending in .php
[]Character class[0-9]Any digit
{n,m}Quantifier range\d{1,3}1 to 3 digits
(?i)Case-insensitive flag(?i)malwareMALWARE, Malware, malware

Security-Relevant Patterns

# IPv4 address (strict)
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b

# IPv4 for log extraction (practical)
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

# URL pattern
https?://[^\s"'<>]+

# Suspicious TLD domains (phishing/malware)
\b\w+\.(xyz|top|club|work|gq|tk|ml|cf|ga)\b

# Email address
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}

Wireshark PCRE Display Filters

# C2 keywords across all frame bytes
frame matches "(?i)(beacon|c2|callback|malware)"

# Encoded PowerShell in HTTP URIs
http.request.uri matches "(?i)(powershell|cmd\.exe|base64)"

# Suspicious file extensions
http.request.uri matches "\.(exe|dll|bat|ps1|vbs|jar)$"

# DNS tunneling: long subdomain names
dns.qry.name matches "^[a-zA-Z0-9]{30,}\."

# SQL injection attempts
http.request.uri matches "(?i)(union.*select|or\s+1=1|drop\s+table)"

# Empty User-Agent (scanner/bot)
http.user_agent matches "^$"

SIEM (Splunk) Rex Integration

# Extract src IPs from firewall logs
index=firewall sourcetype=asa
| rex field=_raw "src=(?P<src_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
| stats count by src_ip

# Web shell access patterns
index=web_logs
| regex uri="(?i)\.(php|aspx|jsp)\?.*cmd="

# Beaconing detection
index=proxy
| rex field=url "^https?://(?P<domain>[^/]+)"
| bucket _time span=1m
| stats count by _time, domain, src_ip
| where count > 10

Threat Hunting Patterns

# C2 beaconing — regular PHP POST intervals
http.request.method == "POST" && http.request.uri matches "^/[a-z]{4,8}\.php$"

# Credential harvesting — cleartext
http.authorization matches "Basic "
ftp.request.command == "PASS"
pop.request.command == "PASS" || imap.request matches "LOGIN"

# Lateral movement — SMB between internal hosts
smb || smb2 && ip.src matches "^10\." && ip.dst matches "^10\."

Key Points — Section 5

Post-Check — Sections 1–5

9. What Wireshark filter detects suspicious file downloads by matching executable extensions in HTTP request URIs?

10. In Splunk's rex command, what is the purpose of named capture groups like (?P<src_ip>...)?

11. Which of the following is the correct Wireshark filter to detect SQL injection attempts in HTTP URIs?

12. An analyst wants to extract all IP addresses from Splunk firewall logs. Which Splunk command should they use?

13. What does the Wireshark filter tcp.flags == 0x000 detect?

14. A host at 10.1.2.50 is generating 847 DNS queries in 30 minutes, all to subdomains of c2.evildomain.xyz, with subdomain labels 40–60 characters long. What attack is most likely occurring?

15. To decrypt HTTPS traffic in Wireshark when using a browser, which approach should an analyst set up before capturing?

Your Progress

Answer Explanations