Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1355708
  • 博文数量: 185
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2664
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-01 13:46
个人简介

鸟在笼中,恨关羽不能张飞;Survival of the fittest

文章分类

全部博文(185)

分类: LINUX

2012-05-16 10:44:35

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://blog.chinaunix.net/uid-10915175-id-3209670.html

 现在介绍一个非常实用的iptables模块--recent,它可以防止字典暴力、端口扫描攻击等

我们来看下,具体关于它的一些实际用法
 
 
iptables -A INPUT -d 192.168.1.100 -p icmp -m recent --seconds 1800 --hitcount 20 --update --name ICMP --rsource -j REJECT --reject-with icmp-host-unreachable 
如果某个源在半个小时内给192.168.1.100发送了20个icmp数据包,则服务器要在1800秒后才会接受该源发出的第21个数据包,--rsource参数也可以不加,因为recent模块默认就是记录规则中的源地址到recent list table中
 
iptables -A INPUT -d 192.168.1.100 -p icmp -m recent --set --name ICMP --rsource -m limit --limit 1/m --limit-burst 1 -j ACCEPT 
本条规则每分钟只匹配一个icmp数据包,如果源的速率大于1/m,则其余的数据包直接交给下一条规则处理,不管数据包的速率为多少,每经过一个数据包,recent模块都会在ICMP table中将该源的old_packets值加1
 
iptables -A INPUT -d 192.168.1.100 -p tcp -m tcp --dport 22 -m state --state NEW -m recent --seconds 3600 --hitcount 5 --update --name SSH --rsource -j REJECT --reject-with tcp-reset 
如果某个源在一小时内访问192.168.1.100的shell端口超过了5次,则从收到该源的第五个包开始到接下来的3600秒内,对其所有发送过来的数据包直接回应tcp-reset(即连接重置)
 
iptables -A INPUT -d 192.168.1.100 -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource -j ACCEPT
如果某个源访问的目的地址是192.168.1.100且协议为tcp、目的端口为22、状态为NEW,则recent模块会在SSH表中记录该源发送数据包的个数,并接受该数据包(如果数据包匹配了上面的规则,则服务器直接重置该链接,而不经过本规则)
 
recent模块的参数:
--name name
              Specify the list to use for the commands. If no name is given then DEFAULT will be used.
 
[!] --set
              This will add the source address of the packet to the list. If the source address is already in the list, this will update the existing entry. This will always return success (or failure if ! is passed in).
 
--rsource
              Match/save the source address of each packet in the recent list table. This is the default.
 
[!] --rcheck
              Check if the source address of the packet is currently in the list.
 
[!] --update
              Like --rcheck, except it will update the "last seen" timestamp if it matches.
 
--seconds seconds
              This option must be used in conjunction with one of --rcheck or --update. When used, this will narrow the match to only happen when the address is in the list and was seen within the last given number of seconds.
 
--hitcount hits
              This option must be used in conjunction with one of --rcheck or --update. When used, this will narrow the match to only happen when the address is in the list and  packets had  been  received  greater  than or equal to the given value. This option may be used along with --seconds to create an even narrower match requiring a certain number of hits within a specific time frame. The maximum value for the hitcount parameter is given by the "ip_pkt_list_tot" parameter of the xt_recent kernel module. Exceeding this value on the command line will cause the rule to be rejected.(默认情况下hitcount的最大值为20)
 
echo +addr >/proc/net/xt_recent/DEFAULT
              to add addr to the DEFAULT list
 
echo -addr >/proc/net/xt_recent/DEFAULT
              to remove addr from the DEFAULT list
 
echo / >/proc/net/xt_recent/DEFAULT
              to flush the DEFAULT list (remove all entries).
 
The module itself accepts parameters, defaults shown:
 
       ip_list_tot=100
              Number of addresses remembered per table.
 
       ip_pkt_list_tot=20
              Number of packets per address remembered.
 
       ip_list_hash_size=0
              Hash table size. 0 means to calculate it based on ip_list_tot, default: 512.
 
       ip_list_perms=0644
              Permissions for /proc/net/xt_recent/* files.
 
       ip_list_uid=0
              Numerical UID for ownership of /proc/net/xt_recent/* files.
 
       ip_list_gid=0
              Numerical GID for ownership of /proc/net/xt_recent/* files.
 
查看recent模块的list表
cat /proc/net/xt_recent/SSH           
此处的SSH是在iptables中启用recent模块时所定义的一个列表的名字
 
iptables1.4.8之前的某些版本为
cat /proc/net/ipt_recent/SSH
 
备注:
1./proc/net/目录下的xt_recent目录是在启用recent模块之后才有的,如果没有在iptables中使用recent模块,/proc/net/目录中是没有xt_recent目录的
 
2.因recent模块最多只能记录20条记录,所以当源发送的数据包超过20后,recent模块的计数器会立刻减掉20,这也就是为什么old_packets的值就总是处于1-20之间
 
3.如果配合seconds参数使用的是--rcheck参数而不是--update,则recent模块会从收到第一个数据包开始计算阻断时间,而--update是从收到的最后一个数据包开始计算阻断时间,即如果服务器在8点收到了源发出第一个icmp数据包,在8点15分收到源发出的第20个数据包,如果使用的是--rcheck参数,那么8点半的时候,用户就又可以发送icmp数据包了,如果使用是--update参数,则用户必须等到8点40才能发送icmp数据包      
 
4.当源发送数据包的个数大于或等于recent模块的hitcount参数所指定的值时,相应的iptables规则才会被激活

本文出自 “单身贵族” 博客,请务必保留此出处http://blog.chinaunix.net/uid-10915175-id-3209670.html

阅读(3320) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~