用Fail2ban和Docker安全服务器
我在一个docker容器中使用了nginx,我可以很容易地在主机上与我的nginx docker容器共享我的日志文件。 日志是在它上面,并在/var/log/nginx
文件夹上工作。
我在主机上安装了fail2ban来检查日志文件,特别是access.log
。
我testing一个简单的filter
# Fail2Ban configuration file # Author: Miniwark [Definition] failregex = ^<HOST> .*"GET .*w00tw00t # try to access to admin directory ^<HOST> .*"GET .*admin.* 403 ^<HOST> .*"GET .*admin.* 404 # try to access to install directory ^<HOST> .*"GET .*install.* 404 # try to access to phpmyadmin ^<HOST> .*"GET .*dbadmin.* 404 ^<HOST> .*"GET .*myadmin.* 404 ^<HOST> .*"GET .*MyAdmin.* 404 ^<HOST> .*"GET .*mysql.* 404 ^<HOST> .*"GET .*websql.* 404 ^<HOST> .*"GET \/pma\/.* 404 # try to access to wordpress (we use another CMS) ^<HOST> .*"GET .*wp-content.* 404 ^<HOST> .*"GET .*wp-login.* 404 # try to access to typo3 (we use another CMS) ^<HOST> .*"GET .*typo3.* 404 # try to access to tomcat (we do not use it) ^<HOST> .*"HEAD .*manager.* 404 # try to access various strange scripts and malwares ^<HOST> .*"HEAD .*blackcat.* 404 ^<HOST> .*"HEAD .*sprawdza.php.* 404 ignoreregex =
我可以在/etc/fail2ban/jail.local
轻松地启用它
[nginx-nokiddies] # ban script kiddies enabled = true port = http,https filter = nginx-nokiddies logpath = /var/log/nginx*/*access.log maxretry = 1
我重新启动/停止/启动/重新加载fail2ban服务。 然后我用这个testing这个正则expression式
fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-nokiddies.conf
它匹配了数千行,特别是与任何pipe理请求。
主要的问题是fail2ban不能自动工作,所以不像以前那样发送邮件。 事实上,当我直接在主机上使用nginx安装时,它是完美的。
日志是基本的格式,调用这样的“组合”格式:
log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
没有权限的问题,因为我的nginx容器和它的孩子是完全的权限(777)可以肯定,我当然改变它!
为什么fail2ban进程不禁止ip和不匹配docker的任何东西?
如果你的nginx在Docker镜像中运行,你可能也想在Docker容器中运行fail2ban?
您可以在主机上安装fail2ban,然后将访问日志文件从nginx容器映射到主机。 像docker run -v /path/in/host:/var/log/nginx/access.log nginx
。 然后在fail2ban中引用该文件。