The CRD System (part 1)
Subsystems (2)
Hi all!
Last year, I wrote a paper about a new system to help defending a network against TCP SYN flooding attacks. I presented the idea at the NordSec conference and you can find the paper itself in the joint files (e-mail part 2).
For the purpose of this paper, I had implemented a prototype of what I called a Connection Request distribution system (for more details, see the actual paper) using netfilter. Now, if you find it interesting, you could develop it to a fully usable version (particularly changing the allocation of memory from static to dynamic). I can't work on that project anymore but you can contact me for any help.
Please, note that I wrote the program with a 2.4.23 kernel with the nfnetlink and Nfnetlink-ctnetlink patches from patch-o-matic. For any question, please contact me.
Emmanuel
extensions/libipt_SYNDIS.c +486 -0
@@ -0,0 +1,486 @@ +/* Shared library add-on to iptables to add SYN-ditributor functionnality support. */ + +/* So far, this code is only a test implementation to validate the idea in: +* "Distributed Defense Against Low-Bandwidth TCP SYN Flooding Attacks", Emmanuel +* Guiton (that's me), to be published (I wish). +*/ + +/* FIX ME & TO DO: +* - command could be more user friendly. Particularly regarding the compulsory +* specification of the lists of hosts on which the SYN requests are distributed. +* - exhaustive testing. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <string.h> +#include <arpa/inet.h> +#include <netinet/in.h> +#include <iptables.h> +#include <linux/netfilter_ipv4/ip_tables.h> +#include <linux/netfilter_ipv4/ipt_SYNDIS.h> + + +/* Function which prints out usage message. */ + +static void +help (void ) +{ +printf ( +"SYNDIS v%s options:\n" +" --sd-relays <ipaddr:p{port}>[:b{backlog size}][:t{timeout length}]\n" +" [<,ipaddr:p{port}>[:b{backlog size}][:t{timeout length}]...]\n" +" Required list of available hosts on which three-way handshakes\n" +" can be performed.\n" +" --sd-params [b{backlog size}][:][t{timeout length}]\n" +" Specify parameters of the host to be defended. You can \n" +" precise the backlog queue size (unit is number of half-open\n" +" connections, default is 2048) and the TCP timeout length in\n" +" seconds (default is 9).\n" +" See /proc/sys/net/ipv4/tcp_max_syn_backlog and\n" +" /proc/sys/net/ipv4/tcp_synack_retries to set these parameters\n" +" on the host.\n" +" --sd-gua <ipaddr>\n" +" Globally unique address of the target. I.e. address of the\n" +" target, as seen from the external network.\n\n" , +IPTABLES_VERSION ); +} + +static struct option opts[] = { + {"sd-params" ,1 ,0 ,'1' }, + {"sd-relays" ,1 ,0 ,'2' }, + {"sd-gua" ,1 ,0 ,'3' }, + {0 } +}; + + +/* Initialize the target. */ +static void +init (struct ipt_entry_target *target,unsigned int *nfcache) +{ +struct ipt_sd_data *sd_data = (struct ipt_sd_data *) target->data; +int i; +struct in_addr temp_addr; + +/* the use of nfcahe is still very unclear for me, +* but this seems to be a safe option. */ + *nfcache |=NFC_UNKNOWN ; + + sd_data->target_backlog_size=SD_DEFAULT_BACKLOG_SIZE ; + sd_data->target_tcp_timeout=SD_DEFAULT_TCP_TIMEOUT ; +inet_aton ("0.0.0.0" ,&temp_addr); + sd_data->gua_target=temp_addr.s_addr; +for (i=0 ;i<15 ;i++) + { +inet_aton ("0.0.0.0" ,&temp_addr); + sd_data->host_list[i].address=temp_addr.s_addr; + sd_data->host_list[i].port=0 ; + sd_data->host_list[i].backlog_size=SD_DEFAULT_BACKLOG_SIZE ; + sd_data->host_list[i].tcp_timeout=SD_DEFAULT_TCP_TIMEOUT ; + } +} + + +/* Fix me: displaying all the information is not necessary +* useful and may fill the screen...*/ + +/* Prints out the targinfo. */ +static void +const struct ipt_ip *ip, +const struct ipt_entry_target *target, +int numeric) +{ +struct ipt_sd_data *sd_data = (struct ipt_sd_data *) target->data; +unsigned int i; +struct in_addr temp_addr; + +printf ("| target parameters: backlog size %ld tcp timeout %lds " , + sd_data->target_backlog_size, sd_data->target_tcp_timeout); + + i=0 ; + temp_addr.s_addr = sd_data->host_list[i].address; +while (strcmp (inet_ntoa (temp_addr),"0.0.0.0" )!=0 && i<15 ) + { +printf ("| relay %d: %s port %d backlog size %ld tcp timeout %lds " , i+1 , +inet_ntoa (temp_addr), sd_data->host_list[i].port, + sd_data->host_list[i].backlog_size, sd_data->host_list[i].tcp_timeout); + i++; + temp_addr.s_addr = sd_data->host_list[i].address; + } + + temp_addr.s_addr = sd_data->gua_target; +if (strcmp ("0.0.0.0" ,inet_ntoa (temp_addr))) +printf ("| GUA address: %s " ,inet_ntoa (temp_addr)); +} + + +/* Function which parses :p argument */ +static int +parse_p (char *arg_str,struct ipt_sd_data *sd_data,int host_index) +{ +char *separator; + + arg_str++; +if (*arg_str=='\0' ) +exit_error (PARAMETER_PROBLEM ,"Value required after parameter :p in --sd-relays" ); + + separator=strchr (arg_str,'-' ); +if (separator) + { +exit_error (PARAMETER_PROBLEM ,"Only one port can be specified after option :p in --sd-relays" ); + } +else + { + sd_data->host_list[host_index].port =strtol (arg_str,NULL ,0 ); +if ( sd_data->host_list[host_index].port <1 || sd_data->host_list[host_index].port >65535 ) +exit_error (PARAMETER_PROBLEM ,"Invalid port number after option :p in --sd-relays" ); + } +return 1 ; +} + + +/* Function which parses :b and :t arguments. +* Caution: this function works differently than the previous one. +* the char *arg_str includes the argument initial in order to reuse +* the function with both b and t parameters. +*/ + +/* Fix me: does not impose any upper bound on backlog size of +* TCP timeout values. Ok in theory, in practice it cannot be +* any value. +*/ + +static int +parse_bt (char *arg_str,long int *param,int host_index) +{ +char argument, *name; + + name=malloc (sizeof (char )*7 ); +if (host_index >-1 ) +strcpy (name,"relays" ); +else +strcpy (name,"params" ); + + argument=*arg_str; + + arg_str++; +if (*arg_str=='\0' ) +exit_error (PARAMETER_PROBLEM ,"Value required after parameter :%c in --sd-%s" , argument, name); + +if ((*param=strtol (arg_str,NULL ,0 ))<1 ) +exit_error (PARAMETER_PROBLEM ,"Invalid value after parameter :%c in --sd-%s" , argument, name); + +return 1 ; +} + + +/* Function which parses relay data */ +static int +parse_relay (char *arguments,struct ipt_sd_data *sd_data,int host_index) +{ +char *separator, *next_separator; +int p=0 , b=0 , t=0 ; +struct in_addr temp_addr; + + separator=strchr (arguments,':' ); +if (separator) + { + *separator='\0' ; +if (!inet_aton (arguments, &(temp_addr))) +exit_error (PARAMETER_PROBLEM ,"Invalid address after --sd-relays" ); +else + sd_data->host_list[host_index].address = temp_addr.s_addr; + separator++; +if (*separator=='\0' ) +exit_error (PARAMETER_PROBLEM ,"Port number required in option --sd-relays" ); +while (*separator !='\0' ) + { +switch (*separator) + { +case 'p' : +if (p==1 ) +exit_error (PARAMETER_PROBLEM ,"Parameter :p specified twice in --sd-relays" ); + + next_separator=strchr (separator,':' ); +if (next_separator) + { + *next_separator='\0' ; + p=parse_p (separator, sd_data, host_index); + separator=next_separator+1 ; + } +else + { + p=parse_p (separator, sd_data, host_index); + *separator='\0' ;/* last argument, end of checking*/ + } +break ; + +case 'b' : +if (b==1 ) +exit_error (PARAMETER_PROBLEM ,"Parameter :b specified twice in --sd-relays" ); + + next_separator=strchr (separator,':' ); +if (next_separator) + { + *next_separator='\0' ; + b=parse_bt (separator, &(sd_data->host_list[host_index].backlog_size), host_index); + separator=next_separator+1 ; + } +else + { + b=parse_bt (separator, &(sd_data->host_list[host_index].backlog_size), host_index); + *separator='\0' ;/* last argument, end of checking*/ + } +break ; + +case 't' : +if (t==1 ) +exit_error (PARAMETER_PROBLEM ,"Parameter :b specified twice in --sd-relays" ); + + next_separator=strchr (separator,':' ); +if (next_separator) + { + *next_separator='\0' ; + t=parse_bt (separator, &(sd_data->host_list[host_index].tcp_timeout), host_index); + separator=next_separator+1 ; + } +else + { + t=parse_bt (separator, &(sd_data->host_list[host_index].tcp_timeout), host_index); + *separator='\0' ;/* last argument, end of checking*/ + } +break ; + +default :/* unknown parameter after ":" */ +exit_error (PARAMETER_PROBLEM ,"Invalid option after \":\" in --sd-relays" ); +break ; + } + } + } +else /* no colon found means only ip address provided */ + { +exit_error (PARAMETER_PROBLEM ,"Missing at least one parameter after --sd-relays" ); + } + +if (!p) +exit_error (PARAMETER_PROBLEM ,"Port number required in option --sd-relays" ); + +return 1 ; +} + + +/* Function which parses command options */ +static int +parse (int c,char **argv,int invert,unsigned int *flags, +const struct ipt_entry *entry, +struct ipt_entry_target **target) +{ +struct ipt_sd_data *sd_data = (struct ipt_sd_data *) (*target)->data; +char *separator, *data; +int host_index=0 ; +int params_b=0 , params_t=0 ; +struct in_addr temp_addr; + + +if (entry->ip.proto !=IPPROTO_TCP ) + { +exit_error (PARAMETER_PROBLEM ,"TCP protocol is required" ); +return 0 ; + } + +/* Fix me: this check is not enough. User could enter a network address, for example. +* The user MUST NOT enter any address other than the address of the host to be defended. +*/ +if (!(entry->nfcache &NFC_IP_DST )) + { +exit_error (PARAMETER_PROBLEM ,"Single host destination (=host to be defended) is required" ); +return 0 ; + } + +switch (c) + { +case '1' :/* --sd-params */ +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM ,"Unexpected `!' after --sd-params" ); + + data=optarg; +if (*data=='\0' ) +exit_error (PARAMETER_PROBLEM ,"Parameter b or t expected after --sd-params" ); + +while (*data!='\0' ) + { +switch (*data) + { +case 'b' : +if (params_b==1 ) +exit_error (PARAMETER_PROBLEM ,"Parameter :b specified twice in --sd-params" ); + + separator=strchr (data,':' ); +if (separator) + { + *separator='\0' ; + params_b=parse_bt (data, &(sd_data->target_backlog_size),-1 ); + data=separator+1 ; + } +else + { + params_b=parse_bt (data, &(sd_data->target_backlog_size),-1 ); + *data='\0' ;/* last argument, end of checking*/ + } +break ; + +case 't' : +if (params_t==1 ) +exit_error (PARAMETER_PROBLEM ,"Parameter :t specified twice in --sd-params" ); + + separator=strchr (data,':' ); +if (separator) + { + *separator='\0' ; + params_t=parse_bt (data, &(sd_data->target_tcp_timeout),-1 ); + data=separator+1 ; + } +else + { + params_t=parse_bt (data, &(sd_data->target_tcp_timeout),-1 ); + *data='\0' ;/* last argument, end of checking*/ + } +break ; + +default :/* unknown parameter after ":" */ +exit_error (PARAMETER_PROBLEM ,"Invalid parameter identifier after \":\" in --sd-params" ); +break ; + } + } +break ; + +case '2' :/* --sd-relays */ +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM ,"Unexpected `!' after --sd-relays" ); + + data=optarg; +if (*data=='\0' )/* Should not happen */ +exit_error (PARAMETER_PROBLEM ,"Empty parameters in --sd-relays" ); +while (*data!='\0' ) + { + separator=strchr (data,',' ); +if (separator) + { + *separator='\0' ; +parse_relay (data, sd_data, host_index); + data=separator+1 ; + host_index++; + } +else /* last relay: set data to '\0' */ + { +parse_relay (data, sd_data, host_index); + *data='\0' ; + } + } + *flags+=1 ; +break ; + +case '3' :/* --sd-gua */ +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM ,"Unexpected `!' after --sd-gua" ); + +if (!inet_aton (optarg, &(temp_addr))) +exit_error (PARAMETER_PROBLEM ,"Invalid address after --sd-gua" ); +else + sd_data->gua_target=temp_addr.s_addr; + +break ; + +default : +return 0 ; + } + +return 1 ;/* Check went fine.*/ +} + +/* Fix me: +* Purging data if --sd-relays is not specified? +* Or modifying --sd-relays in --sd-add-relay (more user friendly) +*/ +/* Final check; */ +static void final_check (unsigned int flags) +{ +if (!flags) +exit_error (PARAMETER_PROBLEM ,"Option --sd-relays required in target SYNDIS" ); +else + { +if (flags!=1 )/* Should not happen. */ +exit_error (PARAMETER_PROBLEM ,"Undefined parameter error in target SYNDIS" ); + } +} + + +/* Saves the union ipt_targinfo in parsable form to stdout. */ +static void +save (const struct ipt_ip *ip,const struct ipt_entry_target *target) +{ +struct ipt_sd_data *sd_data = (struct ipt_sd_data *) target->data; +int i=0 ; +struct in_addr temp_addr; + +printf ("--sd-relays" ); +while (sd_data->host_list[i].port!=0 && i<15 ) + { + temp_addr.s_addr = sd_data->host_list[i].address; +printf (" %s:p%d" ,inet_ntoa (temp_addr), sd_data->host_list[i].port); + +if (sd_data->host_list[i].backlog_size!=SD_DEFAULT_BACKLOG_SIZE + && sd_data->host_list[i].tcp_timeout!=SD_DEFAULT_TCP_TIMEOUT ) + { +printf (":b%ld:t%ld" , sd_data->host_list[0 ].backlog_size, sd_data->host_list[0 ].tcp_timeout); + } +else + { +if (sd_data->host_list[i].backlog_size!=SD_DEFAULT_BACKLOG_SIZE ) +printf (":b%ld" , sd_data->host_list[i].backlog_size); +if (sd_data->host_list[i].tcp_timeout!=SD_DEFAULT_TCP_TIMEOUT ) +printf (":t%ld" , sd_data->host_list[i].tcp_timeout); + } + i++; + } + +if (sd_data->host_list[0 ].backlog_size!=SD_DEFAULT_BACKLOG_SIZE + && sd_data->host_list[0 ].tcp_timeout!=SD_DEFAULT_TCP_TIMEOUT ) + { +printf (" --sd-params :b%ld:t%ld " , sd_data->host_list[0 ].backlog_size, + sd_data->host_list[0 ].tcp_timeout); + } +else + { +if (sd_data->host_list[0 ].backlog_size!=SD_DEFAULT_BACKLOG_SIZE ) +printf (" --sd-params :b%ld" , sd_data->host_list[0 ].backlog_size); +if (sd_data->host_list[0 ].tcp_timeout!=SD_DEFAULT_TCP_TIMEOUT ) +printf (" --sd-params :t%ld" , sd_data->host_list[0 ].tcp_timeout); + } + + temp_addr.s_addr = sd_data->gua_target; +if (!strcmp (inet_ntoa (temp_addr),"0.0.0.0" )) +printf (" --sd-gua %s" ,inet_ntoa (temp_addr)); +} + +static +struct iptables_target syn_distributor += {NULL , +"SYNDIS" , +IPTABLES_VERSION , +IPT_ALIGN (sizeof (struct ipt_sd_data )), +IPT_ALIGN (sizeof (struct ipt_sd_data )), + &help, + &init, + &parse, + &final_check, + &print, + &save, + opts +}; + +void _init (void ) +{ +register_target (&syn_distributor); +}
include/linux/netfilter_ipv4/ip_conntrack_core.h +7 -1
@@ -50,9 +50,15 @@ return NF_ACCEPT; } + +/* SYNDIS specific. Change conntrack information. */ +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +int ip_ct_sd_change_conntrack_info (struct ip_conntrack *conntrack,u_int32_t new_ip,u_int16_t new_tcpport); +#endif + +extern struct list_head *ip_conntrack_hash;extern struct list_head ip_conntrack_ordered_list;extern struct list_head ip_conntrack_expect_list;DECLARE_RWLOCK_EXTERN (ip_conntrack_lock);#endif /* _IP_CONNTRACK_CORE_H */ -
include/linux/netfilter_ipv4/ip_conntrack.h +18 -0
@@ -223,6 +223,14 @@ } nat;#endif /* CONFIG_IP_NF_NAT_NEEDED */ +/* SYNDIS counter, SYNDIS specific. Important: must be initialized to NULL +* (done in ip_conntrack_core.c (ip_conntrack_alloc())). +*/ +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +/* Used to identify the conntrack counter which depends on this conntrack */ +unsigned int *ipt_sd_syndis_counter; +#endif + };/* get master conntrack via master expectation */
@@ -322,6 +330,12 @@ IPCT_PROTOINFO, IPCT_HELPINFO, IPCT_NATINFO, +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +/* Added for ipt_SYNDIS module's purpose. */ +IPCT_SD_TCP_ASSURED , +IPCT_SD_LAST_FINACK , +IPCT_SD_LAST_3WH_ACK +#endif };#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
@@ -375,5 +389,9 @@static inline void ip_conntrack_do_cached_events (struct ip_conntrack *ct) {}#endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */ +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +extern void ip_ct_proto_tcp_set_timeout_syn_received (unsigned long new_value); +#endif +#endif /* __KERNEL__ */ #endif /* _IP_CONNTRACK_H */
include/linux/netfilter_ipv4/ip_conntrack_tcp.h +30 -0
@@ -23,6 +23,36 @@/* Poor man's window tracking: sequence number of valid ACK handshake completion packet */ u_int32_t handshake_ack; +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +u_int32_t handshake_seq;/* the sequence number of valid _INITIAL_ SYN */ +u_int32_t new_handshake_ack;/* value of handshake_ack from the target. */ +/* Various flags: */ + u_int16_t freeze_state: 1, /* stops the analysis of the conntrack state. Used while +* moving the conntrack from a relay to the target. */ + syndis_on: 1, /* On that connection, the SYN packet was directed to a relay */ + last_doff: 4, /* tcp header length, including options. */ + rcvr_doff: 4, /* receiver doff, used to retrieve e.g. receiver timestamp */ + ip_df: 1, /* IP don't fragment. See the remark about ip field hereafter. */ + unused: 5; /* Some free space to implement more things. */ + +u_int32_t original_opts[10 ];/* TCP options in the original direction from ACK packet */ +u_int32_t reply_opts[10 ];/* regularly updated TCP options in the reply direction */ +/* Logic would not place these here as they are not tcp fields. */ +struct timeval original_ack_time;/* used to update sender timestamp in SYNDIS messages. */ +u_int32_t original_ack_timestamp;/* Idem. Value of client timestamp in ACK message */ +u_int32_t reply_ack_timestamp;/* Value of relay timestamp in ACK message (from client). */ + +/* Record first SYN packet if conntrack->proto.tcp.syndis is on. +* if the connection is legitimate, this packet has to be resent later. */ +u_int8_t *ipt_sd_original_syn_packet; +u_int16_t ipt_sd_syn_packet_length; + +/* One data packet may have to be recorded and sent immediately after +* the last message of the three-way handshake; it is recorded here +* thanks to the ipt_sd_record_data_packet function. */ +u_int8_t *ipt_sd_first_data_packet; +u_int16_t ipt_sd_packet_length; +#endif };#endif /* _IP_CONNTRACK_TCP_H */
include/linux/netfilter_ipv4/ip_conntrack_tuple.h +5 -0
@@ -18,6 +18,11 @@struct {u_int16_t port; +/* SYNDIS specific */ +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +u_int32_t seq_offset;/* (receiver) sequence number offset */ +u_int32_t ts_offset;/* (receiver) timestamp offset */ +#endif } tcp;struct {u_int16_t port;
include/linux/netfilter_ipv4/ipt_SYNDIS.h +59 -0
59 lines not on this page · raw message
net/ipv4/netfilter/ip_conntrack_core.c +54 -0
66 lines not on this page · raw message
net/ipv4/netfilter/ip_conntrack_proto_tcp.c +122 -0
164 lines not on this page · raw message
net/ipv4/netfilter/ip_conntrack_standalone.c +4 -0
@@ -504,3 +504,7 @@EXPORT_SYMBOL (ip_conntrack_chain);EXPORT_SYMBOL (ip_conntrack_event_cache);#endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */ +#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) +EXPORT_SYMBOL (ip_ct_proto_tcp_set_timeout_syn_received); +EXPORT_SYMBOL (ip_ct_sd_change_conntrack_info); +#endif
net/ipv4/netfilter/ip_nat_core.c +0 -12
@@ -650,18 +650,6 @@place_in_hashes (conntrack, info); } -#if defined(CONFIG_IP_NF_TARGET_SYNDIS ) || defined(CONFIG_IP_NF_TARGET_SYNDIS_MODULE ) -/* initialize SYNDIS translation of sequence number. Use is specific to SYNDIS module -* and these are not used by any other module. */ -unsigned int j; -for (j=0 ; j<IP_NAT_MAX_MANIPS ; j++) - { - conntrack->nat.info.manips[j].manip.u.tcp.seq_offset =0 ; - conntrack->nat.info.manips[j].manip.u.tcp.ts_offset =0 ; - } -#endif - -return NF_ACCEPT ; }
net/ipv4/netfilter/ip_nat_proto_tcp.c +101 -0
123 lines not on this page · raw message
net/ipv4/netfilter/ipt_SYNDIS.c +1147 -0
1,147 lines not on this page · raw message
net/ipv4/netfilter/Makefile +1 -0
@@ -110,6 +110,7 @@ obj-$(CONFIG_IP_NF_TARGET_NETLINK) += ipt_NETLINK.o obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o obj-$(CONFIG_IP_NF_TARGET_TCPMSS) += ipt_TCPMSS.o +obj-$(CONFIG_IP_NF_TARGET_SYNDIS) += ipt_SYNDIS.o # generic ARP tables obj-$(CONFIG_IP_NF_ARPTABLES) += arp_tables.o