1/36
DDoS protection using Netfilter/iptables
Jesper Dangaard Brouer
Senior Kernel Engineer, Red Hat RMLL Montpellier, July 2014
Email: brouer@redhat.com / netoptimizer@brouer.com / hawk@kernel.org
DDoS protection Using Netfilter/iptables Jesper Dangaard Brouer - - PowerPoint PPT Presentation
DDoS protection Using Netfilter/iptables Jesper Dangaard Brouer Senior Kernel Engineer, Red Hat RMLL Montpellier, July 2014 1/36 Email: brouer@redhat.com / netoptimizer@brouer.com / hawk@kernel.org DDoS protection using Netfilter/iptables
1/36
DDoS protection using Netfilter/iptables
Senior Kernel Engineer, Red Hat RMLL Montpellier, July 2014
Email: brouer@redhat.com / netoptimizer@brouer.com / hawk@kernel.org
2/36
DDoS protection using Netfilter/iptables
– Linux Kernel Developer at Red Hat – Edu: Computer Science for Uni. Copenhagen
– Linux user since 1996, professional since 1998
– OpenSource projects, author of
– ADSL-optimizer, CPAN IPTables::libiptc, IPTV-Analyzer
– Linux kernel, iproute2, iptables, libpcap and Wireshark
– Organizer of Netfilter Workshop 2013
3/36
DDoS protection using Netfilter/iptables
– show it is not enough
– solution is stalled ... how to work-around this
– Where is our pain points – Learn Netfilter tricks: boost performance a factor 20
4/36
DDoS protection using Netfilter/iptables
– First kill “irqbalance” – NIC hardware queue, are CPU aligned – Disable Ethernet flow-control
– single blocked hw queue blocks others
– Fix in kernel v3.5.0 commit 3ebe8fdeb0 (ixgbe: Set Drop_EN bit
when multiple Rx queues are present w/o flow control)
5/36
DDoS protection using Netfilter/iptables
– Attacking the 3-Way HandShake (3WHS) – End-host resource attack
– Attacker often spoofs src IP
TCP SYN Flooding Attacks and Common Mitigations
6/36
DDoS protection using Netfilter/iptables
– SYN “cache”
– SYN “backlog” of outstanding request sockets – Above limit, use SYN “cookies”
7/36
DDoS protection using Netfilter/iptables
– mini sock to represent a connection request
– SLAB behind have sizeof(struct tcp_request_sock)
– Structs embedded in each-other
(note, sizes will increase/change in more recent kernels)
8/36
DDoS protection using Netfilter/iptables
– Only increase, if legitimate traffic cause log:
– Adjust all these:
9/36
DDoS protection using Netfilter/iptables
– SYN packet
– SYN-ACK packet
– ACK packet
– SHA hash is computed with local secret
10/36
DDoS protection using Netfilter/iptables
– TCPReqQFullDoCookies : number of times a
SYNCOOKIE was replied to client
– TCPReqQFullDrop : number of times a SYN request
was dropped because syncookies were not enabled.
– /proc/sys/net/ipv4/tcp_syncookies = 2
11/36
DDoS protection using Netfilter/iptables
– Vulnerable for all floods
– NO LISTEN socket:
– LISTEN socket:
12/36
DDoS protection using Netfilter/iptables
– SYN cookies live under LISTEN lock
– http://thread.gmane.org/gmane.linux.network/232238
– Got rejected, because not general solution
– NFWS2013 got clearance as a first step solution
13/36
DDoS protection using Netfilter/iptables
– Wesley M. Eddy, describes SYN-proxy
Number 4, 2006, link: http://goo.gl/AC1AAZ
– Netfilter: iptables target SYNPROXY
– By Patrick McHardy, Martin Topholm and Me
– Solves SYN and ACK floods
14/36
DDoS protection using Netfilter/iptables
15/36
DDoS protection using Netfilter/iptables
– Will that be a performance issue?
– 2.904.128 pkts/sec -- NO LISTEN sock + no iptables rules – 252.032 pkts/sec -- LISTEN sock + no iptables rules
– 435.520 pkts/sec -- NO LISTEN sock + conntrack – 172.992 pkts/sec -- LISTEN sock + conntrack
– but I have some tricks for you ;-) – Plus fixed in kernel v3.15
16/36
DDoS protection using Netfilter/iptables
– Problem is insert and delete conntracks – Use to protect against SYN+ACK and ACK attacks
– Allow ACK pkts to create new connection – Disable via cmd: sysctl -w net/netfilter/nf_conntrack_tcp_loose=0
– Drop invalid pkts before reaching LISTEN socket
–
iptables -m state --state INVALID -j DROP
17/36
DDoS protection using Netfilter/iptables
– 179.027 pkts/sec
– 235.904 pkts/sec (listen lock scaling)
– 5.533.056 pkts/sec
18/36
DDoS protection using Netfilter/iptables
– SYN-ACKs don't auto create connections – Thus, changing “loose” setting is not important
– 230.348 pkts/sec
– 5.382.265 pkts/sec
– 5.408.307 pkts/sec
19/36
DDoS protection using Netfilter/iptables
– Due to conntrack insert/delete lock scaling
– 244.129 pkts/sec -- LISTEN sock + no iptables rules
– 172.992 pkts/sec -- LISTEN sock + conntrack
– 2.869.824 pkts/sec -- LISTEN sock + synproxy + conntrack
20/36
DDoS protection using Netfilter/iptables
In “raw” table, “notrack” SYN packets:
iptables -t raw -I PREROUTING -i $DEV -p tcp -m tcp --syn \
21/36
DDoS protection using Netfilter/iptables
– Need to get unknown ACKs (from 3WHS) to be
marked as INVALID state
/sbin/sysctl -w net/netfilter/nf_conntrack_tcp_loose=0
22/36
DDoS protection using Netfilter/iptables
– UNTRACKED == SYN packets – INVALID == ACK from 3WHS
iptables -A INPUT -i $DEV -p tcp -m tcp --dport $PORT \
23/36
DDoS protection using Netfilter/iptables
– Drop rest of state INVALID, contains SYN-ACK
iptables -A INPUT -i $DEV -p tcp -m tcp --dport $PORT \
– Because SYN cookies uses TCP options field
/sbin/sysctl -w net/ipv4/tcp_timestamps=1
24/36
DDoS protection using Netfilter/iptables
– Max possible entries 2 Mill
net/netfilter/nf_conntrack_max=2000000
– IMPORTANT: Also adjust hash bucket size
echo 500000 > /sys/module/nf_conntrack/parameters/hashsize
25/36
DDoS protection using Netfilter/iptables
–
https://github.com/netoptimizer/network-testing/blob/master/iptables/ip tables_synproxy.sh
– 2.869.824 pkts/sec – SYN-flood – 4.948.480 pkts/sec – ACK-flood – 5.653.120 pkts/sec – SYN+ACK-flood
26/36
DDoS protection using Netfilter/iptables
– Must match the backend-server TCP options – Manual setup (helper tool nfsynproxy) – Only one setting per rule – Not useful for DHCP based network
– Auto detect server TCP options – Simply allow first SYN through
27/36
DDoS protection using Netfilter/iptables
28/36
DDoS protection using Netfilter/iptables
–
Bug 1057352 - RFE: Improve SYN cookies calculations
29/36
DDoS protection using Netfilter/iptables
30/36
DDoS protection using Netfilter/iptables
– Made it significantly more expensive for attackers
– Removed central lock: Netfilter new conntracks
– Central lock: LISTEN socket lock
31/36
DDoS protection using Netfilter/iptables
– Insert / delete conntracks took central lock – Removed this central lock
– Minor conntrackd issues, fixed in kernel v3.16 ;-)
– 435.520 pkts/sec – conntrack with central lock – 1.626.786 pkts/sec – conntrack with parallel lock
32/36
DDoS protection using Netfilter/iptables
– Simply LISTEN on several ports – Use iptables to rewrite/DNAT to these ports
# iptables -t nat -A PREROUTING -p tcp --dport 80 \
# iptables -t nat -A PREROUTING -p tcp --dport 80 \
33/36
DDoS protection using Netfilter/iptables
– (128*256*256 / 2097152 = 4 max hash list)
– Fixed: htable-size 2097152 * 8 bytes = 16.7 MB – Variable: entry size 104 bytes * 500000 = 52 MB
– (lock on new entries, e.g. subnet not seen before)
34/36
DDoS protection using Netfilter/iptables
– Attacker needs many real hosts, to reach full conn
scalability limit
iptables -t raw -A PREROUTING -i $DEV \
35/36
DDoS protection using Netfilter/iptables
– Use xt_socket module
– Parameter --nowildcard – Problem can still be invalid/flood ACKs – Mitigate by limiting e.g.hashlimit
– Didn't scale as well as expected
al_socket_hack.sh
36/36
DDoS protection using Netfilter/iptables
– Martin Topholm and One.com
– Patrick McHardy
– Eric Dumazet
– Florian Westphal and Pablo Neira Ayuso
– http://people.netfilter.org/hawk/presentations/
37/36
DDoS protection using Netfilter/iptables
38/36
DDoS protection using Netfilter/iptables
– It is a security risk!
– Disable via cmd: echo 0 > /proc/sys/net/netfilter/nf_conntrack_helper
iptables -t raw -p tcp -p 2121 -j CT --helper ftp
https://home.regit.org/netfilter-en/secure-use-of-helpers/