分享互联网知识,建站、IT话题杂谈

Fail2ban 保护 WordPress 免受 xmlrpc.php CC攻击

WordPress本身是一个非常强大的CMS(内容管理系统),功能强大。但是也正是因为其强大的特性,使其很容易被利用,造成服务器的不稳定甚至崩溃。最容易遭受攻击的就是xmlrpc.php这个文件,攻击者只要每秒发送1个post请求到此文件,不出1分钟,一台512M内存Debian7系统的VPS就会崩溃,php进程和Mysql占用内存过高而导致无法访问。在尝试了网上流传的多个防攻击手段之后,fail2ban是效果最好的一个,下面就简单记录一下过程,供大家参考。

分析日志

在服务器出现异常的时候,首先去分析Nginx日志, /var/log/nginx/access.log ,从代码中可以发现如下可以IP不断发送Post请求到xmlrpc.php,每个IP的发送频率大概在2秒钟一次,严格来说频率并不高但足以让php和mysql进程崩溃。

185.188.204.16 - - [15/Jul/2017:23:51:07 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.6 - - [15/Jul/2017:23:51:08 -0400] "POST /xmlrpc.php HTTP/1.0" 499 0 "-" "*****"
185.188.204.8 - - [15/Jul/2017:23:51:08 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.14 - - [15/Jul/2017:23:51:08 -0400] "POST /xmlrpc.php HTTP/1.0" 499 0 "-" "*****"
185.188.204.16 - - [15/Jul/2017:23:51:08 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.7 - - [15/Jul/2017:23:51:08 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.12 - - [15/Jul/2017:23:51:09 -0400] "POST /xmlrpc.php HTTP/1.0" 499 0 "-" "*****"
185.188.204.11 - - [15/Jul/2017:23:51:09 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.15 - - [15/Jul/2017:23:51:09 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"
185.188.204.11 - - [15/Jul/2017:23:51:09 -0400] "POST /xmlrpc.php HTTP/1.0" 502 574 "-" "*****"

安装fail2ban和iptables

安装fail2ban和iptables,这样能使用iptables对攻击进行防御,相对于其它js和php的防御方式自动化更高,更为高效。
apt-get install fail2ban iptables

设置fail2ban jail.local

使用jail.local设定
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

vi /etc/fail2ban/jail.local

[xmlrpc]
enabled = true
filter = xmlrpc
action = iptables[name=xmlrpc, port=http, protocol=tcp]
logpath = /var/log/nginx/access.log
bantime = 43600
maxretry = 2

设置fail2ban 自定义filter

vi /etc/fail2ban/filter.d/xmlrpc.conf
[Definition]
failregex = ^<HOST> .*POST .*xmlrpc\.php.*
ignoreregex =

设置fail2ban jail.local

设置好之后重启fail2ban服务既可生效
service fail2ban restart

查看fail2ban日志查看防御情况
tail -f /var/log/fail2ban.log

2017-07-15 23:51:38,265 fail2ban.jail   : INFO   Creating new jail 'ssh'
2017-07-15 23:51:38,266 fail2ban.jail   : INFO   Jail 'ssh' uses Gamin
2017-07-15 23:51:38,287 fail2ban.filter : INFO   Set maxRetry = 6
2017-07-15 23:51:38,288 fail2ban.filter : INFO   Set findtime = 600
2017-07-15 23:51:38,288 fail2ban.actions: INFO   Set banTime = 600
2017-07-15 23:51:38,320 fail2ban.jail   : INFO   Creating new jail 'xmlrpc'
2017-07-15 23:51:38,320 fail2ban.jail   : INFO   Jail 'xmlrpc' uses Gamin
2017-07-15 23:51:38,321 fail2ban.filter : INFO   Added logfile = /var/log/nginx/access.log
2017-07-15 23:51:38,322 fail2ban.filter : INFO   Set maxRetry = 2
2017-07-15 23:51:38,323 fail2ban.filter : INFO   Set findtime = 600
2017-07-15 23:51:38,323 fail2ban.actions: INFO   Set banTime = 43600
2017-07-15 23:51:38,328 fail2ban.jail   : INFO   Jail 'ssh' started
2017-07-15 23:51:38,329 fail2ban.jail   : INFO   Jail 'xmlrpc' started
# 以上日志显示自定义的xmlrpc filter已经生效
2017-07-15 23:51:45,350 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.8
2017-07-15 23:51:45,361 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.9
2017-07-15 23:51:45,368 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.6
2017-07-15 23:51:45,376 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.7
2017-07-15 23:51:45,382 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.16
2017-07-15 23:51:45,387 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.14
2017-07-15 23:51:45,392 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.15
2017-07-15 23:51:45,400 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.12
2017-07-15 23:51:45,415 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.10
2017-07-15 23:51:45,426 fail2ban.actions: WARNING [xmlrpc] Ban 185.188.204.11
# 以上日志则显示成功防御对xmlrpc进行攻击的IP等情况

参考链接:

Leave a comment

Your email address will not be published. Required fields are marked *

9 thoughts on “Fail2ban 保护 WordPress 免受 xmlrpc.php CC攻击”