2. Capture Filters (BPF Syntax)
Understanding BPF Primitives
Berkeley Packet Filter (BPF) syntax is the industry-standard language for defining which packets a network sniffer should capture. Unlike display filters which hide packets already in memory, capture filters are applied at the NIC driver level, drastically reducing disk I/O and CPU usage by discarding irrelevant traffic before it reaches user space.
The primary primitives include host to filter by IP, net for CIDR blocks, port for specific TCP/UDP services, and proto for underlying protocols like tcp, udp, or icmp.
Example terminal command to capture traffic from a specific host: tcpdump -i eth0 host 192.168.1.5
# Capture all traffic on port 443 for a specific host tcpdump -i eth0 host 10.0.0.1 and port 443 -w capture.pcap
Logical Operators and Combining Primitives
BPF allows complex queries by chaining primitives with logical operators: and (&&), or (||), and not (!). These operators allow you to define a surgical capture environment.
When combining these, it is essential to use single quotes in your shell to prevent the terminal from interpreting special characters like '&' or '!' as shell commands.
The following table summarizes common logical combinations used in security operations.
| Filter | Function |
|---|---|
| host X and port Y | Traffic strictly to/from host X on specific port |
| net 192.168.0.0/24 | All traffic originating from a local network range |
| not port 22 | Excludes SSH traffic to reduce noise |
Optimizing Performance with Pre-Filtering
In a production SOC environment, capturing full-packet traces for high-volume traffic can crash a disk partition in seconds. Pre-filtering is a critical skill for junior analysts.
By using BPF, you drop unwanted packets at the kernel level. This keeps the packet capture (PCAP) files lean and keeps the system responsive during an incident investigation.
The following Python script simulates how one might filter raw byte data using similar logical constraints programmatically.
def filter_traffic(packet_port, target_port): # Simulate kernel-level check if packet_port == target_port: return True return False # Mock packet data packet = {'port': 80, 'data': 'GET /index.html'} if filter_traffic(packet['port'], 80): print('Packet captured successfully.')
Targeted Protocol Captures
Targeted captures are essential when identifying indicators of compromise (IoC) like beaconing activity. By combining host, port, and proto, you can isolate single streams.
For example, if you suspect an attacker is communicating over a non-standard port, you might use: 'host 10.0.0.1 and not port 80 and not port 443'. This captures all traffic to that host except standard web traffic.
Using tools like TShark, you can apply these filters to read existing PCAP files to perform forensic analysis on isolated traffic.
tshark -r traffic.pcap -Y 'tcp.port == 8080 && ip.src == 10.0.0.5'
Knowledge Check
Ready to test your understanding of 2. Capture Filters (BPF Syntax)?