Could anyone explain the following rules:
-A default-INPUT -p tcp -m tcp --sport 0:1023 ! --tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
-A default-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
I think I added them to prevent SYN flood but I'm not sure.
-A default-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
Now for the next command:
-A default-INPUT -p tcp -m tcp --sport 0:1023 ! --tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
SYN ACK FIN RST URG PSH ALL NONE
.iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST SYN
SYN
flag set, and the ACK, FIN and RST
flags unset.FIN,RST,ACK
flags are set and SYN
is unset; which is the reverse matching of SYN set and FIN,RST,ACK
are unset.In order to understand this modules usage you need to have a little understanding of the TCP segment and its 3 way handshake.
Here is the 3 way handshake:
(source: cisco.com)
.
So the TCP segments have flags which control the state of the connection.
TCP segment:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
I don't think this prevents SYN
floods mainly because i haven't tried it yet. Though this one will limit SYN
floods:
# Limit the number of incoming tcp connections
# Interface 0 incoming syn-flood protection
iptables -N syn_flood
iptables -A INPUT -p tcp --syn -j syn_flood
iptables -A syn_flood -m limit --limit 1/s --limit-burst 3 -j RETURN
iptables -A syn_flood -j DROP
Taken from: http://www.cyberciti.biz/tips/howto-limit-linux-syn-attacks.html
The 2nd line is to protect against invalid packets.
-A default-INPUT -p tcp -m tcp --sport 0:1023 ! --tcp-flags FIN,SYN,RST,ACK SYN -j ACCEPT
Rejects all inbound packets that has a SYN bit and any other flag set. This makes sense if this is a server.
Any legitimate inbound connection will send an initial packet with the SYN bit set, but none of the others. Using multiple flags is an attack vector on the tcp stack and need to be dropped.
Two other attacks are NULL, where none of the flags are set and the Christmas Tree, where all flags are set. To protect against those, use
# Protect against common attacks
# Block tcp packets that have no tcp flags set.
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
# Block tcp packets that have all tcp flags set.
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With