2 * Copyright (c) 2003+ Evgeniy Polyakov <zbr@ioremap.net>
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.
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.
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
21 * xtables interface for OS fingerprint matching module.
31 #include <linux/types.h>
35 #include <netinet/ip.h>
36 #include <netinet/tcp.h>
38 #include <linux/netfilter/xt_osf.h>
40 static void osf_help(void)
42 printf("OS fingerprint match options:\n"
43 "[!] --genre string Match a OS genre by passive fingerprinting.\n"
44 "--ttl level Use some TTL check extensions to determine OS:\n"
45 " 0 true ip and fingerprint TTL comparison. Works for LAN.\n"
46 " 1 check if ip TTL is less than fingerprint one. Works for global addresses.\n"
47 " 2 do not compare TTL at all. Allows to detect NMAP, but can produce false results.\n"
48 "--log level Log determined genres into dmesg even if they do not match desired one:\n"
49 " 0 log all matched or unknown signatures.\n"
50 " 1 log only first one.\n"
51 " 2 log all known matched signatures.\n"
56 static const struct option osf_opts[] = {
57 { .name = "genre", .has_arg = true, .val = '1' },
58 { .name = "ttl", .has_arg = true, .val = '2' },
59 { .name = "log", .has_arg = true, .val = '3' },
64 static void osf_parse_string(const char *s, struct xt_osf_info *info)
66 if (strlen(s) < MAXGENRELEN)
67 strcpy(info->genre, s);
69 xtables_error(PARAMETER_PROBLEM,
70 "Genre string too long `%s' [%zd], max=%d",
71 s, strlen(s), MAXGENRELEN);
74 static int osf_parse(int c, char **argv, int invert, unsigned int *flags,
76 struct xt_entry_match **match)
78 struct xt_osf_info *info = (struct xt_osf_info *)(*match)->data;
81 case '1': /* --genre */
82 if (*flags & XT_OSF_GENRE)
83 xtables_error(PARAMETER_PROBLEM,
84 "Can't specify multiple genre parameter");
85 xtables_check_inverse(optarg, &invert, &optind, 0, argv);
86 osf_parse_string(argv[optind-1], info);
88 info->flags |= XT_OSF_INVERT;
89 info->len=strlen(info->genre);
90 *flags |= XT_OSF_GENRE;
93 if (*flags & XT_OSF_TTL)
94 xtables_error(PARAMETER_PROBLEM,
95 "Can't specify multiple ttl parameter");
97 info->flags |= XT_OSF_TTL;
98 if (!xtables_strtoui(argv[optind-1], NULL, &info->ttl, 0, 2))
99 xtables_error(PARAMETER_PROBLEM, "TTL parameter is too big");
101 case '3': /* --log */
102 if (*flags & XT_OSF_LOG)
103 xtables_error(PARAMETER_PROBLEM,
104 "Can't specify multiple log parameter");
105 *flags |= XT_OSF_LOG;
106 if (!xtables_strtoui(argv[optind-1], NULL, &info->loglevel, 0, 2))
107 xtables_error(PARAMETER_PROBLEM, "Log level parameter is too big");
108 info->flags |= XT_OSF_LOG;
117 static void osf_final_check(unsigned int flags)
120 xtables_error(PARAMETER_PROBLEM,
121 "OS fingerprint match: You must specify `--genre'");
124 static void osf_print(const void *ip, const struct xt_entry_match *match, int numeric)
126 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
128 printf("OS fingerprint match %s%s ", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre);
131 static void osf_save(const void *ip, const struct xt_entry_match *match)
133 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
135 printf("--genre %s%s ", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre);
138 static struct xtables_match osf_match = {
140 .version = XTABLES_VERSION,
141 .size = XT_ALIGN(sizeof(struct xt_osf_info)),
142 .userspacesize = XT_ALIGN(sizeof(struct xt_osf_info)),
146 .final_check = osf_final_check,
148 .extra_opts = osf_opts,
149 .family = NFPROTO_IPV4
154 xtables_register_match(&osf_match);