module_dns_mx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. // send module for performing massive UDP DNS OpenResolver scans
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include "../../lib/includes.h"
  22. #include "../../lib/random.h"
  23. #include "probe_modules.h"
  24. #include "packet.h"
  25. #include "logger.h"
  26. #include "module_udp.h"
  27. #include "module_dns_mx.h"
  28. #define MAX_UDP_PAYLOAD_LEN 1472
  29. #define UNUSED __attribute__((unused))
  30. #define DNS_HEAD_LEN 12
  31. #define DNS_TAIL_LEN 4
  32. //static char *udp_send_msg = NULL;
  33. //static int udp_send_msg_len = 0;
  34. //static int udp_send_substitutions = 0;
  35. // std query recursive for www.google.com type A
  36. // HEADER 12 bytes
  37. // \random -> TransactionID
  38. // \x01\x20 -> Flags: 0x Standard query (0100?)
  39. // \x00\x01 -> Questions: 1
  40. // \x00\x00 -> Answer RRs: 0
  41. // \x00\x00 -> Authority RRs: 0
  42. // \x00\x00 -> Additional RRs: 0
  43. // DOMAIN NAME 16 bytes
  44. // default will be replaced by passed in argument
  45. // \x05\x67\x6d\x61\x69\x6c\x03\x63\x6f\x6d\x00
  46. // TAILER 4 bytes
  47. // \x00\x0f -> Type: MX (Mail Exchange)
  48. // \x00\x01 -> Class: IN (0x0001)
  49. //default sends to gmail.com
  50. //static const char dns_msg_default[] = {0xd2,0x8c,0x01,0x20,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x05,0x67,0x6d,0x61,0x69,0x6c,0x03,0x63,0x6f,0x6d,0x00,0x00,0x0f,0x00,0x01,0x00,0x00,0x29,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  51. // ;; alt1
  52. static const unsigned char dns_msg_default[] = {0xb9, 0x58, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x61, 0x6c, 0x74, 0x34, 0x0d, 0x67, 0x6d, 0x61, 0x69, 0x6c, 0x2d, 0x73, 0x6d, 0x74, 0x70, 0x2d, 0x69, 0x6e, 0x01, 0x6c, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01};
  53. const char *dns_response_strings[] = {
  54. "DNS no error",
  55. "DNS format error",
  56. "DNS server failure",
  57. "DNS domain name error",
  58. "DNS query type not implemented",
  59. "DNS query refused",
  60. "DNS Reserved 6",
  61. "DNS Reserved 7",
  62. "DNS Reserved 8",
  63. "DNS Reserved 9",
  64. "DNS Reserved 10",
  65. "DNS Reserved 11",
  66. "DNS Resevered 12",
  67. "DNS Resevered 13",
  68. "DNS Resevered 14",
  69. "DNS Resevered 15"
  70. };
  71. probe_module_t module_dns_mx;
  72. static int num_ports;
  73. int dns_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
  74. uint32_t *validation, int probe_num, __attribute__((unused)) void *arg) {
  75. struct ether_header *eth_header = (struct ether_header *) buf;
  76. struct ip *ip_header = (struct ip*) (&eth_header[1]);
  77. struct udphdr *udp_header= (struct udphdr *) &ip_header[1];
  78. ip_header->ip_src.s_addr = src_ip;
  79. ip_header->ip_dst.s_addr = dst_ip;
  80. udp_header->uh_sport = htons(get_src_port(num_ports, probe_num, validation));
  81. ip_header->ip_sum = 0;
  82. ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);
  83. fprintf(stderr, "make packet finished\n");
  84. return EXIT_SUCCESS;
  85. }
  86. int dns_init_perthread(void* buf, macaddr_t *src,
  87. macaddr_t *gw, __attribute__((unused)) port_h_t dst_port,
  88. __attribute__((unused)) void **arg_ptr) {
  89. fprintf(stderr, "in init_perthread\n");
  90. memset(buf, 0, MAX_PACKET_SIZE);
  91. struct ether_header *eth_header = (struct ether_header *) buf;
  92. make_eth_header(eth_header, src, gw);
  93. struct ip *ip_header = (struct ip*)(&eth_header[1]);
  94. uint16_t len = htons(sizeof(struct ip) + sizeof(struct udphdr) + sizeof(dns_msg_default));
  95. make_ip_header(ip_header, IPPROTO_UDP, len);
  96. struct udphdr *udp_header = (struct udphdr*)(&ip_header[1]);
  97. len = sizeof(struct udphdr) + sizeof(dns_msg_default);
  98. make_udp_header(udp_header, zconf.target_port, len);
  99. char* payload = (char*)(&udp_header[1]);
  100. module_dns_mx.packet_length = sizeof(struct ether_header) + sizeof(struct ip)
  101. + sizeof(struct udphdr) + sizeof(dns_msg_default);
  102. assert(module_dns_mx.packet_length <= MAX_PACKET_SIZE);
  103. memcpy(payload, dns_msg_default, sizeof(dns_msg_default));
  104. uint32_t seed = aesrand_getword(zconf.aes);
  105. aesrand_t *aes = aesrand_init_from_seed(seed);
  106. *arg_ptr = aes;
  107. return EXIT_SUCCESS;
  108. }
  109. void dns_print_packet(FILE *fp, void* packet) {
  110. struct ether_header *ethh = (struct ether_header *) packet;
  111. struct ip *iph = (struct ip *) &ethh[1];
  112. struct udphdr *udph = (struct udphdr*) (iph + 4*iph->ip_hl);
  113. fprintf(fp, "udp_dns { source: %u | dest: %u | checksum: %u }\n",
  114. ntohs(udph->uh_sport),
  115. ntohs(udph->uh_dport),
  116. ntohl(udph->uh_sum));
  117. fprintf_ip_header(fp, iph);
  118. fprintf_eth_header(fp, ethh);
  119. fprintf(fp, "------------------------------------------------------\n");
  120. }
  121. int dns_validate_packet(const struct ip *ip_hdr, uint32_t len,
  122. uint32_t *src_ip, uint32_t *validation)
  123. {
  124. if (!udp_validate_packet(ip_hdr, len, src_ip, validation)) {
  125. return 0;
  126. }
  127. if (ip_hdr->ip_p == IPPROTO_UDP) {
  128. struct udphdr *udp = (struct udphdr *) ((char *) ip_hdr + ip_hdr->ip_hl * 4);
  129. uint16_t sport = ntohs(udp->uh_sport);
  130. if (sport != zconf.target_port) {
  131. return 0;
  132. }
  133. }
  134. return 1;
  135. }
  136. //void remove_erroneous_characters(char* input, int length){
  137. // int i =0;
  138. // char[1] slash = "\\";
  139. // char* to_return;
  140. // to_return = malloc(sizeof(char)*length);
  141. // for(i; i < length; i++){
  142. // if strncmp(input[i], slash, 1){
  143. //
  144. // }
  145. // }
  146. //}
  147. void dns_process_packet(const u_char *packet, UNUSED uint32_t len, fieldset_t *fs) {
  148. struct ip *ip_hdr = (struct ip *) &packet[sizeof(struct ether_header)];
  149. //char char2[2];
  150. //char query_num[2];
  151. if(ip_hdr->ip_p == IPPROTO_UDP){
  152. struct udphdr *udp = (struct udphdr*) ((char*) ip_hdr+ip_hdr->ip_hl * 4);
  153. uint8_t *ptr = (uint8_t *) &udp[1];
  154. if(len >= 69){
  155. fs_add_string(fs, "classification", (char*) "dns_mx", 0);
  156. fs_add_uint64(fs, "success", 1);
  157. fs_add_uint64(fs, "sport", ntohs(udp->uh_sport));
  158. fs_add_uint64(fs, "dport", ntohs(udp->uh_dport));
  159. fs_add_null(fs, "icmp_responder");
  160. fs_add_null(fs, "icmp_type");
  161. fs_add_null(fs, "icmp_code");
  162. fs_add_null(fs, "icmp_unreach_str");
  163. // memcpy(char2, ptr, 2);
  164. // temp16 = ntohs(*((uint16_t *)ptr+1));
  165. // fs_add_uint64(fs, "query_response", temp16);
  166. // temp16 = ntohs(*((uint16_t *)ptr+2));
  167. // fs_add_uint64(fs, "query_num", temp16);
  168. // temp16 = ntohs(*((uint16_t *)ptr+3));
  169. // fs_add_uint64(fs, "answers_rrs", temp16);
  170. // temp16 = ntohs(*((uint16_t *)ptr+4));
  171. // fs_add_uint64(fs, "authority_rrs", temp16);
  172. // temp16 = ntohs(*((uint16_t *)ptr+5));
  173. // fs_add_uint64(fs, "additional_rrs", temp16);
  174. // //for(i; i<2; i++){
  175. // // fprintf(stderr, "byte %i %02x", i, char2[i]);
  176. // //}
  177. // //fs_add_uint64(fs, "query_response", *char2);
  178. // //memcpy(query_num, (ptr+2), 2);
  179. // //for(i=0; i<2; i++){
  180. // // fprintf(stderr, "byte %i %02x", i, *query_num);
  181. // //}
  182. // //fs_add_uint64(fs, "query_num", *query_num);
  183. // //memcpy(answer_rrs, (ptr+4), 2);
  184. // //fs_add_uint64(fs, "answer_rrs", *answer_rrs);
  185. // //memcpy(authority_rrs, (ptr+6), 2);
  186. // //fs_add_uint64(fs, "authority_rrs", *authority_rrs);
  187. // //memcpy(additional_rrs, (ptr+8), 2);
  188. // //fs_add_uint64(fs, "additional_rrs", *additional_rrs);
  189. // //memcpy(char2, (ptr+10), 2);
  190. //
  191. // //27 bytes
  192. // data = malloc(sizeof(char)*(len - 69));
  193. // //fs_add_uint64(fs, "answers", data);
  194. fs_add_binary(fs, "answers", (len-42), (void *)&ptr[0], 0);
  195. //memcpy(data, (ptr+14), len-35);
  196. //fs_add_binary(fs, "answers", len-35, data, 0);
  197. // fs_add_null(fs, "query_num");
  198. // fs_add_null(fs, "answer_rrs");
  199. // fs_add_null(fs, "authority_rrs");
  200. // fs_add_null(fs, "additional_rrs");
  201. // fs_add_null(fs, "answers");
  202. // free(char2);
  203. // free(query_num);
  204. // free(answer_rrs);
  205. // free(authority_rrs);
  206. // free(additional_rrs);
  207. // free(data);
  208. }else{
  209. fs_add_string(fs, "classification", (char*) "dns_mx", 0);
  210. fs_add_uint64(fs, "success", 0);
  211. fs_add_uint64(fs, "sport", ntohs(udp->uh_sport));
  212. fs_add_uint64(fs, "dport", ntohs(udp->uh_dport));
  213. fs_add_null(fs, "icmp_responder");
  214. fs_add_null(fs, "icmp_type");
  215. fs_add_null(fs, "icmp_code");
  216. fs_add_null(fs, "icmp_unreach_str");
  217. fs_add_null(fs, "answers");
  218. // fs_add_null(fs, "query_response");
  219. // fs_add_null(fs, "query_num");
  220. // fs_add_null(fs, "answer_rrs");
  221. // fs_add_null(fs, "authority_rrs");
  222. // fs_add_null(fs, "additional_rrs");
  223. // fs_add_null(fs, "answers");
  224. }
  225. }else if (ip_hdr->ip_p == IPPROTO_ICMP){
  226. struct icmp *icmp = (struct icmp *) ((char *) ip_hdr + ip_hdr -> ip_hl *4);
  227. struct ip *ip_inner = (struct ip *) &icmp[1];
  228. fs_modify_string(fs, "saddr", make_ip_str(ip_inner->ip_dst.s_addr), 1);
  229. fs_add_string(fs, "classification", (char*) "icmp-unreach", 0);
  230. fs_add_uint64(fs, "success", 0);
  231. fs_add_null(fs, "sport");
  232. fs_add_null(fs, "dport");
  233. fs_add_string(fs, "icmp_responder", make_ip_str(ip_hdr->ip_src.s_addr), 1);
  234. fs_add_uint64(fs, "icmp_type", icmp->icmp_type);
  235. fs_add_uint64(fs, "icmp_code", icmp->icmp_code);
  236. fs_add_null(fs, "icmp_unreach_str");
  237. // fs_add_null(fs, "query_response");
  238. // fs_add_null(fs, "query_num");
  239. // fs_add_null(fs, "answer_rrs");
  240. // fs_add_null(fs, "authority_rrs");
  241. // fs_add_null(fs, "additional_rrs");
  242. fs_add_null(fs, "answers");
  243. }else{
  244. fs_add_string(fs, "classification", (char*) "other", 0);
  245. fs_add_uint64(fs, "success", 0);
  246. fs_add_null(fs, "sport");
  247. fs_add_null(fs, "dport");
  248. fs_add_null(fs, "icmp_responder");
  249. fs_add_null(fs, "icmp_type");
  250. fs_add_null(fs, "icmp_code");
  251. fs_add_null(fs, "icmp_unreach_str");
  252. // fs_add_null(fs, "query_response");
  253. // fs_add_null(fs, "query_num");
  254. // fs_add_null(fs, "answer_rrs");
  255. // fs_add_null(fs, "authority_rrs");
  256. // fs_add_null(fs, "additional_rrs");
  257. fs_add_null(fs, "answers");
  258. }
  259. }
  260. static fielddef_t fields[] = {
  261. {.name = "classification", .type="string", .desc = "packet classification"},
  262. {.name = "success", .type="int", .desc = "is response considered success"},
  263. {.name = "sport", .type = "int", .desc = "UDP source port"},
  264. {.name = "dport", .type = "int", .desc = "UDP destination port"},
  265. {.name = "icmp_responder", .type = "string", .desc = "Source IP of ICMP_UNREACH message"},
  266. {.name = "icmp_type", .type = "int", .desc = "icmp message type"},
  267. {.name = "icmp_code", .type = "int", .desc = "icmp message sub type code"},
  268. {.name = "icmp_unreach_str", .type = "string", .desc = "for icmp_unreach responses, the string version of icmp_code (e.g. network-unreach)"},
  269. // {.name = "query_response", .type = "string", .desc = "for DNS responses, the response code meaning of dns answer pkt"},
  270. // {.name = "query_num", .type = "int", .desc ="query_num for packet"},
  271. // {.name = "answer_rrs", .type="int", .desc = "number of answer_rrs for packet"},
  272. // {.name = "authority_rrs", .type="int", .desc = "number of authority_rrs"},
  273. // {.name = "additional_rrs", .type="int", .desc = "number of additional_rrs"},
  274. {.name = "answers", .type="binary", .desc = "answers in binary format"},
  275. };
  276. probe_module_t module_dns_mx = {
  277. .name = "dns_mx",
  278. .packet_length = 1,
  279. .pcap_filter = "udp || icmp",
  280. .pcap_snaplen = 1500, // TO BE CHANGED FOR EXSTIMATE REFLECTION SIZE
  281. .port_args = 1,
  282. .thread_initialize = &dns_init_perthread,
  283. .global_initialize = &udp_global_initialize,
  284. .make_packet = &udp_make_packet,
  285. .print_packet = &dns_print_packet,
  286. .validate_packet = &dns_validate_packet,
  287. .process_packet = &dns_process_packet,
  288. .close = &udp_global_cleanup,
  289. .fields = fields,
  290. .numfields = sizeof(fields)/sizeof(fields[0])
  291. };
  292. ////this will convert www.google.com to
  293. //void convert_to_dns_mx_name_format(unsigned char* dns,unsigned char* host) {
  294. // int lock = 0;
  295. // strcat((char*)host,".");
  296. // for(int i = 0; i < ((int) strlen((char*)host)); i++) {
  297. // if(host[i]=='.') {
  298. // *dns++=i-lock;
  299. // for(;lock<i;lock++) {
  300. // *dns++=host[lock];
  301. // }
  302. // lock++;
  303. // }
  304. // }
  305. // *dns++ = '\0';
  306. //}