This is a basic packet sniffer written in Python using the Scapy library. It captures TCP packets on a specified network interface and logs the connection details to a file.
- Python 3.x
- Scapy library
- Icecream library (for debugging)
- Install Python 3.x from python.org.
- Install the required libraries using pip:
pip install scapy icecream
Run the script with the following command:
python main.py <interface> [verbose]
<interface>
: The network interface to sniff on (e.g.,eth0
,wlan0
).[verbose]
: Optional. If specified, enables verbose output.
To sniff packets on the eth0
interface with verbose output:
python main.py eth0 verbose
sys
: Provides access to some variables used or maintained by the interpreter.
icecream
: A library for debugging.
scapy.all
: Imports all Scapy functions for packet capturing.
scapy.layers.inet
: Imports IP and TCP layers from Scapy.
handle_packet(packet, log)
Handles each captured packet. If the packet has a TCP layer, it extracts the source and destination IP addresses and ports, and writes the connection details to the log file.
main(interface, verbose=False)
Starts packet sniffing on the specified network interface. It opens a log file and uses the
sniff
function from Scapy to capture packets. If verbose mode is enabled, it provides detailed output.
The script checks if the correct number of arguments is provided. If not, it prints a usage message and exits. If the arguments are valid, it calls the
main
function with the specified interface and verbose mode.
The script handles keyboard interrupts (Ctrl+C) to gracefully exit the program.
The captured packet details are logged in a file named sniffer_<interface>_log.txt
in the log_files
directory.