iptables的两个安全模块:connlimit和recent

回复 收藏
本帖最后由 lyhabc 于 2016-1-25 14:32 编辑

iptables的两个安全模块:connlimit和recent
当然,iptables的两个安全模块:connlimit和recent也可以用在http,ftp等端口
之前论坛有人介绍过fail2ban,但是不好用,而且fail2ban的原理其实也是依靠iptables来实现的


限制同一个时刻同一个来源ip只能有3个ssh连接到服务器 这里数量要谨慎,比如原来已经有3个ssh连接了,这时候第4个要进来,必须前3个其中一个直接 点击xshell的关闭按钮才行,connlimit有个好处就是当第4个ssh连接进来不管杀掉之前的3个ssh连接
QQ截图20160125113159.jpg

iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP

第一句话:当有ssh连接进来服务器的时候他会记录到sshuser这个表
第二句话:检查这个新的ssh连接来源是否已经在sshuser这个表中,如果已经在sshuser这个表中,并且60秒之内建立了3个连接,那么将这个ssh连接来源的所有ssh连接拒绝,意思就是,如果尾号111这个ip之前已经有3个ssh连接到155这个服务器,那么当它发起第4个ssh连接,服务器会断开它的所有ssh连接,包括前面的3个已建立连接,在一分钟之后解除,一般--update 和--seconds --hitcount 连用
iptables -I INPUT -p tcp --dport 22  -m state --state NEW -m recent --name sshuser --set
iptables -A INPUT -m recent --update --name sshuser --seconds 60 --hitcount 3 -j DROP


记录日志
iptables -A INPUT -m recent --update --name sshuser --seconds 60 --hitcount 3 -j LOG --log-prefix '/root/sshattack'

connlimit模块和recent模块的--hitcount一般是互拆的
用了connlimit模块就没有必要用recent模块的--hitcount


示例脚本

#!/bin/sh
iptables -F
iptables -X
iptables -t nat -F

#iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
iptables -I INPUT -p tcp --dport 22  -m state --state NEW -m recent --name sshuser --set
iptables -A INPUT -m recent --update --name sshuser --seconds 60 --hitcount 3 -j DROP
iptables -A INPUT -m recent --update --name sshuser --seconds 60 --hitcount 3 -j LOG --log-prefix '/root/sshattack'


#ACCESS ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT


iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT



/etc/init.d/iptables save


参考文章
http://linux.it.net.cn/e/server/iptables/2015/0511/15326.html
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4233922&page=1&extra=#pid24530909




2016-01-25 11:58 举报
已邀请:
0

jinm

赞同来自:

学习

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: