<?php
/**
 * FORENSIC FILE TIMELINE
 * ----------------------
 * Menampilkan daftar file yang diubah/dibuat dalam X hari terakhir
 * di bawah folder tempat script ini berada.
 *
 * Bisa membantu melihat:
 * - File apa saja yang terakhir disentuh (mungkin oleh hacker)
 * - Permission aneh (0444, 0111, dsb)
 */

//////////////// CONFIG ////////////////

// GANTI ini jadi token rahasia kamu
define('FORENSIC_TOKEN', 'dd79ead1e07d3a750218b99655f36f9aaf6c0a70aee8c979cebc757a15df1c6f');

// Root scan = lokasi file ini
define('FORENSIC_ROOT', __DIR__);

// Batas default berapa hari ke belakang
define('DEFAULT_DAYS', 30);

// Maksimal file yang ditampilkan (untuk jaga performa)
define('MAX_RESULTS', 5000);

///////////////////////////////////////

if (!isset($_GET['token']) || $_GET['token'] !== FORENSIC_TOKEN) {
    header('HTTP/1.1 403 Forbidden');
    exit('403 Forbidden - token salah');
}

// Debug sementara
ini_set('display_errors', 1);
error_reporting(E_ALL);

function h($s) {
    return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
}

$days = isset($_GET['days']) ? (int)$_GET['days'] : DEFAULT_DAYS;
if ($days <= 0) $days = DEFAULT_DAYS;

$cutoff = time() - ($days * 86400);

$results = [];
$stack   = [FORENSIC_ROOT];

while (!empty($stack)) {
    $dir = array_pop($stack);

    if (!is_dir($dir) || !is_readable($dir)) {
        continue;
    }

    $dh = @opendir($dir);
    if (!$dh) continue;

    while (($entry = readdir($dh)) !== false) {
        if ($entry === '.' || $entry === '..') continue;

        $path = $dir . DIRECTORY_SEPARATOR . $entry;

        if (is_dir($path)) {
            $stack[] = $path;
            continue;
        }

        if (!is_file($path)) continue;

        $mtime = @filemtime($path);
        if ($mtime === false || $mtime < $cutoff) {
            continue;
        }

        $size = @filesize($path);
        $perm = @fileperms($path);
        $type = 'other';

        $basename = basename($path);
        $ext = strtolower(pathinfo($basename, PATHINFO_EXTENSION));

        if ($basename === '.htaccess') {
            $type = 'htaccess';
        } elseif ($ext === 'php' || $ext === 'php5' || $ext === 'php7' || $ext === 'phtml') {
            $type = 'php';
        }

        $permOct = $perm !== false ? substr(sprintf('%o', $perm), -3) : '???';

        $results[] = [
            'path'  => $path,
            'mtime' => $mtime,
            'size'  => $size,
            'perm'  => $permOct,
            'type'  => $type,
        ];

        if (count($results) >= MAX_RESULTS) {
            break 2;
        }
    }

    closedir($dh);
}

// Urutkan berdasarkan waktu modifikasi terbaru
usort($results, function($a, $b) {
    if ($a['mtime'] == $b['mtime']) return 0;
    return ($a['mtime'] < $b['mtime']) ? 1 : -1;
});

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Forensic File Timeline</title>
    <style>
        body { background:#111; color:#eee; font-family:Consolas,monospace; font-size:14px; }
        h1,h2 { color:#7cf77c; }
        table { width:100%; border-collapse:collapse; margin-top:15px; }
        th,td { border:1px solid #444; padding:4px 6px; font-size:12px; }
        th { background:#222; }
        tr:nth-child(odd) { background:#151515; }
        tr:nth-child(even){ background:#1b1b1b; }
        .php { color:#48dbfb; }
        .htaccess { color:#feca57; }
        .oddperm { color:#ff6b6b; font-weight:bold; }
        input[type=text] {
            width:60%; padding:4px;
            background:#222; border:1px solid #555; color:#eee;
        }
        input[type=submit] {
            padding:4px 10px; background:#28a745;
            border:none; color:#fff; cursor:pointer;
        }
        input[type=submit]:hover { background:#218838; }
    </style>
</head>
<body>
<h1>Forensic File Timeline</h1>

<p>Menampilkan file yang diubah / dibuat dalam <strong><?php echo (int)$days; ?></strong> hari terakhir, di bawah:</p>
<pre><?php echo h(FORENSIC_ROOT); ?></pre>

<form method="get">
    <input type="hidden" name="token" value="<?php echo h($_GET['token']); ?>">
    <label>Rentang hari ke belakang:</label><br>
    <input type="text" name="days" value="<?php echo (int)$days; ?>">
    <input type="submit" value="Update">
</form>

<p>Total file yang ditemukan: <strong><?php echo count($results); ?></strong>
<?php if (count($results) >= MAX_RESULTS): ?>
    (dibatasi <?php echo MAX_RESULTS; ?> pertama)
<?php endif; ?>
</p>

<table>
    <tr>
        <th>#</th>
        <th>Waktu Modifikasi</th>
        <th>Path</th>
        <th>Ukuran</th>
        <th>Perm</th>
        <th>Tipe</th>
    </tr>
    <?php foreach ($results as $i => $r): ?>
        <?php
        $clsType = '';
        if ($r['type'] === 'php') $clsType = 'php';
        elseif ($r['type'] === '.htaccess' || $r['type'] === 'htaccess') $clsType = 'htaccess';

        $clsPerm = '';
        if (in_array($r['perm'], ['000','400','440','444','500','555'], true)) {
            $clsPerm = 'oddperm';
        }
        ?>
        <tr>
            <td><?php echo $i+1; ?></td>
            <td><?php echo date('Y-m-d H:i:s', $r['mtime']); ?></td>
            <td class="<?php echo $clsType; ?>"><?php echo h($r['path']); ?></td>
            <td><?php echo number_format($r['size']); ?> B</td>
            <td class="<?php echo $clsPerm; ?>"><?php echo h($r['perm']); ?></td>
            <td><?php echo h($r['type']); ?></td>
        </tr>
    <?php endforeach; ?>
</table>

<p><small>
Tips:
<ul>
    <li>Perhatikan file <span class="php">PHP</span> dan <span class="htaccess">.htaccess</span> yang baru diubah.</li>
    <li>Permission <span class="oddperm">0444 / 0400 / 000</span> sering dipakai hacker untuk mengunci shell.</li>
    <li>Kalau mau fokus ke hari tertentu (misal mulai hari hack), ubah parameter <code>days</code> di URL.</li>
</ul>
</small></p>

</body>
</html>
