libxt_osf.c revision d09b6d591ca7d7d7575cb6aa20384c9830f777ab
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 */ 23#include <stdbool.h> 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 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" 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 XT_GETOPT_TABLEEND, 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 } 111 112 return 1; 113} 114 115static void osf_final_check(unsigned int flags) 116{ 117 if (!flags) 118 xtables_error(PARAMETER_PROBLEM, 119 "OS fingerprint match: You must specify `--genre'"); 120} 121 122static void osf_print(const void *ip, const struct xt_entry_match *match, int numeric) 123{ 124 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data; 125 126 printf("OS fingerprint match %s%s ", (info->flags & XT_OSF_INVERT) ? "! " : "", info->genre); 127} 128 129static void osf_save(const void *ip, const struct xt_entry_match *match) 130{ 131 const struct xt_osf_info *info = (const struct xt_osf_info*) match->data; 132 133 printf("--genre %s%s ", (info->flags & XT_OSF_INVERT) ? "! ": "", info->genre); 134} 135 136static struct xtables_match osf_match = { 137 .name = "osf", 138 .version = XTABLES_VERSION, 139 .size = XT_ALIGN(sizeof(struct xt_osf_info)), 140 .userspacesize = XT_ALIGN(sizeof(struct xt_osf_info)), 141 .help = osf_help, 142 .parse = osf_parse, 143 .print = osf_print, 144 .final_check = osf_final_check, 145 .save = osf_save, 146 .extra_opts = osf_opts, 147 .family = NFPROTO_IPV4 148}; 149 150void _init(void) 151{ 152 xtables_register_match(&osf_match); 153} 154