入侵检测 denyhosts fail2ban
针对平台
- linux
工具选择
建议用fail2ban,安装和配置比较简单,Start比较多。
denyhosts
-
download the latest version from here
-
install the latest version
-
cp and config the config file
- centos
-
cp and config the daemon-control file
-
enable the service
fail2ban
GitHub - fail2ban/fail2ban: Daemon to ban hosts that cause multiple authentication errors
-
fail2ban需要配合防火墙使用,支持firewalld服务和iptable
-
如果被禁止登录,原来的免密登录也不能进行登录
with iptables
-
config fail2ban
# vim /etc/fail2ban/jail.local [DEFAULT] # 以空格分隔的列表,可以是 IP 地址、CIDR 前缀或者 DNS 主机名 # 用于指定哪些地址可以忽略 fail2ban 防御 172.31.0.0/24 10.10.0.0/24 192.168.0.0/24 ignoreip = 127.0.0.1 bantime = 86400 maxretry = 5 findtime = 600 mta = sendmail # 注意这个名字就是查看和解禁是用的服务名 [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] # dest可以用your@email.com sendmail-whois[name=SSH, dest=root, sender=fail2ban@email.com] # 注意RH是secure,Debian是auth.log logpath = /var/log/secure maxretry = 3 # 或者 # 注意这个名字就是查看和解禁是用的服务名 [sshd] enabled = true filter = sshd port = 22 action = iptables[name=SSH, port=ssh, protocol=tcp] # action = %(action_mwl)s sendmail-whois[name=sshd, dest=root, sender=fail2ban@infvie.com] logpath = /var/log/secure bantime = 86400 findtime = 600 maxretry = 5
with firewalld
-
stop iptables if you turn it on,then start firewalld
# 如果您已经安装iptables建议先关闭 service iptables stop # 查看Firewalld状态 firewall-cmd --state #启动firewalld,注意开启防火墙后请立即放行ssh服务和端口,否则会连不上ssh systemctl start firewalld # 设置开机启动 systemctl enable firewalld.service
-
config fail2ban
# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local # vi /etc/fail2ban/jail.local [DEFAULT] #全局设置 ignoreip = 127.0.0.1 #忽略的IP列表,多个IP以空格分割或IP段127.0.0.1/8不受设置限制(白名单) bantime = 86400 #屏蔽时间,单位:秒 findtime = 600 #这个时间段内超过规定次数会被ban掉 maxretry = 5 #最大尝试次数 backend = auto #日志修改检测机制(gamin、polling和auto这三种) # 这里banaction必须用firewallcmd-ipset,这是fiewalll支持的关键,如果是用Iptables请不要这样填写 banaction = firewallcmd-ipset action = %(action_mwl)s [sshd] enabled = true filter = sshd port = 22 action = %(action_mwl)s sendmail-whois[name=sshd, dest=root, sender=fail2ban@infvie.com] logpath = /var/log/secure bantime = 86400 findtime = 600 maxretry = 5
-
start fail2ban and test
# 启动fail2ban systemctl start fail2ban # 查看iptables规则是否生效 iptables -nL --line-number(注意查看f2b-模块名sshd所在行)
-
解除IP封禁
# 查看黑名单状态 fail2ban-client status sshd(模块名) # 黑名单移除IP fail2ban-client set sshd unbanip IP地址 # 防火墙规则查看与清理 iptables -nL --line-number(注意查看f2b-模块名sshd所在行) iptables -D f2b-sshd num(-D清理 防火墙模块名 编号)
其他
-
对vsftpd服务进行监控,需要vsftpd关闭域名反向解析,直接记录IP
# vi /etc/vsftpd/vsftpd.conf reverse_lookup_enable=NO # /var/log/secure里记录ftp登录失败有一个缺点处理,使用vsftpd.log日志 dual_log_enable=YES use_localtime=YES
-
同时需要编辑fail2ban配置文件
# vim /etc/fail2ban/jail.local [vsftpd-iptables] ... logpath = /var/log/vsftpd.log ...