网站出现错误502,php-fpm CPU占用高:原来是xmlrpc.php受攻击

最近访问艾薇资讯的网友都会发现网站打开得非常慢,而且经常是遇到error 502 Bad Gateway nginx,艾薇资讯的一台VPS主机上安装的是AMH面板,上面挂了几个网站,其中只有艾薇资讯网站出现502的错误提示,而且艾薇资讯收到来自linode发来的监控的报警提示CPU占用康过高,艾薇在检查这台VPS主机的时候看到同一VPS中的其他网站正常可以打开,仅有这个网站出现502错误,而且刷新后非常的慢才可以打开,确定这个网站之前是完全没有问题的,而且我的linode VPS配置也还算高,linode4096套餐的,每月20美元,性能肯定是够的。

Your Linode has exceeded the notification threshold (90) for CPU Usage by averaging 143.4% for the last 2 hours. The dashboard for this specific Linode is located at: <https://manager.linode.com/linodes/dashboard/linode1988808&gt;

This is an automated message, please do not respond to this email. If you have questions, please open a support ticket.

You can view or change your alert thresholds under the “Settings” tab of the Linode Manager.

This is not meant as a warning or a representation that you are misusing your resources. We encourage you to modify the thresholds based on your own individual needs.

You may access the members’ site at <https://manager.linode.com/>;.

刚开始以为是VPS出问题了,于是重启VPS,但是还是经常出现error 502 Bad Gateway nginx,然后开始怀疑是不是 nginx的配置出错了。在网上查了好多资料,由于我的网站是Nginx + PHP(php-fpm)占用率非常的高,达到95%以上。当时我就Google和百度查询了很多资料,也改了好多配置,但是还是出现502网关错误。

干脆先是给AMH提交工单看看有什么办法解决:

本月技术支持数: 1 / 10 工单类型: 授权/技术支持  响应级别: 中 ( 周一 * 周五 / 09:00至16:00) 完成状态: 未完成

艾薇资讯:
VPS使用的是LINODE4096的,配置算高,挂几个网站,流量也不高,这两天有一个网站经常遇到502错误,CPU占用高,这是什么情况?使用的是AMH,从哪排查呢?谢谢
您当前的工单不在响应工作时间之内 (周末)
2017-05-28 16:22:57 1

php-fpm cpu占用率过高,高达96%,平时CPU也就10%以下
您当前的工单不在响应工作时间之内 (周末)

amysql

你虚拟主机开启网站访问日志。
用amlog分析日志,看是否被攻击等。
amh 联合创始人
amysql ©amh.sh
2017-05-29 09:48:08 3

艾薇资讯:

原来的日志文件有6G去,然后被我删除了,我是看社区你的回复说是可以删除的,我再打开看看,如果说被攻击VPS流量应该很高才是,可是我的流量使用不多,内存用得也很少,IO也不高,就是CPU非常高,几个网站就一个网站有问题出现502,一天出现好多次,我看看日志先

 

我想起来了,出问题的网站原来的访问日志我是打开的,原来也就几百M,我前几天看的时候只有3G,后面就升到6G,我看太大就删除了,是不是真被攻击了?求个防范方法,我也怀疑过是受攻击,所以转到了cloudFLARE,但是没有发现攻击。
2017-05-30 12:26:50 5

按照你的方法,看了日志发现有个美国IP不停的访问xmlrpc.php,然后CPU就超高了,这个应该是受攻击想破解我的网站,我先观察一段时间看看
2017-05-30 13:17:32 6

也给linode技术支持去个邮件:

Why is my VPS CPU resource usage as high as 95%?

[8048949] Why is my VPS CPU resource usage as high as 95%?
Status Opened Last Updated Closed On Regarding
OPEN 19 hours ago by aigowqb 15 hours ago by jhaas – linode1988808

19 hours ago The last two days, my VPS CPU utilization rate is very high, not regularly appear, CPU utilization rate as high as 95% or more, leading to the site to run very slowly, xerer.com can not open, there is error 502, other websites normal. After my inspection, I haven’t made any changes to VPS or website recently. I haven’t been attacked. I tried to solve this problem in the last two days, but I didn’t succeed. Did the VPS service go wrong? Or where’s the problem? Thank you
19 hours ago This is the nearest CPU resource usage, usually below 5%, very low
thoplock
Linode
17 hours ago Hi,

I’m sorry to hear about the unexpected CPU usage.

I’m afraid that we don’t really have any way of knowing what the issue is here. Since we’re an infrastructure provider, we don’t have access to your Linode’s data and the administration of the system will be your responsibility. However, I’d be happy to try and point you in the right direction.

Basically, you’ll need to review the processes running on your system to better determine what is using your CPU. These commands should help you do that:

# Live resource monitoring (press ‘q’ to quit):
top

# Get a sorted list of the top 20 processes on your system sorted by CPU usage:
ps auxwf | sort -nrk 3 | head -20

Once you determine what’s using these resources, you’ll need to review your configuration to see why this is happening.

If you have questions about your configuration, it may be a good idea to bring them up to our community:

https://forum.linode.com/
https://www.linode.com/chat

Let us know if you have any questions.

TJ
Linode Support Team
How did I do?
Happy Satisfied Unhappy

aigowqb
16 hours ago Thank you for your help. I’ll try again

jhaas
Linode
15 hours ago Okay,

Let us know if you have any more questions. We’ll also be happy to take a quick glance at that output to point you in the right direction.

Regards,
Josh H
Linode Support
How did I do?

于是按照以下两位大神的指示,艾薇资讯查看了一下TOP服务,发现php-fpm进程占用CPU特别的高,高达95%以下,问题就在这个php-fpm进程上面,于是我就Google解决php-fpm导致502错误的原因和解决办法,顺便优化了一下php.ini的配置,我查看了一个php-fpm 配置文件:
原来,php-fpm有一个参数 max_requests,参数max_requests指明了,每个children最多处理多少个请求后便会被关闭,默认的设置是500。因为php是把请求轮询给每个 children,如果在大流量下,每个childre到达max_requests所用的时间都差不多,这样就造成所有的children基本上在同一时间被关闭。
在这期间,nginx无法将php文件转交给php-fpm处理,所以cpu会降至很低(不用处理php,更不用执行sql),而负载会升至很高(关闭和开启children、nginx等待php-fpm),网卡流量也降至很低(nginx无法生成数据传输给客户端)。
这个问题解决问题很简单,增加children的数量,并且将 max_requests 设置未 0 或者一个比较大的值:
打开 /usr/local/php/etc/php-fpm.conf
然后重启php-fpm,可是我的网站流量不大啊,一天也就1000IP,修改php-fpm.conf之后,发现网站502的问题仍然存在。
再一个方法就是修改request_terminate_timeout的时间参数,
如果主要是在一些post或者数据库操作的时候出现网站502这种情况,而不是在静态页面操作中常见,那么可以查看一下php-fpm.conf设置中的一项:
request_terminate_timeout
这个值是max_execution_time,就是fast-cgi的执行脚本时间。
request_terminate_timeout 0s,0s为关闭,就是无限执行下去。(当时装的时候没仔细看就改了一个数字),在优化fastcgi中,还可以改改这个值5s 看看效果。php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉,都会出现502错误。但是按照以上的修改方法我都试过了,可能网站502错误的问题还是出现。然后AMH的管理员给我回复让我看一下网站访问日志,说我可爱被攻击了,于是我查看了艾薇资讯的访问日志。

最后发现目标锁定网站可能被大量访问某个PHP文件导致的。然后我详细查看艾薇资讯网站下的日志文件,发现有来自美国的一个IP 104.192.3.34的大量访问xmlrpc.php文件,应该是传说中的xmlrpc暴力破解密码导致的,也就是说艾薇资讯被傻逼盯上了,被傻逼攻击了。应该是由于有被攻击利用导致的WEB卡死导致的。原因在于使用的WORDPRESS程序默认xmlrpc.php开启,而被用来DDOS攻击导致占用资源过高。
艾薇资讯然后在网站百度了一下,如何解决WordPress被利用xmlrpc.php暴力破解攻击问题,网上是这么说的,我按照大神的步骤下面功能都给关了:
遇到这样的问题,我们可能想到的直接锁定这个IP 104.192.3.34,但这样不能解决其他IP也出现这样的攻击,我们需要禁止xmlrpc文件的访问功能才可以彻底的解决。
解决xmlrpc.php文件被暴力破解:
第一、如果我们没有第三方接口应用,可以直接关闭XML-RPC接口
add_filter(‘xmlrpc_enabled’, ‘__return_false’);
第二、阻止pingback端口
add_filter( ‘xmlrpc_methods’, ‘remove_xmlrpc_pingback_ping’ );
function remove_xmlrpc_pingback_ping( $methods ) {
unset( $methods[‘pingback.ping’] );
return $methods;
}
一般pingback功能是用不到的,我们直接屏蔽掉,默认当前的wordpress版本是开启的。这样,我们就可以解决WordPress被利用xmlrpc.php暴力破解攻击问题。

 

到目前为止艾薇资讯的CPU占用率降到了10%以下,网站也没有出现502错误的情况,

自从知道网站被人攻击之后,艾薇资讯把网上的所有密码都改了一遍,而且改得比以前要复杂得多,我看这个傻逼攻击者还有没有兴趣再攻击艾薇资讯,现在我在等攻击者的回应,大家有什么想说的可以在下面留言。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注