温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用fail2ban给centos系统做防御

发布时间:2020-10-07 02:43:12 来源:网络 阅读:2107 作者:bbotte 栏目:安全技术

系统为centos 6.4 64位

fail2ban http://www.fail2ban.org/wiki/index.php/Main_Page 

Fail2Ban is able to reduce the rate of incorrect authentications attempts however it cannot eliminate the risk that weak authentication presents. Configure services to use only two factor or public/private authentication mechanisms if you really want to protect services.

http://www.onerussian.com/tmp/fail2ban-pycon2014.pdf 这个是pdf的说明文档,不过较简单

fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员


我们需要对ssh的非法登陆有一定的机制,fail2ban就可以做到

下载:https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.8.13 

安装:

tar -xzf fail2ban-0.8.13.tar.gz
cd fail2ban-0.8.13
python setup.py install 
cd files/
cp ./redhat-initd /etc/init.d/fail2ban
chkconfig --add fail2ban


配置目录:/etc/fail2ban

/etc/fail2ban/action.d #firewall、iptables、mail、sendmail等配置文件

/etc/fail2ban/filter.d   #apache、nagios、nginx-http-auth、php-url-fopen、selinux-ssh等防护策略

/etc/fail2ban/jail.conf #主要配置文件


编辑配置文件:

vim /etc/fail2ban/jail.conf  #在#[ssh-iptables]  #enabled = true 两行的下面添加

[ssh-iptables]
enabled = true
filter = sshd
action  = iptables[name=SSH, port=ssh, protocol=tcp]
       sendmail-whois[name=SSH,dest=your_mail@163.com,sender=root@localhost,sendername="Fail2Ban"]
logpath  = /var/log/secure   #提供分析的日志,centos默认是这个路径
axretry = 5               #尝试次数
bantime = 172800          #拒绝时间/秒
  
  
[DEFAULT]                 #以下是约束下面的全局配置,不要添加
ignoreip = 127.0.0.1/8    #忽略的ip
bantime  = 600            #屏蔽时间
findtime  = 600           #在这个时间段内
maxretry = 3              #错误次数
backend = auto
usedns = warn

如果修改上面的[ssh-iptables]选项,那么将下面的[ssh-iptables]选项注释或删除,要不两个相同的选项设置,服务会不起作用


下面还有好多选项,enabled  = false 改为enabled  = true 就会生效

相应的配置文件路径:  /etc/fail2ban/filter.d/  也需要修改一下


启动服务:

service fail2ban restart

fail2ban的进程

/usr/bin/python /usr/bin/fail2ban-server -b -s /var/run/fail2ban/fail2ban.sock -p /var/run/fail2ban/fail2ban.pid -x

查看状态:

fail2ban-client status

查看被阻止的ip

iptables -L

REJECT     all  --  192.168.22.236       anywhere            reject-with icmp-port-unreachable


如果重起iptables 记的一定还要重起fail2ban,不然不能生效,fail2ban的过滤表是在iptables 启动后在加入的


用fail2ban给centos系统做防御

如果有这个错误提示,把[ssh-iptables]前面的空格去掉

Starting fail2ban: ERROR  Failed during configuration: File contains no section headers.

file: /etc/fail2ban/jail.conf, line: 17

' [ssh-iptables]\n'

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI