Advertisement
Phr0zen_Penguin

pcap_sniff.c - TCP Packet Sniffing Program (Using libpcap)

Jul 7th, 2015
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. /**
  2.  * [pcap_sniff.c]
  3.  *
  4.  * A TCP sniffing program, using raw sockets, @ the sub-Session (5) layer of the OSI model.  It
  5.  * utilizes libpcap to handle raw sockets more accurately.
  6.  *
  7.  * compile with:
  8.  * gcc -o pcap_sniff pcap_sniff.c -l pcap
  9.  */
  10.  
  11. #include <pcap.h>
  12.  
  13. #include "hacking.h"
  14.  
  15.  
  16. /**
  17.  * The pcap_fatal() function:
  18.  */
  19. void pcap_fatal(const char *failed_in, const char *errBuf)
  20. {
  21.     printf("Fatal error in %s: %s\n", failed_in, errBuf);
  22.  
  23.     exit(1);
  24. }
  25.  
  26.  
  27. /**
  28.  * The main() function:
  29.  */
  30. int main(void)
  31. {
  32.             struct  pcap_pkthdr header;
  33.     const   u_char  *packet;
  34.             char    errBuf[PCAP_ERRBUF_SIZE];
  35.             char    *device;
  36.             pcap_t  *pcap_handle;                                   /* Similar to a file descriptor; referencing a packet-capturing object. */
  37.             int     i;
  38.  
  39.     /**
  40.      * Find a suitable device to sniff on:
  41.      */
  42.     device = pcap_lookupdev(errBuf);
  43.  
  44.         if(device == NULL)
  45.             pcap_fatal("pcap_lookupdev", errBuf);
  46.  
  47.     printf("Sniffing on device %s\n", device);
  48.  
  49.  
  50.     /**
  51.      * Open the packet-capturing device:
  52.      */
  53.     pcap_handle = pcap_open_live(device, 4096, 1, 0, errBuf);
  54.  
  55.         if(pcap_handle == NULL)
  56.             pcap_fatal("pcap_open_live", errBuf);
  57.  
  58.  
  59.         /**
  60.          * Capture the (desired) packets (using a packet capture loop):
  61.          */
  62.         for(i = 0; i < 3; i++)
  63.         {
  64.             packet = pcap_next(pcap_handle, &header);
  65.  
  66.             printf("Got a %d byte packet\n", header.len);
  67.             dump(packet, header.len);
  68.         }
  69.  
  70.  
  71.     /**
  72.      * Close the packet capture interface:
  73.      */
  74.     pcap_close(pcap_handle);
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement