libxt_osf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2003+ Evgeniy Polyakov <zbr@ioremap.net>
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * xtables interface for OS fingerprint matching module.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <xtables.h>
  25. #include <netinet/ip.h>
  26. #include <netinet/tcp.h>
  27. #include <linux/netfilter/xt_osf.h>
  28. enum {
  29. O_GENRE = 0,
  30. O_TTL,
  31. O_LOGLEVEL,
  32. };
  33. static void osf_help(void)
  34. {
  35. printf("OS fingerprint match options:\n"
  36. "[!] --genre string Match a OS genre by passive fingerprinting.\n"
  37. "--ttl level Use some TTL check extensions to determine OS:\n"
  38. " 0 true ip and fingerprint TTL comparison. Works for LAN.\n"
  39. " 1 check if ip TTL is less than fingerprint one. Works for global addresses.\n"
  40. " 2 do not compare TTL at all. Allows to detect NMAP, but can produce false results.\n"
  41. "--log level Log determined genres into dmesg even if they do not match desired one:\n"
  42. " 0 log all matched or unknown signatures.\n"
  43. " 1 log only first one.\n"
  44. " 2 log all known matched signatures.\n"
  45. );
  46. }
  47. #define s struct xt_osf_info
  48. static const struct xt_option_entry osf_opts[] = {
  49. {.name = "genre", .id = O_GENRE, .type = XTTYPE_STRING,
  50. .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT,
  51. XTOPT_POINTER(s, genre)},
  52. {.name = "ttl", .id = O_TTL, .type = XTTYPE_UINT32,
  53. .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl), .min = 0, .max = 2},
  54. {.name = "log", .id = O_LOGLEVEL, .type = XTTYPE_UINT32,
  55. .flags = XTOPT_PUT, XTOPT_POINTER(s, loglevel), .min = 0, .max = 2},
  56. XTOPT_TABLEEND,
  57. };
  58. #undef s
  59. static void osf_parse(struct xt_option_call *cb)
  60. {
  61. struct xt_osf_info *info = cb->data;
  62. xtables_option_parse(cb);
  63. switch (cb->entry->id) {
  64. case O_GENRE:
  65. if (cb->invert)
  66. info->flags |= XT_OSF_INVERT;
  67. info->len = strlen(info->genre);
  68. break;
  69. case O_TTL:
  70. info->flags |= XT_OSF_TTL;
  71. break;
  72. case O_LOGLEVEL:
  73. info->flags |= XT_OSF_LOG;
  74. break;
  75. }
  76. }
  77. static void osf_print(const void *ip, const struct xt_entry_match *match, int numeric)
  78. {
  79. const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
  80. printf(" OS fingerprint match %s%s", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre);
  81. }
  82. static void osf_save(const void *ip, const struct xt_entry_match *match)
  83. {
  84. const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
  85. printf(" --genre %s%s", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre);
  86. }
  87. static struct xtables_match osf_match = {
  88. .name = "osf",
  89. .version = XTABLES_VERSION,
  90. .size = XT_ALIGN(sizeof(struct xt_osf_info)),
  91. .userspacesize = XT_ALIGN(sizeof(struct xt_osf_info)),
  92. .help = osf_help,
  93. .x6_parse = osf_parse,
  94. .print = osf_print,
  95. .save = osf_save,
  96. .x6_options = osf_opts,
  97. .family = NFPROTO_IPV4,
  98. };
  99. void _init(void)
  100. {
  101. xtables_register_match(&osf_match);
  102. }