Skip to content

Instantly share code, notes, and snippets.

@ev0rtex
Last active August 29, 2015 13:58
Show Gist options
  • Save ev0rtex/9957600 to your computer and use it in GitHub Desktop.
Save ev0rtex/9957600 to your computer and use it in GitHub Desktop.
Infection Repair
<?php
// Store a list of infected files
$infected = array();
$unrepairable = array();
echo "\nScanning for infected files in the current directory...\n\n";
// Scan all infected files in the current directory and strip out the infected line
foreach(new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(realpath(dirname(__FILE__)), RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
) as $file => $meh) {
if(pathinfo($file, PATHINFO_EXTENSION) == 'php') {
if($f = fopen($file, 'r+')) {
$line = fgets($f);
// Detect infection
if(substr($line, 0, 17) == "<?php \$gyhweuqbjh") {
echo "\nInfected: {$file}";
$infected[] = $file;
}
fclose($f);
}
}
else {
echo ".";
}
}
echo "\n\n";
echo count($infected) . " infected files found representing approximately " . ceil(12970 * count($infected) / 1024) . " KB of extra disk space...attempting to repair these files\n";
// Repair infected files
foreach($infected as $file) {
if(!is_writable($file)) {
echo "\nCould not repair " . basename($file);
$unrepairable[] = $file;
}
else {
$contents = file($file);
$contents[0] = preg_replace('/<\?php \$gyhweuqbjh.*?\?>/', "", $contents[0]);
if(!$contents[0]) {
unset($contents[0]);
}
file_put_contents($file, $contents);
echo "\nRepaired {$file}";
}
}
echo "\n\nThere were " . count($unrepairable) . " unrepaired files";
echo "\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment