zblacklist.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /*
  9. * ZBlacklist is a simple utility that (1) excludes IP addresses on a specified
  10. * blacklist from being scanned, and (2) ensures the uniqueness of output
  11. * addresses such that no host is scanned twice. ZBlacklist takes in a list
  12. * of addresses on stdin and outputs addresses that are acceptable to scan
  13. * on stdout. The utility uses the blacklist data structures from ZMap for
  14. * checking scan eligibility and a paged bitmap for duplicate prevention.
  15. */
  16. #define _GNU_SOURCE
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <getopt.h>
  22. #include <assert.h>
  23. #include <sched.h>
  24. #include <errno.h>
  25. #include <pwd.h>
  26. #include <time.h>
  27. #include "../lib/includes.h"
  28. #include "../lib/blacklist.h"
  29. #include "../lib/logger.h"
  30. #include "../lib/pbm.h"
  31. #include "zbopt.h"
  32. //struct zbl_stats {
  33. // uint32_t cidr_entries;
  34. // uint32_t allowed_addrs;
  35. // uint32_t input_addrs;
  36. // uint32_t uniq_input_addrs;
  37. // uint32_t blocked_addrs;
  38. // uint32_t output_addrs;
  39. // uint32_t duplicates;
  40. //};
  41. #undef MIN
  42. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  43. static inline char* zmin(char *a, char *b) {
  44. if (a && !b)
  45. return a;
  46. else if (b && !a)
  47. return b;
  48. else
  49. return MIN(a,b);
  50. }
  51. struct zbl_conf {
  52. char *blacklist_filename;
  53. char *whitelist_filename;
  54. char *log_filename;
  55. int check_duplicates;
  56. int ignore_errors;
  57. int verbosity;
  58. //struct zbl_stats stats;
  59. };
  60. #define SET_IF_GIVEN(DST,ARG) \
  61. { if (args.ARG##_given) { (DST) = args.ARG##_arg; }; }
  62. #define SET_BOOL(DST,ARG) \
  63. { if (args.ARG##_given) { (DST) = 1; }; }
  64. int main(int argc, char **argv)
  65. {
  66. struct zbl_conf conf;
  67. conf.verbosity = 3;
  68. memset(&conf, 0, sizeof(struct zbl_conf));
  69. int no_dupchk_pres = 0;
  70. conf.ignore_errors = 0;
  71. struct gengetopt_args_info args;
  72. struct cmdline_parser_params *params;
  73. params = cmdline_parser_params_create();
  74. assert(params);
  75. params->initialize = 1;
  76. params->override = 0;
  77. params->check_required = 0;
  78. if (cmdline_parser_ext(argc, argv, &args, params) != 0) {
  79. exit(EXIT_SUCCESS);
  80. }
  81. // Handle help text and version
  82. if (args.help_given) {
  83. cmdline_parser_print_help();
  84. exit(EXIT_SUCCESS);
  85. }
  86. if (args.version_given) {
  87. cmdline_parser_print_version();
  88. exit(EXIT_SUCCESS);
  89. }
  90. // Set the log file and metadata file
  91. if (args.log_file_given) {
  92. conf.log_filename = strdup(args.log_file_arg);
  93. }
  94. if (args.verbosity_given) {
  95. conf.verbosity = args.verbosity_arg;
  96. }
  97. // Blacklist and whitelist
  98. if (args.blacklist_file_given) {
  99. conf.blacklist_filename = strdup(args.blacklist_file_arg);
  100. }
  101. if (args.whitelist_file_given) {
  102. conf.whitelist_filename = strdup(args.whitelist_file_arg);
  103. }
  104. // Read the boolean flags
  105. SET_BOOL(no_dupchk_pres, no_duplicate_checking);
  106. conf.check_duplicates = !no_dupchk_pres;
  107. SET_BOOL(conf.ignore_errors, ignore_blacklist_errors);
  108. // initialize logging
  109. FILE *logfile = stderr;
  110. if (conf.log_filename) {
  111. logfile = fopen(conf.log_filename, "w");
  112. if (!logfile) {
  113. fprintf(stderr, "FATAL: unable to open specified logfile (%s)\n",
  114. conf.log_filename);
  115. exit(1);
  116. }
  117. }
  118. if (log_init(logfile, conf.verbosity, 1, "zblacklist")) {
  119. fprintf(stderr, "FATAL: unable able to initialize logging\n");
  120. exit(1);
  121. }
  122. if (!conf.blacklist_filename && !conf.whitelist_filename) {
  123. log_fatal("zblacklist", "must specify either a whitelist or blacklist file");
  124. }
  125. // parse blacklist
  126. if (conf.blacklist_filename) {
  127. log_debug("zblacklist", "blacklist file at %s to be used", conf.blacklist_filename);
  128. } else {
  129. log_debug("zblacklist", "no blacklist file specified");
  130. }
  131. if (conf.blacklist_filename && access(conf.blacklist_filename, R_OK) == -1) {
  132. log_fatal("zblacklist", "unable to read specified blacklist file (%s)",
  133. conf.blacklist_filename);
  134. }
  135. if (conf.whitelist_filename) {
  136. log_debug("zblacklist", "whitelist file at %s to be used", conf.whitelist_filename);
  137. } else {
  138. log_debug("zblacklist", "no whitelist file specified");
  139. }
  140. if (conf.whitelist_filename && access(conf.whitelist_filename, R_OK) == -1) {
  141. log_fatal("zblacklist", "unable to read specified whitelist file (%s)",
  142. conf.whitelist_filename);
  143. }
  144. if (blacklist_init(conf.whitelist_filename, conf.blacklist_filename,
  145. NULL, 0, NULL, 0, conf.ignore_errors)) {
  146. log_fatal("zmap", "unable to initialize blacklist / whitelist");
  147. }
  148. // initialize paged bitmap
  149. uint8_t **seen = NULL;
  150. if (conf.check_duplicates) {
  151. seen = pbm_init();
  152. if (!seen) {
  153. log_fatal("zblacklist", "unable to initialize paged bitmap");
  154. }
  155. }
  156. // process addresses
  157. char line[1000];
  158. char original[1000];
  159. while (fgets(line, 1000, stdin) != NULL) {
  160. // remove new line
  161. memcpy(original, line, strlen(line) + 1);
  162. char *n = zmin(zmin(zmin(zmin(strchr(line, '\n'),
  163. strchr(line, ',')),
  164. strchr(line, '\t')),
  165. strchr(line, ' ')),
  166. strchr(line, '#'));
  167. assert(n);
  168. n[0] = 0;
  169. log_trace("zblacklist", "input value %s", line);
  170. // parse into int
  171. struct in_addr addr;
  172. if (!inet_aton(line, &addr)) {
  173. log_warn("zblacklist", "invalid input address: %s", line);
  174. }
  175. if (conf.check_duplicates) {
  176. if (pbm_check(seen, ntohl(addr.s_addr))) {
  177. log_trace("zblacklist", "%s is a duplicate: skipped", line);
  178. continue;
  179. } else {
  180. log_trace("zblacklist", "%s not a duplicate: skipped", line);
  181. }
  182. } else {
  183. log_trace("zblacklist", "no duplicate checking for %s", line);
  184. }
  185. // check if in blacklist
  186. if (blacklist_is_allowed(addr.s_addr)) {
  187. if (conf.check_duplicates) {
  188. if (!pbm_check(seen, ntohl(addr.s_addr))) {
  189. pbm_set(seen, ntohl(addr.s_addr));
  190. printf("%s", original);
  191. }
  192. } else {
  193. printf("%s", original);
  194. }
  195. }
  196. }
  197. return EXIT_SUCCESS;
  198. }