filter for adding/checking TCP MD5 option
Hello,
I'm currently starting work on building a netfilter addition that can add the MD5 option field to a TCP connection. This is a
rarely used option, best documented in RFC2385, and is to my knowledge only used as a security mechanism for BGP sessions.
Linux is currently not able to support this. Unfortunately, it is quite commonly used for BGP sessions.
What I would like to be able to do is something like the following:
# iptables -t mangle -A OUTPUT -p tcp -d 1.2.3.4 --dport 179 -j TCPMD5 --md5-key=s3cr3t # iptables -t mangle -A PREROUTING -p tcp -s 1.2.3.4 --dport 179 -j TCPMD5 --md5-key=s3cr3t
The first line would do something as follows:
- decrease MSS to make the extra option fit (a staggering 18 bytes extra in the TCP header)
- calculate the MD5 based on the key and the information contained in the packet
- insert the TCP MD5 option field, recalculate checksum and send the packet on its way
The second line would:
- check whether the MD5 matches the key
- DROP the packet if it doesn't match
- if the MD5 matches, strip off the MD5 option field, recalculate checksum and treat the packet as if nothing happened
As I'm a complete newbie when it comes to netfilter development, could anybody provide me some pointers on how to do
this in the right way ? Borrowing code from TCPMSS and using the Crypto API from the kernel for the MD5 checksum
are things I'm currently considering.
Thanks,
Remco van Mook
Hi,
Quoted 6 lines · Remco van Mook
# iptables -t mangle -A OUTPUT -p tcp -d 1.2.3.4 --dport 179 -j TCPMD5 --md5-key=s3cr3t # iptables -t mangle -A PREROUTING -p tcp -s 1.2.3.4 --dport 179 -j TCPMD5 --md5-key=s3cr3t
Quoted 7 lines
The first line would do something as follows:
- decrease MSS to make the extra option fit (a staggering 18 bytes extra in the TCP header)
- calculate the MD5 based on the key and the information contained in the packet
- insert the TCP MD5 option field, recalculate checksum and send the packet on its way
ok
Quoted 5 lines
The second line would:
- check whether the MD5 matches the key
- DROP the packet if it doesn't match
- if the MD5 matches, strip off the MD5 option field, recalculate checksum and treat the packet as if nothing happened
nok. the target should not drop the packet itself, you should write a match which only accepts valid MD5. And the target would only have to strip MD5. This allows all standard constructions, logging, etc... :
# iptables -t mangle -N novalidmd5 # iptables -t mangle -A novalidmd5 -j LOG --log-prefix "invalid MD5: " # iptables -t mangle -A novalidmd5 -j DROP
# iptables -t mangle -A PREROUTING -p tcp -s 1.2.3.4 --dport 179 ! -m tcpmd5 --md5-key=s3cr3t -j novalidmd5 # iptables -t mangle -A PREROUTING -p tcp -s 1.2.3.4 --dport 179 -j STRIPTCPMD5
Moreover, if '-m tcpmd5' without md5key could match any MD5 packet, it would allow to direct the packet to a user chain reponsible to check between several known secrets, or to systematically strip the MD5 for example.
Quoted 3 lines
As I'm a complete newbie when it comes to netfilter development, could anybody provide me some pointers on how to do
this in the right way ?
I think that everyone does the same : take a simple match/target, remove the simple code, insert yours and start to understand how it works ;-)
Cheers,
Willy
Quoted 8 lines · Remco van Mook
The first line would do something as follows:
- decrease MSS to make the extra option fit (a staggering 18 bytes extra in the TCP header)
- calculate the MD5 based on the key and the information contained in the packet
- insert the TCP MD5 option field, recalculate checksum and send the packet on its way
Quoted 5 lines
The second line would:
- check whether the MD5 matches the key
- DROP the packet if it doesn't match
- if the MD5 matches, strip off the MD5 option field, recalculate checksum and treat the packet as if nothing happened
Some conceptual changes to make this more in line with iptables:
1. a match which can verify the MD5 option. The same match should also be able to check if a MD5 option exists or not, allowing differentiation into the following packet classes:
- Has valid TCPMD5 option
- Has invalid TCPMD5 option
- Does not have TCPMD5 option
2. a target which can add/delete/modify the MD5 option.
Note: As this is TCP payload dependent you need to make sure packets are defragmented before processed. This is most easily accomplished by depending on conntrack.
Quoted 4 lines
As I'm a complete newbie when it comes to netfilter development, could anybody provide me some pointers on how to do this in the right way? Borrowing code from TCPMSS and using the Crypto API from the kernel for the MD5 checksum are things I'm currently considering.
Looks like a very reasonable approach to me.
If you look closely at the TCPMSS you will also notice there is a tcpmss match along the lines described above.
There is also a netfilter hacking howto which partially describes how to write matches/targets which may be good to read if you have not already.
Note: The userspace part is found in the iptables tree under extensions in case you have not found these already.
Regards
Henrik
Quoted 18 lines · Henrik Nordstrom
Quoted 8 lines · Remco van Mook
The first line would do something as follows:
- decrease MSS to make the extra option fit (a staggering 18 bytes extra in the TCP header)
- calculate the MD5 based on the key and the information contained in the packet
- insert the TCP MD5 option field, recalculate checksum and send the packet on its wayQuoted 5 lines
The second line would:
- check whether the MD5 matches the key
- DROP the packet if it doesn't match
- if the MD5 matches, strip off the MD5 option field, recalculate checksum and treat the packet as if nothing happenedSome conceptual changes to make this more in line with iptables:
Quoted 9 lines
1. a match which can verify the MD5 option. The same match should also be able to check if a MD5 option exists or not, allowing differentiation into the following packet classes:
- Has valid TCPMD5 option
- Has invalid TCPMD5 option
- Does not have TCPMD5 option2. a target which can add/delete/modify the MD5 option.
Thanks very much for that. That was indeed a flaw in my thinking. I started from the point that I'm not really interested in what the MD5 on incoming packets looks like anyway, because I don't believe in the security properties that some people attribute to it. My only concern was to fool the other side into thinking that I'm actually using this security mechanism as well. Not a very decent thing to do, I agree.
Quoted 5 lines
Note: As this is TCP payload dependent you need to make sure packets are defragmented before processed. This is most easily accomplished by depending on conntrack.
...which is precisely something I'm not too happy about. A box running tcpmd5 would typically be a router handling positively *huge* amounts of data. Filtering is kept to an absolute minimum, and for the filtering I'm currently using nf-hipac, not netfilter. Adding a dependency on conntrack would, correct me if I'm wrong, require any connection that comes through the box to be tracked. Tens of thousands of connections would wind up in conntrack and eat loads of cpu.
The way BGP is organised, fragmented BGP packets would be horribly braindead and should not ever occur in the wild. I know that this doesn't fix the problem you described, but I would also very much like to see how a box from Vendor C or Vendor J would behave if it would get its BGP session handed to it in fragments, with MD5 enabled.
This still leaves a hole, where things might go horribly wrong, I agree. Any other suggestions are more than welcome.
Thanks for your comments so far.
Regards,
Remco
Quoted 7 lines · Remco van Mook
...which is precisely something I'm not too happy about. A box running tcpmd5 would typically be a router handling positively *huge* amounts of data. Filtering is kept to an absolute minimum, and for the filtering I'm currently using nf-hipac, not netfilter. Adding a dependency on conntrack would, correct me if I'm wrong, require any connection that comes through the box to be tracked. Tens of thousands of connections would wind up in conntrack and eat loads of cpu.
If not conntrack you will need something else which defragments packets, or assume that the tcpmd5 packets will never arrive fragmented.
Quoted 5 lines
The way BGP is organised, fragmented BGP packets would be horribly braindead and should not ever occur in the wild. I know that this doesn't fix the problem you described, but I would also very much like to see how a box from Vendor C or Vendor J would behave if it would get its BGP session handed to it in fragments, with MD5 enabled.
They should not really care if it is fragmented or not at the BGP layer. Fragmented packets is an issue for the IP layer, not TCP. If BGP is run over TCP then the participating routers should all be running IP with a TCP implementation. Normally IP will defragment the packet before handing it to TCP where the MD5 option is then processed.
But I hope these TCP channels all have MTU Discovery enabled in which case there should not be any fragmented packets. In such case support for fragmented packets may be disabled in the IP kernel as a DoS security measure unless there is other services requiring it.
Regards
Henrik
Quoted 7 lines · Remco van Mook
Quoted 4 lines
Note: As this is TCP payload dependent you need to make sure packets are defragmented before processed. This is most easily accomplished by depending on conntrack.
...which is precisely something I'm not too happy about. A box running tcpmd5 would typically be a router handling positively *huge* amounts of data.
For this you can play with the NOTRACK target to escape tracking for normally routed packets.
Quoted 2 lines
Filtering is kept to an absolute minimum, and for the filtering I'm currently using nf-hipac, not netfilter.
Unless I'm wrong, nf-hipac's goal is to allow to keep maximum performance with *huge* amounts of rules.
Quoted 4 lines
Adding a dependency on conntrack would, correct
me if I'm wrong, require any connection that comes through the box to be tracked. Tens of thousands of connections would wind up in conntrack and eat loads of cpu.
For info, I have installed two netfilter boxes (P4/2.4 GHz) load-balanced behind 30 Mbps traffic. Last time I checked, there were 60k sessions in /proc/net/ip_conntrack and they received 1k sessions/second (all logged). All this with less than 12% CPU usage. Admittedly, there only are about one hundred rules.
Quoted 5 lines
The way BGP is organised, fragmented BGP packets would be horribly braindead and should not ever occur in the wild. I know that this doesn't fix the problem you described, but I would also very much like to see how a box from Vendor C or Vendor J would behave if it would get its BGP session handed to it in fragments, with MD5 enabled.
So you can log and drop BGP fragments to ensure that there's no problem. IIRC, the routers in front of the firewalls described above already block all fragments, and use BGP without problem.
Cheers,
Willy
Quoted 2 lines · Willy Tarreau
Unless I'm wrong, nf-hipac's goal is to allow to keep maximum performance with *huge* amounts of rules.
It is, nothing more, nothing less.
If there is only a minimal set of rules the performance is the same.
For bigger not very well structured rulesets nf-hipac is a clear win at the cost of higher memory usage.
Regards
Henrik