douyi9705 2017-02-16 18:38
浏览 168
已采纳

用sed删除长字符串

One of my sites got infected with some malicious code. The code is only added to the first line of all PHP files and is as follows:

<?php $ulhmjwklj = '#-#O#-#N# .......xqxe-1; ?> /*BEGIN LEGIT CODE HERE*/ <?php....

The malicious code is thousands of characters long with lots of special characters and spacing, so I tried creating a script to remove it:

for i in $(find . -name \*.php); do
  sed -i -E "s/<\?php\s$ulhmjwklj.*\?>//" $i;
  echo $i;
done;

This sed command will correctly remove the malicious code while leaving legitimate code on the first line, but then in all subsequent lines it removes all <?php ... ?> tags. So I tried altering the sed command to only search/replace on the first line:

for i in $(find . -name \*.php); do
  sed -i -E "1s/<\?php\s$ulhmjwklj.*\?>//" $i;
done;

Now the sed command will only run on the first line of each file, but it also removes any legitimate PHP tags which are appended to the first line directly after the malicious code.

Can someone please explain where I'm going wrong here?

  • 写回答

1条回答 默认 最新

  • dtqqq24248 2017-02-16 19:04
    关注

    The results of find should not be put through a loop. And, as I mentioned in the comments, $ is a special character for both Bash and a regular expression so has to be dealt with appropriately.

    Finally, as jm666 mentioned in comments, .* is greedy, so .*? limits the search to be as small as possible. But this won't work in sed so we need to use perl instead:

    find . -name '*.php' -print -exec perl -p -i -e 's/<\?php \$ulhmjwklj.*?\?>//' {} \;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作