summary.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "summary.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include "../lib/includes.h"
  16. #include "../lib/logger.h"
  17. #include "../lib/blacklist.h"
  18. #include "state.h"
  19. #include "probe_modules/probe_modules.h"
  20. #include "output_modules/output_modules.h"
  21. #define STRTIME_LEN 1024
  22. #ifdef JSON
  23. #include <json.h>
  24. void json_metadata(FILE *file)
  25. {
  26. char send_start_time[STRTIME_LEN+1];
  27. assert(dstrftime(send_start_time, STRTIME_LEN, "%Y-%m-%dT%H:%M:%S%z", zsend.start));
  28. char send_end_time[STRTIME_LEN+1];
  29. assert(dstrftime(send_end_time, STRTIME_LEN, "%Y-%m-%dT%H:%M:%S%z", zsend.finish));
  30. char recv_start_time[STRTIME_LEN+1];
  31. assert(dstrftime(recv_start_time, STRTIME_LEN, "%Y-%m-%dT%H:%M:%S%z", zrecv.start));
  32. char recv_end_time[STRTIME_LEN+1];
  33. assert(dstrftime(recv_end_time, STRTIME_LEN, "%Y-%m-%dT%H:%M:%S%z", zrecv.finish));
  34. double hitrate = ((double) 100 * zrecv.success_unique)/((double)zsend.sent);
  35. json_object *obj = json_object_new_object();
  36. // scanner host name
  37. char hostname[1024];
  38. if (gethostname(hostname, 1023) < 0) {
  39. log_error("json_metadata", "unable to retrieve local hostname");
  40. } else {
  41. hostname[1023] = '\0';
  42. json_object_object_add(obj, "local_hostname",
  43. json_object_new_string(hostname));
  44. struct hostent* h = gethostbyname(hostname);
  45. if (h) {
  46. json_object_object_add(obj, "full_hostname",
  47. json_object_new_string(h->h_name));
  48. } else {
  49. log_error("json_metadata", "unable to retrieve complete hostname");
  50. }
  51. }
  52. json_object_object_add(obj, "target_port",
  53. json_object_new_int(zconf.target_port));
  54. json_object_object_add(obj, "source_port_first",
  55. json_object_new_int(zconf.source_port_first));
  56. json_object_object_add(obj, "source_port_last",
  57. json_object_new_int(zconf.source_port_last));
  58. json_object_object_add(obj, "max_targets",
  59. json_object_new_int(zconf.max_targets));
  60. json_object_object_add(obj, "max_runtime",
  61. json_object_new_int(zconf.max_runtime));
  62. json_object_object_add(obj, "max_results",
  63. json_object_new_int(zconf.max_results));
  64. if (zconf.iface) {
  65. json_object_object_add(obj, "iface",
  66. json_object_new_string(zconf.iface));
  67. }
  68. json_object_object_add(obj, "rate",
  69. json_object_new_int(zconf.rate));
  70. json_object_object_add(obj, "bandwidth",
  71. json_object_new_int(zconf.bandwidth));
  72. json_object_object_add(obj, "cooldown_secs",
  73. json_object_new_int(zconf.cooldown_secs));
  74. json_object_object_add(obj, "senders",
  75. json_object_new_int(zconf.senders));
  76. json_object_object_add(obj, "use_seed",
  77. json_object_new_int(zconf.use_seed));
  78. json_object_object_add(obj, "seed",
  79. json_object_new_int64(zconf.seed));
  80. json_object_object_add(obj, "generator",
  81. json_object_new_int64(zconf.generator));
  82. json_object_object_add(obj, "hitrate",
  83. json_object_new_double(hitrate));
  84. json_object_object_add(obj, "shard_num",
  85. json_object_new_int(zconf.shard_num));
  86. json_object_object_add(obj, "total_shards",
  87. json_object_new_int(zconf.total_shards));
  88. json_object_object_add(obj, "min_hitrate",
  89. json_object_new_double(zconf.min_hitrate));
  90. json_object_object_add(obj, "max_sendto_failures",
  91. json_object_new_int(zconf.max_sendto_failures));
  92. json_object_object_add(obj, "syslog",
  93. json_object_new_int(zconf.syslog));
  94. json_object_object_add(obj, "filter_duplicates",
  95. json_object_new_int(zconf.filter_duplicates));
  96. json_object_object_add(obj, "filter_unsuccessful",
  97. json_object_new_int(zconf.filter_unsuccessful));
  98. json_object_object_add(obj, "pcap_recv",
  99. json_object_new_int(zrecv.pcap_recv));
  100. json_object_object_add(obj, "pcap_drop",
  101. json_object_new_int(zrecv.pcap_drop));
  102. json_object_object_add(obj, "pcap_ifdrop",
  103. json_object_new_int(zrecv.pcap_ifdrop));
  104. json_object_object_add(obj, "blacklist_total_allowed",
  105. json_object_new_int64(zconf.total_allowed));
  106. json_object_object_add(obj, "blacklist_total_not_allowed",
  107. json_object_new_int64(zconf.total_disallowed));
  108. // json_object_object_add(obj, "blacklisted",
  109. // json_object_new_int64(zsend.blacklisted));
  110. // json_object_object_add(obj, "whitelisted",
  111. // json_object_new_int64(zsend.whitelisted));
  112. json_object_object_add(obj, "first_scanned",
  113. json_object_new_int64(zsend.first_scanned));
  114. json_object_object_add(obj, "send_to_failures",
  115. json_object_new_int64(zsend.sendto_failures));
  116. json_object_object_add(obj, "total_sent",
  117. json_object_new_int64(zsend.sent));
  118. json_object_object_add(obj, "success_total",
  119. json_object_new_int64(zrecv.success_total));
  120. json_object_object_add(obj, "success_unique",
  121. json_object_new_int64(zrecv.success_unique));
  122. if (zconf.fsconf.app_success_index >= 0) {
  123. json_object_object_add(obj, "app_success_total",
  124. json_object_new_int64(zrecv.app_success_total));
  125. json_object_object_add(obj, "app_success_unique",
  126. json_object_new_int64(zrecv.app_success_unique));
  127. }
  128. json_object_object_add(obj, "success_cooldown_total",
  129. json_object_new_int64(zrecv.cooldown_total));
  130. json_object_object_add(obj, "success_cooldown_unique",
  131. json_object_new_int64(zrecv.cooldown_unique));
  132. json_object_object_add(obj, "failure_total",
  133. json_object_new_int64(zrecv.failure_total));
  134. json_object_object_add(obj, "packet_streams",
  135. json_object_new_int(zconf.packet_streams));
  136. json_object_object_add(obj, "probe_module",
  137. json_object_new_string(((probe_module_t *)zconf.probe_module)->name));
  138. json_object_object_add(obj, "output_module",
  139. json_object_new_string(((output_module_t *)zconf.output_module)->name));
  140. json_object_object_add(obj, "send_start_time",
  141. json_object_new_string(send_start_time));
  142. json_object_object_add(obj, "send_end_time",
  143. json_object_new_string(send_end_time));
  144. json_object_object_add(obj, "recv_start_time",
  145. json_object_new_string(recv_start_time));
  146. json_object_object_add(obj, "recv_end_time",
  147. json_object_new_string(recv_end_time));
  148. if (zconf.output_filter_str) {
  149. json_object_object_add(obj, "output_filter",
  150. json_object_new_string(zconf.output_filter_str));
  151. }
  152. if (zconf.log_file) {
  153. json_object_object_add(obj, "log_file",
  154. json_object_new_string(zconf.log_file));
  155. }
  156. if (zconf.log_directory) {
  157. json_object_object_add(obj, "log_directory",
  158. json_object_new_string(zconf.log_directory));
  159. }
  160. if (zconf.destination_cidrs_len) {
  161. json_object *cli_dest_cidrs = json_object_new_array();
  162. for (int i=0; i < zconf.destination_cidrs_len; i++) {
  163. json_object_array_add(cli_dest_cidrs, json_object_new_string(zconf.destination_cidrs[i]));
  164. }
  165. json_object_object_add(obj, "cli_cidr_destinations",
  166. cli_dest_cidrs);
  167. }
  168. if (zconf.probe_args) {
  169. json_object_object_add(obj, "probe_args",
  170. json_object_new_string(zconf.probe_args));
  171. }
  172. if (zconf.output_args) {
  173. json_object_object_add(obj, "output_args",
  174. json_object_new_string(zconf.output_args));
  175. }
  176. if (zconf.gw_mac) {
  177. char mac_buf[ (MAC_ADDR_LEN * 2) + (MAC_ADDR_LEN - 1) + 1 ];
  178. memset(mac_buf, 0, sizeof(mac_buf));
  179. char *p = mac_buf;
  180. for(int i=0; i < MAC_ADDR_LEN; i++) {
  181. if (i == MAC_ADDR_LEN-1) {
  182. snprintf(p, 3, "%.2x", zconf.gw_mac[i]);
  183. p += 2;
  184. } else {
  185. snprintf(p, 4, "%.2x:", zconf.gw_mac[i]);
  186. p += 3;
  187. }
  188. }
  189. json_object_object_add(obj, "gateway_mac", json_object_new_string(mac_buf));
  190. }
  191. if (zconf.gw_ip) {
  192. struct in_addr addr;
  193. addr.s_addr = zconf.gw_ip;
  194. json_object_object_add(obj, "gateway_ip", json_object_new_string(inet_ntoa(addr)));
  195. }
  196. if (zconf.hw_mac) {
  197. char mac_buf[(ETHER_ADDR_LEN * 2) + (ETHER_ADDR_LEN - 1) + 1];
  198. char *p = mac_buf;
  199. for(int i=0; i < ETHER_ADDR_LEN; i++) {
  200. if (i == ETHER_ADDR_LEN-1) {
  201. snprintf(p, 3, "%.2x", zconf.hw_mac[i]);
  202. p += 2;
  203. } else {
  204. snprintf(p, 4, "%.2x:", zconf.hw_mac[i]);
  205. p += 3;
  206. }
  207. }
  208. json_object_object_add(obj, "source_mac", json_object_new_string(mac_buf));
  209. }
  210. json_object_object_add(obj, "source_ip_first",
  211. json_object_new_string(zconf.source_ip_first));
  212. json_object_object_add(obj, "source_ip_last",
  213. json_object_new_string(zconf.source_ip_last));
  214. if (zconf.output_filename) {
  215. json_object_object_add(obj, "output_filename",
  216. json_object_new_string(zconf.output_filename));
  217. }
  218. if (zconf.blacklist_filename) {
  219. json_object_object_add(obj,
  220. "blacklist_filename",
  221. json_object_new_string(zconf.blacklist_filename));
  222. }
  223. if (zconf.whitelist_filename) {
  224. json_object_object_add(obj,
  225. "whitelist_filename",
  226. json_object_new_string(zconf.whitelist_filename));
  227. }
  228. json_object_object_add(obj, "dryrun",
  229. json_object_new_int(zconf.dryrun));
  230. json_object_object_add(obj, "quiet",
  231. json_object_new_int(zconf.quiet));
  232. json_object_object_add(obj, "log_level",
  233. json_object_new_int(zconf.log_level));
  234. // parse out JSON metadata that was supplied on the command-line
  235. if (zconf.custom_metadata_str) {
  236. json_object *user = json_tokener_parse(zconf.custom_metadata_str);
  237. if (!user) {
  238. log_error("json-metadata", "unable to parse user metadata");
  239. } else {
  240. json_object_object_add(obj, "user-metadata", user);
  241. }
  242. }
  243. if (zconf.notes) {
  244. json_object_object_add(obj, "notes",
  245. json_object_new_string(zconf.notes));
  246. }
  247. // add blacklisted and whitelisted CIDR blocks
  248. bl_cidr_node_t *b = get_blacklisted_cidrs();
  249. if (b) {
  250. json_object *blacklisted_cidrs = json_object_new_array();
  251. do {
  252. char cidr[50];
  253. struct in_addr addr;
  254. addr.s_addr = b->ip_address;
  255. sprintf(cidr, "%s/%i", inet_ntoa(addr), b->prefix_len);
  256. json_object_array_add(blacklisted_cidrs,
  257. json_object_new_string(cidr));
  258. } while (b && (b = b->next));
  259. json_object_object_add(obj, "blacklisted_networks", blacklisted_cidrs);
  260. }
  261. b = get_whitelisted_cidrs();
  262. if (b) {
  263. json_object *whitelisted_cidrs = json_object_new_array();
  264. do {
  265. char cidr[50];
  266. struct in_addr addr;
  267. addr.s_addr = b->ip_address;
  268. sprintf(cidr, "%s/%i", inet_ntoa(addr), b->prefix_len);
  269. json_object_array_add(whitelisted_cidrs,
  270. json_object_new_string(cidr));
  271. } while (b && (b = b->next));
  272. json_object_object_add(obj, "whitelisted_networks", whitelisted_cidrs);
  273. }
  274. fprintf(file, "%s\n", json_object_to_json_string(obj));
  275. json_object_put(obj);
  276. }
  277. #else
  278. void json_metadata(FILE *file)
  279. {
  280. (void) file;
  281. log_error("metadata", "JSON support not compiled in");
  282. }
  283. #endif