Tshark
Why Use tshark
Over Wireshark?¶
Tshark
is a powerful command-line alternative to Wireshark, especially useful in scenarios where efficiency, automation, and resource constraints are factors. Unlike Wiresharkâs GUI, tshark
excels in processing large data sets, automating repetitive tasks, and operating in headless environments, making it ideal for large-scale or automated network analysis tasks.
Basic Capture Commands¶
Capture Traffic¶
On a Specific Interface.
tshark -i eth0
With a Filter.
tshark -i eth0 -f "port 80"
Write Capture to a File¶
tshark -i eth0 -w capture.pcap
Setting Up Captures on VMs
There are some considerations to be aware of when capturing from VMs; more on this can be found over here.
Read a Capture File¶
tshark -r capture.pcap
4. Apply Display Filters¶
Filter HTTP Traffic.
tshark -r <file_name>.pcap -Y "http"
Filter Specific IP.
tshark -r <file_name>.pcap -Y "ip.src == 192.168.1.1"
Malicious Traffic Detection¶
Detecting DNS Tunneling¶
DNS tunneling is a method attackers use to exfiltrate data or communicate with a compromised system through DNS queries. Tshark
can help identify such traffic by filtering for specific domain patterns or unusual query behavior.
tshark -r capture.pcap -Y "dns.qry.name contains 'example.com'" -T fields -e dns.qry.name
Reads the capture file capture.pcap
, applies a display filter to show DNS queries that contain "example.com", and extracts only the queried domain names. This is useful to spot potential DNS tunneling activity, where a specific domain is repeatedly queried.
Detecting Command-and-Control (C2) Communication¶
Filter for consistent timing patterns in SSL/TLS traffic.
tshark -r capture.pcap -q -z conv,ip,tcp,1,"frame.time_delta>1 && frame.time_delta<5"
Identify Long-Lived Connections: C2 traffic may involve persistent connections.
tshark -r capture.pcap -z conv,tcp -q
Filter by Uncommon Ports or Protocols: C2 traffic might use non-standard ports.
tshark -r capture.pcap -Y "tcp.port > 1024 && tcp.flags.syn == 1"
Beaconing Detection¶
Beaconing occurs when malware repeatedly contacts an external server at regular intervals. This behavior can be flagged by filtering traffic based on consistent timing patterns.
tshark -r capture.pcap -q -z io,stat,1,"COUNT(frame)frame.time_delta>0.1&&frame.time_delta<0.5"
Identifying Data Exfiltration¶
Data exfiltration can occur through large, unexpected data transfers, especially via uncommon or unauthorized protocols. Tshark
can filter for these scenarios and flag suspicious activity.
tshark -r capture.pcap -Y "tcp.flags == 0x18 && frame.len > 1500"
0x18
), indicating data transmission, and where the packet length is greater than 1500 bytes. Large packets like these could signal data exfiltration if they are unexpected. Knowing what is normal on your network provides more context for identifying anomalous results.
Anomaly Detection in HTTP Traffic¶
Unusual HTTP requests, such as those with large payloads or requests to uncommon endpoints, might indicate exploitation attempts. Tshark
can filter and analyze this traffic.
tshark -r capture.pcap -Y "http.request.method == 'POST' && http.content_length > 1000"