之前搭的一个WordPress 站点,没想到最近被黑了。情况跟这个问题类似,网站强制跳转到 https://crow.lowerthenskyactive.ga

幸亏之前有备份代码和数据库,保留现场后迅速从git 恢复代码,导入数据库备份。

也算是一次有趣的经历,这里记录下经验。

  1. 备份很重要

  2. 记得保留现场,方便事后排查

  3. 这次被黑原因是代码目录是777 权限,被webshell 写入了恶意php 脚本。除了wp-content/uploads 目录外,其他都改成755 即可。还有nginx 的用户不能跟代码目录用户相同。

  4. wp-content 下的php 文件都不能被执行。nginx 配置如下:

    location = /favicon.ico {
                   log_not_found off;
                   access_log off;
           }
   
           location = /robots.txt {
                   allow all;
                   log_not_found off;
                   access_log off;
           }
   
   	location ~ /\. {
   		deny all;
   	}
   
           location / {
                   try_files $uri $uri/ /index.php?$args;
           }
   
   	location ~* \.(?:ico|css|js|gif|bmp|jpe?g|mp4|pdf|mp3|png|svg|ttf|woff|woff2|otf|eot)$ {
   		access_log off;
   		expires 30d;
   		add_header Pragma public;
   		add_header Cache-Control "public, mustrevalidate, proxy-revalidate";
   	}
   
   	location ~* \.(pl|cgi|py|sh|lua)\$ {
   		deny all;
   	}
   
   	location ~ /(\.|wp-config.php|readme.html|license.txt|xmlrpc.php) {
   		deny all;
   	}
   
   	location ~* ^/wp-content/.*.(php|phps)$ {
   		deny all;
   	}
   
   	location ~* ^/wp-includes/.*\.(php|phps)$ {
   		internal;
   	}
   
     location ~ \.php$ {
   		try_files $uri =404;
       include fastcgi.conf;
       fastcgi_intercept_errors on;
   		fastcgi_pass unix:/dev/shm/php-cgi.sock;
   		fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }
  1. 删除不用的插件,定期更新WordPress 和插件

  2. 像WordPress 这种万人捅的东西,最好部署在docker, 隔离影响。

参考:https://wordpress.org/support/article/hardening-wordpress/