This is a simple packet capture program written in C for Linux. It allows you to capture network packets and analyze their contents. This program uses the popular libpcap library to capture packets and provides a basic command-line interface for interacting with the program.
- Capture network packets on a specified network interface.
- Add network filter to the capture. See tcpdump filters.
- Specify number of packets to capture.
- Parse packet details such as source and destination IP addresses, protocols, etc.
- Save captured packets to a log file for later analysis.
Before using this program, ensure you have the following prerequisites installed:
- Linux OS
- C Compiler (e.g., GCC)
- libpcap library
- Clone this repository to your local machine:
- SSH:
[email protected]:r-karunathilake/packet_sniffer.git
- HTTPS:
https://github.com/r-karunathilake/packet_sniffer.git
- Compile the program using your C compiler:
e.g.
gcc packet_capture.c -o packet_capture -lpcap
You can run the packet capture program by executing the following command:
./packet_capture [options]
Replace [options]
with the following:
-
-l <log_file>
: (Optional) Specify the output file to save captured packets (in text format). Default is a "log.txt" file in the current working directory. -
-n <num_packets>
: (Optional) Specify the maximum number of packets to capture. Default is continuous capture until program is terminated. -
-i <interface>
: (Optional) Specify the network interface to capture packets (e.g., eth0). If not provided, the default interface is selected. -
-f <filter_pattern>
: (Optional) Specify packet filter string based on TCPDump format. -
-t <time>
: (Optional) Specify the time to run the capture in milliseconds. Default is 10 seconds. Note: this option overrides the number of packets. -
-h
: Display help and usage information.
Example usage:
sudo ./packet_sniffer -i eth0 -n 20 -f "tcp or udp or icmp" -t 5000 -l captured_packets.txt
This command shows a capture instance on eth0
interface for 20
packets for 5 seconds
. Additionally, tcp or udp or icmp
filter is configured. Finally, the captured output is logged to captured_packets.txt
.
Note: require super user privileges to run the program.
This packet capture program is open-source and available under the MIT License.
Ravindu Karunathilake
Email: [email protected]
GitHub: https://github.com/r-karunathilake