Browse Source
renamed ts3init_reset to TS3INIT_RESET moved cookie seed spefic things to ts3init_cookie_seed.h moved cache specific things to ts3init_cachepull/1/head
Maximilian Münchow
8 years ago
15 changed files with 615 additions and 207 deletions
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* "ts3init_set_cookie" target 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 "ts3init_cookie_seed.h" |
||||
#include "ts3init_target.h" |
||||
|
||||
#define param_act(t, s, f) xtables_param_act((t), "ts3init_set_cookie", (s), (f)) |
||||
|
||||
static void ts3init_set_cookie_tg_help(void) |
||||
{ |
||||
printf( |
||||
"ts3init_get_cookie match options:\n" |
||||
" --zero-random-sequence Always return 0 as random sequence.\n" |
||||
" --cookie-seed seed Seed is a 60 byte random number in lowecase\n" |
||||
" hex. A source could be /dev/random.\n"); |
||||
} |
||||
|
||||
static const struct option ts3init_set_cookie_tg_opts[] = { |
||||
{.name = "zero-random-sequence", .has_arg = false, .val = '1'}, |
||||
{.name = "cookie-seed", .has_arg = true, .val = '2'}, |
||||
{NULL}, |
||||
}; |
||||
|
||||
static int ts3init_set_cookie_tg_parse(int c, char **argv, |
||||
int invert, unsigned int *flags, const void *entry, |
||||
struct xt_entry_target **target) |
||||
{ |
||||
struct xt_ts3init_set_cookie_tginfo *info = (void *)(*target)->data; |
||||
switch (c) { |
||||
case '1': |
||||
param_act(XTF_ONLY_ONCE, "--zero-random-sequence", info->specific_options & TARGET_SET_COOKIE_ZERO_RANDOM_SEQUENCE); |
||||
param_act(XTF_NO_INVERT, "--check-time", invert); |
||||
info->specific_options |= TARGET_SET_COOKIE_ZERO_RANDOM_SEQUENCE; |
||||
return true; |
||||
case '2': |
||||
param_act(XTF_ONLY_ONCE, "--cookie-seed", info->specific_options & TARGET_SET_COOKIE_SEED); |
||||
param_act(XTF_NO_INVERT, "--cookie-seed", invert); |
||||
if (strlen(optarg) != (COOKIE_SEED_LEN * 2)) |
||||
xtables_error(PARAMETER_PROBLEM, |
||||
"TS3INIT_SET_COOKIE: invalid cookie-seed length"); |
||||
if (!hex2int_seed(optarg, info->cookie_seed)) |
||||
xtables_error(PARAMETER_PROBLEM, |
||||
"TS3INIT_SET_COOKIE: invalid cookie-seed. (not lowercase hex)"); |
||||
info->specific_options |= TARGET_SET_COOKIE_SEED; |
||||
*flags |= TARGET_SET_COOKIE_SEED; |
||||
return true; |
||||
|
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
static void ts3init_set_cookie_tg_save(const void *ip, const struct xt_entry_target *target) |
||||
{ |
||||
const struct xt_ts3init_set_cookie_tginfo *info = (const void *)target->data; |
||||
if (info->specific_options & TARGET_SET_COOKIE_ZERO_RANDOM_SEQUENCE) |
||||
{ |
||||
printf("--zero-random-sequence "); |
||||
} |
||||
if (info->specific_options & TARGET_SET_COOKIE_SEED) |
||||
{ |
||||
printf("--cookie-seed "); |
||||
for (int i = 0; i < COOKIE_SEED_LEN; i++) |
||||
{ |
||||
printf("%02X", info->cookie_seed[i]); |
||||
} |
||||
printf(" "); |
||||
} |
||||
} |
||||
|
||||
static void ts3init_set_cookie_tg_print(const void *ip, const struct xt_entry_target *target, |
||||
int numeric) |
||||
{ |
||||
printf(" -j TS3INIT_SET_COOKIE "); |
||||
ts3init_set_cookie_tg_save(ip, target); |
||||
} |
||||
|
||||
static void ts3init_set_cookie_tg_check(unsigned int flags) |
||||
{ |
||||
if ((flags & TARGET_SET_COOKIE_SEED) == 0) |
||||
{ |
||||
xtables_error(PARAMETER_PROBLEM, |
||||
"TS3INIT_SET_COOKIE: --cookie-seed must be specified"); |
||||
} |
||||
} |
||||
|
||||
/* register and init */ |
||||
static struct xtables_target ts3init_set_cookie_tg_reg = |
||||
{ |
||||
.name = "TS3INIT_SET_COOKIE", |
||||
.revision = 0, |
||||
.family = NFPROTO_UNSPEC, |
||||
.version = XTABLES_VERSION, |
||||
.size = XT_ALIGN(sizeof(struct xt_ts3init_set_cookie_tginfo)), |
||||
.userspacesize = XT_ALIGN(sizeof(struct xt_ts3init_set_cookie_tginfo)), |
||||
.help = ts3init_set_cookie_tg_help, |
||||
.parse = ts3init_set_cookie_tg_parse, |
||||
.print = ts3init_set_cookie_tg_print, |
||||
.save = ts3init_set_cookie_tg_save, |
||||
.final_check = ts3init_set_cookie_tg_check, |
||||
.extra_opts = ts3init_set_cookie_tg_opts, |
||||
}; |
||||
|
||||
static __attribute__((constructor)) void ts3init_set_cookie_tg_ldr(void) |
||||
{ |
||||
xtables_register_target(&ts3init_set_cookie_tg_reg); |
||||
} |
@ -0,0 +1,118 @@
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* "ts3init" extension for Xtables |
||||
* |
||||
* Description: A module to aid in ts3 spoof protection |
||||
* This is the "caching of cookies" related code |
||||
* |
||||
* Authors: |
||||
* Niels Werensteijn <niels werensteijn [at] teampseak 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 <linux/kernel.h> |
||||
#include <linux/skbuff.h> |
||||
#include <linux/netfilter/x_tables.h> |
||||
#include <linux/udp.h> |
||||
#include <linux/time.h> |
||||
#include <linux/jiffies.h> |
||||
#include <linux/percpu.h> |
||||
#include "ts3init_cookie.h" |
||||
#include "ts3init_cache.h" |
||||
|
||||
struct ts3init_cache_t |
||||
{ |
||||
unsigned long saved_jiffies; |
||||
time_t unix_time; |
||||
struct xt_ts3init_cookie_cache cookie_cache; |
||||
}; |
||||
|
||||
DEFINE_PER_CPU(struct ts3init_cache_t, ts3init_cache); |
||||
|
||||
static inline void update_cache_time(unsigned long jifs, |
||||
struct ts3init_cache_t* cache) |
||||
{ |
||||
if (((long)jifs - (long)cache->saved_jiffies) >= HZ) |
||||
{ |
||||
/* it's been 1 second sinds last time update.
|
||||
* Get the new unix time and cache it*/ |
||||
struct timeval tv; |
||||
cache->saved_jiffies = jifs; |
||||
do_gettimeofday(&tv); |
||||
cache->unix_time = tv.tv_sec; |
||||
} |
||||
} |
||||
|
||||
time_t get_cached_unix_time(void) |
||||
{ |
||||
struct ts3init_cache_t* cache; |
||||
unsigned long jifs; |
||||
time_t current_unix_time; |
||||
|
||||
jifs = jiffies; |
||||
|
||||
cache = &get_cpu_var(ts3init_cache); |
||||
|
||||
update_cache_time(jifs, cache); |
||||
|
||||
current_unix_time = cache->unix_time; |
||||
|
||||
put_cpu_var(ts3init_cache); |
||||
|
||||
return current_unix_time; |
||||
} |
||||
|
||||
bool get_cookie_for_package_index(u8 packet_index, const u8* seed, u64 (*cookie)[2]) |
||||
{ |
||||
struct ts3init_cache_t* cache; |
||||
u64* result; |
||||
unsigned long jifs; |
||||
time_t current_unix_time; |
||||
|
||||
jifs = jiffies; |
||||
cache = &get_cpu_var(ts3init_cache); |
||||
|
||||
update_cache_time(jifs, cache); |
||||
|
||||
current_unix_time = cache->unix_time; |
||||
|
||||
result = ts3init_get_cookie_seed(current_unix_time, |
||||
packet_index, &cache->cookie_cache, seed); |
||||
|
||||
if (result) |
||||
{ |
||||
(*cookie)[0] = result[0]; |
||||
(*cookie)[1] = result[1]; |
||||
} |
||||
put_cpu_var(ts3init_cache); |
||||
return result; |
||||
} |
||||
|
||||
bool get_current_cookie(const u8* seed, u64 (*cookie)[2], u8 *packet_index) |
||||
{ |
||||
struct ts3init_cache_t* cache; |
||||
u64* result; |
||||
unsigned long jifs; |
||||
time_t current_unix_time; |
||||
|
||||
jifs = jiffies; |
||||
cache = &get_cpu_var(ts3init_cache); |
||||
|
||||
update_cache_time(jifs, cache); |
||||
|
||||
current_unix_time = cache->unix_time; |
||||
|
||||
*packet_index = current_unix_time % 8; |
||||
|
||||
result = ts3init_get_cookie_seed(current_unix_time, |
||||
*packet_index, &cache->cookie_cache, seed); |
||||
|
||||
if (result) |
||||
{ |
||||
(*cookie)[0] = result[0]; |
||||
(*cookie)[1] = result[1]; |
||||
} |
||||
put_cpu_var(ts3init_cache); |
||||
return result; |
||||
} |
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
#ifndef _TS3INIT_CACHE_H |
||||
#define _TS3INIT_CACHE_H |
||||
|
||||
time_t get_cached_unix_time(void); |
||||
|
||||
bool get_cookie_for_package_index(u8 packet_index, const u8* seed, u64 (*cookie)[2]); |
||||
|
||||
bool get_current_cookie(const u8* seed, u64 (*cookie)[2], u8 *packet_index); |
||||
|
||||
#endif /* _TS3INIT_CACHE_H */ |
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
#ifndef _TS3INIT_COOKIE_SEED_H |
||||
#define _TS3INIT_COOKIE_SEED_H |
||||
|
||||
enum { |
||||
COOKIE_SEED_LEN = 60 |
||||
}; |
||||
|
||||
static inline bool hex2int_seed(const char *src, __u8* dst) |
||||
{ |
||||
int i, j; |
||||
for (i = 0; i < 60; ++i) |
||||
{ |
||||
int v = 0; |
||||
for ( j = 0; j < 2; ++j) |
||||
{ |
||||
uint8_t byte = *src++; |
||||
if (byte >= '0' && byte <= '9') byte = byte - '0'; |
||||
else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10; |
||||
else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10; |
||||
else return false; |
||||
v = (v << 4) | byte; |
||||
} |
||||
*dst++ = v; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
#endif /* _TS3INIT_COOKIE_SEED_H */ |
@ -1,4 +1,27 @@
@@ -1,4 +1,27 @@
|
||||
#ifndef _TS3INIT_TARGET_H |
||||
#define _TS3INIT_TARGET_H |
||||
|
||||
/* Common Enums for targets */ |
||||
enum |
||||
{ |
||||
TARGET_COMMON_VALID_MASK = (1 << 0) -1 |
||||
}; |
||||
|
||||
/* Enums and structs for set_cookie */ |
||||
enum |
||||
{ |
||||
TARGET_SET_COOKIE_ZERO_RANDOM_SEQUENCE = 1 << 0, |
||||
TARGET_SET_COOKIE_SEED = 1 << 1, |
||||
TARGET_SET_COOKIE_VALID_MASK = (1 << 2) - 1 |
||||
}; |
||||
|
||||
|
||||
struct xt_ts3init_set_cookie_tginfo |
||||
{ |
||||
__u8 common_options; |
||||
__u8 specific_options; |
||||
__u16 reserved1; |
||||
__u8 cookie_seed[COOKIE_SEED_LEN]; |
||||
}; |
||||
|
||||
#endif /* _TS3INIT_TARGET_H */ |
||||
|
Loading…
Reference in new issue