Advertisement
oaktree

sniffer by pysec

Jul 7th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. /*
  2.     changes by oaktree:
  3.  
  4.     * make argv[1] specify the number of
  5.     packets to sniff
  6.  
  7.     * make argv[2] optional argument for
  8.     file to write sniff results to
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <netinet/udp.h>
  15. #include <netinet/tcp.h>
  16. #include <netinet/ether.h>
  17. #include <netinet/ip.h>
  18. #include <netinet/ip_icmp.h>
  19. #include <arpa/inet.h>
  20. #include <pcap.h>
  21.  
  22. char* checkFlag(struct tcphdr* tcp)
  23. {
  24.     if      (tcp->th_flags == TH_SYN)
  25.         return "SYN";
  26.     else if (tcp->th_flags == TH_RST)
  27.         return "RST";
  28.     else if (tcp->th_flags == TH_FIN)
  29.         return "FIN";
  30.            
  31.     return "Unknown";
  32. }
  33.  
  34. int main (int argc, char** argv) {
  35.  
  36.     if (argc < 2 || argc > 3) {
  37.         printf("Usage: ./sniffer <# of packets to sniff> [file to write to (optional)]\n");
  38.         exit (2);
  39.     }
  40.     int n = atoi(argv[1]);
  41.  
  42.     /*
  43.         initialize stuff
  44.     */
  45.     char err[PCAP_ERRBUF_SIZE];
  46.     char *device;
  47.     pcap_t *handle;
  48.  
  49.     const u_char *packet;
  50.  
  51.     if ((device = pcap_lookupdev(err)) == NULL) {
  52.         perror("device:");
  53.     }
  54.  
  55.     printf("\nSniffing on interface %s\n", device);
  56.  
  57.     if ((handle = pcap_open_live (device, BUFSIZ, 0, 1000, err)) == NULL)
  58.     {
  59.         fprintf (stderr, "%s\n", err);
  60.         exit (1);
  61.     }
  62.  
  63.     struct ip           *ip;
  64.     struct udphdr       *udp;
  65.     struct tcphdr       *tcp;
  66.     struct ether_header *eh;
  67.     struct icmphdr      *icmp;
  68.     struct pcap_pkthdr header;
  69.  
  70.     int etype;
  71.  
  72.     char buf[BUFSIZ];
  73.     FILE* fp = NULL;
  74.     if (argc == 3) {
  75.         fp = fopen(argv[2], "w");
  76.        
  77.         if (fp == NULL) {
  78.             printf ("The file %s could not be opened/created.\n", argv[2]);
  79.             exit(3);
  80.         }
  81.     }
  82.  
  83.     for (int i = 0; i < n; i++) {
  84.  
  85.         packet = pcap_next (handle, &header);
  86.         usleep (0);
  87.  
  88.         if (!packet)
  89.             continue;
  90.  
  91.         /* get some useful info */
  92.         /* Assuming Ethernet Link 802.3 */
  93.         eh = (struct ether_header*)packet;
  94.         etype = ntohs (eh->ether_type);
  95.  
  96.         /* Ignore non IP packets... */
  97.         if (etype != ETHERTYPE_IP)
  98.             continue;
  99.    
  100.         ip = (struct ip*) (packet + ETH_HLEN);
  101.  
  102.         // clear buffer
  103.         memset(buf, '\0', BUFSIZ);
  104.  
  105.         if (ip->ip_p == IPPROTO_UDP) {
  106.             udp = (struct udphdr*) (packet + ETH_HLEN + sizeof(struct ip));
  107.             sprintf (buf, "\n[ UDP ] Src Port: %u  Dst Port: %u\n", ntohs(udp->source), ntohs(udp->dest));
  108.         }
  109.  
  110.         if (ip->ip_p == IPPROTO_TCP) {
  111.             tcp = (struct tcphdr*) (packet + ETH_HLEN + sizeof(struct ip));
  112.             sprintf (buf, "\n[ TCP ] Src Port: %u  Dst Port: %u Flags Set: %s\n", ntohs(tcp->source), ntohs(tcp->dest), checkFlag(tcp));
  113.         }
  114.  
  115.         if (ip->ip_p == IPPROTO_ICMP) {
  116.             icmp = (struct icmphdr*) (packet + ETH_HLEN + sizeof(struct ip));
  117.             sprintf (buf, "\n[ ICMP ] Type: %u  Code: %u\n", ntohs(icmp->type), ntohs(icmp->code));
  118.         }
  119.  
  120.         printf("%s", buf);
  121.  
  122.         if (fp != NULL) {
  123.             fwrite(buf, sizeof(char), strlen(buf), fp);
  124.         }
  125.  
  126.     }
  127.  
  128.     fclose (fp);
  129.     pcap_close (handle);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement