TShark — Wireshark CLI
What It Does
Section titled “What It Does”TShark is the command-line version of Wireshark. It captures and analyzes network traffic without a GUI. Essential for packet analysis, credential extraction, protocol debugging, and forensics. Uses the same display filters and dissectors as Wireshark, so anything you learn here transfers directly.
Running the Examples Script
Section titled “Running the Examples Script”# No target argument required — works with local interfacesbash scripts/tshark/examples.sh
# Or via Makefilemake tshark
# Examplesbash scripts/tshark/examples.shThe script prints 10 example commands for capturing and analyzing traffic, then offers to list available network interfaces.
Install
Section titled “Install”brew install wiresharksudo apt install tsharksudo dnf install wireshark-cliKey Flags to Remember
Section titled “Key Flags to Remember”| Flag | What It Does |
|---|---|
-i <interface> | Capture on specific interface (en0, lo0, etc.) |
-Y '<filter>' | Display filter (same syntax as Wireshark) |
-T fields -e <field> | Extract specific fields from packets |
-r <file> | Read from a capture file |
-w <file> | Write captured packets to file |
-c <count> | Stop after N packets |
-f '<filter>' | Capture filter (BPF syntax, applied at capture time) |
-q -z <stat> | Quiet mode with statistics (dns,tree / http,tree / io,phs) |
--export-objects <proto>,<dir> | Export files transferred over HTTP/SMB/DICOM |
-V | Verbose — show full packet details |
-D | List available capture interfaces |
Capture Progression (recommended order)
Section titled “Capture Progression (recommended order)”tshark -D— what interfaces are available?sudo tshark -i en0 -c 20— capture a quick samplesudo tshark -i en0 -Y 'http' -c 20— filter to protocol of interestsudo tshark -i en0 -w capture.pcap— save for offline analysistshark -r capture.pcap -q -z io,phs— get protocol statistics
Use-Case Scripts
Section titled “Use-Case Scripts”capture-http-credentials.sh — Extract credentials from unencrypted HTTP
Section titled “capture-http-credentials.sh — Extract credentials from unencrypted HTTP”Captures and extracts credentials from unencrypted HTTP traffic. Demonstrates how POST form data, Basic Auth headers, and session cookies are visible in plaintext over HTTP. This is why HTTPS matters.
When to use: Testing web apps served over HTTP, demonstrating the danger of unencrypted traffic, checking if the lab targets leak credentials.
Key commands:
# Capture HTTP POST data (form submissions, login attempts)sudo tshark -i lo0 -Y 'http.request.method==POST' -T fields -e http.host -e http.request.uri -e http.file_data
# Extract Basic Authentication headerssudo tshark -i lo0 -Y 'http.authbasic' -T fields -e ip.src -e http.authbasic
# Filter for packets containing "password"sudo tshark -i lo0 -Y 'http contains "password"' -T fields -e ip.src -e http.host -e http.file_data
# Extract session cookiessudo tshark -i lo0 -Y 'http.cookie' -T fields -e http.host -e http.cookie
# Monitor FTP login attemptssudo tshark -i lo0 -Y 'ftp.request.command==USER || ftp.request.command==PASS' -T fields -e ftp.request.arg
# Full credential extraction pipelinesudo tshark -i lo0 -Y 'http.request.method==POST and http contains "login"' -T fields -e frame.time -e ip.src -e http.host -e http.request.uri -e http.file_dataMake target: make capture-creds
analyze-dns-queries.sh — Monitor DNS for recon detection, tunneling, and C2
Section titled “analyze-dns-queries.sh — Monitor DNS for recon detection, tunneling, and C2”Monitors and analyzes DNS query traffic. DNS is a goldmine for both attackers and defenders — attackers use it for infrastructure mapping and data exfiltration (DNS tunneling), while defenders can detect these patterns.
When to use: Detecting reconnaissance against your network, identifying DNS tunneling or command-and-control channels, monitoring which domains targets are querying.
Suspicious patterns to watch for:
- Unusually long domain names (tunneling encodes data in labels)
- High volume of TXT record queries (potential C2)
- NXDOMAIN floods (domain generation algorithms)
- Zone transfer attempts (AXFR)
Key commands:
# Show all DNS queries in real-timesudo tshark -i en0 -Y 'dns.flags.response==0' -T fields -e dns.qry.name
# DNS queries with responsessudo tshark -i en0 -Y 'dns' -T fields -e dns.qry.name -e dns.a
# Detect DNS zone transfer attemptssudo tshark -i en0 -Y 'dns.qry.type==252'
# Find TXT record queries (potential C2)sudo tshark -i en0 -Y 'dns.qry.type==16' -T fields -e dns.qry.name -e dns.txt
# Monitor for unusually long DNS names (tunneling indicator)sudo tshark -i en0 -Y 'dns.qry.name.len > 50' -T fields -e dns.qry.name
# Count queries per domain from a capture filetshark -r capture.pcap -Y 'dns.flags.response==0' -T fields -e dns.qry.name | sort | uniq -c | sort -rn
# DNS query statistics summarytshark -r capture.pcap -q -z dns,treeMake target: make analyze-dns
extract-files-from-capture.sh — Export transferred files from packet captures
Section titled “extract-files-from-capture.sh — Export transferred files from packet captures”Exports files transferred over HTTP, SMB, FTP, and other protocols from packet capture files. Reconstructs documents, images, executables, and other files from network traffic.
When to use: Incident response (what files were exfiltrated?), malware analysis (what was downloaded?), forensics (reconstruct attacker activity).
Key commands:
# Export all HTTP objects (files) from pcaptshark -r capture.pcap --export-objects http,exported_files/
# Export SMB/CIFS file transferstshark -r capture.pcap --export-objects smb,smb_files/
# List HTTP file transfers without extractingtshark -r capture.pcap -Y 'http.content_type' -T fields -e http.host -e http.request.uri -e http.content_type
# Filter for specific file types (e.g., PDFs)tshark -r capture.pcap -Y 'http.content_type contains "pdf"' -T fields -e http.request.uri
# Show file sizes transferredtshark -r capture.pcap -Y 'http.content_length' -T fields -e http.host -e http.request.uri -e http.content_length
# HTTP transfer statisticstshark -r capture.pcap -q -z http,tree
# Complete workflow: capture then extractsudo tshark -i en0 -f 'port 80' -w traffic.pcap -c 500tshark -r traffic.pcap --export-objects http,extracted/Make target: make extract-files TARGET=<pcap>
Practice Against Lab Targets
Section titled “Practice Against Lab Targets”make lab-up
# Capture HTTP traffic on loopback while browsing lab appssudo tshark -i lo0 -Y 'http' -c 20
# Capture login attempts to DVWA (port 8080)sudo tshark -i lo0 -f 'port 8080' -Y 'http.request.method==POST' -T fields -e http.file_data
# Monitor DNS queries generated by lab activitysudo tshark -i en0 -Y 'dns.flags.response==0' -T fields -e dns.qry.name -c 30
# Save a capture for offline analysissudo tshark -i lo0 -f 'port 8080 or port 3000 or port 8888' -w lab-traffic.pcap -c 500- Most capture commands require
sudobecause raw packet capture needs root privileges - Use
-f(capture filter, BPF syntax) to limit what gets captured — reduces file size and noise - Use
-Y(display filter, Wireshark syntax) to filter what gets shown — more flexible but applied after capture - The loopback interface (
lo0on macOS,loon Linux) is safest for lab practice — only local traffic - Capture files (
.pcap) can be opened in Wireshark GUI for visual analysis - TShark display filters are powerful:
http.request.method==POST,dns.qry.type==16,tcp.port==443 - When extracting fields with
-T fields, chain multiple-eflags for the columns you need - Protocol statistics (
-q -z) work best on saved capture files, not live captures