blob: 88274a0eb84495ecffba10a8fed84913dc6dd034 [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 */
Patrick McHardy588b6152009-11-12 13:01:30 +010023#include <stdio.h>
Patrick McHardy588b6152009-11-12 13:01:30 +010024#include <string.h>
Patrick McHardy588b6152009-11-12 13:01:30 +010025#include <xtables.h>
Patrick McHardy588b6152009-11-12 13:01:30 +010026#include <netinet/ip.h>
27#include <netinet/tcp.h>
Patrick McHardy588b6152009-11-12 13:01:30 +010028#include <linux/netfilter/xt_osf.h>
29
Jan Engelhardt94cd6832011-05-06 22:59:07 +020030enum {
31 O_GENRE = 0,
32 O_TTL,
33 O_LOGLEVEL,
34};
35
Patrick McHardy588b6152009-11-12 13:01:30 +010036static void osf_help(void)
37{
38 printf("OS fingerprint match options:\n"
Jan Engelhardt23e718b2010-03-27 12:38:45 +010039 "[!] --genre string Match a OS genre by passive fingerprinting.\n"
40 "--ttl level Use some TTL check extensions to determine OS:\n"
Patrick McHardy588b6152009-11-12 13:01:30 +010041 " 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 Engelhardt94cd6832011-05-06 22:59:07 +020051#define s struct xt_osf_info
52static 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 McHardy588b6152009-11-12 13:01:30 +010061};
Jan Engelhardt94cd6832011-05-06 22:59:07 +020062#undef s
Patrick McHardy588b6152009-11-12 13:01:30 +010063
Jan Engelhardt94cd6832011-05-06 22:59:07 +020064static void osf_parse(struct xt_option_call *cb)
Patrick McHardy588b6152009-11-12 13:01:30 +010065{
Jan Engelhardt94cd6832011-05-06 22:59:07 +020066 struct xt_osf_info *info = cb->data;
Patrick McHardy588b6152009-11-12 13:01:30 +010067
Jan Engelhardt94cd6832011-05-06 22:59:07 +020068 xtables_option_parse(cb);
69 switch (cb->entry->id) {
70 case O_GENRE:
71 if (cb->invert)
Patrick McHardy588b6152009-11-12 13:01:30 +010072 info->flags |= XT_OSF_INVERT;
Jan Engelhardt94cd6832011-05-06 22:59:07 +020073 info->len = strlen(info->genre);
Patrick McHardy588b6152009-11-12 13:01:30 +010074 break;
Jan Engelhardt94cd6832011-05-06 22:59:07 +020075 case O_TTL:
Patrick McHardy588b6152009-11-12 13:01:30 +010076 info->flags |= XT_OSF_TTL;
Patrick McHardy588b6152009-11-12 13:01:30 +010077 break;
Jan Engelhardt94cd6832011-05-06 22:59:07 +020078 case O_LOGLEVEL:
Patrick McHardy588b6152009-11-12 13:01:30 +010079 info->flags |= XT_OSF_LOG;
80 break;
Patrick McHardy588b6152009-11-12 13:01:30 +010081 }
Patrick McHardy588b6152009-11-12 13:01:30 +010082}
83
84static 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 Engelhardt73866352010-12-18 02:04:59 +010088 printf(" OS fingerprint match %s%s", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre);
Patrick McHardy588b6152009-11-12 13:01:30 +010089}
90
91static 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 Engelhardt73866352010-12-18 02:04:59 +010095 printf(" --genre %s%s", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre);
Patrick McHardy588b6152009-11-12 13:01:30 +010096}
97
98static 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 Engelhardt94cd6832011-05-06 22:59:07 +0200104 .x6_parse = osf_parse,
Patrick McHardy588b6152009-11-12 13:01:30 +0100105 .print = osf_print,
Patrick McHardy588b6152009-11-12 13:01:30 +0100106 .save = osf_save,
Jan Engelhardt94cd6832011-05-06 22:59:07 +0200107 .x6_options = osf_opts,
108 .family = NFPROTO_IPV4,
Patrick McHardy588b6152009-11-12 13:01:30 +0100109};
110
111void _init(void)
112{
113 xtables_register_match(&osf_match);
114}