module_ntp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #include "../../lib/includes.h"
  15. #include "probe_modules.h"
  16. #include "module_udp.h"
  17. #include "module_ntp.h"
  18. #include "packet.h"
  19. #include "logger.h"
  20. #define MAX_NTP_PAYLOAD_LEN 1472
  21. #define ICMP_UNREACH_HEADER_SIZE 8
  22. #define UNUSED __attribute__((unused))
  23. probe_module_t module_ntp;
  24. static int num_ports;
  25. int ntp_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
  26. uint32_t *validation, int probe_num)
  27. {
  28. struct ether_header *eth_header = (struct ether_header *) buf;
  29. struct ip *ip_header = (struct ip*) (&eth_header[1]);
  30. struct udphdr *udp_header = (struct udphdr *) &ip_header[1];
  31. struct ntphdr *ntp = (struct ntphdr *) &udp_header[1];
  32. ip_header->ip_src.s_addr = src_ip;
  33. ip_header->ip_dst.s_addr = dst_ip;
  34. udp_header->uh_sport = htons(get_src_port(num_ports, probe_num, validation));
  35. ip_header->ip_sum = 0;
  36. ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);
  37. //Sets LI_VN_MODE to 11100011
  38. //Sends a packet with NTP version 4 and as a client (to a server)
  39. ntp->LI_VN_MODE = 227;
  40. return EXIT_SUCCESS;
  41. }
  42. int ntp_validate_packet(const struct ip *ip_hdr, uint32_t len,
  43. uint32_t *src_ip, uint32_t *validation)
  44. {
  45. if (!udp_validate_packet(ip_hdr, len, src_ip, validation)) {
  46. return 0;
  47. }
  48. if (ip_hdr->ip_p == IPPROTO_UDP) {
  49. struct udphdr *udp = (struct udphdr *) ((char *) ip_hdr + ip_hdr->ip_hl * 4);
  50. uint16_t sport = ntohs(udp->uh_sport);
  51. if (sport != zconf.target_port) {
  52. return 0;
  53. }
  54. }
  55. return 1;
  56. }
  57. void ntp_process_packet(const u_char *packet,
  58. __attribute__((unused)) uint32_t len, fieldset_t *fs)
  59. {
  60. struct ip *ip_hdr = (struct ip*) &packet[sizeof(struct ether_header)];
  61. uint64_t temp64;
  62. uint8_t temp8;
  63. uint32_t temp32;
  64. if (ip_hdr->ip_p == IPPROTO_UDP){
  65. struct udphdr *udp = (struct udphdr*) ((char *) ip_hdr + ip_hdr->ip_hl * 4);
  66. uint8_t *ptr = (uint8_t*) &udp[1];
  67. fs_add_string(fs, "classification", (char*) "ntp", 0);
  68. fs_add_uint64(fs, "success", 1);
  69. fs_add_uint64(fs, "sport", ntohs(udp->uh_sport));
  70. fs_add_uint64(fs, "dport", ntohs(udp->uh_dport));
  71. fs_add_null(fs, "icmp_responder");
  72. fs_add_null(fs, "icmp_type");
  73. fs_add_null(fs, "icmp_code");
  74. fs_add_null(fs, "icmp_unreach_str");
  75. if (len > 90) {
  76. temp8 = *((uint8_t *)ptr);
  77. fs_add_uint64(fs, "LI_VN_MODE", temp8);
  78. temp8 = *((uint8_t *)ptr +1);
  79. fs_add_uint64(fs, "stratum", temp8);
  80. temp8 = *((uint8_t *)ptr +2);
  81. fs_add_uint64(fs, "poll", temp8);
  82. temp8 = *((uint8_t *)ptr + 3);
  83. fs_add_uint64(fs, "precision", temp8);
  84. temp32 = *((uint32_t *)ptr + 4);
  85. fs_add_uint64(fs, "root_delay", temp32);
  86. temp32 = *((uint32_t *)ptr + 8);
  87. fs_add_uint64(fs, "root_dispersion", temp32);
  88. temp32 = *((uint32_t *)ptr + 12);
  89. fs_add_uint64(fs, "reference_clock_identifier", temp32);
  90. temp64 = *((uint64_t *)ptr + 16);
  91. fs_add_uint64(fs, "reference_timestamp", temp64);
  92. temp64 = *((uint64_t *)ptr + 24);
  93. fs_add_uint64(fs, "originate_timestap", temp64);
  94. temp64 = *((uint64_t *)ptr + 32);
  95. fs_add_uint64(fs, "receive_timestamp", temp64);
  96. temp64 = *((uint64_t *)ptr + 39);
  97. fs_add_uint64(fs, "transmit_timestamp", temp64);
  98. } else {
  99. fs_add_null(fs, "LI_VN_MODE");
  100. fs_add_null(fs, "stratum");
  101. fs_add_null(fs, "poll");
  102. fs_add_null(fs, "precision");
  103. fs_add_null(fs, "root_delay");
  104. fs_add_null(fs, "root_dispersion");
  105. fs_add_null(fs, "reference_clock_identifier");
  106. fs_add_null(fs, "reference_timestamp");
  107. fs_add_null(fs, "originate_timestamp");
  108. fs_add_null(fs, "receive_timestamp");
  109. fs_add_null(fs, "transmit_timestamp");
  110. }
  111. } else if (ip_hdr->ip_p == IPPROTO_ICMP) {
  112. struct icmp *icmp = (struct icmp *) ((char *) ip_hdr + ip_hdr -> ip_hl * 4);
  113. struct ip *ip_inner = (struct ip *) ((char*) icmp + ICMP_UNREACH_HEADER_SIZE);
  114. fs_modify_string(fs, "saddr", make_ip_str(ip_inner->ip_dst.s_addr), 1);
  115. fs_add_string(fs, "classification", (char*) "icmp-unreach", 0);
  116. fs_add_uint64(fs, "success", 0);
  117. fs_add_null(fs, "sport");
  118. fs_add_null(fs, "dport");
  119. fs_add_string(fs, "icmp_responder", make_ip_str(ip_hdr->ip_src.s_addr), 1);
  120. fs_add_uint64(fs, "icmp_type", icmp->icmp_type);
  121. fs_add_uint64(fs, "icmp_code", icmp->icmp_code);
  122. fs_add_null(fs, "icmp_unreach_str");
  123. fs_add_null(fs, "LI_VN_MODE");
  124. fs_add_null(fs, "stratum");
  125. fs_add_null(fs, "poll");
  126. fs_add_null(fs, "precision");
  127. fs_add_null(fs, "root_delay");
  128. fs_add_null(fs, "root_dispersion");
  129. fs_add_null(fs, "reference_clock_identifier");
  130. fs_add_null(fs, "reference_timestamp");
  131. fs_add_null(fs, "originate_timestap");
  132. fs_add_null(fs, "receive_timestamp");
  133. fs_add_null(fs, "transmit_timestamp");
  134. } else {
  135. fs_add_string(fs, "classification", (char *) "other", 0);
  136. fs_add_uint64(fs, "success", 0);
  137. fs_add_null(fs, "sport");
  138. fs_add_null(fs, "dport");
  139. fs_add_null(fs, "icmp_responder");
  140. fs_add_null(fs, "icmp_type");
  141. fs_add_null(fs, "icmp_code");
  142. fs_add_null(fs, "icmp_unreach_str");
  143. fs_add_null(fs, "LI_VN_MODE");
  144. fs_add_null(fs, "stratum");
  145. fs_add_null(fs, "poll");
  146. fs_add_null(fs, "precision");
  147. fs_add_null(fs, "root_delay");
  148. fs_add_null(fs, "root_dispersion");
  149. fs_add_null(fs, "reference_clock_identifier");
  150. fs_add_null(fs, "reference_timestamp");
  151. fs_add_null(fs, "originate_timestap");
  152. fs_add_null(fs, "receive_timestamp");
  153. fs_add_null(fs, "transmit_timestamp");
  154. }
  155. }
  156. int ntp_init_perthread(void *buf, macaddr_t *src,
  157. macaddr_t *gw, __attribute__((unused)) port_h_t dst_port,
  158. void **arg)
  159. {
  160. memset(buf, 0, MAX_PACKET_SIZE);
  161. struct ether_header *eth_header = (struct ether_header *) buf;
  162. make_eth_header(eth_header, src, gw);
  163. struct ip *ip_header = (struct ip*)(&eth_header[1]);
  164. uint16_t len = htons(sizeof(struct ip) + sizeof(struct udphdr) + sizeof(struct ntphdr));
  165. make_ip_header(ip_header, IPPROTO_UDP, len);
  166. struct udphdr *udp_header = (struct udphdr*)(&ip_header[1]);
  167. struct ntphdr *ntp_header = (struct ntphdr*)(&udp_header[1]);
  168. ntp_header -> LI_VN_MODE = 227;
  169. len = sizeof(struct udphdr) + sizeof(struct ntphdr);
  170. make_udp_header(udp_header, zconf.target_port, len);
  171. char* payload = (char*)(&ntp_header[1]);
  172. module_ntp.packet_length = sizeof(struct ether_header) + sizeof(struct ip)
  173. + sizeof(struct udphdr) + sizeof(struct ntphdr);
  174. assert(module_ntp.packet_length <= MAX_PACKET_SIZE);
  175. memcpy(payload, ntp_header, module_ntp.packet_length);
  176. uint32_t seed = aesrand_getword(zconf.aes);
  177. aesrand_t *aes = aesrand_init_from_seed(seed);
  178. *arg = aes;
  179. return EXIT_SUCCESS;
  180. }
  181. void ntp_print_packet(FILE *fp, void *packet){
  182. struct ether_header *ethh = (struct ether_header *)packet;
  183. struct ip *iph = (struct ip *) &ethh[1];
  184. struct udphdr *udph = (struct udphdr *) (iph + 4*iph->ip_hl);
  185. struct ntphdr *ntph = (struct ntphdr *) &udph[1];
  186. fprintf(fp, "ntp { LI_VN_MODE: %u | stratum: %u | poll: %u }\n",
  187. ntph->LI_VN_MODE, ntph->stratum, ntph->poll);
  188. fprintf(fp, "udp { source: %u | dest: %u | checksum: %#04X }\n",
  189. ntohs(udph->uh_sport),
  190. ntohs(udph->uh_dport),
  191. ntohs(udph->uh_sum));
  192. fprintf_ip_header(fp, iph);
  193. fprintf_eth_header(fp, ethh);
  194. fprintf(fp, "-------------------------------------------------\n");
  195. }
  196. static fielddef_t fields[] = {
  197. {.name = "classification", .type = "string", .desc = "packet classification"},
  198. {.name = "success", .type = "int", .desc = "is response considered success"},
  199. {.name = "sport", .type = "int", .desc = "UDP source port"},
  200. {.name = "dport", .type = "int", .desc = "UDP destination port"},
  201. {.name = "icmp_responder", .type = "string", .desc = "Source IP of ICMP_UNREACH messages"},
  202. {.name = "icmp_type", .type = "int", .desc = "icmp message type"},
  203. {.name = "icmp_code", .type = "int", .desc = "icmp message sub type code"},
  204. {.name = "icmp_unreach_str", .type = "string", .desc = "for icmp_unreach responses, the string version of icmp_code "},
  205. {.name = "LI_VN_MODE", .type = "int", .desc = "leap indication, version number, mode"},
  206. {.name = "stratum", .type = "int", .desc = "stratum"},
  207. {.name = "poll", .type ="int", .desc = "poll"},
  208. {.name = "precision", .type = "int", .desc = "precision"},
  209. {.name = "root_delay", .type = "int", .desc = "root delay"},
  210. {.name = "root_dispersion", .type = "int", .desc = "root dispersion"},
  211. {.name = "reference_clock_identifier", .type = "int", .desc = "code identifying clock reference"},
  212. {.name = "reference_timestamp", .type = "int", .desc = "local time at which local clock was last set or corrected"},
  213. {.name = "originate_timestamp", .type = "int", .desc = "local time at which request deparated client for service"},
  214. {.name = "receive_timestamp", .type = "int", .desc = "local time at which request arrvied at service host"},
  215. {.name = "transmit_timestamp", .type = "int", .desc = "local time which reply departed service host for client"},
  216. };
  217. probe_module_t module_ntp = {
  218. .name = "ntp",
  219. .packet_length = 1,
  220. .pcap_filter = "udp || icmp",
  221. .pcap_snaplen = 1500,
  222. .port_args = 1,
  223. .thread_initialize = &ntp_init_perthread,
  224. .global_initialize = &udp_global_initialize,
  225. .make_packet = &udp_make_packet,
  226. .print_packet = &ntp_print_packet,
  227. .validate_packet = &ntp_validate_packet,
  228. .process_packet = &ntp_process_packet,
  229. .close = &udp_global_cleanup,
  230. .fields = fields,
  231. .numfields = sizeof(fields)/sizeof(fields[0])
  232. };