Overview
The traffic capture module provides a Python wrapper aroundtcpdump for capturing network traffic to PCAP files. It’s used to record beacon traffic during experiments for subsequent flow analysis and feature extraction.
Source: telemetry/traffic_capture.py
Key Features
- tcpdump Integration: Spawns and manages tcpdump subprocesses
- BPF Filtering: Supports Berkeley Packet Filter expressions
- Automatic Directory Management: Creates
pcaps/directory automatically - UTC+7 Timestamping: Generates timestamps in UTC+7 timezone for filenames
- Graceful Shutdown: Handles SIGINT/SIGTERM with clean capture termination
- Profile Labeling: Embeds evasion profile parameters in PCAP filenames
Core Functions
start_capture
Launches tcpdump as a background subprocess:interface(str): Network interface to capture on (e.g.,eth0,enp0s8)output_file(str): Output filename (automatically placed inpcaps/directory)bpf_filter(str): BPF filter expression (default:'tcp port 443')
subprocess.Popen object for the tcpdump process
Requirements:
tcpdumpmust be installed and on PATH- Root/sudo privileges typically required on Linux
stop_capture
Terminates a running tcpdump process gracefully:proc(subprocess.Popen): Process returned bystart_capture()
- Sends SIGTERM to tcpdump
- Waits up to 5 seconds for clean exit
- Sends SIGKILL if timeout expires
- Logs warnings if process already exited
label_capture
Generates standardized PCAP filenames embedding profile parameters:{base_name}_jitter{pct}_pad{max}_{timestamp}.pcap
Command-Line Usage
Run as a standalone module:--interface(required): Network interface name--output(required): Output PCAP filename--filter(optional): BPF filter expression (default:tcp port 443)--duration(optional): Capture duration in seconds (default: 0 = until Ctrl+C)
Integration Example
Typical usage in experiment scripts:File Organization
Capture Directory: All PCAPs are automatically saved topcaps/ directory
Configuration
Default Constants (defined intelemetry/traffic_capture.py:15-17):
Common BPF Filters
Error Handling
tcpdump Not Found:- Run with
sudoon Linux:sudo python -m telemetry.traffic_capture ... - Or grant capabilities:
sudo setcap cap_net_raw,cap_net_admin=eip $(which tcpdump)
Logging
Capture operations are logged via structured logger:capture started: Logged when tcpdump spawns (includes PID)capture stopped: Logged on clean exit (includes return code)capture process already exited: Warning if stop called on dead process
Next Steps
After capturing traffic:- Parse flows: Use Flow Parser to extract FlowRecords
- Extract features: Use Feature Extractor for ML features
- Run experiments: See Experiments for automated pipelines
See Also
- Flow Analysis - Parse PCAPs into flow records
- Feature Extraction - Extract ML features from flows
- Experiments - Automated capture and analysis pipelines