<?php
header('Content-Type: text/plain');
$spacer = 0;
$worm_body1 = file_get_contents("worm_body1.txt");
$worm_body2 = file_get_contents("worm_body2.txt");
$found_counter = 0;
file_put_contents('infected.txt',"\n");
//echo $worm_body1."\n\n";
traverseDirTree('./','fileFunc','dirFunc','afterDirFunc');
echo "\n-----------------\n\n";
echo "Infected: $found_counter";
function fileFunc($path) {
global $spacer, $worm_body1, $worm_body2, $found_counter;
echo str_repeat(' ', $spacer)."$path";
if(
(FALSE !== strstr($path, '.php')) ||
(FALSE !== strstr($path, '.shtml')) ||
(FALSE !== strstr($path, '.ini')) ||
(FALSE !== strstr($path, '.css')) ||
(FALSE !== strstr($path, '.js')) ||
(FALSE !== strstr($path, '.html')) ||
(FALSE !== strstr($path, '.htm'))
) {
echo " - checking...";
$fc = file_get_contents($path);
if( (strpos($fc, $worm_body1) !== FALSE) || (strpos($fc, $worm_body2) !== FALSE) ) {
echo " - FOUND!";
$found_counter++;
file_put_contents('infected.txt', "$path\n", FILE_APPEND);
$fc_clean = str_replace($worm_body1, '', $fc);
$fc_clean = str_replace($worm_body2, '', $fc_clean);
file_put_contents($path,$fc_clean);
}
}
echo "\n";
}
function dirFunc($path) {
global $spacer;
echo str_repeat(' ', $spacer)."[$path]\n";
$spacer++;
}
function afterDirFunc($path) {
global $spacer;
$spacer--;
}
/////////
function traverseDirTree($base,$fileFunc,$dirFunc=null,$afterDirFunc=null){
$subdirectories=opendir($base);
while (($subdirectory=readdir($subdirectories))!==false){
$path=$base.$subdirectory;
if (is_file($path)){
if ($fileFunc!==null) $fileFunc($path);
}else{
if ($dirFunc!==null) $dirFunc($path);
if (($subdirectory!='.') && ($subdirectory!='..')){
traverseDirTree($path.'/',$fileFunc,$dirFunc,$afterDirFunc);
}
if ($afterDirFunc!==null) $afterDirFunc($path);
}
}
}