The pcap library is the library behind tcpdump. It lets us capture network packets from a C program.
What is pcap#
pcap stands for: Packet Capture. The library which contains the pcap module, is called libpcap and its availability depends on the operating system and installed packages. If you’re on a Mac (like me), installing this library is easy-peasy thanks to the amazing (and almighty) Homebrew.
The library works with C and C++, and there are bindings for Java, Python and other languages.
How to install pcap#
To install pcap, we need to have Homebrew installed in our system, you can check if you have Homebrew by running:
$ brew --version
Which should show you something like:
Homebrew 2.0.0
Homebrew/homebrew-core (git revision a968a; last commit 2019-02-05)
Homebrew/homebrew-cask (git revision f3d7c; last commit 2019-02-05)
In case you do not have installed Homebrew yet, go to Homebrew’s homepage and follow the instructions.
If you got brew, then the next step is simple as in:
$ brew install libpcap
After that, you should be able to use pcap in your C/C++ programs. Hooray!
Linker and Includes#
In order to get the library into your code, you need to specify the object files and the directories from which the compiler will grab the headers. Usually, this means knowing the exact location of your library and checking for version upgrades and changes.
The compiler searches for headers; the linker searches for libraries. -lpcap requests the library from the linker’s search directories. It does not use the shell’s PATH to locate headers or libraries.
On macOS, you may already have system capture support. If you specifically want the Homebrew installation, pass its include and library directories explicitly, as shown below.
Sample program in C#
This is a sample program in C which does 2 basic things:
- Displays a list of all the NICs in your computer and allows you to select one to perform the packet capturing.
- Captures 15 network packets and displays them with the Source MAC, Destiny MAC and the Packet Type.
Source code:
# ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
# define _CRT_SECURE_NO_WARNINGS
# endif
# include <pcap.h>
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
/*prototype of the packet handler*/
void packet_handler(u_char *param, const struct pcap_pkthdr*header, const u_char *pkt_data);
int main()
{
pcap_if_t *alldevs;
pcap_if_t*d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
/*Retrieve the device list*/
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/*Print the list*/
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Check capture permissions and available interfaces.\n");
pcap_freealldevs(alldevs);
return -1;
}
printf("Enter the interface number (1-%d):",i);
if (scanf("%d", &inum) != 1)
{
pcap_freealldevs(alldevs);
return 1;
}
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
/*Free the device list*/
pcap_freealldevs(alldevs);
return -1;
}
/*Jump to the selected adapter*/
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/*Open the device*/
/*Open the adapter*/
if ((adhandle= pcap_open_live(
d->name, // name of the device
65536, // portion of the packet to capture.
// packets longer than this snapshot length are truncated.
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter: %s\n", errbuf);
/*Free the device list*/
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->name);
if (pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr, "This example requires Ethernet frames.\n");
pcap_freealldevs(alldevs);
pcap_close(adhandle);
return 1;
}
/*At this point, we don't need any more the device list. Free it*/
pcap_freealldevs(alldevs);
/*start the capture*/
pcap_loop(adhandle, 15, packet_handler, NULL);
pcap_close(adhandle);
return 0;
}
/*Callback function invoked by libpcap for every incoming packet*/
void packet_handler(u_char *param, const struct pcap_pkthdr*header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;
/*
* unused parameters
*/
(void)(param);
(void)(pkt_data);
/*convert the timestamp to readable format*/
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
if (ltime == NULL) return;
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s,%06ld len:%u\n", timestr, (long)header->ts.tv_usec, header->len);
if (header->caplen < 14)
{
printf("Truncated Ethernet header\n");
return;
}
int j=0,k=0;
printf("Destiny MAC:\n");
for(j=0;j<6;j++){
printf("%02X:",pkt_data[j]);
}
printf("\n Source MAC:\n");
for(k=6;k<12;k++){
printf("%02X: ",pkt_data[k]);
}
printf(" \n\n");
unsigned short tipo = (pkt_data[12]*256)+pkt_data[13];
printf("Type: %d %02X %02X \n",tipo,pkt_data[12],pkt_data[13]);
}
Save your program as AwesomePacketSniffer.c (or another filename), and you should be good to go.
To compile the program you’ll need to run:
PCAP_PREFIX="$(brew --prefix libpcap)"
cc -Wall -Wextra -I"$PCAP_PREFIX/include" -L"$PCAP_PREFIX/lib" AwesomePacketSniffer.c -lpcap -o AwesomePacketSniffer.out
Finally, run the program with:
$ ./AwesomePacketSniffer.out
In case the program isn’t able to detect any network interfaces, try running it with sudo privileges. Note that this is not the intended way to run this kind of program so you should check your libpcap permissions and configuration.
The output#
The expected output should be something like:
1. en0 (No description available)
2. p2p0 (No description available)
3. awdl0 (No description available)
4. bridge0 (No description available)
5. utun0 (No description available)
6. en1 (No description available)
7. lo0 (No description available)
8. gif0 (No description available)
9. stf0 (No description available)
10. XHC20 (No description available)
Enter the interface number (1-10): 1
listening on (en0)...
09:57:38,578314 len:140
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,578397 len:662
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,611104 len:66
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
09:57:38,611414 len:66
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
09:57:38,632868 len:147
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
09:57:38,632872 len:830
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
09:57:38,632971 len:66
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,632972 len:66
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,633381 len:112
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
09:57:38,633423 len:66
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,635294 len:112
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,645214 len:347
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,645274 len:112
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,645284 len:709
Destiny MAC:
E0:55:3D:62:77:A0:
Source MAC:
D4: 61: 9D: 2D: E1: 58:
Type: 2048 08 00
09:57:38,662611 len:129
Destiny MAC:
D4:61:9D:2D:E1:58:
Source MAC:
E0: 55: 3D: 62: 77: A0:
Type: 2048 08 00
Conclusion#
This library allows us to access network packets and the information we can get from them goes beyond this simple tutorial.