PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/vendor/phpseclib/Net/SSH1.php

https://bitbucket.org/arkross/venus
PHP | 1130 lines | 476 code | 119 blank | 535 comment | 60 complexity | 06e44a0c74b02a21c2d8e07ca7b875e2 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. namespace PHPSecLib;
  4. /**
  5. * Pure-PHP implementation of SSHv1.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * Here's a short example of how to use this library:
  10. * <code>
  11. * <?php
  12. * include('Net/SSH1.php');
  13. *
  14. * $ssh = new Net_SSH1('www.domain.tld');
  15. * if (!$ssh->login('username', 'password')) {
  16. * exit('Login Failed');
  17. * }
  18. *
  19. * while (true) {
  20. * echo $ssh->interactiveRead();
  21. *
  22. * $read = array(STDIN);
  23. * $write = $except = NULL;
  24. * if (stream_select($read, $write, $except, 0)) {
  25. * $ssh->interactiveWrite(fread(STDIN, 1));
  26. * }
  27. * }
  28. * ?>
  29. * </code>
  30. *
  31. * Here's another short example:
  32. * <code>
  33. * <?php
  34. * include('Net/SSH1.php');
  35. *
  36. * $ssh = new Net_SSH1('www.domain.tld');
  37. * if (!$ssh->login('username', 'password')) {
  38. * exit('Login Failed');
  39. * }
  40. *
  41. * echo $ssh->exec('ls -la');
  42. * ?>
  43. * </code>
  44. *
  45. * More information on the SSHv1 specification can be found by reading
  46. * {@link http://www.snailbook.com/docs/protocol-1.5.txt protocol-1.5.txt}.
  47. *
  48. * LICENSE: This library is free software; you can redistribute it and/or
  49. * modify it under the terms of the GNU Lesser General Public
  50. * License as published by the Free Software Foundation; either
  51. * version 2.1 of the License, or (at your option) any later version.
  52. *
  53. * This library is distributed in the hope that it will be useful,
  54. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  56. * Lesser General Public License for more details.
  57. *
  58. * You should have received a copy of the GNU Lesser General Public
  59. * License along with this library; if not, write to the Free Software
  60. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  61. * MA 02111-1307 USA
  62. *
  63. * @category Net
  64. * @package Net_SSH1
  65. * @author Jim Wigginton <terrafrost@php.net>
  66. * @copyright MMVII Jim Wigginton
  67. * @license http://www.gnu.org/licenses/lgpl.txt
  68. * @version $Id: SSH1.php,v 1.15 2010/03/22 22:01:38 terrafrost Exp $
  69. * @link http://phpseclib.sourceforge.net
  70. */
  71. /**#@+
  72. * Protocol Flags
  73. *
  74. * @access private
  75. */
  76. define('NET_SSH1_MSG_DISCONNECT', 1);
  77. define('NET_SSH1_SMSG_PUBLIC_KEY', 2);
  78. define('NET_SSH1_CMSG_SESSION_KEY', 3);
  79. define('NET_SSH1_CMSG_USER', 4);
  80. define('NET_SSH1_CMSG_AUTH_PASSWORD', 9);
  81. define('NET_SSH1_CMSG_REQUEST_PTY', 10);
  82. define('NET_SSH1_CMSG_EXEC_SHELL', 12);
  83. define('NET_SSH1_CMSG_EXEC_CMD', 13);
  84. define('NET_SSH1_SMSG_SUCCESS', 14);
  85. define('NET_SSH1_SMSG_FAILURE', 15);
  86. define('NET_SSH1_CMSG_STDIN_DATA', 16);
  87. define('NET_SSH1_SMSG_STDOUT_DATA', 17);
  88. define('NET_SSH1_SMSG_STDERR_DATA', 18);
  89. define('NET_SSH1_SMSG_EXITSTATUS', 20);
  90. define('NET_SSH1_CMSG_EXIT_CONFIRMATION', 33);
  91. /**#@-*/
  92. /**#@+
  93. * Encryption Methods
  94. *
  95. * @see Net_SSH1::getSupportedCiphers()
  96. * @access public
  97. */
  98. /**
  99. * No encryption
  100. *
  101. * Not supported.
  102. */
  103. define('NET_SSH1_CIPHER_NONE', 0);
  104. /**
  105. * IDEA in CFB mode
  106. *
  107. * Not supported.
  108. */
  109. define('NET_SSH1_CIPHER_IDEA', 1);
  110. /**
  111. * DES in CBC mode
  112. */
  113. define('NET_SSH1_CIPHER_DES', 2);
  114. /**
  115. * Triple-DES in CBC mode
  116. *
  117. * All implementations are required to support this
  118. */
  119. define('NET_SSH1_CIPHER_3DES', 3);
  120. /**
  121. * TRI's Simple Stream encryption CBC
  122. *
  123. * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h),
  124. * although it doesn't use it (see cipher.c)
  125. */
  126. define('NET_SSH1_CIPHER_BROKEN_TSS', 4);
  127. /**
  128. * RC4
  129. *
  130. * Not supported.
  131. *
  132. * @internal According to the SSH1 specs:
  133. *
  134. * "The first 16 bytes of the session key are used as the key for
  135. * the server to client direction. The remaining 16 bytes are used
  136. * as the key for the client to server direction. This gives
  137. * independent 128-bit keys for each direction."
  138. *
  139. * This library currently only supports encryption when the same key is being used for both directions. This is
  140. * because there's only one $crypto object. Two could be added ($encrypt and $decrypt, perhaps).
  141. */
  142. define('NET_SSH1_CIPHER_RC4', 5);
  143. /**
  144. * Blowfish
  145. *
  146. * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and
  147. * uses it (see cipher.c)
  148. */
  149. define('NET_SSH1_CIPHER_BLOWFISH', 6);
  150. /**#@-*/
  151. /**#@+
  152. * Authentication Methods
  153. *
  154. * @see Net_SSH1::getSupportedAuthentications()
  155. * @access public
  156. */
  157. /**
  158. * .rhosts or /etc/hosts.equiv
  159. */
  160. define('NET_SSH1_AUTH_RHOSTS', 1);
  161. /**
  162. * pure RSA authentication
  163. */
  164. define('NET_SSH1_AUTH_RSA', 2);
  165. /**
  166. * password authentication
  167. *
  168. * This is the only method that is supported by this library.
  169. */
  170. define('NET_SSH1_AUTH_PASSWORD', 3);
  171. /**
  172. * .rhosts with RSA host authentication
  173. */
  174. define('NET_SSH1_AUTH_RHOSTS_RSA', 4);
  175. /**#@-*/
  176. /**#@+
  177. * Terminal Modes
  178. *
  179. * @link http://3sp.com/content/developer/maverick-net/docs/Maverick.SSH.PseudoTerminalModesMembers.html
  180. * @access private
  181. */
  182. define('NET_SSH1_TTY_OP_END', 0);
  183. /**#@-*/
  184. /**
  185. * The Response Type
  186. *
  187. * @see Net_SSH1::_get_binary_packet()
  188. * @access private
  189. */
  190. define('NET_SSH1_RESPONSE_TYPE', 1);
  191. /**
  192. * The Response Data
  193. *
  194. * @see Net_SSH1::_get_binary_packet()
  195. * @access private
  196. */
  197. define('NET_SSH1_RESPONSE_DATA', 2);
  198. /**#@+
  199. * Execution Bitmap Masks
  200. *
  201. * @see Net_SSH1::bitmap
  202. * @access private
  203. */
  204. define('NET_SSH1_MASK_CONSTRUCTOR', 0x00000001);
  205. define('NET_SSH1_MASK_LOGIN', 0x00000002);
  206. define('NET_SSH1_MASK_SHELL', 0x00000004);
  207. /**#@-*/
  208. /**
  209. * Pure-PHP implementation of SSHv1.
  210. *
  211. * @author Jim Wigginton <terrafrost@php.net>
  212. * @version 0.1.0
  213. * @access public
  214. * @package Net_SSH1
  215. */
  216. class Net_SSH1 {
  217. /**
  218. * The SSH identifier
  219. *
  220. * @var String
  221. * @access private
  222. */
  223. var $identifier = 'SSH-1.5-phpseclib';
  224. /**
  225. * The Socket Object
  226. *
  227. * @var Object
  228. * @access private
  229. */
  230. var $fsock;
  231. /**
  232. * The cryptography object
  233. *
  234. * @var Object
  235. * @access private
  236. */
  237. var $crypto = false;
  238. /**
  239. * Execution Bitmap
  240. *
  241. * The bits that are set reprsent functions that have been called already. This is used to determine
  242. * if a requisite function has been successfully executed. If not, an error should be thrown.
  243. *
  244. * @var Integer
  245. * @access private
  246. */
  247. var $bitmap = 0;
  248. /**
  249. * The Server Key Public Exponent
  250. *
  251. * Logged for debug purposes
  252. *
  253. * @see Net_SSH1::getServerKeyPublicExponent()
  254. * @var String
  255. * @access private
  256. */
  257. var $server_key_public_exponent;
  258. /**
  259. * The Server Key Public Modulus
  260. *
  261. * Logged for debug purposes
  262. *
  263. * @see Net_SSH1::getServerKeyPublicModulus()
  264. * @var String
  265. * @access private
  266. */
  267. var $server_key_public_modulus;
  268. /**
  269. * The Host Key Public Exponent
  270. *
  271. * Logged for debug purposes
  272. *
  273. * @see Net_SSH1::getHostKeyPublicExponent()
  274. * @var String
  275. * @access private
  276. */
  277. var $host_key_public_exponent;
  278. /**
  279. * The Host Key Public Modulus
  280. *
  281. * Logged for debug purposes
  282. *
  283. * @see Net_SSH1::getHostKeyPublicModulus()
  284. * @var String
  285. * @access private
  286. */
  287. var $host_key_public_modulus;
  288. /**
  289. * Supported Ciphers
  290. *
  291. * Logged for debug purposes
  292. *
  293. * @see Net_SSH1::getSupportedCiphers()
  294. * @var Array
  295. * @access private
  296. */
  297. var $supported_ciphers = array(
  298. NET_SSH1_CIPHER_NONE => 'No encryption',
  299. NET_SSH1_CIPHER_IDEA => 'IDEA in CFB mode',
  300. NET_SSH1_CIPHER_DES => 'DES in CBC mode',
  301. NET_SSH1_CIPHER_3DES => 'Triple-DES in CBC mode',
  302. NET_SSH1_CIPHER_BROKEN_TSS => 'TRI\'s Simple Stream encryption CBC',
  303. NET_SSH1_CIPHER_RC4 => 'RC4',
  304. NET_SSH1_CIPHER_BLOWFISH => 'Blowfish'
  305. );
  306. /**
  307. * Supported Authentications
  308. *
  309. * Logged for debug purposes
  310. *
  311. * @see Net_SSH1::getSupportedAuthentications()
  312. * @var Array
  313. * @access private
  314. */
  315. var $supported_authentications = array(
  316. NET_SSH1_AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv',
  317. NET_SSH1_AUTH_RSA => 'pure RSA authentication',
  318. NET_SSH1_AUTH_PASSWORD => 'password authentication',
  319. NET_SSH1_AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication'
  320. );
  321. /**
  322. * Server Identification
  323. *
  324. * @see Net_SSH1::getServerIdentification()
  325. * @var String
  326. * @access private
  327. */
  328. var $server_identification = '';
  329. /**
  330. * Default Constructor.
  331. *
  332. * Connects to an SSHv1 server
  333. *
  334. * @param String $host
  335. * @param optional Integer $port
  336. * @param optional Integer $timeout
  337. * @param optional Integer $cipher
  338. * @return Net_SSH1
  339. * @access public
  340. */
  341. function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
  342. {
  343. $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
  344. if (!$this->fsock) {
  345. user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE);
  346. return;
  347. }
  348. $this->server_identification = $init_line = fgets($this->fsock, 255);
  349. if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) {
  350. user_error('Can only connect to SSH servers', E_USER_NOTICE);
  351. return;
  352. }
  353. if ($parts[1][0] != 1) {
  354. user_error("Cannot connect to SSH $parts[1] servers", E_USER_NOTICE);
  355. return;
  356. }
  357. fputs($this->fsock, $this->identifier."\r\n");
  358. $response = $this->_get_binary_packet();
  359. if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
  360. user_error('Expected SSH_SMSG_PUBLIC_KEY', E_USER_NOTICE);
  361. return;
  362. }
  363. $anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
  364. $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
  365. $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
  366. $server_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
  367. $this->server_key_public_exponent = $server_key_public_exponent;
  368. $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
  369. $server_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
  370. $this->server_key_public_modulus = $server_key_public_modulus;
  371. $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
  372. $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
  373. $host_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
  374. $this->host_key_public_exponent = $host_key_public_exponent;
  375. $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
  376. $host_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
  377. $this->host_key_public_modulus = $host_key_public_modulus;
  378. $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
  379. // get a list of the supported ciphers
  380. extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
  381. foreach ($this->supported_ciphers as $mask=>$name) {
  382. if (($supported_ciphers_mask & (1 << $mask)) == 0) {
  383. unset($this->supported_ciphers[$mask]);
  384. }
  385. }
  386. // get a list of the supported authentications
  387. extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
  388. foreach ($this->supported_authentications as $mask=>$name) {
  389. if (($supported_authentications_mask & (1 << $mask)) == 0) {
  390. unset($this->supported_authentications[$mask]);
  391. }
  392. }
  393. $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
  394. $session_key = '';
  395. for ($i = 0; $i < 32; $i++) {
  396. $session_key.= chr(crypt_random(0, 255));
  397. }
  398. $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
  399. if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
  400. $double_encrypted_session_key = $this->_rsa_crypt(
  401. $double_encrypted_session_key,
  402. array(
  403. $server_key_public_exponent,
  404. $server_key_public_modulus
  405. )
  406. );
  407. $double_encrypted_session_key = $this->_rsa_crypt(
  408. $double_encrypted_session_key,
  409. array(
  410. $host_key_public_exponent,
  411. $host_key_public_modulus
  412. )
  413. );
  414. } else {
  415. $double_encrypted_session_key = $this->_rsa_crypt(
  416. $double_encrypted_session_key,
  417. array(
  418. $host_key_public_exponent,
  419. $host_key_public_modulus
  420. )
  421. );
  422. $double_encrypted_session_key = $this->_rsa_crypt(
  423. $double_encrypted_session_key,
  424. array(
  425. $server_key_public_exponent,
  426. $server_key_public_modulus
  427. )
  428. );
  429. }
  430. $cipher = isset($this->supported_ciphers[$cipher]) ? $cipher : NET_SSH1_CIPHER_3DES;
  431. $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
  432. if (!$this->_send_binary_packet($data)) {
  433. user_error('Error sending SSH_CMSG_SESSION_KEY', E_USER_NOTICE);
  434. return;
  435. }
  436. switch ($cipher) {
  437. //case NET_SSH1_CIPHER_NONE:
  438. // $this->crypto = new Crypt_Null();
  439. // break;
  440. case NET_SSH1_CIPHER_DES:
  441. $this->crypto = new Crypt_DES();
  442. $this->crypto->disablePadding();
  443. $this->crypto->enableContinuousBuffer();
  444. $this->crypto->setKey(substr($session_key, 0, 8));
  445. break;
  446. case NET_SSH1_CIPHER_3DES:
  447. $this->crypto = new Crypt_TripleDES(CRYPT_DES_MODE_3CBC);
  448. $this->crypto->disablePadding();
  449. $this->crypto->enableContinuousBuffer();
  450. $this->crypto->setKey(substr($session_key, 0, 24));
  451. break;
  452. //case NET_SSH1_CIPHER_RC4:
  453. // $this->crypto = new Crypt_RC4();
  454. // $this->crypto->enableContinuousBuffer();
  455. // $this->crypto->setKey(substr($session_key, 0, 16));
  456. // break;
  457. }
  458. $response = $this->_get_binary_packet();
  459. if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
  460. user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE);
  461. return;
  462. }
  463. $this->bitmap = NET_SSH1_MASK_CONSTRUCTOR;
  464. }
  465. /**
  466. * Login
  467. *
  468. * @param String $username
  469. * @param optional String $password
  470. * @return Boolean
  471. * @access public
  472. */
  473. function login($username, $password = '')
  474. {
  475. if (!($this->bitmap & NET_SSH1_MASK_CONSTRUCTOR)) {
  476. return false;
  477. }
  478. $data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username);
  479. if (!$this->_send_binary_packet($data)) {
  480. user_error('Error sending SSH_CMSG_USER', E_USER_NOTICE);
  481. return false;
  482. }
  483. $response = $this->_get_binary_packet();
  484. if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) {
  485. $this->bitmap |= NET_SSH1_MASK_LOGIN;
  486. return true;
  487. } else if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) {
  488. user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE);
  489. return false;
  490. }
  491. $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password);
  492. if (!$this->_send_binary_packet($data)) {
  493. user_error('Error sending SSH_CMSG_AUTH_PASSWORD', E_USER_NOTICE);
  494. return false;
  495. }
  496. $response = $this->_get_binary_packet();
  497. if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) {
  498. $this->bitmap |= NET_SSH1_MASK_LOGIN;
  499. return true;
  500. } else if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) {
  501. return false;
  502. } else {
  503. user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE', E_USER_NOTICE);
  504. return false;
  505. }
  506. }
  507. /**
  508. * Executes a command on a non-interactive shell, returns the output, and quits.
  509. *
  510. * An SSH1 server will close the connection after a command has been executed on a non-interactive shell. SSH2
  511. * servers don't, however, this isn't an SSH2 client. The way this works, on the server, is by initiating a
  512. * shell with the -s option, as discussed in the following links:
  513. *
  514. * {@link http://www.faqs.org/docs/bashman/bashref_65.html http://www.faqs.org/docs/bashman/bashref_65.html}
  515. * {@link http://www.faqs.org/docs/bashman/bashref_62.html http://www.faqs.org/docs/bashman/bashref_62.html}
  516. *
  517. * To execute further commands, a new Net_SSH1 object will need to be created.
  518. *
  519. * Returns false on failure and the output, otherwise.
  520. *
  521. * @see Net_SSH1::interactiveRead()
  522. * @see Net_SSH1::interactiveWrite()
  523. * @param String $cmd
  524. * @return mixed
  525. * @access public
  526. */
  527. function exec($cmd)
  528. {
  529. if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
  530. user_error('Operation disallowed prior to login()', E_USER_NOTICE);
  531. return false;
  532. }
  533. // connect using the sample parameters in protocol-1.5.txt.
  534. // according to wikipedia.org's entry on text terminals, "the fundamental type of application running on a text
  535. // terminal is a command line interpreter or shell". thus, opening a terminal session to run the shell.
  536. $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END);
  537. if (!$this->_send_binary_packet($data)) {
  538. user_error('Error sending SSH_CMSG_REQUEST_PTY', E_USER_NOTICE);
  539. return false;
  540. }
  541. $response = $this->_get_binary_packet();
  542. if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
  543. user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE);
  544. return false;
  545. }
  546. $data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd);
  547. if (!$this->_send_binary_packet($data)) {
  548. user_error('Error sending SSH_CMSG_EXEC_CMD', E_USER_NOTICE);
  549. return false;
  550. }
  551. $output = '';
  552. $response = $this->_get_binary_packet();
  553. do {
  554. $output.= substr($response[NET_SSH1_RESPONSE_DATA], 4);
  555. $response = $this->_get_binary_packet();
  556. } while ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS);
  557. $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION);
  558. // i don't think it's really all that important if this packet gets sent or not.
  559. $this->_send_binary_packet($data);
  560. fclose($this->fsock);
  561. // reset the execution bitmap - a new Net_SSH1 object needs to be created.
  562. $this->bitmap = 0;
  563. return $output;
  564. }
  565. /**
  566. * Creates an interactive shell
  567. *
  568. * @see Net_SSH1::interactiveRead()
  569. * @see Net_SSH1::interactiveWrite()
  570. * @return Boolean
  571. * @access private
  572. */
  573. function _initShell()
  574. {
  575. $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END);
  576. if (!$this->_send_binary_packet($data)) {
  577. user_error('Error sending SSH_CMSG_REQUEST_PTY', E_USER_NOTICE);
  578. return false;
  579. }
  580. $response = $this->_get_binary_packet();
  581. if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
  582. user_error('Expected SSH_SMSG_SUCCESS', E_USER_NOTICE);
  583. return false;
  584. }
  585. $data = pack('C', NET_SSH1_CMSG_EXEC_SHELL);
  586. if (!$this->_send_binary_packet($data)) {
  587. user_error('Error sending SSH_CMSG_EXEC_SHELL', E_USER_NOTICE);
  588. return false;
  589. }
  590. $this->bitmap |= NET_SSH1_MASK_SHELL;
  591. //stream_set_blocking($this->fsock, 0);
  592. return true;
  593. }
  594. /**
  595. * Inputs a command into an interactive shell.
  596. *
  597. * @see Net_SSH1::interactiveRead()
  598. * @param String $cmd
  599. * @return Boolean
  600. * @access public
  601. */
  602. function interactiveWrite($cmd)
  603. {
  604. if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
  605. user_error('Operation disallowed prior to login()', E_USER_NOTICE);
  606. return false;
  607. }
  608. if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
  609. user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
  610. return false;
  611. }
  612. $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd);
  613. if (!$this->_send_binary_packet($data)) {
  614. user_error('Error sending SSH_CMSG_STDIN', E_USER_NOTICE);
  615. return false;
  616. }
  617. return true;
  618. }
  619. /**
  620. * Reads the output of an interactive shell.
  621. *
  622. * Requires PHP 4.3.0 or later due to the use of the stream_select() function. If you see stuff like
  623. * "", you're seeing ANSI escape codes. According to
  624. * {@link http://support.microsoft.com/kb/101875 How to Enable ANSI.SYS in a Command Window}, "Windows NT
  625. * does not support ANSI escape sequences in Win32 Console applications", so if you're a Windows user,
  626. * there's not going to be much recourse.
  627. *
  628. * @see Net_SSH1::interactiveRead()
  629. * @return String
  630. * @access public
  631. */
  632. function interactiveRead()
  633. {
  634. if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
  635. user_error('Operation disallowed prior to login()', E_USER_NOTICE);
  636. return false;
  637. }
  638. if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
  639. user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
  640. return false;
  641. }
  642. $read = array($this->fsock);
  643. $write = $except = null;
  644. if (stream_select($read, $write, $except, 0)) {
  645. $response = $this->_get_binary_packet();
  646. return substr($response[NET_SSH1_RESPONSE_DATA], 4);
  647. } else {
  648. return '';
  649. }
  650. }
  651. /**
  652. * Disconnect
  653. *
  654. * @access public
  655. */
  656. function disconnect()
  657. {
  658. $this->_disconnect();
  659. }
  660. /**
  661. * Destructor.
  662. *
  663. * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
  664. * disconnect().
  665. *
  666. * @access public
  667. */
  668. function __destruct()
  669. {
  670. $this->_disconnect();
  671. }
  672. /**
  673. * Disconnect
  674. *
  675. * @param String $msg
  676. * @access private
  677. */
  678. function _disconnect($msg = 'Client Quit')
  679. {
  680. if ($this->bitmap) {
  681. $data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg);
  682. $this->_send_binary_packet($data);
  683. fclose($this->fsock);
  684. $this->bitmap = 0;
  685. }
  686. }
  687. /**
  688. * Gets Binary Packets
  689. *
  690. * See 'The Binary Packet Protocol' of protocol-1.5.txt for more info.
  691. *
  692. * Also, this function could be improved upon by adding detection for the following exploit:
  693. * http://www.securiteam.com/securitynews/5LP042K3FY.html
  694. *
  695. * @see Net_SSH1::_send_binary_packet()
  696. * @return Array
  697. * @access private
  698. */
  699. function _get_binary_packet()
  700. {
  701. if (feof($this->fsock)) {
  702. //user_error('connection closed prematurely', E_USER_NOTICE);
  703. return false;
  704. }
  705. $temp = unpack('Nlength', fread($this->fsock, 4));
  706. $padding_length = 8 - ($temp['length'] & 7);
  707. $length = $temp['length'] + $padding_length;
  708. $raw = fread($this->fsock, $length);
  709. if ($this->crypto !== false) {
  710. $raw = $this->crypto->decrypt($raw);
  711. }
  712. $padding = substr($raw, 0, $padding_length);
  713. $type = $raw[$padding_length];
  714. $data = substr($raw, $padding_length + 1, -4);
  715. $temp = unpack('Ncrc', substr($raw, -4));
  716. //if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) {
  717. // user_error('Bad CRC in packet from server', E_USER_NOTICE);
  718. // return false;
  719. //}
  720. return array(
  721. NET_SSH1_RESPONSE_TYPE => ord($type),
  722. NET_SSH1_RESPONSE_DATA => $data
  723. );
  724. }
  725. /**
  726. * Sends Binary Packets
  727. *
  728. * Returns true on success, false on failure.
  729. *
  730. * @see Net_SSH1::_get_binary_packet()
  731. * @param String $data
  732. * @return Boolean
  733. * @access private
  734. */
  735. function _send_binary_packet($data) {
  736. if (feof($this->fsock)) {
  737. //user_error('connection closed prematurely', E_USER_NOTICE);
  738. return false;
  739. }
  740. $length = strlen($data) + 4;
  741. $padding_length = 8 - ($length & 7);
  742. $padding = '';
  743. for ($i = 0; $i < $padding_length; $i++) {
  744. $padding.= chr(crypt_random(0, 255));
  745. }
  746. $data = $padding . $data;
  747. $data.= pack('N', $this->_crc($data));
  748. if ($this->crypto !== false) {
  749. $data = $this->crypto->encrypt($data);
  750. }
  751. $packet = pack('Na*', $length, $data);
  752. return strlen($packet) == fputs($this->fsock, $packet);
  753. }
  754. /**
  755. * Cyclic Redundancy Check (CRC)
  756. *
  757. * PHP's crc32 function is implemented slightly differently than the one that SSH v1 uses, so
  758. * we've reimplemented it. A more detailed discussion of the differences can be found after
  759. * $crc_lookup_table's initialization.
  760. *
  761. * @see Net_SSH1::_get_binary_packet()
  762. * @see Net_SSH1::_send_binary_packet()
  763. * @param String $data
  764. * @return Integer
  765. * @access private
  766. */
  767. function _crc($data)
  768. {
  769. static $crc_lookup_table = array(
  770. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
  771. 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  772. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  773. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  774. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  775. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  776. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
  777. 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  778. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  779. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  780. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
  781. 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  782. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
  783. 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  784. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  785. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  786. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
  787. 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  788. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
  789. 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  790. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  791. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  792. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
  793. 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  794. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  795. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  796. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  797. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  798. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
  799. 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  800. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
  801. 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  802. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  803. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  804. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  805. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  806. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
  807. 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  808. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  809. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  810. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
  811. 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  812. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
  813. 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  814. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  815. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  816. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
  817. 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  818. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
  819. 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  820. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  821. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  822. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
  823. 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  824. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  825. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  826. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  827. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  828. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
  829. 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  830. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
  831. 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  832. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  833. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  834. );
  835. // For this function to yield the same output as PHP's crc32 function, $crc would have to be
  836. // set to 0xFFFFFFFF, initially - not 0x00000000 as it currently is.
  837. $crc = 0x00000000;
  838. $length = strlen($data);
  839. for ($i=0;$i<$length;$i++) {
  840. // We AND $crc >> 8 with 0x00FFFFFF because we want the eight newly added bits to all
  841. // be zero. PHP, unfortunately, doesn't always do this. 0x80000000 >> 8, as an example,
  842. // yields 0xFF800000 - not 0x00800000. The following link elaborates:
  843. // http://www.php.net/manual/en/language.operators.bitwise.php#57281
  844. $crc = (($crc >> 8) & 0x00FFFFFF) ^ $crc_lookup_table[($crc & 0xFF) ^ ord($data[$i])];
  845. }
  846. // In addition to having to set $crc to 0xFFFFFFFF, initially, the return value must be XOR'd with
  847. // 0xFFFFFFFF for this function to return the same thing that PHP's crc32 function would.
  848. return $crc;
  849. }
  850. /**
  851. * String Shift
  852. *
  853. * Inspired by array_shift
  854. *
  855. * @param String $string
  856. * @param optional Integer $index
  857. * @return String
  858. * @access private
  859. */
  860. function _string_shift(&$string, $index = 1)
  861. {
  862. $substr = substr($string, 0, $index);
  863. $string = substr($string, $index);
  864. return $substr;
  865. }
  866. /**
  867. * RSA Encrypt
  868. *
  869. * Returns mod(pow($m, $e), $n), where $n should be the product of two (large) primes $p and $q and where $e
  870. * should be a number with the property that gcd($e, ($p - 1) * ($q - 1)) == 1. Could just make anything that
  871. * calls this call modexp, instead, but I think this makes things clearer, maybe...
  872. *
  873. * @see Net_SSH1::Net_SSH1()
  874. * @param Math_BigInteger $m
  875. * @param Array $key
  876. * @return Math_BigInteger
  877. * @access private
  878. */
  879. function _rsa_crypt($m, $key)
  880. {
  881. /*
  882. if (!class_exists('Crypt_RSA')) {
  883. require_once('Crypt/RSA.php');
  884. }
  885. $rsa = new Crypt_RSA();
  886. $rsa->loadKey($key, CRYPT_RSA_PUBLIC_FORMAT_RAW);
  887. $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
  888. return $rsa->encrypt($m);
  889. */
  890. // To quote from protocol-1.5.txt:
  891. // The most significant byte (which is only partial as the value must be
  892. // less than the public modulus, which is never a power of two) is zero.
  893. //
  894. // The next byte contains the value 2 (which stands for public-key
  895. // encrypted data in the PKCS standard [PKCS#1]). Then, there are non-
  896. // zero random bytes to fill any unused space, a zero byte, and the data
  897. // to be encrypted in the least significant bytes, the last byte of the
  898. // data in the least significant byte.
  899. // Presumably the part of PKCS#1 they're refering to is "Section 7.2.1 Encryption Operation",
  900. // under "7.2 RSAES-PKCS1-v1.5" and "7 Encryption schemes" of the following URL:
  901. // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf
  902. $temp = chr(0) . chr(2);
  903. $modulus = $key[1]->toBytes();
  904. $length = strlen($modulus) - strlen($m) - 3;
  905. for ($i = 0; $i < $length; $i++) {
  906. $temp.= chr(crypt_random(1, 255));
  907. }
  908. $temp.= chr(0) . $m;
  909. $m = new Math_BigInteger($temp, 256);
  910. $m = $m->modPow($key[0], $key[1]);
  911. return $m->toBytes();
  912. }
  913. /**
  914. * Return the server key public exponent
  915. *
  916. * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
  917. * the raw bytes. This behavior is similar to PHP's md5() function.
  918. *
  919. * @param optional Boolean $raw_output
  920. * @return String
  921. * @access public
  922. */
  923. function getServerKeyPublicExponent($raw_output = false)
  924. {
  925. return $raw_output ? $this->server_key_public_exponent->toBytes() : $this->server_key_public_exponent->toString();
  926. }
  927. /**
  928. * Return the server key public modulus
  929. *
  930. * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
  931. * the raw bytes. This behavior is similar to PHP's md5() function.
  932. *
  933. * @param optional Boolean $raw_output
  934. * @return String
  935. * @access public
  936. */
  937. function getServerKeyPublicModulus($raw_output = false)
  938. {
  939. return $raw_output ? $this->server_key_public_modulus->toBytes() : $this->server_key_public_modulus->toString();
  940. }
  941. /**
  942. * Return the host key public exponent
  943. *
  944. * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
  945. * the raw bytes. This behavior is similar to PHP's md5() function.
  946. *
  947. * @param optional Boolean $raw_output
  948. * @return String
  949. * @access public
  950. */
  951. function getHostKeyPublicExponent($raw_output = false)
  952. {
  953. return $raw_output ? $this->host_key_public_exponent->toBytes() : $this->host_key_public_exponent->toString();
  954. }
  955. /**
  956. * Return the host key public modulus
  957. *
  958. * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
  959. * the raw bytes. This behavior is similar to PHP's md5() function.
  960. *
  961. * @param optional Boolean $raw_output
  962. * @return String
  963. * @access public
  964. */
  965. function getHostKeyPublicModulus($raw_output = false)
  966. {
  967. return $raw_output ? $this->host_key_public_modulus->toBytes() : $this->host_key_public_modulus->toString();
  968. }
  969. /**
  970. * Return a list of ciphers supported by SSH1 server.
  971. *
  972. * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output
  973. * is set to true, returns, instead, an array of constants. ie. instead of array('Triple-DES in CBC mode'), you'll
  974. * get array(NET_SSH1_CIPHER_3DES).
  975. *
  976. * @param optional Boolean $raw_output
  977. * @return Array
  978. * @access public
  979. */
  980. function getSupportedCiphers($raw_output = false)
  981. {
  982. return $raw_output ? array_keys($this->supported_ciphers) : array_values($this->supported_ciphers);
  983. }
  984. /**
  985. * Return a list of authentications supported by SSH1 server.
  986. *
  987. * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output
  988. * is set to true, returns, instead, an array of constants. ie. instead of array('password authentication'), you'll
  989. * get array(NET_SSH1_AUTH_PASSWORD).
  990. *
  991. * @param optional Boolean $raw_output
  992. * @return Array
  993. * @access public
  994. */
  995. function getSupportedAuthentications($raw_output = false)
  996. {
  997. return $raw_output ? array_keys($this->supported_authentications) : array_values($this->supported_authentications);
  998. }
  999. /**
  1000. * Return the server identification.
  1001. *
  1002. * @return String
  1003. * @access public
  1004. */
  1005. function getServerIdentification()
  1006. {
  1007. return rtrim($this->server_identification);
  1008. }
  1009. }
  1010. ?>