This article introduces how Iptables limits the number of connections of the same IP address in linux to prevent CC/DDOS attacks. This is only the most basic method. If the attack is real, we still need hardware compaction to prevent it.
1. Set the maximum number of connections to port 80 to 10, which can be customized.
The Code is as follows: |
Copy code |
Iptables-I INPUT-p tcp -- dport 80-m connlimit -- connlimit-abve 10-j DROP |
2. Use the recent module to limit the number of new requests within the same IP time. For more recent functions, see the recent application in the Iptables module.
The Code is as follows: |
Copy code |
Iptables-a input-p tcp -- dport 80 -- syn-m recent -- name webpool -- rcheck -- seconds 60 -- hitcount 10-j LOG -- log-prefix 'ddos: '-- log-ip-options #10 new connections in 60 seconds, exceeding the record log. Iptables-a input-p tcp -- dport 80 -- syn-m recent -- name webpool -- rcheck -- seconds 60 -- hitcount 10-j DROP #10 new connections in 60 seconds, exceeding the discarded packets. Iptables-a input-p tcp -- dport 80 -- syn-m recent -- name webpool -- set-j ACCEPT # Pass is allowed within the specified range. |
The above is relatively simple. Next I will analyze more specific configuration methods. CentOS/Redhat/Fedora
Run on the server
The Code is as follows: |
Copy code |
Vi/etc/sysconfig/iptables Delete original content input the following content to save # Generated by iptables-save v1.3.5 on Sun Dec 12 23:55:59 2010 * Filter : Input drop [385263: 27864079] : Forward accept [0: 0] : Output accept [4367656: 3514692346] -A input-I lo-j ACCEPT -A input-m state-state RELATED, ESTABLISHED-j ACCEPT -A input-p icmp-j ACCEPT -A input-s 127.0.0.1-j ACCEPT -A input-p tcp-m tcp-dport 80-m state-state NEW-m recent-set-name WEB-rsource -A input-p tcp-m tcp-dport 80-m state-state NEW-m recent-update-seconds 5-hitcount 20-rttl-name WEB-rsource-j DROP -A input-p tcp-m multiport-ports 21,22, 80-j ACCEPT -A input-p tcp-m tcp-flags SYN, RST, ack syn-m ttl-eq 117-j DROP -A input-p tcp-m tcp-flags SYN, RST, ack syn-m length-length 0: 40-j DROP -A input-p tcp-m tcp! -Tcp-flags SYN, RST, ack syn-m state-state NEW-j DROP COMMIT # Completed on Sun Dec 12 23:55:59 2010 |
This setting only opens TCP ports 21 (FTP), 22 (SSH), and 80 (http website. Set port 80 to 20 connections in five seconds
Restart iptables service/etc/init. d/iptables restart
Set iptables to start chkconfig iptables on randomly