A Linux netfilter module to aid in (d)dos protection
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

191 lines
7.1 KiB

/*
* "ts3init_get_cookie and ts3init_get_puzzle" match extension for iptables
* Niels Werensteijn <niels werensteijn [at] teamspeak com>, 2016-10-03
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License; either version 2
* or 3 of the License, as published by the Free Software Foundation.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <xtables.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "ts3init_random_seed.h"
#include "ts3init_match.h"
#define param_act(t, s, f) xtables_param_act((t), "ts3init_get_puzzle", (s), (f))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
static void ts3init_get_puzzle_help(void)
{
printf(
"ts3init_get_puzzle match options:\n"
" --min-client n The client needs to be at least version n.\n"
" --check-cookie Check that the cookie was generated by same seed.\n"
" --random-seed <seed> Seed is a %i byte hex number.\n"
" A source could be /dev/random.\n"
" --random-seed-file <file> Read the seed from a file.\n",
RANDOM_SEED_LEN
);
}
static const struct option ts3init_get_puzzle_opts[] = {
{.name = "min-client", .has_arg = true, .val = '1'},
{.name = "check-cookie", .has_arg = false, .val = '2'},
{.name = "random-seed", .has_arg = true, .val = '3'},
{.name = "random-seed-file", .has_arg = true, .val = '4'},
{NULL},
};
static int ts3init_get_puzzle_parse(int c, char **argv, int invert, unsigned int *flags,
const void *entry, struct xt_entry_match **match)
{
struct xt_ts3init_get_puzzle_mtinfo *info = (void *)(*match)->data;
int client_version;
switch (c) {
case '1':
param_act(XTF_ONLY_ONCE, "--min-client", info->common_options & CHK_COMMON_CLIENT_VERSION);
param_act(XTF_NO_INVERT, "--min-client", invert);
client_version = atoi(optarg);
if (client_version <= 0)
xtables_error(PARAMETER_PROBLEM,
"ts3init_get_cookie: invalid min-client version");
info->common_options |= CHK_COMMON_CLIENT_VERSION;
info->min_client_version = client_version - CLIENT_VERSION_OFFSET;
return true;
case '2':
param_act(XTF_ONLY_ONCE, "--check-cookie", info->specific_options & CHK_GET_PUZZLE_CHECK_COOKIE);
param_act(XTF_NO_INVERT, "--check-cookie", invert);
info->specific_options |= CHK_GET_PUZZLE_CHECK_COOKIE;
*flags |= CHK_GET_PUZZLE_CHECK_COOKIE;
return true;
case '3':
param_act(XTF_ONLY_ONCE, "--random-seed", info->specific_options & CHK_GET_PUZZLE_RANDOM_SEED_FROM_ARGUMENT);
param_act(XTF_NO_INVERT, "--random-seed", invert);
if (strlen(optarg) != (RANDOM_SEED_LEN * 2))
xtables_error(PARAMETER_PROBLEM,
"ts3init_get_puzzle: invalid random seed length");
if (!parse_random_seed(optarg, info->random_seed))
xtables_error(PARAMETER_PROBLEM,
"ts3init_get_puzzle: invalid random seed. (not lowercase hex)");
info->specific_options |= CHK_GET_PUZZLE_RANDOM_SEED_FROM_ARGUMENT;
*flags |= CHK_GET_PUZZLE_RANDOM_SEED_FROM_ARGUMENT;
return true;
case '4':
param_act(XTF_ONLY_ONCE, "--random-seed-file", info->specific_options & CHK_GET_PUZZLE_RANDOM_SEED_FROM_FILE);
param_act(XTF_NO_INVERT, "--random-seed-file", invert);
if (read_random_seed_from_file("ts3init_get_puzzle", optarg, info->random_seed))
memcpy(info->random_seed_path, optarg, strlen(optarg) + 1);
info->specific_options |= CHK_GET_PUZZLE_RANDOM_SEED_FROM_FILE;
*flags |= CHK_GET_PUZZLE_RANDOM_SEED_FROM_FILE;
return true;
default:
return false;
}
}
static void ts3init_get_puzzle_save(const void *ip, const struct xt_entry_match *match)
{
int i;
const struct xt_ts3init_get_puzzle_mtinfo *info = (const void *)match->data;
if (info->common_options & CHK_COMMON_CLIENT_VERSION)
{
printf(" --min-client %u", info->min_client_version + CLIENT_VERSION_OFFSET);
}
if (info->specific_options & CHK_GET_PUZZLE_CHECK_COOKIE)
{
printf(" --check-cookie");
}
if (info->specific_options & CHK_GET_PUZZLE_RANDOM_SEED_FROM_ARGUMENT)
{
printf(" --random-seed ");
for (i = 0; i < RANDOM_SEED_LEN; i++)
{
printf("%02X", info->random_seed[i]);
}
}
if (info->specific_options & CHK_GET_PUZZLE_RANDOM_SEED_FROM_FILE)
{
printf(" --random-seed-file \"%s\"", info->random_seed_path);
}
}
static void ts3init_get_puzzle_print(const void *ip, const struct xt_entry_match *match,
int numeric)
{
printf(" -m ts3init_get_puzzle");
ts3init_get_puzzle_save(ip, match);
}
static void ts3init_get_puzzle_check(unsigned int flags)
{
bool seed_from_argument = flags & CHK_GET_PUZZLE_RANDOM_SEED_FROM_ARGUMENT;
bool seed_from_file = flags & CHK_GET_PUZZLE_RANDOM_SEED_FROM_FILE;
if (seed_from_argument && seed_from_file)
{
xtables_error(PARAMETER_PROBLEM,
"ts3init_get_puzzle: --random-seed and --random-seed-file "
"can not be specified at the same time");
}
if (flags & CHK_GET_PUZZLE_CHECK_COOKIE)
{
if (!seed_from_argument && !seed_from_file)
{
xtables_error(PARAMETER_PROBLEM,
"ts3init_get_puzzle: --check-cookie requires either "
"--random-seed or --random-seed-file");
}
}
}
/* register and init */
static struct xtables_match ts3init_mt_reg[] =
{
{
.name = "ts3init_get_puzzle",
.revision = 0,
.family = NFPROTO_IPV4,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ts3init_get_puzzle_mtinfo)),
.userspacesize = XT_ALIGN(sizeof(struct xt_ts3init_get_puzzle_mtinfo)),
.help = ts3init_get_puzzle_help,
.parse = ts3init_get_puzzle_parse,
.print = ts3init_get_puzzle_print,
.save = ts3init_get_puzzle_save,
.extra_opts = ts3init_get_puzzle_opts,
.final_check = ts3init_get_puzzle_check,
},
{
.name = "ts3init_get_puzzle",
.revision = 0,
.family = NFPROTO_IPV6,
.version = XTABLES_VERSION,
.size = XT_ALIGN(sizeof(struct xt_ts3init_get_puzzle_mtinfo)),
.userspacesize = XT_ALIGN(sizeof(struct xt_ts3init_get_puzzle_mtinfo)),
.help = ts3init_get_puzzle_help,
.parse = ts3init_get_puzzle_parse,
.print = ts3init_get_puzzle_print,
.save = ts3init_get_puzzle_save,
.extra_opts = ts3init_get_puzzle_opts,
.final_check = ts3init_get_puzzle_check,
}
};
static __attribute__((constructor)) void ts3init_mt_ldr(void)
{
xtables_register_matches(ts3init_mt_reg, ARRAY_SIZE(ts3init_mt_reg));
}