blob: 3c0ea2dd542d94c4f49f8e041da60092a0a2ae4d [file] [log] [blame]
Patrick McHardy588b6152009-11-12 13:01:30 +01001/*
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 */
23
24#include <stdio.h>
25#include <netdb.h>
26#include <string.h>
27#include <stdlib.h>
28#include <getopt.h>
29#include <ctype.h>
30
31#include <linux/types.h>
32
33#include <xtables.h>
34
35#include <netinet/ip.h>
36#include <netinet/tcp.h>
37
38#include <linux/netfilter/xt_osf.h>
39
40static void osf_help(void)
41{
42 printf("OS fingerprint match options:\n"
43 "--genre [!] string Match a OS genre by passive fingerprinting.\n"
44 "--ttl 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"
52 );
53}
54
55
56static 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' },
60 { .name = NULL }
61};
62
63
64static void osf_parse_string(const char *s, struct xt_osf_info *info)
65{
66 if (strlen(s) < MAXGENRELEN)
67 strcpy(info->genre, s);
68 else
69 xtables_error(PARAMETER_PROBLEM,
70 "Genre string too long `%s' [%zd], max=%d",
71 s, strlen(s), MAXGENRELEN);
72}
73
74static int osf_parse(int c, char **argv, int invert, unsigned int *flags,
75 const void *entry,
76 struct xt_entry_match **match)
77{
78 struct xt_osf_info *info = (struct xt_osf_info *)(*match)->data;
79
80 switch(c) {
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);
87 if (invert)
88 info->flags |= XT_OSF_INVERT;
89 info->len=strlen(info->genre);
90 *flags |= XT_OSF_GENRE;
91 break;
92 case '2': /* --ttl */
93 if (*flags & XT_OSF_TTL)
94 xtables_error(PARAMETER_PROBLEM,
95 "Can't specify multiple ttl parameter");
96 *flags |= XT_OSF_TTL;
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");
100 break;
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;
109 break;
110 default:
111 return 0;
112 }
113
114 return 1;
115}
116
117static void osf_final_check(unsigned int flags)
118{
119 if (!flags)
120 xtables_error(PARAMETER_PROBLEM,
121 "OS fingerprint match: You must specify `--genre'");
122}
123
124static void osf_print(const void *ip, const struct xt_entry_match *match, int numeric)
125{
126 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
127
128 printf("OS fingerprint match %s%s ", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre);
129}
130
131static void osf_save(const void *ip, const struct xt_entry_match *match)
132{
133 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data;
134
135 printf("--genre %s%s ", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre);
136}
137
138static struct xtables_match osf_match = {
139 .name = "osf",
140 .version = XTABLES_VERSION,
141 .size = XT_ALIGN(sizeof(struct xt_osf_info)),
142 .userspacesize = XT_ALIGN(sizeof(struct xt_osf_info)),
143 .help = osf_help,
144 .parse = osf_parse,
145 .print = osf_print,
146 .final_check = osf_final_check,
147 .save = osf_save,
148 .extra_opts = osf_opts,
149 .family = NFPROTO_IPV4
150};
151
152void _init(void)
153{
154 xtables_register_match(&osf_match);
155}