+
80
-

centos下iptables如何限制同一ip并发连接数?

请问centos下iptables如何限制同一ip并发连接数?

网友回复

+
0
-

1.限制与80端口连接的IP最大连接数为10,可自定义修改。

iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP 2.使用recent模块还可以实现限制同IP一段时间内最大新请求连接数

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

#60秒10个新连接,超过记录日志。

iptables -A INPUT -p tcp --dport 80 --syn -m recent --name webpool --rcheck --seconds 60 --hitcount 10 -j DROP

#60秒10个新连接,超过丢弃数据包。

iptables -A INPUT -p tcp --dport 80 --syn -m recent --name webpool --set -j ACCEPT

#范围内允许通过。 还可以通过直接编辑iptables的规则文件来进行更详细的预防ddos攻击。

vi /etc/sysconfig/iptables

删除原来的内容输入如下内容 保存

*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 –tcp-flags SYN,RST,ACK SYN -m ttl –ttl-eq 117 -j DROP
-A INPUT -p tcp -m tcp –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

说明此设定仅对外开放21(FTP),22(SSH),80(http网站)三个TCP端口。设置80端口5秒内20个连接

重启iptables服务 /etc/init.d/iptables restart

设定iptables随机启动chkconfig iptables on

我知道答案,我要回答