PHP script to scan PHP files for dangerous functions

🧩 Syntax:
<?php
/**
 * Script untuk memindai file PHP yang berpotensi berbahaya
 * di dalam server Apache, diurutkan berdasarkan waktu modifikasi.
 * * Fitur:
 * - Memindai file di seluruh subdirektori dari lokasi skrip ini.
 * - Hanya memindai file dengan ekstensi .php (case-insensitive).
 * - Menandai file yang mengandung fungsi-fungsi PHP berbahaya/mencurigakan.
 * - Mengurutkan hasil berdasarkan waktu modifikasi, dari yang terlama ke terbaru.
 */

// Batas waktu eksekusi skrip tidak terbatas
set_time_limit(0);

// Direktori awal pemindaian (lokasi skrip ini)
$start_dir = __DIR__;

// Daftar fungsi PHP yang dianggap mencurigakan
$suspicious_functions = [
    'base64_decode',
    'eval(',
    'gzinflate',
    'str_rot13',
    'shell_exec',
    'passthru',
    'exec(',
    'popen',
    'file_get_contents',
    'curl_init',
    'set_time_limit',            
    'proc_open',
    'system('
];

// Inisialisasi array untuk menyimpan data file
$files = [];

// Iterator untuk membaca semua file di dalam folder secara rekursif
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir));

// Loop melalui setiap file
foreach ($iterator as $file) {
    // Pastikan itu adalah sebuah file dan memiliki ekstensi .php
    if ($file->isFile() && strtolower($file->getExtension()) === 'php') {
        // Ambil isi file tanpa menampilkan error jika gagal
        $contents = @file_get_contents($file->getPathname());
        
        // Cek apakah ada fungsi mencurigakan di dalam konten file
        $is_suspicious = false;
        if ($contents !== false) { // Pastikan konten berhasil dibaca
            foreach ($suspicious_functions as $func) {
                // Gunakan strpos untuk pencarian string yang lebih cepat
                if (strpos($contents, $func) !== false) {
                    $is_suspicious = true;
                    break; // Keluar dari loop setelah menemukan satu fungsi mencurigakan
                }
            }
        }

        // Tambahkan informasi file ke dalam array
        $files[] = [
            'path' => $file->getPathname(),
            'mtime' => $file->getMTime(),
            'suspicious' => $is_suspicious
        ];
    }
}

// Urutkan file berdasarkan waktu modifikasi (lama ke baru)
usort($files, function($a, $b) {
    return $a['mtime'] - $b['mtime'];
});

// Mulai menampilkan hasil di browser
echo "<h2>πŸ“‚ Scan File: Lama &rarr; Baru + Pantau File Mencurigakan Tuan Brayend😎</h2>";
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<tr><th>No</th><th>Waktu Modifikasi</th><th>File Path</th><th>Status</th></tr>";

$no = 1;
foreach ($files as $file) {
    $time = date("Y-m-d H:i:s", $file['mtime']);
    $status = $file['suspicious'] ? "⚠ Mencurigakan" : "βœ… Aman";
    $color = $file['suspicious'] ? " style='background-color:#ffcccc'" : "";
    
    echo "<tr{$color}>";
    echo "<td>{$no}</td>";
    echo "<td>{$time}</td>";
    // Gunakan htmlspecialchars untuk mencegah XSS
    echo "<td>" . htmlspecialchars($file['path']) . "</td>";
    echo "<td>{$status}</td>";
    echo "</tr>";
    
    $no++;
}

echo "</table>";
echo "<hr><small>Hapus file scanner ini setelah selesai untuk keamanan.</small>";
?>