Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include "pcap.h"
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- #define PINGS_NUM 3
- #define SLEEP_SEC 10
- #define INTERFACE "lo"
- int packet_number=0;
- pthread_t *p;
- void *trd(void *arg)
- {
- //ADD YOUR CODE WHICH WILL BE EXECUTED AFTER EVERY PINGS_NUM HERE:
- system("iptables -D INPUT -p tcp --dport 22 -m state --state NEW -j DROP");
- sleep(SLEEP_SEC);
- //ADD YOUR CODE WHICH WILL BE EXECUTED AFTER SLEEPING SLEEP_SEC HERE:
- system("iptables -I INPUT -p tcp --dport 22 -m state --state NEW -j DROP");
- delete p;
- p=NULL;
- }
- struct ether_header
- {
- u_int8_t ether_dhost[6];
- u_int8_t ether_shost[6];
- u_int16_t ether_type;
- };
- typedef u_int32_t in_addr_t;
- struct ip_header
- {
- #ifdef WORDS_BIGENDIAN
- u_int8_t ip_version:4;
- u_int8_t ip_header_length:4;
- #else
- u_int8_t ip_header_length:4;
- u_int8_t ip_version:4;
- #endif
- u_int8_t ip_tos;
- u_int16_t ip_length;
- u_int16_t ip_id;
- u_int16_t ip_off;
- u_int8_t ip_ttl;
- u_int8_t ip_protocol;
- u_int16_t ip_checksum;
- struct in_addr ip_source_address;
- struct in_addr ip_destination_address;
- };
- struct icmp_header
- {
- u_int8_t icmp_type;
- u_int8_t icmp_code;
- u_int16_t icmp_checksum;
- u_int16_t icmp_identifier;
- u_int16_t icmp_sequence;
- };
- void icmp_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr *packet_header,const u_char *packet_content)
- {
- struct icmp_header *icmp_protocol;
- icmp_protocol=(struct icmp_header*)(packet_content+14+20);
- switch(icmp_protocol->icmp_type)
- {
- case 8:
- packet_number++;
- if(packet_number>=PINGS_NUM)
- {
- packet_number = 0;
- if(p==NULL)
- {
- p=new pthread_t;
- pthread_create(p, NULL, trd, NULL);
- }
- }
- break;
- default:
- break;
- }
- }
- void ip_protocol_packet_callback(u_char * argument,const struct pcap_pkthdr * packet_header,
- const u_char * packet_content)
- {
- struct ip_header * ip_protocol;
- u_int header_length;
- u_int offset;
- u_char tos;
- u_int16_t checksum;
- ip_protocol=(struct ip_header*)(packet_content+14);
- checksum=ntohs(ip_protocol->ip_checksum);
- header_length=ip_protocol->ip_header_length*4;
- tos=ip_protocol->ip_tos;
- offset=ntohs(ip_protocol->ip_off);
- switch(ip_protocol->ip_protocol)
- {
- case 1:
- icmp_protocol_packet_callback(argument,packet_header,packet_content);
- break;
- default:
- break;
- }
- }
- void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr * packet_header,
- const u_char * packet_content)
- {
- u_short ethernet_type;
- struct ether_header *ethernet_protocol;
- u_char *mac_string;
- ethernet_protocol=(struct ether_header *)packet_content;
- ethernet_type=ntohs(ethernet_protocol->ether_type);
- mac_string=ethernet_protocol->ether_shost;
- mac_string=ethernet_protocol->ether_dhost;
- switch(ethernet_type)
- {
- case 0x0800:
- ip_protocol_packet_callback(argument,packet_header,packet_content);
- break;
- default:
- break;
- }
- }
- int main()
- {
- pcap_t *pcap_handle=NULL;
- char error_content[PCAP_ERRBUF_SIZE];
- char *net_interface;
- net_interface = new char[256];
- strcpy(net_interface, INTERFACE);
- struct bpf_program bpf_filter;
- char bpf_filter_string[]="icmp";
- bpf_u_int32 net_mask;
- bpf_u_int32 net_ip;
- pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content);
- if( (pcap_handle=pcap_open_live(net_interface,20480,1,100,error_content)) == NULL)
- {
- printf("Error: %s",error_content);
- }
- pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_mask);
- pcap_setfilter(pcap_handle,&bpf_filter);
- if(pcap_datalink(pcap_handle)!=DLT_EN10MB)
- {
- printf("Error\n");
- return 1;
- }
- p=NULL;
- if(pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL)==-1)
- {
- pcap_geterr(pcap_handle);
- return 1;
- }
- pcap_close(pcap_handle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement