Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 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 | /* |
| 21 | * xtables interface for OS fingerprint matching module. |
| 22 | */ |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 24 | #include <string.h> |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 25 | #include <xtables.h> |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 26 | #include <netinet/ip.h> |
| 27 | #include <netinet/tcp.h> |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 28 | #include <linux/netfilter/xt_osf.h> |
| 29 | |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 30 | enum { |
| 31 | O_GENRE = 0, |
| 32 | O_TTL, |
| 33 | O_LOGLEVEL, |
| 34 | }; |
| 35 | |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 36 | static void osf_help(void) |
| 37 | { |
| 38 | printf("OS fingerprint match options:\n" |
Jan Engelhardt | 23e718b | 2010-03-27 12:38:45 +0100 | [diff] [blame] | 39 | "[!] --genre string Match a OS genre by passive fingerprinting.\n" |
| 40 | "--ttl level Use some TTL check extensions to determine OS:\n" |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 41 | " 0 true ip and fingerprint TTL comparison. Works for LAN.\n" |
| 42 | " 1 check if ip TTL is less than fingerprint one. Works for global addresses.\n" |
| 43 | " 2 do not compare TTL at all. Allows to detect NMAP, but can produce false results.\n" |
| 44 | "--log level Log determined genres into dmesg even if they do not match desired one:\n" |
| 45 | " 0 log all matched or unknown signatures.\n" |
| 46 | " 1 log only first one.\n" |
| 47 | " 2 log all known matched signatures.\n" |
| 48 | ); |
| 49 | } |
| 50 | |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 51 | #define s struct xt_osf_info |
| 52 | static const struct xt_option_entry osf_opts[] = { |
| 53 | {.name = "genre", .id = O_GENRE, .type = XTTYPE_STRING, |
| 54 | .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT, |
| 55 | XTOPT_POINTER(s, genre)}, |
| 56 | {.name = "ttl", .id = O_TTL, .type = XTTYPE_UINT32, |
| 57 | .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl), .min = 0, .max = 2}, |
| 58 | {.name = "log", .id = O_LOGLEVEL, .type = XTTYPE_UINT32, |
| 59 | .flags = XTOPT_PUT, XTOPT_POINTER(s, loglevel), .min = 0, .max = 2}, |
| 60 | XTOPT_TABLEEND, |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 61 | }; |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 62 | #undef s |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 63 | |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 64 | static void osf_parse(struct xt_option_call *cb) |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 65 | { |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 66 | struct xt_osf_info *info = cb->data; |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 67 | |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 68 | xtables_option_parse(cb); |
| 69 | switch (cb->entry->id) { |
| 70 | case O_GENRE: |
| 71 | if (cb->invert) |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 72 | info->flags |= XT_OSF_INVERT; |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 73 | info->len = strlen(info->genre); |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 74 | break; |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 75 | case O_TTL: |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 76 | info->flags |= XT_OSF_TTL; |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 77 | break; |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 78 | case O_LOGLEVEL: |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 79 | info->flags |= XT_OSF_LOG; |
| 80 | break; |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 81 | } |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void osf_print(const void *ip, const struct xt_entry_match *match, int numeric) |
| 85 | { |
| 86 | const struct xt_osf_info *info = (const struct xt_osf_info*) match->data; |
| 87 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 88 | printf(" OS fingerprint match %s%s", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre); |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void osf_save(const void *ip, const struct xt_entry_match *match) |
| 92 | { |
| 93 | const struct xt_osf_info *info = (const struct xt_osf_info*) match->data; |
| 94 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 95 | printf(" --genre %s%s", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre); |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static struct xtables_match osf_match = { |
| 99 | .name = "osf", |
| 100 | .version = XTABLES_VERSION, |
| 101 | .size = XT_ALIGN(sizeof(struct xt_osf_info)), |
| 102 | .userspacesize = XT_ALIGN(sizeof(struct xt_osf_info)), |
| 103 | .help = osf_help, |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 104 | .x6_parse = osf_parse, |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 105 | .print = osf_print, |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 106 | .save = osf_save, |
Jan Engelhardt | 94cd683 | 2011-05-06 22:59:07 +0200 | [diff] [blame] | 107 | .x6_options = osf_opts, |
| 108 | .family = NFPROTO_IPV4, |
Patrick McHardy | 588b615 | 2009-11-12 13:01:30 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | void _init(void) |
| 112 | { |
| 113 | xtables_register_match(&osf_match); |
| 114 | } |