Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <string.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include "pcap.h"
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. #define PINGS_NUM 3
  10. #define SLEEP_SEC 10
  11. #define INTERFACE "lo"
  12.  
  13. int packet_number=0;
  14. pthread_t *p;
  15.  
  16. void *trd(void *arg)
  17. {
  18.  
  19. //ADD YOUR CODE WHICH WILL BE EXECUTED AFTER EVERY PINGS_NUM HERE:
  20. system("iptables -D INPUT -p tcp --dport 22 -m state --state NEW -j DROP");
  21.  
  22. sleep(SLEEP_SEC);
  23.  
  24. //ADD YOUR CODE WHICH WILL BE EXECUTED AFTER SLEEPING SLEEP_SEC HERE:
  25. system("iptables -I INPUT -p tcp --dport 22 -m state --state NEW -j DROP");
  26.  
  27. delete p;
  28. p=NULL;
  29. }
  30.  
  31.  
  32. struct ether_header
  33. {
  34.   u_int8_t ether_dhost[6];
  35.   u_int8_t ether_shost[6];
  36.   u_int16_t ether_type;
  37. };
  38.  
  39. typedef u_int32_t in_addr_t;
  40.  
  41. struct ip_header
  42. {
  43. #ifdef WORDS_BIGENDIAN
  44.   u_int8_t ip_version:4;
  45.   u_int8_t ip_header_length:4;
  46. #else
  47.   u_int8_t ip_header_length:4;
  48.   u_int8_t ip_version:4;
  49. #endif
  50.   u_int8_t ip_tos;
  51.   u_int16_t ip_length;
  52.   u_int16_t ip_id;
  53.   u_int16_t ip_off;
  54.   u_int8_t ip_ttl;
  55.   u_int8_t ip_protocol;
  56.   u_int16_t ip_checksum;
  57.   struct in_addr ip_source_address;
  58.   struct in_addr ip_destination_address;
  59. };
  60.  
  61. struct icmp_header
  62. {
  63.   u_int8_t icmp_type;
  64.   u_int8_t icmp_code;
  65.  
  66.   u_int16_t icmp_checksum;
  67.   u_int16_t icmp_identifier;
  68.   u_int16_t icmp_sequence;
  69. };
  70.  
  71. void icmp_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content)
  72. {
  73.   struct icmp_header *icmp_protocol;
  74.   icmp_protocol=(struct icmp_header*)(packet_content+14+20);
  75.   switch(icmp_protocol->icmp_type)
  76.     {
  77.     case 8:
  78.         packet_number++;
  79.         if(packet_number>=PINGS_NUM)
  80.         {
  81.         packet_number = 0;
  82.         if(p==NULL)
  83.             {
  84.             p=new pthread_t;
  85.             pthread_create(p, NULL, trd, NULL);
  86.             }
  87.         }
  88.       break;
  89.     default:
  90.       break;
  91.     }
  92. }
  93.  
  94. void ip_protocol_packet_callback(u_char * argument,const struct pcap_pkthdr * packet_header,
  95.                  const u_char * packet_content)
  96. {
  97.   struct ip_header * ip_protocol;
  98.   u_int header_length;
  99.   u_int offset;
  100.   u_char tos;
  101.   u_int16_t checksum;
  102.   ip_protocol=(struct ip_header*)(packet_content+14);
  103.   checksum=ntohs(ip_protocol->ip_checksum);
  104.   header_length=ip_protocol->ip_header_length*4;
  105.   tos=ip_protocol->ip_tos;
  106.   offset=ntohs(ip_protocol->ip_off);
  107.   switch(ip_protocol->ip_protocol)
  108.     {
  109.     case 1:
  110.       icmp_protocol_packet_callback(argument,packet_header,packet_content);
  111.       break;
  112.     default:
  113.       break;
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr * packet_header,
  120.                        const u_char * packet_content)
  121. {
  122.   u_short ethernet_type;
  123.   struct ether_header *ethernet_protocol;
  124.   u_char *mac_string;
  125.   ethernet_protocol=(struct ether_header *)packet_content;
  126.   ethernet_type=ntohs(ethernet_protocol->ether_type);
  127.   mac_string=ethernet_protocol->ether_shost;
  128.   mac_string=ethernet_protocol->ether_dhost;
  129.   switch(ethernet_type)
  130.     {
  131.     case 0x0800:
  132.       ip_protocol_packet_callback(argument,packet_header,packet_content);
  133.       break;
  134.     default:
  135.       break;
  136.     }
  137. }
  138.  
  139. int  main()
  140. {
  141.   pcap_t *pcap_handle=NULL;
  142.   char error_content[PCAP_ERRBUF_SIZE];
  143.   char *net_interface;
  144.   net_interface = new char[256];
  145.   strcpy(net_interface, INTERFACE);
  146.   struct bpf_program bpf_filter;
  147.   char bpf_filter_string[]="icmp";
  148.   bpf_u_int32 net_mask;
  149.   bpf_u_int32 net_ip;
  150.  
  151.   pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content);
  152.   if( (pcap_handle=pcap_open_live(net_interface,20480,1,100,error_content)) == NULL)
  153.     {
  154.     printf("Error: %s",error_content);
  155.     }
  156.   pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_mask);
  157.   pcap_setfilter(pcap_handle,&bpf_filter);
  158.   if(pcap_datalink(pcap_handle)!=DLT_EN10MB)
  159.     {
  160.     printf("Error\n");
  161.     return 1;
  162.     }
  163.   p=NULL;
  164.   if(pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL)==-1)
  165.     {
  166.     pcap_geterr(pcap_handle);
  167.     return 1;
  168.     }
  169.   pcap_close(pcap_handle);
  170.   return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement