Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.99 KB | None | 0 0
  1. // #include <Python.h>
  2.  
  3. #include <pcap.h>
  4. #include <pcap/pcap.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netpacket/packet.h>
  10. #include <net/ethernet.h> /* the L2 protocols */
  11. #include <net/if.h>
  12. #include <netdb.h>
  13. #include <strings.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <netinet/in.h>  /* sockaddr_in{} and other Internet defns */
  18. #include <netinet/udp.h>
  19. #include <netinet/ip.h>
  20. #include <ctype.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <arpa/inet.h>
  24. #include <sys/ioctl.h>
  25.  
  26. #define SNAP_LEN 100
  27.  
  28. char *dev;          /* The device to sniff on */
  29.  
  30. int tcp=0;
  31. int udp=0;
  32. int icmp=0;
  33. int ip=0;
  34. int unknown=0;
  35. int sctp=0;
  36.  
  37. int cols = 80;
  38. int lines = 24;
  39.  
  40. const char *_inet_ntop( struct sockaddr  *pcliaddr, char *str, int len)
  41. {
  42.     struct sockaddr_in6*     cliaddr;
  43.     struct sockaddr_in*  cliaddrv4;
  44.  
  45.     if(pcliaddr == NULL)
  46.         return NULL;
  47.  
  48.     if( pcliaddr->sa_family == AF_INET6 ){
  49.         cliaddr = (struct sockaddr_in6*) pcliaddr;
  50.         return inet_ntop(AF_INET6,  (struct sockaddr  *) &cliaddr->sin6_addr,  str, len);
  51.     }
  52.     else{
  53.         cliaddrv4 = (struct sockaddr_in*) pcliaddr;
  54.         return inet_ntop(AF_INET, (struct sockaddr  *) &cliaddrv4->sin_addr,  str, len);
  55.     }
  56. }
  57.  
  58. int analyse(const u_char* buff, struct pcap_pkthdr header)
  59. {
  60.     // u_char out[2048];
  61.     // char buf[100];
  62.  
  63.     int n = header.len;
  64.  
  65.     struct ethhdr *hdr;
  66.     struct ip *ipv4hdr;
  67.     struct udphdr *uhdr;
  68.     hdr = (struct ethhdr *)buff;
  69.  
  70.     ipv4hdr = (struct ip *)(buff+sizeof(struct ethhdr));
  71.     // uhdr = (struct udphdr *)(buff+sizeof(struct ethhdr)+sizeof(struct ip));
  72.  
  73.     // printf("SRC MAC addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
  74.     //         (int) hdr->h_source[0], (int) hdr->h_source[1], (int) hdr->h_source[2],
  75.     //         (int) hdr->h_source[3], (int) hdr->h_source[4],(int) hdr->h_source[5] );
  76.     // printf("DST MAC addr = %02x:%02x:%02x:%02x:%02x:%02x\n",
  77.     //         (int) hdr->h_dest[0], (int) hdr->h_dest[1], (int) hdr->h_dest[2],
  78.     //         (int) hdr->h_dest[3], (int) hdr->h_dest[4],(int) hdr->h_dest[5] );
  79.     // printf("Proto = 0x%04x\n", ntohs( hdr->h_proto));
  80.  
  81.  
  82.     int type = ntohs(hdr->h_proto);
  83.     // int method;
  84.     if( type == ETHERTYPE_IPV6 || type  == ETHERTYPE_IP ) {
  85.     //     if(type == ETHERTYPE_IPV6)
  86.     //         method = AF_INET6;
  87.     //     else
  88.     //         method = AF_INET;
  89.  
  90.     //     inet_ntop( method, &ipv4hdr->ip_src, buf, 100);
  91.     //     printf("IP src addr = %s\n", buf);
  92.     //     inet_ntop( method, &ipv4hdr->ip_dst, buf, 100);
  93.     //     printf("IP dst addr = %s\n", buf);
  94.  
  95.         switch(ipv4hdr->ip_p) {
  96.             case IPPROTO_TCP:
  97.                 tcp++;
  98.                 break;
  99.             case IPPROTO_UDP:
  100.                 udp++;
  101.                 break;
  102.             case IPPROTO_ICMP:
  103.                 icmp++;
  104.                 break;
  105.             case IPPROTO_IP:
  106.                 ip++;
  107.                 break;
  108.             case IPPROTO_SCTP:
  109.                 sctp++;
  110.                 break;
  111.             default:
  112.                 unknown++;
  113.                 break;
  114.         }  
  115.     //         //char *out=buff+sizeof(struct ethhdr)+sizeof(struct ip)+sizeof(struct udphdr);
  116.  
  117.     //         memcpy(out,buff+sizeof(struct ethhdr)+sizeof(struct ip)+sizeof(struct udphdr),
  118.     //                 (n-sizeof(struct ethhdr)+sizeof(struct ip)+sizeof(struct udphdr)) );
  119.  
  120.     //         int k=0;
  121.     //         printf("DATA = ");
  122.     //         for(k=0; k< (n-sizeof(struct ethhdr)+sizeof(struct ip)+sizeof(struct udphdr)); k++){
  123.     //             if((isprint(out[k])))
  124.     //                 printf("%c",out[k] );
  125.     //             else
  126.     //                 printf("-");    
  127.     //         }
  128.     //         printf("\n");
  129.     //         fflush(stdout);
  130.     //         return 1;
  131.     //     }
  132.     //     else
  133.     //         printf("IP PROTO = %d", ipv4hdr->ip_p );
  134.     }
  135.     // printf("\n");
  136.     // fflush(stdout);
  137.     return n;
  138. }
  139.  
  140. pcap_t* setup(char* device, char* filter_exp)
  141. {
  142.     char errbuf[PCAP_ERRBUF_SIZE];  /* Error string */
  143.     struct bpf_program fp;      /* The compiled filter */
  144.     bpf_u_int32 mask;       /* Our netmask */
  145.     bpf_u_int32 net;        /* Our IP */
  146.     char str1[INET6_ADDRSTRLEN], str2[INET6_ADDRSTRLEN];
  147.     pcap_if_t *alldevsp=NULL, *devsp=NULL;
  148.     pcap_addr_t *p_addr;
  149.  
  150.     /* Define the device */
  151.     dev = pcap_lookupdev(errbuf);
  152.     if (dev == NULL) {
  153.         fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
  154.     }
  155.  
  156.     /* Find the properties for the default device */
  157.     if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
  158.         fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
  159.         net = 0;
  160.         mask = 0;
  161.     }
  162.     printf("localnet = %s, netmask = %s\n",
  163.             inet_ntop(AF_INET, &net, str1, sizeof(str1)),
  164.             inet_ntop(AF_INET, &mask, str2, sizeof(str2)));
  165.  
  166.     /*Find all devices*/
  167.     printf("Other devices to be used are: \n");
  168.  
  169.     if( pcap_findalldevs( &alldevsp, errbuf) == -1 ) {
  170.         fprintf(stderr, "Couldn't get devices %s\n", errbuf);
  171.     }else{
  172.         devsp = alldevsp;
  173.         while( devsp != NULL ){
  174.             fprintf(stdout, "\tDevice name %s [%s]\n", devsp->name, devsp->description );
  175.             p_addr = devsp->addresses;
  176.             while( p_addr != NULL ){
  177.                 printf("\t   localnet = %s, netmask = %s\n",      
  178.                         _inet_ntop(p_addr->addr, str1, sizeof(str1)),
  179.                         _inet_ntop(p_addr->netmask, str2, sizeof(str2)));
  180.                 p_addr=p_addr->next;
  181.             }
  182.             devsp=devsp->next;
  183.             printf("\n");
  184.         }
  185.         pcap_freealldevs(alldevsp);
  186.     }
  187.  
  188.     /* Open the session in promiscuous mode */
  189.     pcap_t* handle = pcap_open_live(device, SNAP_LEN, 1, 2000, errbuf);
  190.    
  191.     if (handle == NULL) {
  192.         fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
  193.     }
  194.  
  195.     printf("Link header = %s\n", pcap_datalink_val_to_name( pcap_datalink(handle)));
  196.     if( pcap_set_datalink(handle,DLT_EN10MB) == -1 ){
  197.         printf("pcap_set_datalink error!");
  198.     }
  199.     /* Compile and apply the filter */
  200.     if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
  201.         fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
  202.     }
  203.     if (pcap_setfilter(handle, &fp) == -1) {
  204.         fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
  205.     }
  206.     struct winsize ts;
  207.     ioctl(0, TIOCGWINSZ, &ts);
  208.     cols = ts.ws_col;
  209.     lines = ts.ws_row;
  210.  
  211.     return handle;
  212. }
  213.  
  214. void print_data(){
  215.     system("clear");
  216.     printf(" Listening on: %s\n", dev);
  217.     printf("\n\t\t\t\t\t\tTCP: %i\n", tcp);
  218.     printf("\n\t\t\t\t\t\tUDP: %i\n", udp);
  219.     printf("\n\t\t\t\t\t\tICMP: %i\n", icmp);
  220.  
  221.     // printf("\n\t\t\t\tTCP\t\tUDP\t\tICMP\n");
  222.  
  223.     // printf("\n\t\t\t\t%i\t\t%i\t\t%i\n", tcp, udp, icmp);
  224. }
  225.  
  226. int main(int argc, char *argv[])
  227. {
  228.     // pcap_t *handle = malloc(sizeof(pcap_t));         /* Session handle */
  229.  
  230.     struct pcap_pkthdr header;  /* The header that pcap gives us */
  231.     const u_char *buff;     /* The actual packet */
  232.     //u_char *buff;     /* The actual packet */
  233.     char buf[100];
  234.     char filter_exp[1024] = ""; /* The filter expression */
  235.  
  236.     int n;
  237.     int datalink=0;
  238.  
  239.     if( argc == 3)
  240.         strncpy(filter_exp, argv[2], 2048);
  241.  
  242.     if ( (argc != 2) && (argc != 3) ){
  243.         fprintf(stderr, "usage: %s <Interface name> '<filter>'\n", argv[0]);
  244.         return 1;
  245.     }
  246.  
  247.     pcap_t *handle = setup(argv[1], filter_exp);
  248.  
  249.     int i=0, j=0;
  250.     for(;;){
  251.         j++;
  252.         buff = pcap_next(handle, &header);
  253.         if(buff == NULL)
  254.             continue;
  255.        
  256.         datalink = pcap_datalink(handle);
  257.  
  258.         i += analyse(buff, header);
  259.         print_data();
  260.     }
  261.    
  262.     printf("Sniffed %d packets\n",j);
  263.     /* And close the session */
  264.     pcap_close(handle);
  265.     return(0);
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement