前一段时间我的三个服务器分别被攻击,造成80美元的损失,Linode,Vultr分别流量跑出了4T和1.5T的,严重超出了每月的限定流量,甚至Vultr干脆给我开了个欠费68美元的欠条,DO干脆给我把VPS给锁定断网了,数据什么的都拿不出来,这次事件也算是血的教训了吧,让我开始对网络安全感兴趣。
为了避免服务器再度被攻击,我直接删除了Vultr和DO的VPS,因为他们两家相对于Linode还是不够人性化,功能不够完善,Linode我也是重新更换系统,所有的数据直接不要了(其实也没多少),那么第一件事就是放弃各种一键安装包,一键安装包的确很傻瓜,功能很强大,一直惯用军哥的LNMP和阿里云的LNAMP的一键安装包,但是阿里云的就不说了,人家官方指定的应该没啥问题,军哥的呢?口碑在这里,应该也没问题,毕竟用了好几年,为了以防万一,我还是自己手动编译安装LNMP,其次在安装完成后,立马开启Linode的监控,LongView很强大回头在介绍,顺便打开防火墙,安装Fail2Ban。
Fail2Ban是一个Python写的安全工具,其用途就是判断是否有人破例破解密码,是否有人使用DDOS攻击和CC攻击,刚开始也是抱着尝试的心理,根据网上教程总结安装成功,并且配置了邮件报警(需要安装Sendmail),下面来看看效果吧:
Hi,
The IP 43.229.53.43 has just been banned by Fail2Ban after
5 attempts against SSH.Here are more information about 43.229.53.43:
[Querying whois.v6nic.net]
[whois.v6nic.net: Name or service not known]
[Unable to connect to remote host]Regards,
Fail2Ban
Hi,
The IP 218.65.30.217 has just been banned by Fail2Ban after
5 attempts against SSH.Here are more information about 218.65.30.217:
[Querying whois.apnic.net]
[whois.apnic.net]
% [whois.apnic.net]
% Whois data copyright terms http://www.apnic.net/db/dbcopyright.html% Information related to ‘218.64.0.0 – 218.65.127.255′
inetnum: 218.64.0.0 – 218.65.127.255
netname: CHINANET-JX
country: CN
descr: CHINANET jiangxi province network
descr: China Telecom
descr: No.31,jingrong street
descr: Beijing 100032
admin-c: CH93-AP
tech-c: JN113-AP
changed: hostmaster@cn.net 20020829
mnt-by: MAINT-CHINANET
mnt-lower: MAINT-IP-WWF
status: ALLOCATED NON-PORTABLE
source: APNICrole: JXDCB NET
address: Jiangxi telecom network operation support department
address: No.2009, Beijing East Road , nanchang,jiangxi province
country: CN
phone: +86 79186600000
e-mail: wzzx_2013@189.cn
remarks: send spam reports to wzzx_2013@189.cn
remarks: and abuse reports to wzzx_2013@189.cn
remarks: http://www.online.jx.cn
admin-c: XY1-AP
tech-c: WZ1-CN
tech-c: WW49-AP
nic-hdl: JN113-AP
notify: wzzx_2013@189.cn
mnt-by: MAINT-IP-WWF
changed: hm-changed@apnic.net 20020812
changed: chenyiq@gsta.com 20130221
source: APNICperson: Chinanet Hostmaster
nic-hdl: CH93-AP
e-mail: anti-spam@ns.chinanet.cn.net
address: No.31 ,jingrong street,beijing
address: 100032
phone: +86-10-58501724
fax-no: +86-10-58501724
country: CN
changed: dingsy@cndata.com 20070416
changed: zhengzm@gsta.com 20140227
mnt-by: MAINT-CHINANET
source: APNIC% This query was served by the APNIC Whois Service version 1.69.1-APNICv1r0 (UNDEFINED)
Regards,
Fail2Ban
还有很多就不列举了,可见还是有人瞄上我了,不过5次尝试就会被ban一天,让他们慢慢穷举去吧!下面介绍怎么安装:
首先配置YUM源:
vim /etc/yum.repos.d/CentOS-Base.repo
在最后新增:
[atrpms] name=Red Hat Enterprise Linux $releasever - $basearch - ATrpms baseurl=http://dl.atrpms.net/el$releasever-$basearch/atrpms/stable gpgkey=http://ATrpms.net/RPM-GPG-KEY.atrpms gpgcheck=1 enabled=1
然后执行:
yum -y install fail2ban
等待安装,几秒钟就安装成功,或者采用RPM安装:
Centos6:
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install fail2ban
Centos7:
rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm yum install fail2ban
安装成功后,服务配置目录为:/etc/fail2ban
/etc/fail2ban/action.d #动作文件夹,内含默认文件。iptables以及mail等动作配置 /etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置 /etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置 /etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值 /etc/rc.d/init.d/fail2ban #启动脚本文件
其他的默认即可,只需要修改etc/fail2ban/jail.conf文件即可。常用的修改的参数如下:
ignoreip = 127.0.0.1 #忽略的IP列表,不受设置限制(白名单) bantime = 600 #屏蔽时间,单位:秒 findtime = 600 #这个时间段内超过规定次数会被ban掉 maxretry = 3 #最大尝试次数 backend = auto #日志修改检测机制(gamin、polling和auto这三种) [ssh-iptables] #针对各服务的检查配置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置 enabled = true #是否激活此项(true/false) filter = sshd #过滤规则filter的名字,对应filter.d目录下的sshd.conf action = iptables[name=SSH, port=ssh, protocol=tcp] #动作的相关参数 sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com] #触发报警的收件人 logpath = /var/log/secure #检测的系统的登陆日志文件 maxretry = 5 #最大尝试次数
启动Fail2Ban:
CentOS 6: service fail2ban restart CentOS 7: systemctl restart fail2ban.service
加入开机启动:
CentOS 6: chkconfig fail2ban on CentOS 7: systemctl enable fail2ban
这样就能保证不会被穷举到SSH的密码,当然这个工具是很强大的,还可以监控Nginx是否被DDOS攻击,具体配置如下:
首先在/etc/fail2ban/jail.conf里加入:
[yunvn-get-dos] enabled = true port = http,https filter = nginx-bansniffer action = iptables[name=IT300, port=http, protocol=tcp] sendmail-whois[name=IT300, dest=xxxxx@csli.cc, sender=xxxxxx@csli.cc] logpath = /var/logs/yunvn.log maxretry = 300 findtime = 60 bantime = 3600
然后创建文件/etc/fail2ban/filter.d/nginx-bansniffer.conf,内容如下:
[Definition] failregex = <HOST> -.*- .*HTTP/1.* .* .*$ ignoreregex =
最后重启fail2ban服务即可,在上面的配置中,我们对每60秒有超过300次访问的ip,封禁1小时。
未经允许不得转载:李阳博客 » 为了安全,给你的VPS按个Fail2Ban吧