module_tcp_synscan_proberesponse.c.save 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. // probe module for performing TCP SYN scans extended for probe response attacks
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include "../../lib/includes.h"
  16. #include "../fieldset.h"
  17. #include "probe_modules.h"
  18. #include "packet.h"
  19. #include "../../lib/logger.h"
  20. #include <inttypes.h>
  21. #define ICMP_SMALLEST_SIZE 5
  22. #define ICMP_TIMXCEED_UNREACH_HEADER_SIZE
  23. probe_module_t module_tcp_synscan_pra_proberesponse;
  24. static uint32_t num_ports;
  25. int synscan_pra_global_initialize(struct state_conf *state)
  26. {
  27. num_ports = state->source_port_last - state->source_port_first + 1;
  28. return EXIT_SUCCESS;
  29. }
  30. int synscan_pra_init_perthread(void* buf, macaddr_t *src,
  31. macaddr_t *gw, port_h_t dst_port,
  32. __attribute__((unused)) void **arg_ptr)
  33. {
  34. memset(buf, 0, MAX_PACKET_SIZE);
  35. struct ether_header *eth_header = (struct ether_header *) buf;
  36. make_eth_header(eth_header, src, gw);
  37. struct ip *ip_header = (struct ip*)(&eth_header[1]);
  38. uint16_t len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
  39. make_ip_header(ip_header, IPPROTO_TCP, len);
  40. struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]);
  41. make_tcp_header(tcp_header, dst_port);
  42. return EXIT_SUCCESS;
  43. }
  44. int synscan_pra_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
  45. uint32_t *validation, int probe_num, __attribute__((unused)) void *arg)
  46. {
  47. // ipaddr_n_t = uint32_t
  48. struct ether_header *eth_header = (struct ether_header *)buf;
  49. struct ip *ip_header = (struct ip*)(&eth_header[1]);
  50. struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]);
  51. uint32_t tcp_seq = validation[0];
  52. // needed to reach the correct destination
  53. ip_header->ip_dst.s_addr = dst_ip;
  54. uint32_t marker_value = 0;
  55. // how many bits to be used for IP and checksum
  56. int amount_bits_marker_value = zconf.markerbits_ip;
  57. int amount_bits_checksum = zconf.markerbits_checksum;
  58. if(!zconf.use_markervalue) {
  59. // further calculations are based on host byte order
  60. // lib/types.h:typedef uint32_t ipaddr_n_t; // IPv4 address network order
  61. // 1.2.3.4 -> 1.2 (based on amount of bits used)
  62. marker_value = ntohl(dst_ip) >> (32 - amount_bits_marker_value);
  63. }
  64. // take given IP address as marker value instead of destination address (Stage > 1)
  65. else {
  66. marker_value = (uint32_t)zconf.markervalue;
  67. }
  68. //log_debug("encoder", "marker value: 0x%.16" PRIX32, marker_value);
  69. // stores [destination IP][checksum(IP) | 0]
  70. /*
  71. 291 -> b"00000123" (hex) -> b"000123" (marker value: eg 3 bytes)
  72. -> chk = checksum(b"000123" -> b"00012300") = b"abcdefgh"
  73. -> b"000123" (marker value) + b"abcdefgh"[:checksum_len] (marker checksum)
  74. */
  75. uint64_t markervalue_and_checksum = 0;
  76. markervalue_and_checksum |= marker_value;
  77. // encode marker value:
  78. // 0x00000000 00AAAAAA -> 0xAAAAAA00 00000000
  79. markervalue_and_checksum <<= (64 - amount_bits_marker_value);
  80. if (amount_bits_checksum != 0) {
  81. // encode marker checksum:
  82. //log_info("encoder", "encoding checksum bits: %d", amount_bits_checksum);
  83. // 0xAAAAAA00 00000000 -> 0x00000000 AAAAAA00 -> 0xAAAAAA00
  84. uint32_t marker_value_for_checksum = (markervalue_and_checksum >> 32);
  85. //log_debug("encoder", "marker value for checksum: 0x%.8" PRIX32, marker_value_for_checksum);
  86. marker_value_for_checksum = htonl(marker_value_for_checksum);
  87. // checksum is calculated oder 4 bytes (padded from right with 0)
  88. // nw order -> fletcher32 -> host order -> nw order
  89. // return value: we need more space to shift more than 32 Bit (return value of fletcher32 if uint32_t)
  90. uint64_t sum_marker_value = (uint64_t)(fletcher32((uint16_t*)&marker_value_for_checksum, 2) & 0x00000000FFFFFFFF);
  91. //log_debug("encoder", "checksum: 0x%.16" PRIX64, sum_marker_value);
  92. // encode checksum:
  93. // 0x00000000 CCCCCCCC
  94. // 0xAAAAAA00 00000000 -> 0xAAAAAACC CCCCCC00
  95. // Unneeded trailing checksum bits don't need to be removed: just stop reading after x bits
  96. markervalue_and_checksum |= (sum_marker_value << (32 - amount_bits_marker_value));
  97. }
  98. int amount_bits_to_encode = amount_bits_marker_value + amount_bits_checksum;
  99. //printf("total bits to encode : %d\n", amount_bits_to_encode);
  100. //printf("%u\n", markervalue_and_checksum >> 32);
  101. //printf("Value to encode: 0x%.16" PRIX64 "\n", markervalue_and_checksum);
  102. // will be overwritten if marker type is given
  103. ip_header->ip_src.s_addr = src_ip;
  104. // will be overwritten if marker type is given
  105. tcp_header->th_sport = htons(get_src_port(num_ports, probe_num, validation));
  106. int bitpos = 1;
  107. // encoded bits so far
  108. int marker_bitcount = 0;
  109. //log_debug("encoder", "encoding %d bits: 0x%.16" PRIX64, amount_bits_to_encode, markervalue_and_checksum);
  110. // check for every available encoder marker to place destination address (+checksum)
  111. // order of encoding: dport, source ip, sport (source ip should not be equal to destination to avoid filtering)
  112. if (amount_bits_to_encode)
  113. while (bitpos <= 31 && marker_bitcount < amount_bits_to_encode) {
  114. if ((zconf.marker_encoding & bitpos) == 1) {
  115. //log_debug("encoder", "encoding to dport");
  116. if (!zconf.marker_encoding_dst_small) {
  117. // encode into destination port
  118. tcp_header->th_dport = htons((uint16_t)( (markervalue_and_checksum & 0xFFFF000000000000) >> 48) );
  119. } else {
  120. // if marker_encoding_mitigation: assume destination port is the only marker and limited
  121. // to 10 bits: 0->1023 (0000 0000 00 -> 1111 1111 11)
  122. tcp_header->th_dport = htons((uint16_t)( (markervalue_and_checksum & 0xFFC0000000000000) >> (64-10)) );
  123. // just use destination port, skip all other
  124. break;
  125. }
  126. marker_bitcount += 16;
  127. }
  128. else if ((zconf.marker_encoding & bitpos) == 2) {
  129. //log_debug("encoder", "encoding to src ip");
  130. // encode into source address
  131. ip_header->ip_src.s_addr = htonl((uint32_t)( (markervalue_and_checksum & (0xFFFFFFFF00000000 >> marker_bitcount)) >> (32 - marker_bitcount)) );
  132. //printf("source IP: %u\n", ip_header->ip_src.s_addr);
  133. marker_bitcount += 32;
  134. }
  135. else if ((zconf.marker_encoding & bitpos) == 4) {
  136. //log_debug("encoder", "encoding to sport");
  137. // encode into source port
  138. tcp_header->th_sport = htons((uint16_t)( (markervalue_and_checksum & (0xFFFF000000000000 >> marker_bitcount)) >> (48 - marker_bitcount)) );
  139. marker_bitcount += 16;
  140. }
  141. /*
  142. else if ((zconf.marker_encoding & bitpos) == 8) {
  143. }
  144. else if ((zconf.marker_encoding & bitpos) == 16) {
  145. }
  146. */
  147. bitpos <<= 1;
  148. }
  149. tcp_header->th_seq = tcp_seq;
  150. tcp_header->th_sum = 0;
  151. tcp_header->th_sum = tcp_checksum(sizeof(struct tcphdr),
  152. ip_header->ip_src.s_addr, ip_header->ip_dst.s_addr, tcp_header);
  153. ip_header->ip_sum = 0;
  154. ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);
  155. return EXIT_SUCCESS;
  156. }
  157. void synscan_pra_print_packet(FILE *fp, void* packet)
  158. {
  159. struct ether_header *ethh = (struct ether_header *) packet;
  160. struct ip *iph = (struct ip *) &ethh[1];
  161. struct tcphdr *tcph = (struct tcphdr *) &iph[1];
  162. fprintf(fp, "tcp { source: %u | dest: %u | seq: %u | checksum: %#04X }\n",
  163. ntohs(tcph->th_sport),
  164. ntohs(tcph->th_dport),
  165. ntohl(tcph->th_seq),
  166. ntohs(tcph->th_sum));
  167. fprintf_ip_header(fp, iph);
  168. fprintf_eth_header(fp, ethh);
  169. fprintf(fp, "------------------------------------------------------\n");
  170. }
  171. // scanner feedback is meant for first stage only
  172. int synscan_pra_validate_packet(const struct ip *ip_hdr, uint32_t len,
  173. __attribute__((unused))uint32_t *src_ip,
  174. uint32_t *validation)
  175. {
  176. //log_debug("decoder", "got feedback packet");
  177. if (zconf.use_markervalue) {
  178. //log_debug("decoder", "not collecting feedback: using marker value");
  179. return 0;
  180. }
  181. if ((zconf.marker_encoding & 2) == 2) {
  182. //log_debug("decoder", "not collecting feedback: spoofed address");
  183. // we won't get any answers for spoofed addresses -> only check in case of modes 1, 4, 5 (1+4)
  184. return 0;
  185. }
  186. if ((4*ip_hdr->ip_hl + sizeof(struct tcphdr)) > len) {
  187. //log_debug("decoder", "not collecting feedback: packet to large");
  188. // buffer not large enough to contain expected tcp header
  189. return 0;
  190. }
  191. // arriving packet
  192. uint16_t sport_remote = 0;
  193. uint16_t dport_remote = 0;
  194. uint32_t source_ip_remote = 0;
  195. uint32_t ack_remote = 0;
  196. // sent packet
  197. uint16_t sport_local = 0;
  198. //uint16_t sport_local_viamarker = 0;
  199. uint16_t dport_local = htons((uint16_t)zconf.target_port);
  200. if (ip_hdr->ip_p == IPPROTO_TCP) {
  201. //log_debug("decoder", "got TCP feedback");
  202. struct tcphdr *tcp = (struct tcphdr*)((char *) ip_hdr + 4*ip_hdr->ip_hl);
  203. sport_remote = tcp->th_sport;
  204. dport_remote = tcp->th_dport;
  205. ack_remote = tcp->th_ack;
  206. source_ip_remote = ip_hdr->ip_src.s_addr;
  207. }
  208. // in case of ICMP responses: extract IP and TCP structure
  209. else if (ip_hdr->ip_p == IPPROTO_ICMP) {
  210. //log_debug("decoder", "got ICMP feedback");
  211. if (((uint32_t) 4 * ip_hdr->ip_hl + ICMP_SMALLEST_SIZE) > len) {
  212. // buffer not large enough to contain expected icmp header
  213. //log_debug("decoder", "buffer not large enough to contain expected icmp header");
  214. return 0;
  215. }
  216. struct icmp *icmp_hdr = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  217. // for some packet types we must look inside the payload
  218. // TODO: add redirect?
  219. if (icmp_hdr->icmp_type == ICMP_TIMXCEED || icmp_hdr->icmp_type == ICMP_UNREACH) {
  220. //log_debug("decoder", "icmp type: %d, code: %d, checksum: %d",
  221. // icmp_hdr->icmp_type,
  222. // icmp_hdr->icmp_code,
  223. // ntohs(icmp_hdr->icmp_cksum));
  224. // get inner IP by skipping 4 bytes ICMP header, increment by unit size did not work
  225. struct ip *ip_inner_icmp = (struct ip *)(((char*)icmp_hdr) + 8);
  226. //log_debug("decoder", "ip hl in icmp: %d", ip_inner_icmp->ip_hl);
  227. struct tcphdr *tcp_inner_icmp = (struct tcphdr*)((char *) ip_inner_icmp + 4*ip_inner_icmp->ip_hl);
  228. // replace port and adress information
  229. // as we got the original message back we need to take the reverse order (src => dst, dst => srs)
  230. sport_remote = tcp_inner_icmp->th_dport;
  231. dport_remote = tcp_inner_icmp->th_sport;
  232. source_ip_remote = ip_inner_icmp->ip_dst.s_addr;
  233. // Avoid filtering duplicates on ICMP repsonses
  234. // ZMap sets source (remote) IP itself to filter out duplicates (see src/recv.c -> handle_packet())
  235. // ICMP responses can arrive from same source but "belong" to different targets -> replace source ip with
  236. // destination IP extracted from ICMP payload (the real intended target)
  237. //
  238. *src_ip = source_ip_remote;
  239. //log_debug("decoder", "extracted ICMP data: remote port source <-> dst: %d <-> %d",
  240. // ntohs(sport_remote),
  241. // ntohs(dport_remote));
  242. }
  243. else {
  244. //log_debug("decoder", "icmp message type was not checkable (other than ICMP_TIMXCEED or ICMP_UNREACH): %d",
  245. // icmp_hdr->icmp_type);
  246. return 0;
  247. }
  248. }
  249. else {
  250. // validation is based on IP, TCP and ICMP -> ignore all other
  251. //log_debug("decoder", "can't validate this protocol: %d", ip_hdr->ip_p);
  252. return 0;
  253. }
  254. uint16_t bitpos = 1;
  255. uint16_t marker_bitcount = 0;
  256. // extract source/destination port from source address (attack target)
  257. // destination IP = [[dport]...[sport]] -> sport (and dport) could be ommitted
  258. // we had at maximum markerbits_ip Bits to encode sport abd dport
  259. if (zconf.markerbits_ip) {
  260. uint32_t source_ip_remote_ho = ntohl(source_ip_remote);
  261. // remove not encoded bits if any
  262. int bits_to_remove = 32 - zconf.markerbits_ip;
  263. source_ip_remote_ho = (source_ip_remote_ho >> bits_to_remove) << bits_to_remove;
  264. while (bitpos <= 31 && marker_bitcount < zconf.markerbits_ip) {
  265. if ((zconf.marker_encoding & bitpos) == 1) {
  266. //log_debug("decoder", "decoding dport");
  267. if (!zconf.marker_encoding_dst_small) {
  268. // assume full destination port was encoded
  269. dport_local = (source_ip_remote_ho & 0xFFFF0000) >> 16;
  270. //log_debug("decoder", "decoded dport: %d", dport_local);
  271. marker_bitcount += 16;
  272. } else {
  273. //log_debug("decoder", "small encoding");
  274. // if marker encoding mitigation: assume destination port is the only marker (see encoding)
  275. dport_local = (source_ip_remote_ho & 0xFFC00000) >> (32-10);
  276. // just destination port, skip all other
  277. break;
  278. }
  279. dport_local = htons(dport_local);
  280. }
  281. else if ((zconf.marker_encoding & bitpos) == 4) {
  282. //log_debug("decoder", "decoding sport");
  283. sport_local = (source_ip_remote_ho & (0xFFFF0000 >> marker_bitcount)) >> (16 - marker_bitcount);
  284. //log_debug("decoder", "decoded sport: %d", sport_local);
  285. // [dst][src] -> [src][dst]: remove checksum bits from remote dst
  286. uint32_t dport_remote_ho = ntohs(dport_remote);
  287. dport_remote_ho = (dport_remote_ho >> zconf.markerbits_checksum) << zconf.markerbits_checksum;
  288. dport_remote = htons(dport_remote_ho);
  289. //log_debug("decoder", "changed remote dport to: %d", dport_remote_ho);
  290. marker_bitcount += 16;
  291. //sport_local_viamarker = 1;
  292. sport_local = htons(sport_local);
  293. }
  294. /*
  295. else if ((zconf.marker_encoding & bitpos) == 8) {
  296. }
  297. else if ((zconf.marker_encoding & bitpos) == 16) {
  298. }
  299. */
  300. bitpos <<= 1;
  301. }
  302. }
  303. //log_debug("decoder", "local src/dst -> remote src/dst: %d %d -> %d %d", ntohs(sport_local), ntohs(dport_local), ntohs(sport_remote), ntohs(dport_remote));
  304. // validate remote source port. Check via "&" as source port could be only partially encoded
  305. if ((dport_local & sport_remote) != dport_local) {
  306. //log_debug("decoder", "ports did not match: local dst != remote src -> %d != %d", ntohs(dport_local), ntohs(sport_remote));
  307. return 0;
  308. }
  309. // validate remote destination port: extracted via arriving IP address or local config
  310. //if ((sport_local_viamarker && (sport_local != dport_remote)) || !check_dst_port(ntohs(dport_remote), num_ports, validation)) {
  311. if (sport_local != dport_remote) {
  312. /*
  313. log_debug("decoder", "ports did not match: local src != remote dst -> %d != %d (via marker? %d)",
  314. ntohs(sport_local),
  315. ntohs(dport_remote),
  316. sport_local_viamarker);
  317. */
  318. return 0;
  319. }
  320. // validate tcp acknowledgement number
  321. if (ack_remote != 0 && ( htonl(ack_remote) != htonl(validation[0])+1 ) ) {
  322. //log_debug("decoder", "sequence number did not match");
  323. return 0;
  324. }
  325. //log_debug("decoder", "This was an answer packet!!!");
  326. return 1;
  327. }
  328. void synscan_pra_process_packet(const u_char *packet,
  329. __attribute__((unused)) uint32_t len, fieldset_t *fs)
  330. {
  331. //log_debug("encoder", "writing feedback packet to file");
  332. // Idea: how to correlate reponse <-> marker group? This has to be done by comparing
  333. // response IP addresses to groups.
  334. struct ip *ip_hdr = (struct ip *)&packet[sizeof(struct ether_header)];
  335. // WARNING: fields have to be written in the same order as defined in fields[]
  336. // That is the reason why this code looks a bit redundant
  337. if (ip_hdr->ip_p == IPPROTO_TCP) {
  338. //log_debug("encoder", "storing TCP response");
  339. struct tcphdr *tcp = (struct tcphdr*)((char *)ip_hdr + 4*ip_hdr->ip_hl);
  340. fs_add_uint64(fs, "sport", (uint64_t) ntohs(tcp->th_sport));
  341. fs_add_uint64(fs, "dport", (uint64_t) ntohs(tcp->th_dport));
  342. fs_add_uint64(fs, "seqnum", (uint64_t) ntohl(tcp->th_seq));
  343. fs_add_uint64(fs, "acknum", (uint64_t) ntohl(tcp->th_ack));
  344. fs_add_uint64(fs, "window", (uint64_t) ntohs(tcp->th_win));
  345. fs_add_string(fs, "daddr_inner_icmp", (char*) "(None)", 0);
  346. if (tcp->th_flags & TH_RST) { // RST packet
  347. fs_add_string(fs, "classification", (char*) "rst", 0);
  348. fs_add_uint64(fs, "success", 0);
  349. } else { // SYNACK packet
  350. fs_add_string(fs, "classification", (char*) "synack", 0);
  351. fs_add_uint64(fs, "success", 1);
  352. }
  353. }
  354. else {
  355. // prefiltering was already done in synscan_pra_validate_packet()
  356. //log_debug("encoder", "storing ICMP response");
  357. struct icmp *icmp_hdr = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  358. struct ip *ip_hdr_from_icmp = (struct ip *)(((char * )icmp_hdr) + 8);
  359. struct tcphdr *tcp = (struct tcphdr*)((char *)ip_hdr_from_icmp + 4*ip_hdr_from_icmp->ip_hl);
  360. // switch source/destination port: source is always the attacker
  361. fs_add_uint64(fs, "sport", (uint64_t) ntohs(tcp->th_dport));
  362. fs_add_uint64(fs, "dport", (uint64_t) ntohs(tcp->th_sport));
  363. fs_add_uint64(fs, "seqnum", (uint64_t) 0);
  364. fs_add_uint64(fs, "acknum", (uint64_t) 0);
  365. fs_add_uint64(fs, "window", (uint64_t) 0);
  366. fs_add_string(fs, "daddr_inner_icmp", make_ip_str(ip_hdr_from_icmp->ip_dst.s_addr), 0);
  367. if (icmp_hdr->icmp_type == ICMP_UNREACH)
  368. fs_add_string(fs, "classification", (char*) "icmp_unreach", 0);
  369. else if(icmp_hdr->icmp_type == ICMP_TIMXCEED)
  370. fs_add_string(fs, "classification", (char*) "icmp_timeexceed", 0);
  371. else
  372. fs_add_string(fs, "classification", (char*) "icmp_other", 0);
  373. fs_add_uint64(fs, "success", 1);
  374. }
  375. }
  376. static fielddef_t fields[] = {
  377. {.name = "sport", .type = "int", .desc = "TCP source port"},
  378. {.name = "dport", .type = "int", .desc = "TCP destination port"},
  379. {.name = "seqnum", .type = "int", .desc = "TCP sequence number"},
  380. {.name = "acknum", .type = "int", .desc = "TCP acknowledgement number"},
  381. {.name = "window", .type = "int", .desc = "TCP window"},
  382. {.name = "daddr_inner_icmp", .type="string", .desc = "Destination address of IP header contained in ICMP response"},
  383. {.name = "classification", .type="string", .desc = "packet classification"},
  384. {.name = "success", .type="int", .desc = "is response considered success"}
  385. };
  386. probe_module_t module_tcp_synscan_proberesponse = {
  387. .name = "tcp_synscan_pra",
  388. .packet_length = 54,
  389. //.packet_length = 82,
  390. .pcap_filter = "icmp || (tcp && tcp[13] & 4 != 0 || tcp[13] == 18)",
  391. .pcap_snaplen = 96,
  392. .port_args = 1,
  393. .global_initialize = &synscan_pra_global_initialize,
  394. .thread_initialize = &synscan_pra_init_perthread,
  395. .make_packet = &synscan_pra_make_packet,
  396. .print_packet = &synscan_pra_print_packet,
  397. .process_packet = &synscan_pra_process_packet,
  398. .validate_packet = &synscan_pra_validate_packet,
  399. .close = NULL,
  400. .helptext = "Extended TCP probe module for probe response attacks. ",
  401. .fields = fields,
  402. .numfields = 8};