module_icmp_echo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 ICMP echo request (ping) scans
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include "../../lib/includes.h"
  16. #include "probe_modules.h"
  17. #include "../fieldset.h"
  18. #include "packet.h"
  19. #include "validate.h"
  20. #define ICMP_SMALLEST_SIZE 5
  21. #define ICMP_TIMXCEED_UNREACH_HEADER_SIZE 8
  22. probe_module_t module_icmp_echo;
  23. static int icmp_echo_init_perthread(void* buf, macaddr_t *src,
  24. macaddr_t *gw, __attribute__((unused)) port_h_t dst_port,
  25. __attribute__((unused)) void **arg_ptr)
  26. {
  27. memset(buf, 0, MAX_PACKET_SIZE);
  28. struct ether_header *eth_header = (struct ether_header *) buf;
  29. make_eth_header(eth_header, src, gw);
  30. struct ip *ip_header = (struct ip *) (&eth_header[1]);
  31. uint16_t len = htons(sizeof(struct ip) + sizeof(struct icmp) - 8);
  32. make_ip_header(ip_header, IPPROTO_ICMP, len);
  33. struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
  34. make_icmp_header(icmp_header);
  35. return EXIT_SUCCESS;
  36. }
  37. static int icmp_echo_make_packet(void *buf, ipaddr_n_t src_ip, ipaddr_n_t dst_ip,
  38. uint32_t *validation, __attribute__((unused)) int probe_num,
  39. __attribute__((unused)) void *arg)
  40. {
  41. struct ether_header *eth_header = (struct ether_header *) buf;
  42. struct ip *ip_header = (struct ip *)(&eth_header[1]);
  43. struct icmp *icmp_header = (struct icmp*)(&ip_header[1]);
  44. uint16_t icmp_idnum = validation[2] & 0xFFFF;
  45. ip_header->ip_src.s_addr = src_ip;
  46. ip_header->ip_dst.s_addr = dst_ip;
  47. icmp_header->icmp_id = icmp_idnum;
  48. icmp_header->icmp_cksum = 0;
  49. icmp_header->icmp_cksum = icmp_checksum((unsigned short *) icmp_header);
  50. ip_header->ip_sum = 0;
  51. ip_header->ip_sum = zmap_ip_checksum((unsigned short *) ip_header);
  52. return EXIT_SUCCESS;
  53. }
  54. static void icmp_echo_print_packet(FILE *fp, void* packet)
  55. {
  56. struct ether_header *ethh = (struct ether_header *) packet;
  57. struct ip *iph = (struct ip *) &ethh[1];
  58. struct icmp *icmp_header = (struct icmp*) (&iph[1]);
  59. fprintf(fp, "icmp { type: %u | code: %u "
  60. "| checksum: %#04X | id: %u | seq: %u }\n",
  61. icmp_header->icmp_type,
  62. icmp_header->icmp_code,
  63. ntohs(icmp_header->icmp_cksum),
  64. ntohs(icmp_header->icmp_id),
  65. ntohs(icmp_header->icmp_seq));
  66. fprintf_ip_header(fp, iph);
  67. fprintf_eth_header(fp, ethh);
  68. fprintf(fp, "------------------------------------------------------\n");
  69. }
  70. static int icmp_validate_packet(const struct ip *ip_hdr,
  71. uint32_t len, uint32_t *src_ip, uint32_t *validation)
  72. {
  73. if (ip_hdr->ip_p != IPPROTO_ICMP) {
  74. return 0;
  75. }
  76. if (((uint32_t) 4 * ip_hdr->ip_hl + ICMP_SMALLEST_SIZE) > len) {
  77. // buffer not large enough to contain expected icmp header
  78. return 0;
  79. }
  80. struct icmp *icmp_h = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  81. uint16_t icmp_idnum = icmp_h->icmp_id;
  82. // ICMP validation is tricky: for some packet types, we must look inside
  83. // the payload
  84. if (icmp_h->icmp_type == ICMP_TIMXCEED || icmp_h->icmp_type == ICMP_UNREACH) {
  85. // Should have 16B TimeExceeded/Dest_Unreachable header + original IP header
  86. // + 1st 8B of original ICMP frame
  87. if ((4*ip_hdr->ip_hl + ICMP_TIMXCEED_UNREACH_HEADER_SIZE +
  88. sizeof(struct ip)) > len) {
  89. return 0;
  90. }
  91. struct ip *ip_inner = (struct ip *)(icmp_h + 1);
  92. if (((uint32_t) 4 * ip_hdr->ip_hl + ICMP_TIMXCEED_UNREACH_HEADER_SIZE +
  93. 4*ip_inner->ip_hl + 8 /*1st 8 bytes of original*/ ) > len) {
  94. return 0;
  95. }
  96. struct icmp *icmp_inner = (struct icmp *)((char *) ip_inner + 4*ip_hdr->ip_hl);
  97. // Regenerate validation and icmp id based off inner payload
  98. icmp_idnum = icmp_inner->icmp_id;
  99. *src_ip = ip_inner->ip_dst.s_addr;
  100. validate_gen(ip_hdr->ip_dst.s_addr, ip_inner->ip_dst.s_addr,
  101. (uint8_t *) validation);
  102. }
  103. // validate icmp id
  104. if (icmp_idnum != (validation[2] & 0xFFFF)) {
  105. return 0;
  106. }
  107. return 1;
  108. }
  109. static void icmp_echo_process_packet(const u_char *packet,
  110. __attribute__((unused)) uint32_t len, fieldset_t *fs)
  111. {
  112. struct ip *ip_hdr = (struct ip *) &packet[sizeof(struct ether_header)];
  113. struct icmp *icmp_hdr = (struct icmp *) ((char *) ip_hdr + 4*ip_hdr->ip_hl);
  114. fs_add_uint64(fs, "type", icmp_hdr->icmp_type);
  115. fs_add_uint64(fs, "code", icmp_hdr->icmp_code);
  116. fs_add_uint64(fs, "icmp-id", ntohs(icmp_hdr->icmp_id));
  117. fs_add_uint64(fs, "seq", ntohs(icmp_hdr->icmp_seq));
  118. switch (icmp_hdr->icmp_type) {
  119. case ICMP_ECHOREPLY:
  120. fs_add_string(fs, "classification", (char*) "echoreply", 0);
  121. fs_add_uint64(fs, "success", 1);
  122. break;
  123. case ICMP_UNREACH:
  124. fs_add_string(fs, "classification", (char*) "unreach", 0);
  125. fs_add_uint64(fs, "success", 0);
  126. break;
  127. case ICMP_SOURCEQUENCH:
  128. fs_add_string(fs, "classification", (char*) "sourcequench", 0);
  129. fs_add_uint64(fs, "success", 0);
  130. break;
  131. case ICMP_REDIRECT:
  132. fs_add_string(fs, "classification", (char*) "redirect", 0);
  133. fs_add_uint64(fs, "success", 0);
  134. break;
  135. case ICMP_TIMXCEED:
  136. fs_add_string(fs, "classification", (char*) "timxceed", 0);
  137. fs_add_uint64(fs, "success", 0);
  138. break;
  139. default:
  140. fs_add_string(fs, "classification", (char*) "other", 0);
  141. fs_add_uint64(fs, "success", 0);
  142. break;
  143. }
  144. }
  145. static fielddef_t fields[] = {
  146. {.name="type", .type="int", .desc="icmp message type"},
  147. {.name="code", .type="int", .desc="icmp message sub type code"},
  148. {.name="icmp-id", .type="int", .desc="icmp id number"},
  149. {.name="seq", .type="int", .desc="icmp sequence number"},
  150. {.name="classification", .type="string", .desc="probe module classification"},
  151. {.name="success", .type="int", .desc="did probe module classify response as success"}
  152. };
  153. probe_module_t module_icmp_echo = {
  154. .name = "icmp_echoscan",
  155. .packet_length = 62,
  156. .pcap_filter = "icmp and icmp[0]!=8",
  157. .pcap_snaplen = 96,
  158. .port_args = 0,
  159. .thread_initialize = &icmp_echo_init_perthread,
  160. .make_packet = &icmp_echo_make_packet,
  161. .print_packet = &icmp_echo_print_packet,
  162. .process_packet = &icmp_echo_process_packet,
  163. .validate_packet = &icmp_validate_packet,
  164. .close = NULL,
  165. .fields = fields,
  166. .numfields = 6};