<?php

set_time_limit(0);
error_reporting(E_ALL);

$root = __DIR__;

/*
|--------------------------------------------------------------------------
| Malware / suspicious patterns
|--------------------------------------------------------------------------
*/

$patterns = [
    'eval(',
    'hex2bin',
    'mbd;',
    '71-53-64-68-88-29-m6SQizGa',
    'str_rot13(',
    '(4+4+4+4)',
    'abcdefghijklmnopqrstuvwxyz0123456789',
    '$comp1 = \'746\';',
    'f480b16fed3f32a537404bd16d598355',
    '$approve_request4',
    '/var/tmp',
    'Feature-Policy',
    'php_uname(',
    'file_put_contents(',
    'curl_exec(',
    'cmd_hex',
    'fputs_enc',
    '<PHPDATA>',
    'sys_GeT_TEMp_DIr',
    'eval headers',
    '$_COOKIE'
];

/*
|--------------------------------------------------------------------------
| Directories to skip
|--------------------------------------------------------------------------
*/

$excludeDirs = [
    'vendor',
    'node_modules',
    '.git',
    'cache',
    'storage',
    'logs'
];

/*
|--------------------------------------------------------------------------
| Scanner
|--------------------------------------------------------------------------
*/

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator(
        $root,
        FilesystemIterator::SKIP_DOTS
    )
);

echo "<h2>Scanning Website...</h2>";
echo "<pre>";

$found = false;

foreach ($iterator as $file) {

    try {

        if (!$file->isFile()) {
            continue;
        }

        $path = $file->getPathname();

        /*
        |--------------------------------------------------------------------------
        | Skip excluded directories
        |--------------------------------------------------------------------------
        */

        foreach ($excludeDirs as $dir) {
            if (strpos($path, DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR) !== false) {
                continue 2;
            }
        }

        /*
        |--------------------------------------------------------------------------
        | Only scan PHP files
        |--------------------------------------------------------------------------
        */

        if (strtolower($file->getExtension()) !== 'php') {
            continue;
        }

        /*
        |--------------------------------------------------------------------------
        | Read file safely
        |--------------------------------------------------------------------------
        */

        $content = @file_get_contents($path);

        if ($content === false) {
            echo "[ERROR] Cannot read: " . htmlspecialchars($path) . "\n";
            continue;
        }

        /*
        |--------------------------------------------------------------------------
        | Search patterns
        |--------------------------------------------------------------------------
        */

        foreach ($patterns as $pattern) {

            if (stripos($content, $pattern) !== false) {

                $found = true;

                echo "[FOUND] Pattern: " . htmlspecialchars($pattern) . "\n";
                echo "File: " . htmlspecialchars($path) . "\n";
                echo str_repeat("-", 60) . "\n";

                break;
            }
        }

    } catch (Exception $e) {

        echo "[ERROR] " . htmlspecialchars($e->getMessage()) . "\n";
    }
}

if (!$found) {
    echo "No suspicious patterns found.\n";
}

echo "\nScan Finished.";
echo "</pre>";

?>