30 Mins

5. Command-Line Analysis with TShark

TShark CLI Fundamentals

TShark is the command-line equivalent of Wireshark, designed for analyzing packets without a GUI. It is an essential tool for headless environments and automated incident response.

The most critical flags are -i for capturing from a specific interface and -r for reading from a stored PCAP file. To narrow down your view, use Capture Filters (-f) which use Berkeley Packet Filter (BPF) syntax at the data link layer, and Display Filters (-Y) which leverage Wireshark's deep packet inspection syntax.

Security Operator Note: Always use capture filters (-f) when live sniffing to reduce CPU load, as they drop unwanted packets at the kernel level.

Text
# Capture traffic on interface eth0 and filter for HTTP traffic
tshark -i eth0 -f "tcp port 80"

# Read a pcap file and apply a display filter for a specific IP
tshark -r capture.pcap -Y "ip.addr == 192.168.1.1"

Extracting Data Fields

One of TShark's most powerful features is the -T fields output mode. This allows you to extract specific packet header values directly into a text format, which is perfect for parsing.

By combining -T fields with the -e flag, you define exactly which protocol fields you want to export. This transforms raw binary PCAPs into clean, tabular data.

Security Operator Note: Use -E separator=comma to output the data as CSV, making it instantly ready for Excel or data analysis scripts.

Text
# Extract source IP and destination TCP port from traffic in a file
tshark -r investigation.pcap -T fields -e ip.src -e tcp.dstport -Y "tcp.flags.syn == 1"

Shell Pipeline Automations

The power of TShark lies in its interoperability with standard Linux utilities. By piping the TShark output into sort and uniq, you can quickly identify top talkers or common connection points in a large dataset.

This workflow is the backbone of rapid network triage, allowing you to summarize gigabytes of traffic in seconds.

Security Operator Note: Use awk after TShark to filter or reformat specific columns based on mathematical logic.

Text
# Find the top 5 most common source IPs in a PCAP
tshark -r traffic.pcap -T fields -e ip.src | sort | uniq -c | sort -nr | head -n 5

Programmatic Analysis with Python

For complex tasks that require state tracking or session reassembly, you can use the pyshark wrapper for TShark or the dpkt library for low-level parsing of raw packets.

While pyshark provides an easy interface to TShark features, dpkt requires manual unpacking of bytes, giving you total control over the binary structures.

Security Operator Note: Use pyshark when you need high-level protocol dissectors, and dpkt when you need maximum performance for massive log files.

Python 3
import dpkt
import socket

def parse_pcap_stream(packet_bytes):
    eth = dpkt.ethernet.Ethernet(packet_bytes)
    ip = eth.data
    if isinstance(ip, dpkt.ip.IP):
        src_ip = socket.inet_ntoa(ip.src)
        dst_ip = socket.inet_ntoa(ip.dst)
        print(f"IP Header Parsed: {src_ip} -> {dst_ip}")

# Mock Ethernet II Frame
mock_packet = b'\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\x08\x00\x45\x00\x00\x28\x00\x01\x00\x00\x40\x06\x7c\xcd\x0a\x00\x00\x01\x0a\x00\x00\x02'
parse_pcap_stream(mock_packet)

Knowledge Check

Ready to test your understanding of 5. Command-Line Analysis with TShark?

Back