全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 882|回复: 5
打印 上一主题 下一主题

用fail2ban来保护linux安全

[复制链接]
跳转到指定楼层
1#
发表于 2012-6-24 00:55:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 master 于 2012-6-24 00:58 编辑

一、下载安装
#wget http://cdnetworks-kr-2.dl.sourceforge.net/project/fail2ban/fail2ban-stable/fail2ban-0.8.4/fail2ban-0.8.4.tar.bz2
  
#tar xvfj fail2ban-0.8.4.tar.bz2
  #cd fail2ban-0.8.4
  #python setup.py install
  #cd files
  # cp ./redhat-initd /etc/init.d/fail2ban
  # chkconfig --add fail2ban
  #service fail2ban start 注:如果重起iptables 记的一定还要重起fail2ban,不然他就不能生效,fail2ban的过滤表是在iptables 启动后在加入的.
二、配置
  默认fail2ban.conf里面就三个参数,而且都有注释。
#默认日志的级别
loglevel = 3
  #日志的目的
logtarget = /var/log/fail2ban.log
  #socket的位置
socket = /tmp/fail2ban.sock
  jail.conf配置里是fail2ban所保护的具体服务的配置,这里以SSH来讲。
  在jail.conf里有一个[DEFAULT]段,在这个段下的参数是全局参数,可以被其它段所覆盖。
  
#忽略IP,在这个清单里的IP不会被屏蔽
ignoreip = 127.0.0.1 172.13.14.15
  #屏蔽时间
bantime = 600
  #发现时间,在此期间内重试超过规定次数,会激活fail2ban
  findtime = 600
  #尝试次数
maxretry = 3
  #日志修改检测机制
backend = auto
  
[ssh-iptables]
  #激活
enabled = true
  #filter的名字,在filter.d目录下
filter = sshd
  #所采用的工作,按照名字可在action.d目录下找到
action = iptables[name=SSH, port=ssh, protocol=tcp]
  mail-whois[name=SSH, dest=root]
  #目的分析日志
logpath = /var/log/secure
  #覆盖全局重试次数
maxretry = 5
  #覆盖全局屏蔽时间
bantime = 3600
  
对jail.conf进行一定的设置后,就可以使用fail2ban了。
  
启动fail2ban
  /etc/init.d/fail2ban start
启动之后,只要符合filter所定义的正则式规则的日志项出现,就会执行相应的action。由于0.8源码树采用客户机/服务器的模式,因此可以很方便的查询fail2ban的执行情况。比方所,要查询刚才定义的“ssh-iptables”段的情况,只要执行
  
三、测试:
fail2ban-client status ssh-iptables
  
会打印出结果
Status for the jail: ssh-iptables
  |- filter
  | |- Currently failed: 0
  | `- Total failed: 5
  `- action
  |- Currently banned: 1
  | `- IP list: 192.168.210.21
`- Total banned: 1
  fail2ban-client也可以直接定义运行中的fail2ban参数
  比如增加屏蔽时间为一天
fail2ban-client set ssh-iptables bantime 86400
重新读入配置文件
fail2ban-client reload
其它还有很多用法,可以不带参数执行fail2ban-client查看更多选项。
  因为fail2ban的框架,所以可以执行修改filter或者action来满足自己的特殊需要,比如我希望改变fail2ban默认的iptables规则插入方式,那么我就可以到action.d目录下,找到希望修改的action,这里的例子是iptables.conf
默认actionstart的iptables规则有一条是
iptables -I INPUT -p <protocol> --dport <port> -j fail2ban-<name>
这样就把fail2ban的规则插到INPUT链的最前面,而我希望自己写的一条iptables -A INPUT -p ALL -s 1.2.3.4/32 -j ACCEPT一直作为第一条规则从而使自己的IP作为信任IP不受防火墙后面规则的限制。那么就要修改fail2ban的启动规则,把上面那条改为
iptables -I INPUT 2 -p <protocol> --dport <port> -j fail2ban-<name>
这样fail2ban就会把自己的规则作为INPUT链的第二条规则插入,而不影响第一条。
  这里只是一个很简单的例子,你可以根据自己的规则,对action做更多的修改。
  而在filter.d目录里就是一些日志的正则式匹配规则,系统自带了一些常见软件的匹配,如sshd,apache,postfix,vsftpd,pure-ftpd等等。来看看sshd的规则,就能了解这些filter应该怎么写,你就可以 用fail2ban来保护更多自己的服务。
sshd.conf的内容
[Definition] www.sunk.eu.org
  failregex = Authentication failure for .* from <HOST>
  Failed [-/\w]+ for .* from <HOST>
  ROOT LOGIN REFUSED .* FROM <HOST>
  [iI](?:llegal|nvalid) user .* from <HOST>
  ignoreregex =
可以看到,每行一则正则式,对应各种错误认证,如果你的sshd版本错误认证日志项不太一样,可以修改这里的,或者加入更多。
  完全设置完毕后,过了一段时间,查看日志/var/log/fail2ban.log,cool:
2012-05-30 17:42:49,681 fail2ban.actions: WARNING [ssh-iptables] Ban 219.235.231.76
  2012-05-30 17:48:00,823 fail2ban.actions: WARNING [ssh-iptables] Ban 60.191.63.180
  2012-05-30 18:42:50,456 fail2ban.actions: WARNING [ssh-iptables] Unban 219.235.231.76
  2012-05-30 18:48:01,424 fail2ban.actions: WARNING [ssh-iptables] Unban 60.191.63.180
  2012-05-30 23:14:43,921 fail2ban.actions: WARNING [ssh-iptables] Ban 59.42.210.176
  2012-05-31 00:14:44,797 fail2ban.actions: WARNING [ssh-iptables] Unban 59.42.210.176
  2012-05-31 01:49:14,241 fail2ban.actions: WARNING [ssh-iptables] Ban 58.143.242.123
  2012-05-31 02:49:15,236 fail2ban.actions: WARNING [ssh-iptables] Unban 58.143.242.123
  2012-05-31 07:20:54,717 fail2ban.actions: WARNING [ssh-iptables] Ban 210.51.22.207
  2012-05-31 08:20:55,297 fail2ban.actions: WARNING [ssh-iptables] Unban 210.51.22.207
   
2#
发表于 2012-6-24 00:56:32 | 只看该作者
  楼主你好,楼下再见,,睡觉
3#
发表于 2012-6-24 01:19:27 | 只看该作者
denyhosts+csf也不错哦
4#
发表于 2012-6-24 01:25:08 | 只看该作者
了解了。

csf是什么?
5#
发表于 2012-6-24 02:01:39 | 只看该作者
同问csf是啥
6#
 楼主| 发表于 2012-6-24 02:11:51 | 只看该作者
smyz 发表于 2012-6-24 01:25
了解了。

csf是什么?

一、安装依赖包:
yum install perl-libwww-perl perl iptables
二、下载并安装 CSF:
wget http://www.configserver.com/free/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
三、测试 CSF 是否能正常工作:
[root@localhost csf]# perl /etc/csf/csftest.pl
Testing ip_tables/iptable_filter…OK
Testing ipt_LOG…OK
Testing ipt_multiport/xt_multiport…OK
Testing ipt_REJECT…OK
Testing ipt_state/xt_state…OK
Testing ipt_limit/xt_limit…OK
Testing ipt_recent…OK
Testing xt_connlimit…OK
Testing ipt_owner/xt_owner…OK
Testing iptable_nat/ipt_REDIRECT…OK
Testing iptable_nat/ipt_DNAT…OK
RESULT: csf should function on this server
csf的配置:
CSF的配置文件是 /etc/csf/csf.conf
# Allow incoming TCP ports
# 推荐您更改 SSH 的默认端口(22)为其他端口,但请注意一定要把新的端口加到下一行中
TCP_IN = “20,21,47,81,1723,25,53,80,110,143,443,465,587,993,995″
# Allow outgoing TCP ports同上,把 SSH 的登录端口加到下一行。
# 在某些程序要求打开一定范围的端口的情况下,例如Pureftpd的passive mode,可使用类似 30000:35000 的方式打开30000-35000范围的端口。
TCP_OUT = “20,21,47,81,1723,25,53,80,110,113,443″
# Allow incoming UDP ports
UDP_IN = “20,21,53″
# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list
UDP_OUT = “20,21,53,113,123″
# Allow incoming PING 是否允许别人ping你的服务器,默认为1,允许。0为不允许。
ICMP_IN = “1″
以上这些配置大家一看就懂了,下面再介绍几个比较常用的:
免疫某些类型的小规模 DDos 攻击:
# Connection Tracking. This option enables tracking of all connections from IP
# addresses to the server. If the total number of connections is greater than
# this value then the offending IP address is blocked. This can be used to help
# prevent some types of DOS attack.
#
# Care should be taken with this option. It’s entirely possible that you will
# see false-positives. Some protocols can be connection hungry, e.g. FTP, IMAPD
# and HTTP so it could be quite easy to trigger, especially with a lot of
# closed connections in TIME_WAIT. However, for a server that is prone to DOS
# attacks this may be very useful. A reasonable setting for this option might
# be arround 200.
#
# To disable this feature, set this to 0
CT_LIMIT = “200″##固定时间内同一个IP请求的此数
# Connection Tracking interval. Set this to the the number of seconds between
# connection tracking scans
CT_INTERVAL = “30″ ##指上面的固定时间,单位为秒
# Send an email alert if an IP address is blocked due to connection tracking
CT_EMAIL_ALERT = “1″ ##是否发送邮件
# If you want to make IP blocks permanent then set this to 1, otherwise blocks
# will be temporary and will be cleared after CT_BLOCK_TIME seconds
# 是否对可疑IP采取永久屏蔽,默认为0,即临时性屏蔽。
CT_PERMANENT = “0″
# If you opt for temporary IP blocks for CT, then the following is the interval
# in seconds that the IP will remained blocked for (e.g. 1800 = 30 mins)
# 临时性屏蔽时间
CT_BLOCK_TIME = “1800″
# If you don’t want to count the TIME_WAIT state against the connection count
# then set the following to “1″
CT_SKIP_TIME_WAIT = “0″ ##是否统计TIME_WAIT链接状态
# If you only want to count specific states (e.g. SYN_RECV) then add the states
# to the following as a comma separated list. E.g. “SYN_RECV,TIME_WAIT”
# Leave this option empty to count all states against CT_LIMIT
CT_STATES = “” ##是否分国家来统计,填写的是国家名
# If you only want to count specific ports (e.g. 80,443) then add the ports
# to the following as a comma separated list. E.g. “80,443″
#
# Leave this option empty to count all ports against CT_LIMIT
# 对什么端口进行检测,为空则检测所有,防止ssh的话可以为空,统计所有的。
CT_PORTS = “”

做了以上设置之后,可以先测试一下。如果没有问题的话,就更改为正式模式,刚才只是测试模式。
# 把默认的1修改为0。
TESTING = “0″
在/etc/csf/下面还有2个文件
csf.allow和csf.deny
这里面保存的就是允许的IP和禁止的IP,可以手动编辑这两个文件,比如想禁止某人来访问本站,就把他的IP加入到csf.deny列表里面去,或者是为了防止误封。可以把自己的IP加入到csf.allow列表里面去。加入之后记得重启一下csf防火墙。
/etc/init.d/csf restart或者csf -r 都可以重启。

评分

参与人数 1威望 +5 收起 理由
smyz + 5 很给力!

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-11-6 23:34 , Processed in 0.201596 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表