Wordfence CLI是一款开源、高性能的安全扫描器,使用Python编写,能够快速扫描文件系统,检测PHP及其他恶意软件和WordPress漏洞。CLI支持并行运行、定时执行,可以通过管道接收输入,也可以将输出通过管道传递给其他命令。
官方提供了多种安装方式,包括pip包、deb包、二进制文件等等,我这里选择二进制文件安装,因为Debian现在不允许直接用pip全局安装pip包了,你要装一个包还得先建个venv,特别麻烦。deb软件包在Debian 13有依赖问题,这坑我已经踩过了:
apt install ./wordfence.deb
Note, selecting 'wordfence' instead of './wordfence.deb'
Solving dependencies... Error!
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
Unsatisfied dependencies:
wordfence : Depends: libpcre3 but it is not installable
Error: Unable to correct problems, you have held broken packages.
Error: The following information from --solver 3.0 may provide additional context:
Unable to satisfy dependencies. Reached two conflicting decisions:
1. wordfence:amd64=5.0.5rc1 is selected for install
2. wordfence:amd64 Depends libpcre3
but none of the choices are installable:
[no choices]
所以二进制是最舒服的,下载解压就能用了:
wget https://github.com/wordfence/wordfence-cli/releases/download/v5.0.4/wordfence_amd64.tar.gz
tar -xvf wordfence_amd64.tar.gz
试试看能不能运行,首次运行应该会提示让你注册一个免费的许可证,以及生成默认的配置文件:
./wordfence version
同时看一下扫描引擎的支持情况,现在PCRE和Vectorscan应该都显示的是No:
Wordfence CLI 5.0.4
PCRE Supported: No
Vectorscan Supported: No
我找了半天硬是找不到Debian 13的这个PCRE3的依赖包名叫啥,应该和之前安装deb包的那个依赖问题一样,索性我干脆放弃PCRE了,直接使用Vectorscan。
实际上也是Vectorscan更好用,Vectorscan的扫描速度比PCRE快几十倍,所以支不支持PCRE已经不重要了。在Debian 13安装这个包即可:
apt install libvectorscan5
我还有一台Debian 11的机器也需要装,发现Debian 11根本没有这个libvectorscan5,取而代之可以安装libhyperscan5,因为Vectorscan是Hyperscan的一个分支,并保持着兼容的API:
apt install libhyperscan5
Wordfence CLI目前同时支持这两种技术,但如果Vectorscan的API与Hyperscan的API出现差异,这种情况可能会随时间而改变,到时候就看具体情况怎么处理了。再次检查一下扫描引擎的支持情况,如果正常应该显示:
Wordfence CLI 5.0.4
PCRE Supported: No
Vectorscan Supported: Yes - Version: 5.4.2 2024-12-30 (API Version: 5.4.2)
默认情况下,CLI将使用PCRE进行扫描。要将CLI配置为使用Vectorscan,可以使用以下命令行参数:
./wordfence malware-scan --match-engine=vectorscan /var/www/wordpress
但每次都加上--match-engine=vectorscan使用起来不太方便,可以编辑配置文件:
nano ~/.config/wordfence/wordfence-cli.ini
在[MALWARE_SCAN]下面写入:
[MALWARE_SCAN]
match_engine=vectorscan
开始扫描:
./wordfence malware-scan --output-format csv --output-path scan_report.csv /var/www/wordpress
默认情况下Wordfence CLI不会扫描全部文件,例如图片之类的文件会跳过,如果你想扫描全部文件请使用:
./wordfence malware-scan --include-all-files --output-format csv --output-path scan_report.csv /var/www/wordpress
扫描结果会保存至scan_report.csv:
cat scan_report.csv
测试了一下,可以扫到后门:
/tmp/wf_test/test_webshell.php,11121,Backdoor:PHP/short.assert.11121,Short RCE,
如果没有检测到任何恶意程序,则scan_report.csv内容为空,你看到文件内没有内容应该感到高兴而不应该认为是Wordfence CLI没有正常工作。也可能是人家的后门太牛逼,Wordfence CLI扫不出来。
Wordfence CLI虽然是专为WordPress打造的,但请注意Wordfence CLI也可以扫描其它网站程序的PHP后门和恶意程序,并不是说你的网站程序不是WordPress就不能用Wordfence CLI。只是Wordfence CLI针对WordPress有更多的功能,比如扫描CVE漏洞,自动修复被篡改的文件等。
Wordfence CLI其实是一款收费软件,只是官方同时提供了免费版本,免费版本与收费版本的区别在于免费版本的数据库比收费版本慢了30天。如果你使用Wordfence CLI扫描后还不太放心,可以再试试YARA。
YARA是一款旨在(但不限于)帮助恶意软件研究人员识别和分类恶意软件样本的工具。YARA被誉为“恶意软件研究人员的瑞士军刀”,由VirusTotal的安全团队开发和维护。这么说吧,在网络安全行业里,几乎所有主流的杀毒软件和安全大厂(如卡巴斯基、赛门铁克等等)都在广泛使用YARA。
安装YARA:
apt install yara
YARA只是一个检测引擎,规则还需要自己写或者用别人现成的,Github上有很多,这里我使用signature-base。我们把规则下载放到目录内:
mkdir yara-rule && cd yara-rule/
wget https://raw.githubusercontent.com/Neo23x0/signature-base/master/yara/gen_webshells.yar
然后就可以使用这个规则来扫描了:
yara -r yara-rule/gen_webshells.yar /tmp/wf_test/
如果目录内有多个规则,可以用通配符:
yara -r yara-rule/*.yar /tmp/wf_test/
测试了一下,也可以检测到:
EXT_WEBSHELL_PHP_Generic /tmp/wf_test//test_webshell.php
WEBSHELL_PHP_Base64_Encoded_Payloads /tmp/wf_test//test_webshell.php
WEBSHELL_PHP_Gzinflated /tmp/wf_test//test_webshell.php
WEBSHELL_PHP_OBFUSC_3 /tmp/wf_test//test_webshell.php
WEBSHELL_PHP_Dynamic_Big /tmp/wf_test//test_webshell.php
补充点内容,如果你的站点使用WordPress,可以使用WP-CLI校验Core文件的官方哈希值:
sudo -u www-data wp core verify-checksums --path=/var/www/wordpress
一旦发现某个文件的指纹对不上就说明:文件被黑客修改、注入了恶意代码。请注意WP-CLI默认只拿英文版本做比对,如果你的WordPress是简中版本,可能会有个别文件误报,为了让它精准比对简中版本,请加上locale参数:
sudo -u www-data wp core verify-checksums --path=/var/www/wordpress --locale=zh_CN
WP-CLI还可以校验主题、插件的哈希值,但是仅支持WordPress官方市场安装的主题、插件。考虑到大部分站点都会从其它地方安装主题和插件,所以校验的价值就不太高了。
南无阿弥陀佛,佛祖保佑,希望自己永远也不会用到这些工具!!
荒岛













