PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/phpseclib/Net/SSH1.php

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