scrubbing support in Netfilter
Here is the iptables patch.
commit c23a3f536b11396c2a11df39fe0ae8386d5a12b1
Author: Nicolas Bareil <[email protected]> Date: Tue May 27 17:01:00 2008 +0200
scrubbing support
extensions/libip6t_SCRUB.c new file +218 -0
@@ -0,0 +1,218 @@ +/* Shared library add-on to iptables for the SCRUB target +* (C) 2008 by Nicolas Bareil <[email protected]> +* +* This program is distributed under the terms of GNU GPL +*/ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <getopt.h> +#include <iptables.h> + +#include <linux/netfilter_ipv4/ip_tables.h> +#include <linux/netfilter_ipv6/ip6_tables.h> + +#include <linux/netfilter/ipt_scrub.h> +#include <linux/netfilter/nf_conntrack_common.h> + +#define SCRUB_ALL (SCRUB_IP_TOS_ADJUST|SCRUB_IP_RAND_ID_ADJUST|SCRUB_IP_TTL_ADJUST|SCRUB_TCP_SEQ_ADJUST|SCRUB_TCP_OPT_TIMESTAMP_ADJUST|SCRUB_TCP_OPT_MSS_ADJUST) + +static void init (struct xt_entry_target *t) +{ +struct ipt_scrub_info *info = (struct ipt_scrub_info *) t->data; + + info->flags =0 ; + info->ttl.scrubbing_default =SCRUB_NORMALIZE_TTL ; + info->ttl.modifying_threshold =SCRUB_TTL_LOW_THRESHOLD ; + info->ttl.dropping_threshold =SCRUB_TTL_DROP_IF_LOWER ; +} + +static void help (void ) +{ +printf ( +"SCRUB target v%s options\n" +" --rand-ipid value Set a random IP ID\n" +,XTABLES_VERSION ); +} + +static int parse (int c,char **argv,int invert,unsigned int *flags, +const void *entry, +struct xt_entry_target **target) +{ +struct ipt_scrub_info *info = (struct ipt_scrub_info *) (*target)->data; + + *flags=1 ; + +switch (c) { +case '!' : +/* do not overwrite "--no-something" */ + info->flags= (SCRUB_ALL & ~info->flags) ; + +case 'a' : + info->flags = (info->flags & ~SCRUB_IP_TOS_ADJUST ); +break ; +case '0' : + info->flags |=SCRUB_IP_TOS_ADJUST ; +break ; + +case 'b' : + info->flags = (info->flags & ~SCRUB_IP_RAND_ID_ADJUST ); +break ; +case '1' : + info->flags |=SCRUB_IP_RAND_ID_ADJUST ; +break ; + +case 'j' : + info->flags = (info->flags & ~SCRUB_IP_TTL_ADJUST ); +break ; +case '9' : + info->flags |=SCRUB_IP_TTL_ADJUST ; +break ; + +case '2' : +if (string_to_number (optarg,0 ,255 , (unsigned int *) &(info->ttl.scrubbing_default)) ==-1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ; + +case '3' : +if (string_to_number (optarg,0 ,255 , (unsigned int *) &(info->ttl.modifying_threshold)) ==-1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ; + +case '4' : +if (string_to_number (optarg,0 ,255 , (unsigned int *) &(info->ttl.dropping_threshold)) ==-1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ; + +case 'd' : + info->flags = (info->flags & ~SCRUB_IP_TTL_ADJUST ); +break ; +case '5' : + info->flags |=SCRUB_IP_TTL_ADJUST ; +break ; + +case 'e' : + info->flags = (info->flags & ~SCRUB_TCP_SEQ_ADJUST ); +break ; +case '6' : + info->flags |=SCRUB_TCP_SEQ_ADJUST ; +break ; + +case 'f' : + info->flags = (info->flags & ~SCRUB_TCP_OPT_TIMESTAMP_ADJUST ); +break ; +case '7' : + info->flags |=SCRUB_TCP_OPT_TIMESTAMP_ADJUST ; +break ; + +case 'g' : + info->flags = (info->flags & ~SCRUB_TCP_OPT_MSS_ADJUST ); +break ; +case '8' : + info->flags |=SCRUB_TCP_OPT_MSS_ADJUST ; +break ; + +default : +return 0 ; + + } + +return 1 ; +} + +static void final_check (unsigned int flags) +{ + +} + +static void save (const void *ip,const struct xt_entry_target *target) +{ +} + +static void const void *ip, +const struct xt_entry_target *target,int numeric) +{ +const struct ipt_scrub_info *info = + (struct ipt_scrub_info *) target->data; + +printf ("SCRUB " ); + +if (info->flags ==SCRUB_ALL ) { +printf ("everything" ); +return ; + } + +if (info->flags &SCRUB_TCP_SEQ_ADJUST ) +printf ("random-tcp-isn " ); + +if (info->flags &SCRUB_IP_RAND_ID_ADJUST ) +printf ("random-ip-id " ); + +if (info->flags &SCRUB_IP_TOS_ADJUST ) +printf ("zeroify-ip-tos " ); + +if (info->flags &SCRUB_IP_TTL_ADJUST ) +printf ("default-ttl=%u low-threshold=%u dropping-threschold=%u " + , info->ttl.scrubbing_default + , info->ttl.modifying_threshold + , info->ttl.dropping_threshold); + +if (info->flags &SCRUB_TCP_OPT_MSS_ADJUST ) +printf ("check-tcp-mss " ); + +if (info->flags &SCRUB_TCP_OPT_TIMESTAMP_ADJUST ) +printf ("random-tcp-timestamp " ); + +if (info->flags &SCRUB_IP_SEALED_TTL_ADJUST ) +printf ("ip-ttl-sealed " ); +} + +static struct option opts[] = { + {"scrub-everything" ,0 ,0 ,'!' }, + {"ip-zero-tos" ,0 ,0 ,'0' }, + {"ip-rand-id" ,0 ,0 ,'1' }, + + {"ip-ttl-scrub" ,0 ,0 ,'9' }, + {"ip-ttl-default" ,1 ,0 ,'2' }, + {"ip-ttl-low-threshold" ,1 ,0 ,'3' }, + {"ip-ttl-dropping-threshold" ,1 ,0 ,'4' }, + {"ip-ttl-sealed" ,0 ,0 ,'5' }, + + {"tcp-rand-seq" ,0 ,0 ,'6' }, + {"tcp-opt-rand-timestamp" ,0 ,0 ,'7' }, + {"tcp-opt-check-mss" ,0 ,0 ,'8' }, + +/* inverse parameter, is there another way */ + + {"no-ip-zero-tos" ,0 ,0 ,'a' }, + {"no-ip-rand-id" ,0 ,0 ,'b' }, + + {"no-ip-ttl-scrub" ,0 ,0 ,'c' }, + {"no-ip-ttl-sealed" ,0 ,0 ,'d' }, + + {"no-tcp-rand-seq" ,0 ,0 ,'e' }, + {"no-tcp-opt-rand-timestamp" ,0 ,0 ,'f' }, + {"no-tcp-opt-check-mss" ,0 ,0 ,'g' }, + + {0 } +}; + +static struct xtables_target SCRUB = { + .next =NULL , + .name ="SCRUB" , + .version =XTABLES_VERSION , + .size =XT_ALIGN (sizeof (struct ipt_scrub_info )), + .userspacesize =XT_ALIGN (sizeof (struct ipt_scrub_info )), + .help = help, + .init = init, + .parse = parse, + .final_check = final_check, + .print = print, + .save = save, + .extra_opts = opts +}; + +void _init (void ) +{ +xtables_register_target (&SCRUB ); +}
extensions/libipt_SCRUB.c new file +218 -0
218 lines not on this page · raw message
include/linux/netfilter/nf_conntrack_common.h +25 -0
@@ -132,4 +132,29 @@ enum ip_conntrack_expect_events { IPEXP_NEW = (1 << IPEXP_NEW_BIT), }; +enum nf_scrub_transformations { +/* scrub tcp sequence adjusting */ +SCRUB_TCP_SEQ_ADJUST_BIT =1 , +SCRUB_TCP_SEQ_ADJUST = (1 <<SCRUB_TCP_SEQ_ADJUST_BIT ), + +SCRUB_IP_RAND_ID_ADJUST_BIT =2 , +SCRUB_IP_RAND_ID_ADJUST = (1 <<SCRUB_IP_RAND_ID_ADJUST_BIT ), + +SCRUB_IP_TTL_ADJUST_BIT =3 , +SCRUB_IP_TTL_ADJUST = (1 <<SCRUB_IP_TTL_ADJUST_BIT ), + +SCRUB_IP_TOS_ADJUST_BIT =4 , +SCRUB_IP_TOS_ADJUST = (1 <<SCRUB_IP_TOS_ADJUST_BIT ), + +SCRUB_TCP_OPT_MSS_ADJUST_BIT =5 , +SCRUB_TCP_OPT_MSS_ADJUST = (1 <<SCRUB_TCP_OPT_MSS_ADJUST_BIT ), + +SCRUB_TCP_OPT_TIMESTAMP_ADJUST_BIT =6 , +SCRUB_TCP_OPT_TIMESTAMP_ADJUST = (1 <<SCRUB_TCP_OPT_TIMESTAMP_ADJUST_BIT ), + +SCRUB_IP_SEALED_TTL_ADJUST_BIT =7 , +SCRUB_IP_SEALED_TTL_ADJUST = (1 <<SCRUB_IP_SEALED_TTL_ADJUST_BIT ), +}; + +#endif /* _NF_CONNTRACK_COMMON_H */
Quoted 5 lines · Nicolas Bareil
I developped a Netfilter module which performs packet normalization, the "scrubbing" feature of OpenBSD[1]. Normalized trafic offers the following possibilities :
I seem to remember that Linux's TCP or nf_conntrack already does some scrubbing.
Quoted 5 lines
The current patch achieves the following transformations :
* IPv4
- Random IP ID
- Zeroify ToS
Zeroify? Clearing the TOS is probably not a good idea because it defeats packet scheduling (if it uses TOS).
Quoted 4 lines
- TTL normalization
* TCP
- Random TCP Sequence
I wonder if Linux already has this.
Quoted 2 lines
- TCP Options
- Random Timestamp
Is this even RFC compatible?
Quoted 5 lines
- Sanity checks on MSS
The following patch is "self-contained" : only
nf_conntrack_proto_tcp.cis modified, the other files are new. The iptables patch is following this email.
I suggest you have a look at "Writing your own Netfilter modules" (abbreviated NFM in this reply) document from
http://jengelh.medozas.de/ , since I have seen a few outdated constructs in your code.
Review
Quoted 11 lines
(quoted diff) +11
-0+ ct = nf_ct_get(skb, &ctinfo); + if (!ct) { + DEBUGP("ipt_SCRUB: No conntrack matching this packet.\n"); + return XT_CONTINUE; + } + nfscrub = nfct_scrub(ct); + if (!nfscrub) + nf_scrub_setup(skb, ct, ctinfo, info); + + return XT_CONTINUE; /* XXX final decision: NF_ACCEPT ou XT_CONTINUE ? */ +}
XT_CONTINUE is correct here, since you are in the mangle table, so it is not an XXX case anymore :-)
Review
Quoted 8 lines
(quoted diff) +8
-0+static bool ipt_scrub_checkentry(const char *tablename, + const void *e, + const struct xt_target *target, + void *targinfo, + unsigned int hook_mask) +{ + return 1; /* There should be no invalid configuration possible */ +}
Remove this function.
Quoted 4 lines
(quoted diff) +4
-0+/* + * nf_scrub_ip_tos_adjust() + * + * Overwrite the IP ToS with 0. Things like SSH, telnet use
Review
Quoted 10 lines
(quoted diff) +10
-0+ * this field (in vain on Internet ?). + * + */ +void nf_scrub_ip_tos_adjust(struct iphdr *iph + , struct nf_conn *ct) +{ + NF_CT_ASSERT(iph); + csum_replace2(&iph->check, ntohs(iph->tos), 0); + iph->tos = 0; +}
Yes they do, don't just nuke it!
Quoted 4 lines
(quoted diff) +4
-0+bool nf_scrub_ipv4_adjust(struct sk_buff *skb + , struct nf_conn *ct + , enum ip_conntrack_info ctinfo) +{
Review
Quoted 10 lines
(quoted diff) +10
-0+ struct iphdr *iph = ip_hdr(skb); + enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); + struct tcphdr *th; + NF_CT_ASSERT(iph && ct); + + if (ct->scrub & SCRUB_TCP_OPTIONS) { + th = (struct tcphdr *) (((caddr_t)iph)+iph->ihl*4); + if (!nf_scrub_tcp_parse_options(th, ct, ctinfo)) + return NF_DROP; + }
Use tcp_hdr(). caddr_t? No, this is not BSD. [Also elsewhere.] Try the book :)
Quoted 1 line
+obj-$(CONFIG_NF_SCRUB) += ip6t_SCRUB.o
Write a single module, as described in NFM.
Review
Quoted 7 lines
(quoted diff) +7
-0+ if (nexthdr == IPPROTO_TCP) { + tcp = (struct tcphdr *)(skb_network_header(skb)+tcpoff); + nf_scrub_tcp_setup(skb, tcp, ct, ctinfo); + } + return true; +} +EXPORT_SYMBOL(nf_scrub_ipv6_setup);
tcp_hdr?
Quoted 10 lines
+++
b/net/netfilter/nf_conntrack_proto_tcp.c(quoted diff) +6
-0@@ -26,6 +26,10 @@ + if (test_bit(SCRUB_TCP_SEQ_ADJUST_BIT, &ct->scrub) + && dir == IP_CT_DIR_ORIGINAL) { + if (!nf_scrub_tcp_seq_adjust((struct tcphdr *)th, ct, ctinfo)) + return -NF_ACCEPT; + } +#endif
Review
Quoted 10 lines
(quoted diff) +10
-0+#ifdef CONFIG_NF_SCRUB_NEEDED + if (test_bit(SCRUB_TCP_SEQ_ADJUST_BIT, &ct->scrub) + && dir == IP_CT_DIR_REPLY) { + if (!nf_scrub_tcp_ack_adjust((struct tcphdr *)th, ct, ctinfo)) + return -NF_ACCEPT; + + if (!nf_scrub_tcp_sack_adjust((struct tcphdr *)th, ct, ctinfo)) + return -NF_ACCEPT; + } +#endif
Instead of adding casts to th, just remove the const from th, no?
extensions/libip6t_SCRUB.c line 36
Quoted 9 lines
Here is the iptables patch.
extensions/libip6t_SCRUB.c new file +7
-0+static void help (void ) +{ +printf ( +"SCRUB target v%s options\n" +" --rand-ipid value Set a random IP ID\n" +,XTABLES_VERSION ); +}
XTABLES_VERSION does not really reflect the SCRUB version. Just write "SCRUB target options:\n"
extensions/libip6t_SCRUB.c line 50
Quoted 13 lines
extensions/libip6t_SCRUB.c new file +13
-0+static int parse (int c,char **argv,int invert,unsigned int *flags, +const void *entry, +struct xt_entry_target **target) +{ +struct ipt_scrub_info *info = (struct ipt_scrub_info *) (*target)->data; + + *flags=1 ; + +switch (c) { +case '!' : +/* do not overwrite "--no-something" */ + info->flags= (SCRUB_ALL & ~info->flags) ; +
Possibly unwanted fallthrough?
Review
Quoted 2 lines
(quoted diff) +2
-0+ case '2': + if (string_to_number(optarg, 0, 255, (unsigned int *) &(info->ttl.scrubbing_default)) == -1)
Stack corruption. Don't just mindlessy use casts...
extensions/libip6t_SCRUB.c line 85
Quoted 12 lines
extensions/libip6t_SCRUB.c new file +12
-0+exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ; + +case '3' : +if (string_to_number (optarg,0 ,255 , (unsigned int *) &(info->ttl.modifying_threshold)) ==-1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ; + +case '4' : +if (string_to_number (optarg,0 ,255 , (unsigned int *) &(info->ttl.dropping_threshold)) ==-1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid threshold" ); +break ;
Same..
extensions/libip6t_SCRUB.c line 131
Quoted 4 lines
extensions/libip6t_SCRUB.c new file +4
-0+static void save (const void *ip,const struct xt_entry_target *target) +{ +} +
Something *is* *missing*. :)
Quoted 6 lines
extensions/libip6t_SCRUB.c new file +6
-0+static struct option opts[] = { + {"scrub-everything" ,0 ,0 ,'!' }, + {"ip-zero-tos" ,0 ,0 ,'0' }, + {"ip-rand-id" ,0 ,0 ,'1' }, + + {"ip-ttl-scrub" ,0 ,0 ,'9' },
extensions/libip6t_SCRUB.c line 185
Quoted 10 lines
extensions/libip6t_SCRUB.c new file +10
-0+ { "ip-ttl-default", 1, 0, '2'}, + { "ip-ttl-low-threshold", 1, 0, '3'}, + { "ip-ttl-dropping-threshold", 1, 0, '4'}, + { "ip-ttl-sealed", 0, 0, '5'}, + + { "tcp-rand-seq", 0, 0, '6'}, + { "tcp-opt-rand-timestamp", 0, 0, '7'}, + { "tcp-opt-check-mss", 0, 0, '8'}, + + /* inverse parameter, is there another way */
Well you could make use of iptables -j SCRUB ! --tcp-rand-seq. (See NFM book on how to scan for '!'.)
Review
Quoted 2 lines
(quoted diff) +2
-0+static struct xtables_target SCRUB = { + .next = NULL,
Unspecified fields are always initialized to NULL, so..
extensions/libipt_SCRUB.c
Quoted 18 lines
extensions/libip6t_SCRUB.c new file +13
-0+ .name = "SCRUB", + .version = XTABLES_VERSION, + .size = XT_ALIGN(sizeof(struct ipt_scrub_info)), + .userspacesize = XT_ALIGN(sizeof(struct ipt_scrub_info)), + .help = help, + .init = init, + .parse = parse, + .final_check = final_check, + .print = print, + .save = save, + .extra_opts = opts +}; +index 0000000..7d21083
extensions/libipt_SCRUB.c new file +0
-0
And _everything_ again. Suggest to combine all the code.
Quoted 8 lines · Jan Engelhardt
Quoted 4 lines · Nicolas Bareil
I developped a Netfilter module which performs packet normalization, the "scrubbing" feature of OpenBSD[1]. Normalized trafic offers the following possibilities :
I seem to remember that Linux's TCP or
nf_conntrackalready does some scrubbing.
No, unless you're refering to the unwanted side-effects from defragmentation and refragmentation for IPv4. I also don't want to include something like this in netfilter, NAT is already bad enough and the threats it *might* protect against seem a bit vague. Better throw your broken IDS out if can be fooled by changing TTLs.
I don't want to sound too discouraging though, I have no problem adding it to the pom-ng sources.list.
Quoted 8 lines
Quoted 5 lines
The current patch achieves the following transformations :
* IPv4
- Random IP ID
- Zeroify ToSZeroify? Clearing the TOS is probably not a good idea because it defeats packet scheduling (if it uses TOS).
Well .. ToS is only useful within your own administrative boundaries anyways, I've seen quite a few ISPs overwriting it during transit.
Quoted 6 lines
Quoted 4 lines
- TTL normalization
* TCP
- Random TCP SequenceI wonder if Linux already has this.
For forwarded traffic? No.
Quoted 5 lines
Quoted 3 lines
- TCP Options
- Random TimestampIs this even RFC compatible?
I assume its a random offset per connection, but still, no. You can also still distinguish different hosts by their clock rates.
Hello Jan,
Thanks for your review!
Quoted 2 lines · Jan Engelhardt
I seem to remember that Linux's TCP or
nf_conntrackalready does some scrubbing.
That's right, that's the reason why it's not that hard to compete with the OpenBSD features. The hard work is already done : fragmentation, tcp sequence management, packet in the tcp window, sanity checks for invalid constructed packets.
Quoted 5 lines
Quoted 2 lines
- Random IP ID
- Zeroify ToSZeroify? Clearing the TOS is probably not a good idea because it defeats packet scheduling (if it uses TOS).
Correct me if I'm wrong, but on Internet (I mean, just after the first ISP's router), the ToS is not used or even looked up.
I tried to make the scrubbing modular : you can choose to scrub a field or not. I'm open to discuss the default flags for --scrub-everything
Quoted 6 lines
Quoted 4 lines
- TTL normalization
* TCP
- Random TCP SequenceI wonder if Linux already has this.
Don't think so, AFAIK, Linux only randomizes the TCP sequence for its own packet (in OUTPUT), not for the packet in transit.
Quoted 4 lines
Quoted 2 lines
- TCP Options
- Random TimestampIs this even RFC compatible?
Yes I guess : I'm doing like for the TCP sequence stuff, when a conntrack is created, I randomize the timestamp, compute the difference (a substraction) between the original timestamp and mine that I store in the conntrack.
Then, when I receive a packet, I apply the offset to reflect the change :
the meaning is intact because we are not putting a random value at each packet.
Quoted 4 lines
I suggest you have a look at "Writing your own Netfilter modules" (abbreviated NFM in this reply) document from
http://jengelh.medozas.de/ , since I have seen a few outdated constructs in your code.
Ok, I noted your remarks and will submit tomorrow a new patch based on the feedbacks!
Signature · 2 lines
Nicolas Bareil http://chdir.org/~nico/ OpenPGP=0xAE4F7057 Fingerprint=34DB22091049FB2F33E6B71580F314DAAE4F7057
Quoted 8 lines · Nicolas Bareil
Quoted 5 lines
Quoted 2 lines
- Random IP ID
- Zeroify ToSZeroify? Clearing the TOS is probably not a good idea because it defeats packet scheduling (if it uses TOS).
Correct me if I'm wrong, but on Internet (I mean, just after the first ISP's router), the ToS is not used or even looked up.
I was not even thinking about the ISP using TOS, or the remote side, to address Patrick's concern that some ISP possibly zero it.
The TOS field may very well by an internal network (i.e. campus) and you could run into a situation where there is a Linux gateway with (xt_SCRUB) enabled, and another gateway after it (which _really_ connects to the ISP) that actually uses TOS for packet scheduling on the (perhaps slower) link towards the ISP.
SSH does set IPTOS_LOWDELAY for interactive sessions and clears it for bulk transfers (e.g. rsync), so there is definitely value for it within your own realm. I was not e
Quoted 6 lines · Patrick McHardy
No, unless you're refering to the unwanted side-effects from defragmentation and refragmentation for IPv4. I also don't want to include something like this in netfilter, NAT is already bad enough and the threats it *might* protect against seem a bit vague. Better throw your broken IDS out if can be fooled by changing TTLs.
Indeed, you're totally right : in an ideal world, it should be useless and avoided, but there are cases where you need "a workaround" because you have some legacy equipement, broken IDS, broken TCP/IP stack, etc.
Quoted 2 lines
I don't want to sound too discouraging though, I have no problem adding it to the pom-ng sources.list.
No problem, if you feel it better fits there, I'm ok with that.
Quoted 3 lines
I assume its a random offset per connection, but still, no. You can also still distinguish different hosts by their clock rates.
What do you mean precisely ? Variation of the TCP Timestamp ? TCP retransmission mechanisms ?
Thanks
Signature · 2 lines
Nicolas Bareil http://chdir.org/~nico/ OpenPGP=0xAE4F7057 Fingerprint=34DB22091049FB2F33E6B71580F314DAAE4F7057