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

/system/expressionengine/third_party/updater/libraries/phpseclib/Net/SSH2.php

https://bitbucket.org/studiobreakfast/sync
PHP | 2659 lines | 1408 code | 321 blank | 930 comment | 227 complexity | 7b74b23941e07be4e74004b2552e82e3 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP implementation of SSHv2.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * Here are some examples of how to use this library:
  9. * <code>
  10. * <?php
  11. * include('Net/SSH2.php');
  12. *
  13. * $ssh = new Net_SSH2('www.domain.tld');
  14. * if (!$ssh->login('username', 'password')) {
  15. * exit('Login Failed');
  16. * }
  17. *
  18. * echo $ssh->exec('pwd');
  19. * echo $ssh->exec('ls -la');
  20. * ?>
  21. * </code>
  22. *
  23. * <code>
  24. * <?php
  25. * include('Crypt/RSA.php');
  26. * include('Net/SSH2.php');
  27. *
  28. * $key = new Crypt_RSA();
  29. * //$key->setPassword('whatever');
  30. * $key->loadKey(file_get_contents('privatekey'));
  31. *
  32. * $ssh = new Net_SSH2('www.domain.tld');
  33. * if (!$ssh->login('username', $key)) {
  34. * exit('Login Failed');
  35. * }
  36. *
  37. * echo $ssh->read('username@username:~$');
  38. * $ssh->write("ls -la\n");
  39. * echo $ssh->read('username@username:~$');
  40. * ?>
  41. * </code>
  42. *
  43. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  44. * of this software and associated documentation files (the "Software"), to deal
  45. * in the Software without restriction, including without limitation the rights
  46. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  47. * copies of the Software, and to permit persons to whom the Software is
  48. * furnished to do so, subject to the following conditions:
  49. *
  50. * The above copyright notice and this permission notice shall be included in
  51. * all copies or substantial portions of the Software.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  54. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  55. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  56. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  57. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  58. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  59. * THE SOFTWARE.
  60. *
  61. * @category Net
  62. * @package Net_SSH2
  63. * @author Jim Wigginton <terrafrost@php.net>
  64. * @copyright MMVII Jim Wigginton
  65. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  66. * @version $Id: SSH2.php,v 1.53 2010-10-24 01:24:30 terrafrost Exp $
  67. * @link http://phpseclib.sourceforge.net
  68. */
  69. /**
  70. * Include Math_BigInteger
  71. *
  72. * Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
  73. */
  74. require_once(dirname(dirname(__FILE__)).'/Math/BigInteger.php');
  75. /**
  76. * Include Crypt_Random
  77. */
  78. require_once(dirname(dirname(__FILE__)).'/Crypt/Random.php');
  79. /**
  80. * Include Crypt_Hash
  81. */
  82. require_once(dirname(dirname(__FILE__)).'/Crypt/Hash.php');
  83. /**
  84. * Include Crypt_TripleDES
  85. */
  86. require_once(dirname(dirname(__FILE__)).'/Crypt/TripleDES.php');
  87. /**
  88. * Include Crypt_RC4
  89. */
  90. require_once(dirname(dirname(__FILE__)).'/Crypt/RC4.php');
  91. /**
  92. * Include Crypt_AES
  93. */
  94. require_once(dirname(dirname(__FILE__)).'/Crypt/AES.php');
  95. /**#@+
  96. * Execution Bitmap Masks
  97. *
  98. * @see Net_SSH2::bitmap
  99. * @access private
  100. */
  101. define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001);
  102. define('NET_SSH2_MASK_LOGIN', 0x00000002);
  103. define('NET_SSH2_MASK_SHELL', 0x00000004);
  104. /**#@-*/
  105. /**#@+
  106. * Channel constants
  107. *
  108. * RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer
  109. * to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with
  110. * a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a
  111. * recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel
  112. * would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet:
  113. * The 'recipient channel' is the channel number given in the original
  114. * open request, and 'sender channel' is the channel number allocated by
  115. * the other side.
  116. *
  117. * @see Net_SSH2::_send_channel_packet()
  118. * @see Net_SSH2::_get_channel_packet()
  119. * @access private
  120. */
  121. define('NET_SSH2_CHANNEL_EXEC', 0); // PuTTy uses 0x100
  122. define('NET_SSH2_CHANNEL_SHELL',1);
  123. /**#@-*/
  124. /**#@+
  125. * @access public
  126. * @see Net_SSH2::getLog()
  127. */
  128. /**
  129. * Returns the message numbers
  130. */
  131. define('NET_SSH2_LOG_SIMPLE', 1);
  132. /**
  133. * Returns the message content
  134. */
  135. define('NET_SSH2_LOG_COMPLEX', 2);
  136. /**#@-*/
  137. /**#@+
  138. * @access public
  139. * @see Net_SSH2::read()
  140. */
  141. /**
  142. * Returns when a string matching $expect exactly is found
  143. */
  144. define('NET_SSH2_READ_SIMPLE', 1);
  145. /**
  146. * Returns when a string matching the regular expression $expect is found
  147. */
  148. define('NET_SSH2_READ_REGEX', 2);
  149. /**#@-*/
  150. /**
  151. * Pure-PHP implementation of SSHv2.
  152. *
  153. * @author Jim Wigginton <terrafrost@php.net>
  154. * @version 0.1.0
  155. * @access public
  156. * @package Net_SSH2
  157. */
  158. class Net_SSH2 {
  159. /**
  160. * The SSH identifier
  161. *
  162. * @var String
  163. * @access private
  164. */
  165. var $identifier = 'SSH-2.0-phpseclib_0.2';
  166. /**
  167. * The Socket Object
  168. *
  169. * @var Object
  170. * @access private
  171. */
  172. var $fsock;
  173. /**
  174. * Execution Bitmap
  175. *
  176. * The bits that are set reprsent functions that have been called already. This is used to determine
  177. * if a requisite function has been successfully executed. If not, an error should be thrown.
  178. *
  179. * @var Integer
  180. * @access private
  181. */
  182. var $bitmap = 0;
  183. /**
  184. * Error information
  185. *
  186. * @see Net_SSH2::getErrors()
  187. * @see Net_SSH2::getLastError()
  188. * @var String
  189. * @access private
  190. */
  191. var $errors = array();
  192. /**
  193. * Server Identifier
  194. *
  195. * @see Net_SSH2::getServerIdentification()
  196. * @var String
  197. * @access private
  198. */
  199. var $server_identifier = '';
  200. /**
  201. * Key Exchange Algorithms
  202. *
  203. * @see Net_SSH2::getKexAlgorithims()
  204. * @var Array
  205. * @access private
  206. */
  207. var $kex_algorithms;
  208. /**
  209. * Server Host Key Algorithms
  210. *
  211. * @see Net_SSH2::getServerHostKeyAlgorithms()
  212. * @var Array
  213. * @access private
  214. */
  215. var $server_host_key_algorithms;
  216. /**
  217. * Encryption Algorithms: Client to Server
  218. *
  219. * @see Net_SSH2::getEncryptionAlgorithmsClient2Server()
  220. * @var Array
  221. * @access private
  222. */
  223. var $encryption_algorithms_client_to_server;
  224. /**
  225. * Encryption Algorithms: Server to Client
  226. *
  227. * @see Net_SSH2::getEncryptionAlgorithmsServer2Client()
  228. * @var Array
  229. * @access private
  230. */
  231. var $encryption_algorithms_server_to_client;
  232. /**
  233. * MAC Algorithms: Client to Server
  234. *
  235. * @see Net_SSH2::getMACAlgorithmsClient2Server()
  236. * @var Array
  237. * @access private
  238. */
  239. var $mac_algorithms_client_to_server;
  240. /**
  241. * MAC Algorithms: Server to Client
  242. *
  243. * @see Net_SSH2::getMACAlgorithmsServer2Client()
  244. * @var Array
  245. * @access private
  246. */
  247. var $mac_algorithms_server_to_client;
  248. /**
  249. * Compression Algorithms: Client to Server
  250. *
  251. * @see Net_SSH2::getCompressionAlgorithmsClient2Server()
  252. * @var Array
  253. * @access private
  254. */
  255. var $compression_algorithms_client_to_server;
  256. /**
  257. * Compression Algorithms: Server to Client
  258. *
  259. * @see Net_SSH2::getCompressionAlgorithmsServer2Client()
  260. * @var Array
  261. * @access private
  262. */
  263. var $compression_algorithms_server_to_client;
  264. /**
  265. * Languages: Server to Client
  266. *
  267. * @see Net_SSH2::getLanguagesServer2Client()
  268. * @var Array
  269. * @access private
  270. */
  271. var $languages_server_to_client;
  272. /**
  273. * Languages: Client to Server
  274. *
  275. * @see Net_SSH2::getLanguagesClient2Server()
  276. * @var Array
  277. * @access private
  278. */
  279. var $languages_client_to_server;
  280. /**
  281. * Block Size for Server to Client Encryption
  282. *
  283. * "Note that the length of the concatenation of 'packet_length',
  284. * 'padding_length', 'payload', and 'random padding' MUST be a multiple
  285. * of the cipher block size or 8, whichever is larger. This constraint
  286. * MUST be enforced, even when using stream ciphers."
  287. *
  288. * -- http://tools.ietf.org/html/rfc4253#section-6
  289. *
  290. * @see Net_SSH2::Net_SSH2()
  291. * @see Net_SSH2::_send_binary_packet()
  292. * @var Integer
  293. * @access private
  294. */
  295. var $encrypt_block_size = 8;
  296. /**
  297. * Block Size for Client to Server Encryption
  298. *
  299. * @see Net_SSH2::Net_SSH2()
  300. * @see Net_SSH2::_get_binary_packet()
  301. * @var Integer
  302. * @access private
  303. */
  304. var $decrypt_block_size = 8;
  305. /**
  306. * Server to Client Encryption Object
  307. *
  308. * @see Net_SSH2::_get_binary_packet()
  309. * @var Object
  310. * @access private
  311. */
  312. var $decrypt = false;
  313. /**
  314. * Client to Server Encryption Object
  315. *
  316. * @see Net_SSH2::_send_binary_packet()
  317. * @var Object
  318. * @access private
  319. */
  320. var $encrypt = false;
  321. /**
  322. * Client to Server HMAC Object
  323. *
  324. * @see Net_SSH2::_send_binary_packet()
  325. * @var Object
  326. * @access private
  327. */
  328. var $hmac_create = false;
  329. /**
  330. * Server to Client HMAC Object
  331. *
  332. * @see Net_SSH2::_get_binary_packet()
  333. * @var Object
  334. * @access private
  335. */
  336. var $hmac_check = false;
  337. /**
  338. * Size of server to client HMAC
  339. *
  340. * We need to know how big the HMAC will be for the server to client direction so that we know how many bytes to read.
  341. * For the client to server side, the HMAC object will make the HMAC as long as it needs to be. All we need to do is
  342. * append it.
  343. *
  344. * @see Net_SSH2::_get_binary_packet()
  345. * @var Integer
  346. * @access private
  347. */
  348. var $hmac_size = false;
  349. /**
  350. * Server Public Host Key
  351. *
  352. * @see Net_SSH2::getServerPublicHostKey()
  353. * @var String
  354. * @access private
  355. */
  356. var $server_public_host_key;
  357. /**
  358. * Session identifer
  359. *
  360. * "The exchange hash H from the first key exchange is additionally
  361. * used as the session identifier, which is a unique identifier for
  362. * this connection."
  363. *
  364. * -- http://tools.ietf.org/html/rfc4253#section-7.2
  365. *
  366. * @see Net_SSH2::_key_exchange()
  367. * @var String
  368. * @access private
  369. */
  370. var $session_id = false;
  371. /**
  372. * Exchange hash
  373. *
  374. * The current exchange hash
  375. *
  376. * @see Net_SSH2::_key_exchange()
  377. * @var String
  378. * @access private
  379. */
  380. var $exchange_hash = false;
  381. /**
  382. * Message Numbers
  383. *
  384. * @see Net_SSH2::Net_SSH2()
  385. * @var Array
  386. * @access private
  387. */
  388. var $message_numbers = array();
  389. /**
  390. * Disconnection Message 'reason codes' defined in RFC4253
  391. *
  392. * @see Net_SSH2::Net_SSH2()
  393. * @var Array
  394. * @access private
  395. */
  396. var $disconnect_reasons = array();
  397. /**
  398. * SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
  399. *
  400. * @see Net_SSH2::Net_SSH2()
  401. * @var Array
  402. * @access private
  403. */
  404. var $channel_open_failure_reasons = array();
  405. /**
  406. * Terminal Modes
  407. *
  408. * @link http://tools.ietf.org/html/rfc4254#section-8
  409. * @see Net_SSH2::Net_SSH2()
  410. * @var Array
  411. * @access private
  412. */
  413. var $terminal_modes = array();
  414. /**
  415. * SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
  416. *
  417. * @link http://tools.ietf.org/html/rfc4254#section-5.2
  418. * @see Net_SSH2::Net_SSH2()
  419. * @var Array
  420. * @access private
  421. */
  422. var $channel_extended_data_type_codes = array();
  423. /**
  424. * Send Sequence Number
  425. *
  426. * See 'Section 6.4. Data Integrity' of rfc4253 for more info.
  427. *
  428. * @see Net_SSH2::_send_binary_packet()
  429. * @var Integer
  430. * @access private
  431. */
  432. var $send_seq_no = 0;
  433. /**
  434. * Get Sequence Number
  435. *
  436. * See 'Section 6.4. Data Integrity' of rfc4253 for more info.
  437. *
  438. * @see Net_SSH2::_get_binary_packet()
  439. * @var Integer
  440. * @access private
  441. */
  442. var $get_seq_no = 0;
  443. /**
  444. * Server Channels
  445. *
  446. * Maps client channels to server channels
  447. *
  448. * @see Net_SSH2::_get_channel_packet()
  449. * @see Net_SSH2::exec()
  450. * @var Array
  451. * @access private
  452. */
  453. var $server_channels = array();
  454. /**
  455. * Channel Buffers
  456. *
  457. * If a client requests a packet from one channel but receives two packets from another those packets should
  458. * be placed in a buffer
  459. *
  460. * @see Net_SSH2::_get_channel_packet()
  461. * @see Net_SSH2::exec()
  462. * @var Array
  463. * @access private
  464. */
  465. var $channel_buffers = array();
  466. /**
  467. * Channel Status
  468. *
  469. * Contains the type of the last sent message
  470. *
  471. * @see Net_SSH2::_get_channel_packet()
  472. * @var Array
  473. * @access private
  474. */
  475. var $channel_status = array();
  476. /**
  477. * Packet Size
  478. *
  479. * Maximum packet size indexed by channel
  480. *
  481. * @see Net_SSH2::_send_channel_packet()
  482. * @var Array
  483. * @access private
  484. */
  485. var $packet_size_client_to_server = array();
  486. /**
  487. * Message Number Log
  488. *
  489. * @see Net_SSH2::getLog()
  490. * @var Array
  491. * @access private
  492. */
  493. var $message_number_log = array();
  494. /**
  495. * Message Log
  496. *
  497. * @see Net_SSH2::getLog()
  498. * @var Array
  499. * @access private
  500. */
  501. var $message_log = array();
  502. /**
  503. * The Window Size
  504. *
  505. * Bytes the other party can send before it must wait for the window to be adjusted (0x7FFFFFFF = 4GB)
  506. *
  507. * @var Integer
  508. * @see Net_SSH2::_send_channel_packet()
  509. * @see Net_SSH2::exec()
  510. * @access private
  511. */
  512. var $window_size = 0x7FFFFFFF;
  513. /**
  514. * Window size
  515. *
  516. * Window size indexed by channel
  517. *
  518. * @see Net_SSH2::_send_channel_packet()
  519. * @var Array
  520. * @access private
  521. */
  522. var $window_size_client_to_server = array();
  523. /**
  524. * Server signature
  525. *
  526. * Verified against $this->session_id
  527. *
  528. * @see Net_SSH2::getServerPublicHostKey()
  529. * @var String
  530. * @access private
  531. */
  532. var $signature = '';
  533. /**
  534. * Server signature format
  535. *
  536. * ssh-rsa or ssh-dss.
  537. *
  538. * @see Net_SSH2::getServerPublicHostKey()
  539. * @var String
  540. * @access private
  541. */
  542. var $signature_format = '';
  543. /**
  544. * Interactive Buffer
  545. *
  546. * @see Net_SSH2::read()
  547. * @var Array
  548. * @access private
  549. */
  550. var $interactiveBuffer = '';
  551. /**
  552. * Default Constructor.
  553. *
  554. * Connects to an SSHv2 server
  555. *
  556. * @param String $host
  557. * @param optional Integer $port
  558. * @param optional Integer $timeout
  559. * @return Net_SSH2
  560. * @access public
  561. */
  562. function Net_SSH2($host, $port = 22, $timeout = 10)
  563. {
  564. $this->message_numbers = array(
  565. 1 => 'NET_SSH2_MSG_DISCONNECT',
  566. 2 => 'NET_SSH2_MSG_IGNORE',
  567. 3 => 'NET_SSH2_MSG_UNIMPLEMENTED',
  568. 4 => 'NET_SSH2_MSG_DEBUG',
  569. 5 => 'NET_SSH2_MSG_SERVICE_REQUEST',
  570. 6 => 'NET_SSH2_MSG_SERVICE_ACCEPT',
  571. 20 => 'NET_SSH2_MSG_KEXINIT',
  572. 21 => 'NET_SSH2_MSG_NEWKEYS',
  573. 30 => 'NET_SSH2_MSG_KEXDH_INIT',
  574. 31 => 'NET_SSH2_MSG_KEXDH_REPLY',
  575. 50 => 'NET_SSH2_MSG_USERAUTH_REQUEST',
  576. 51 => 'NET_SSH2_MSG_USERAUTH_FAILURE',
  577. 52 => 'NET_SSH2_MSG_USERAUTH_SUCCESS',
  578. 53 => 'NET_SSH2_MSG_USERAUTH_BANNER',
  579. 80 => 'NET_SSH2_MSG_GLOBAL_REQUEST',
  580. 81 => 'NET_SSH2_MSG_REQUEST_SUCCESS',
  581. 82 => 'NET_SSH2_MSG_REQUEST_FAILURE',
  582. 90 => 'NET_SSH2_MSG_CHANNEL_OPEN',
  583. 91 => 'NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION',
  584. 92 => 'NET_SSH2_MSG_CHANNEL_OPEN_FAILURE',
  585. 93 => 'NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST',
  586. 94 => 'NET_SSH2_MSG_CHANNEL_DATA',
  587. 95 => 'NET_SSH2_MSG_CHANNEL_EXTENDED_DATA',
  588. 96 => 'NET_SSH2_MSG_CHANNEL_EOF',
  589. 97 => 'NET_SSH2_MSG_CHANNEL_CLOSE',
  590. 98 => 'NET_SSH2_MSG_CHANNEL_REQUEST',
  591. 99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS',
  592. 100 => 'NET_SSH2_MSG_CHANNEL_FAILURE'
  593. );
  594. $this->disconnect_reasons = array(
  595. 1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
  596. 2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
  597. 3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
  598. 4 => 'NET_SSH2_DISCONNECT_RESERVED',
  599. 5 => 'NET_SSH2_DISCONNECT_MAC_ERROR',
  600. 6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR',
  601. 7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE',
  602. 8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED',
  603. 9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE',
  604. 10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST',
  605. 11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION',
  606. 12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS',
  607. 13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER',
  608. 14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
  609. 15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
  610. );
  611. $this->channel_open_failure_reasons = array(
  612. 1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED'
  613. );
  614. $this->terminal_modes = array(
  615. 0 => 'NET_SSH2_TTY_OP_END'
  616. );
  617. $this->channel_extended_data_type_codes = array(
  618. 1 => 'NET_SSH2_EXTENDED_DATA_STDERR'
  619. );
  620. $this->_define_array(
  621. $this->message_numbers,
  622. $this->disconnect_reasons,
  623. $this->channel_open_failure_reasons,
  624. $this->terminal_modes,
  625. $this->channel_extended_data_type_codes,
  626. array(60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'),
  627. array(60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'),
  628. array(60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
  629. 61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE')
  630. );
  631. $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
  632. if (!$this->fsock) {
  633. user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"), E_USER_NOTICE);
  634. return;
  635. }
  636. /* According to the SSH2 specs,
  637. "The server MAY send other lines of data before sending the version
  638. string. Each line SHOULD be terminated by a Carriage Return and Line
  639. Feed. Such lines MUST NOT begin with "SSH-", and SHOULD be encoded
  640. in ISO-10646 UTF-8 [RFC3629] (language is not specified). Clients
  641. MUST be able to process such lines." */
  642. $temp = '';
  643. $extra = '';
  644. while (!feof($this->fsock) && !preg_match('#^SSH-(\d\.\d+)#', $temp, $matches)) {
  645. if (substr($temp, -2) == "\r\n") {
  646. $extra.= $temp;
  647. $temp = '';
  648. }
  649. $temp.= fgets($this->fsock, 255);
  650. }
  651. if (feof($this->fsock)) {
  652. user_error('Connection closed by server', E_USER_NOTICE);
  653. return false;
  654. }
  655. $ext = array();
  656. if (extension_loaded('mcrypt')) {
  657. $ext[] = 'mcrypt';
  658. }
  659. if (extension_loaded('gmp')) {
  660. $ext[] = 'gmp';
  661. } else if (extension_loaded('bcmath')) {
  662. $ext[] = 'bcmath';
  663. }
  664. if (!empty($ext)) {
  665. $this->identifier.= ' (' . implode(', ', $ext) . ')';
  666. }
  667. if (defined('NET_SSH2_LOGGING')) {
  668. $this->message_number_log[] = '<-';
  669. $this->message_number_log[] = '->';
  670. if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
  671. $this->message_log[] = $temp;
  672. $this->message_log[] = $this->identifier . "\r\n";
  673. }
  674. }
  675. $this->server_identifier = trim($temp, "\r\n");
  676. if (!empty($extra)) {
  677. $this->errors[] = utf8_decode($extra);
  678. }
  679. if ($matches[1] != '1.99' && $matches[1] != '2.0') {
  680. user_error("Cannot connect to SSH $matches[1] servers", E_USER_NOTICE);
  681. return;
  682. }
  683. fputs($this->fsock, $this->identifier . "\r\n");
  684. $response = $this->_get_binary_packet();
  685. if ($response === false) {
  686. user_error('Connection closed by server', E_USER_NOTICE);
  687. return;
  688. }
  689. if (ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
  690. user_error('Expected SSH_MSG_KEXINIT', E_USER_NOTICE);
  691. return;
  692. }
  693. if (!$this->_key_exchange($response)) {
  694. return;
  695. }
  696. $this->bitmap = NET_SSH2_MASK_CONSTRUCTOR;
  697. }
  698. /**
  699. * Key Exchange
  700. *
  701. * @param String $kexinit_payload_server
  702. * @access private
  703. */
  704. function _key_exchange($kexinit_payload_server)
  705. {
  706. static $kex_algorithms = array(
  707. 'diffie-hellman-group1-sha1', // REQUIRED
  708. 'diffie-hellman-group14-sha1' // REQUIRED
  709. );
  710. static $server_host_key_algorithms = array(
  711. 'ssh-rsa', // RECOMMENDED sign Raw RSA Key
  712. 'ssh-dss' // REQUIRED sign Raw DSS Key
  713. );
  714. static $encryption_algorithms = array(
  715. // from <http://tools.ietf.org/html/rfc4345#section-4>:
  716. 'arcfour256',
  717. 'arcfour128',
  718. 'arcfour', // OPTIONAL the ARCFOUR stream cipher with a 128-bit key
  719. 'aes128-cbc', // RECOMMENDED AES with a 128-bit key
  720. 'aes192-cbc', // OPTIONAL AES with a 192-bit key
  721. 'aes256-cbc', // OPTIONAL AES in CBC mode, with a 256-bit key
  722. // from <http://tools.ietf.org/html/rfc4344#section-4>:
  723. 'aes128-ctr', // RECOMMENDED AES (Rijndael) in SDCTR mode, with 128-bit key
  724. 'aes192-ctr', // RECOMMENDED AES with 192-bit key
  725. 'aes256-ctr', // RECOMMENDED AES with 256-bit key
  726. '3des-ctr', // RECOMMENDED Three-key 3DES in SDCTR mode
  727. '3des-cbc', // REQUIRED three-key 3DES in CBC mode
  728. 'none' // OPTIONAL no encryption; NOT RECOMMENDED
  729. );
  730. static $mac_algorithms = array(
  731. 'hmac-sha1-96', // RECOMMENDED first 96 bits of HMAC-SHA1 (digest length = 12, key length = 20)
  732. 'hmac-sha1', // REQUIRED HMAC-SHA1 (digest length = key length = 20)
  733. 'hmac-md5-96', // OPTIONAL first 96 bits of HMAC-MD5 (digest length = 12, key length = 16)
  734. 'hmac-md5', // OPTIONAL HMAC-MD5 (digest length = key length = 16)
  735. 'none' // OPTIONAL no MAC; NOT RECOMMENDED
  736. );
  737. static $compression_algorithms = array(
  738. 'none' // REQUIRED no compression
  739. //'zlib' // OPTIONAL ZLIB (LZ77) compression
  740. );
  741. static $str_kex_algorithms, $str_server_host_key_algorithms,
  742. $encryption_algorithms_server_to_client, $mac_algorithms_server_to_client, $compression_algorithms_server_to_client,
  743. $encryption_algorithms_client_to_server, $mac_algorithms_client_to_server, $compression_algorithms_client_to_server;
  744. if (empty($str_kex_algorithms)) {
  745. $str_kex_algorithms = implode(',', $kex_algorithms);
  746. $str_server_host_key_algorithms = implode(',', $server_host_key_algorithms);
  747. $encryption_algorithms_server_to_client = $encryption_algorithms_client_to_server = implode(',', $encryption_algorithms);
  748. $mac_algorithms_server_to_client = $mac_algorithms_client_to_server = implode(',', $mac_algorithms);
  749. $compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms);
  750. }
  751. $client_cookie = '';
  752. for ($i = 0; $i < 16; $i++) {
  753. $client_cookie.= chr(crypt_random(0, 255));
  754. }
  755. $response = $kexinit_payload_server;
  756. $this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT)
  757. $server_cookie = $this->_string_shift($response, 16);
  758. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  759. $this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
  760. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  761. $this->server_host_key_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
  762. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  763. $this->encryption_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
  764. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  765. $this->encryption_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
  766. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  767. $this->mac_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
  768. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  769. $this->mac_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
  770. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  771. $this->compression_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
  772. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  773. $this->compression_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
  774. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  775. $this->languages_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
  776. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  777. $this->languages_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
  778. extract(unpack('Cfirst_kex_packet_follows', $this->_string_shift($response, 1)));
  779. $first_kex_packet_follows = $first_kex_packet_follows != 0;
  780. // the sending of SSH2_MSG_KEXINIT could go in one of two places. this is the second place.
  781. $kexinit_payload_client = pack('Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN',
  782. NET_SSH2_MSG_KEXINIT, $client_cookie, strlen($str_kex_algorithms), $str_kex_algorithms,
  783. strlen($str_server_host_key_algorithms), $str_server_host_key_algorithms, strlen($encryption_algorithms_client_to_server),
  784. $encryption_algorithms_client_to_server, strlen($encryption_algorithms_server_to_client), $encryption_algorithms_server_to_client,
  785. strlen($mac_algorithms_client_to_server), $mac_algorithms_client_to_server, strlen($mac_algorithms_server_to_client),
  786. $mac_algorithms_server_to_client, strlen($compression_algorithms_client_to_server), $compression_algorithms_client_to_server,
  787. strlen($compression_algorithms_server_to_client), $compression_algorithms_server_to_client, 0, '', 0, '',
  788. 0, 0
  789. );
  790. if (!$this->_send_binary_packet($kexinit_payload_client)) {
  791. return false;
  792. }
  793. // here ends the second place.
  794. // we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange
  795. for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_server_to_client); $i++);
  796. if ($i == count($encryption_algorithms)) {
  797. user_error('No compatible server to client encryption algorithms found', E_USER_NOTICE);
  798. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  799. }
  800. // we don't initialize any crypto-objects, yet - we do that, later. for now, we need the lengths to make the
  801. // diffie-hellman key exchange as fast as possible
  802. $decrypt = $encryption_algorithms[$i];
  803. switch ($decrypt) {
  804. case '3des-cbc':
  805. case '3des-ctr':
  806. $decryptKeyLength = 24; // eg. 192 / 8
  807. break;
  808. case 'aes256-cbc':
  809. case 'aes256-ctr':
  810. $decryptKeyLength = 32; // eg. 256 / 8
  811. break;
  812. case 'aes192-cbc':
  813. case 'aes192-ctr':
  814. $decryptKeyLength = 24; // eg. 192 / 8
  815. break;
  816. case 'aes128-cbc':
  817. case 'aes128-ctr':
  818. $decryptKeyLength = 16; // eg. 128 / 8
  819. break;
  820. case 'arcfour':
  821. case 'arcfour128':
  822. $decryptKeyLength = 16; // eg. 128 / 8
  823. break;
  824. case 'arcfour256':
  825. $decryptKeyLength = 32; // eg. 128 / 8
  826. break;
  827. case 'none';
  828. $decryptKeyLength = 0;
  829. }
  830. for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_client_to_server); $i++);
  831. if ($i == count($encryption_algorithms)) {
  832. user_error('No compatible client to server encryption algorithms found', E_USER_NOTICE);
  833. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  834. }
  835. $encrypt = $encryption_algorithms[$i];
  836. switch ($encrypt) {
  837. case '3des-cbc':
  838. case '3des-ctr':
  839. $encryptKeyLength = 24;
  840. break;
  841. case 'aes256-cbc':
  842. case 'aes256-ctr':
  843. $encryptKeyLength = 32;
  844. break;
  845. case 'aes192-cbc':
  846. case 'aes192-ctr':
  847. $encryptKeyLength = 24;
  848. break;
  849. case 'aes128-cbc':
  850. case 'aes128-ctr':
  851. $encryptKeyLength = 16;
  852. break;
  853. case 'arcfour':
  854. case 'arcfour128':
  855. $encryptKeyLength = 16;
  856. break;
  857. case 'arcfour256':
  858. $encryptKeyLength = 32;
  859. break;
  860. case 'none';
  861. $encryptKeyLength = 0;
  862. }
  863. $keyLength = $decryptKeyLength > $encryptKeyLength ? $decryptKeyLength : $encryptKeyLength;
  864. // through diffie-hellman key exchange a symmetric key is obtained
  865. for ($i = 0; $i < count($kex_algorithms) && !in_array($kex_algorithms[$i], $this->kex_algorithms); $i++);
  866. if ($i == count($kex_algorithms)) {
  867. user_error('No compatible key exchange algorithms found', E_USER_NOTICE);
  868. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  869. }
  870. switch ($kex_algorithms[$i]) {
  871. // see http://tools.ietf.org/html/rfc2409#section-6.2 and
  872. // http://tools.ietf.org/html/rfc2412, appendex E
  873. case 'diffie-hellman-group1-sha1':
  874. $p = pack('H256', 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
  875. '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
  876. '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
  877. 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF');
  878. $keyLength = $keyLength < 160 ? $keyLength : 160;
  879. $hash = 'sha1';
  880. break;
  881. // see http://tools.ietf.org/html/rfc3526#section-3
  882. case 'diffie-hellman-group14-sha1':
  883. $p = pack('H512', 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
  884. '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
  885. '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
  886. 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05' .
  887. '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB' .
  888. '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' .
  889. 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' .
  890. '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF');
  891. $keyLength = $keyLength < 160 ? $keyLength : 160;
  892. $hash = 'sha1';
  893. }
  894. $p = new Math_BigInteger($p, 256);
  895. //$q = $p->bitwise_rightShift(1);
  896. /* To increase the speed of the key exchange, both client and server may
  897. reduce the size of their private exponents. It should be at least
  898. twice as long as the key material that is generated from the shared
  899. secret. For more details, see the paper by van Oorschot and Wiener
  900. [VAN-OORSCHOT].
  901. -- http://tools.ietf.org/html/rfc4419#section-6.2 */
  902. $q = new Math_BigInteger(1);
  903. $q = $q->bitwise_leftShift(2 * $keyLength);
  904. $q = $q->subtract(new Math_BigInteger(1));
  905. $g = new Math_BigInteger(2);
  906. $x = new Math_BigInteger();
  907. $x->setRandomGenerator('crypt_random');
  908. $x = $x->random(new Math_BigInteger(1), $q);
  909. $e = $g->modPow($x, $p);
  910. $eBytes = $e->toBytes(true);
  911. $data = pack('CNa*', NET_SSH2_MSG_KEXDH_INIT, strlen($eBytes), $eBytes);
  912. if (!$this->_send_binary_packet($data)) {
  913. user_error('Connection closed by server', E_USER_NOTICE);
  914. return false;
  915. }
  916. $response = $this->_get_binary_packet();
  917. if ($response === false) {
  918. user_error('Connection closed by server', E_USER_NOTICE);
  919. return false;
  920. }
  921. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  922. if ($type != NET_SSH2_MSG_KEXDH_REPLY) {
  923. user_error('Expected SSH_MSG_KEXDH_REPLY', E_USER_NOTICE);
  924. return false;
  925. }
  926. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  927. $this->server_public_host_key = $server_public_host_key = $this->_string_shift($response, $temp['length']);
  928. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  929. $public_key_format = $this->_string_shift($server_public_host_key, $temp['length']);
  930. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  931. $fBytes = $this->_string_shift($response, $temp['length']);
  932. $f = new Math_BigInteger($fBytes, -256);
  933. $temp = unpack('Nlength', $this->_string_shift($response, 4));
  934. $this->signature = $this->_string_shift($response, $temp['length']);
  935. $temp = unpack('Nlength', $this->_string_shift($this->signature, 4));
  936. $this->signature_format = $this->_string_shift($this->signature, $temp['length']);
  937. $key = $f->modPow($x, $p);
  938. $keyBytes = $key->toBytes(true);
  939. $this->exchange_hash = pack('Na*Na*Na*Na*Na*Na*Na*Na*',
  940. strlen($this->identifier), $this->identifier, strlen($this->server_identifier), $this->server_identifier,
  941. strlen($kexinit_payload_client), $kexinit_payload_client, strlen($kexinit_payload_server),
  942. $kexinit_payload_server, strlen($this->server_public_host_key), $this->server_public_host_key, strlen($eBytes),
  943. $eBytes, strlen($fBytes), $fBytes, strlen($keyBytes), $keyBytes
  944. );
  945. $this->exchange_hash = pack('H*', $hash($this->exchange_hash));
  946. if ($this->session_id === false) {
  947. $this->session_id = $this->exchange_hash;
  948. }
  949. for ($i = 0; $i < count($server_host_key_algorithms) && !in_array($server_host_key_algorithms[$i], $this->server_host_key_algorithms); $i++);
  950. if ($i == count($server_host_key_algorithms)) {
  951. user_error('No compatible server host key algorithms found', E_USER_NOTICE);
  952. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  953. }
  954. if ($public_key_format != $server_host_key_algorithms[$i] || $this->signature_format != $server_host_key_algorithms[$i]) {
  955. user_error('Sever Host Key Algorithm Mismatch', E_USER_NOTICE);
  956. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  957. }
  958. $packet = pack('C',
  959. NET_SSH2_MSG_NEWKEYS
  960. );
  961. if (!$this->_send_binary_packet($packet)) {
  962. return false;
  963. }
  964. $response = $this->_get_binary_packet();
  965. if ($response === false) {
  966. user_error('Connection closed by server', E_USER_NOTICE);
  967. return false;
  968. }
  969. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  970. if ($type != NET_SSH2_MSG_NEWKEYS) {
  971. user_error('Expected SSH_MSG_NEWKEYS', E_USER_NOTICE);
  972. return false;
  973. }
  974. switch ($encrypt) {
  975. case '3des-cbc':
  976. $this->encrypt = new Crypt_TripleDES();
  977. // $this->encrypt_block_size = 64 / 8 == the default
  978. break;
  979. case '3des-ctr':
  980. $this->encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
  981. // $this->encrypt_block_size = 64 / 8 == the default
  982. break;
  983. case 'aes256-cbc':
  984. case 'aes192-cbc':
  985. case 'aes128-cbc':
  986. $this->encrypt = new Crypt_AES();
  987. $this->encrypt_block_size = 16; // eg. 128 / 8
  988. break;
  989. case 'aes256-ctr':
  990. case 'aes192-ctr':
  991. case 'aes128-ctr':
  992. $this->encrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
  993. $this->encrypt_block_size = 16; // eg. 128 / 8
  994. break;
  995. case 'arcfour':
  996. case 'arcfour128':
  997. case 'arcfour256':
  998. $this->encrypt = new Crypt_RC4();
  999. break;
  1000. case 'none';
  1001. //$this->encrypt = new Crypt_Null();
  1002. }
  1003. switch ($decrypt) {
  1004. case '3des-cbc':
  1005. $this->decrypt = new Crypt_TripleDES();
  1006. break;
  1007. case '3des-ctr':
  1008. $this->decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
  1009. break;
  1010. case 'aes256-cbc':
  1011. case 'aes192-cbc':
  1012. case 'aes128-cbc':
  1013. $this->decrypt = new Crypt_AES();
  1014. $this->decrypt_block_size = 16;
  1015. break;
  1016. case 'aes256-ctr':
  1017. case 'aes192-ctr':
  1018. case 'aes128-ctr':
  1019. $this->decrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
  1020. $this->decrypt_block_size = 16;
  1021. break;
  1022. case 'arcfour':
  1023. case 'arcfour128':
  1024. case 'arcfour256':
  1025. $this->decrypt = new Crypt_RC4();
  1026. break;
  1027. case 'none';
  1028. //$this->decrypt = new Crypt_Null();
  1029. }
  1030. $keyBytes = pack('Na*', strlen($keyBytes), $keyBytes);
  1031. if ($this->encrypt) {
  1032. $this->encrypt->enableContinuousBuffer();
  1033. $this->encrypt->disablePadding();
  1034. $iv = pack('H*', $hash($keyBytes . $this->exchange_hash . 'A' . $this->session_id));
  1035. while ($this->encrypt_block_size > strlen($iv)) {
  1036. $iv.= pack('H*', $hash($keyBytes . $this->exchange_hash . $iv));
  1037. }
  1038. $this->encrypt->setIV(substr($iv, 0, $this->encrypt_block_size));
  1039. $key = pack('H*', $hash($keyBytes . $this->exchange_hash . 'C' . $this->session_id));
  1040. while ($encryptKeyLength > strlen($key)) {
  1041. $key.= pack('H*', $hash($keyBytes . $this->exchange_hash . $key));
  1042. }
  1043. $this->encrypt->setKey(substr($key, 0, $encryptKeyLength));
  1044. }
  1045. if ($this->decrypt) {
  1046. $this->decrypt->enableContinuousBuffer();
  1047. $this->decrypt->disablePadding();
  1048. $iv = pack('H*', $hash($keyBytes . $this->exchange_hash . 'B' . $this->session_id));
  1049. while ($this->decrypt_block_size > strlen($iv)) {
  1050. $iv.= pack('H*', $hash($keyBytes . $this->exchange_hash . $iv));
  1051. }
  1052. $this->decrypt->setIV(substr($iv, 0, $this->decrypt_block_size));
  1053. $key = pack('H*', $hash($keyBytes . $this->exchange_hash . 'D' . $this->session_id));
  1054. while ($decryptKeyLength > strlen($key)) {
  1055. $key.= pack('H*', $hash($keyBytes . $this->exchange_hash . $key));
  1056. }
  1057. $this->decrypt->setKey(substr($key, 0, $decryptKeyLength));
  1058. }
  1059. /* The "arcfour128" algorithm is the RC4 cipher, as described in
  1060. [SCHNEIER], using a 128-bit key. The first 1536 bytes of keystream
  1061. generated by the cipher MUST be discarded, and the first byte of the
  1062. first encrypted packet MUST be encrypted using the 1537th byte of
  1063. keystream.
  1064. -- http://tools.ietf.org/html/rfc4345#section-4 */
  1065. if ($encrypt == 'arcfour128' || $encrypt == 'arcfour256') {
  1066. $this->encrypt->encrypt(str_repeat("\0", 1536));
  1067. }
  1068. if ($decrypt == 'arcfour128' || $decrypt == 'arcfour256') {
  1069. $this->decrypt->decrypt(str_repeat("\0", 1536));
  1070. }
  1071. for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_client_to_server); $i++);
  1072. if ($i == count($mac_algorithms)) {
  1073. user_error('No compatible client to server message authentication algorithms found', E_USER_NOTICE);
  1074. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  1075. }
  1076. $createKeyLength = 0; // ie. $mac_algorithms[$i] == 'none'
  1077. switch ($mac_algorithms[$i]) {
  1078. case 'hmac-sha1':
  1079. $this->hmac_create = new Crypt_Hash('sha1');
  1080. $createKeyLength = 20;
  1081. break;
  1082. case 'hmac-sha1-96':
  1083. $this->hmac_create = new Crypt_Hash('sha1-96');
  1084. $createKeyLength = 20;
  1085. break;
  1086. case 'hmac-md5':
  1087. $this->hmac_create = new Crypt_Hash('md5');
  1088. $createKeyLength = 16;
  1089. break;
  1090. case 'hmac-md5-96':
  1091. $this->hmac_create = new Crypt_Hash('md5-96');
  1092. $createKeyLength = 16;
  1093. }
  1094. for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_server_to_client); $i++);
  1095. if ($i == count($mac_algorithms)) {
  1096. user_error('No compatible server to client message authentication algorithms found', E_USER_NOTICE);
  1097. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  1098. }
  1099. $checkKeyLength = 0;
  1100. $this->hmac_size = 0;
  1101. switch ($mac_algorithms[$i]) {
  1102. case 'hmac-sha1':
  1103. $this->hmac_check = new Crypt_Hash('sha1');
  1104. $checkKeyLength = 20;
  1105. $this->hmac_size = 20;
  1106. break;
  1107. case 'hmac-sha1-96':
  1108. $this->hmac_check = new Crypt_Hash('sha1-96');
  1109. $checkKeyLength = 20;
  1110. $this->hmac_size = 12;
  1111. break;
  1112. case 'hmac-md5':
  1113. $this->hmac_check = new Crypt_Hash('md5');
  1114. $checkKeyLength = 16;
  1115. $this->hmac_size = 16;
  1116. break;
  1117. case 'hmac-md5-96':
  1118. $this->hmac_check = new Crypt_Hash('md5-96');
  1119. $checkKeyLength = 16;
  1120. $this->hmac_size = 12;
  1121. }
  1122. $key = pack('H*', $hash($keyBytes . $this->exchange_hash . 'E' . $this->session_id));
  1123. while ($createKeyLength > strlen($key)) {
  1124. $key.= pack('H*', $hash($keyBytes . $this->exchange_hash . $key));
  1125. }
  1126. $this->hmac_create->setKey(substr($key, 0, $createKeyLength));
  1127. $key = pack('H*', $hash($keyBytes . $this->exchange_hash . 'F' . $this->session_id));
  1128. while ($checkKeyLength > strlen($key)) {
  1129. $key.= pack('H*', $hash($keyBytes . $this->exchange_hash . $key));
  1130. }
  1131. $this->hmac_check->setKey(substr($key, 0, $checkKeyLength));
  1132. for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_server_to_client); $i++);
  1133. if ($i == count($compression_algorithms)) {
  1134. user_error('No compatible server to client compression algorithms found', E_USER_NOTICE);
  1135. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  1136. }
  1137. $this->decompress = $compression_algorithms[$i] == 'zlib';
  1138. for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_client_to_server); $i++);
  1139. if ($i == count($compression_algorithms)) {
  1140. user_error('No compatible client to server compression algorithms found', E_USER_NOTICE);
  1141. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  1142. }
  1143. $this->compress = $compression_algorithms[$i] == 'zlib';
  1144. return true;
  1145. }
  1146. /**
  1147. * Login
  1148. *
  1149. * The $password parameter can be a plaintext password or a Crypt_RSA object.
  1150. *
  1151. * @param String $username
  1152. * @param optional String $password
  1153. * @return Boolean
  1154. * @access public
  1155. * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
  1156. * by sending dummy SSH_MSG_IGNORE messages.
  1157. */
  1158. function login($username, $password = '')
  1159. {
  1160. if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) {
  1161. return false;
  1162. }
  1163. $packet = pack('CNa*',
  1164. NET_SSH2_MSG_SERVICE_REQUEST, strlen('ssh-userauth'), 'ssh-userauth'
  1165. );
  1166. if (!$this->_send_binary_packet($packet)) {
  1167. return false;
  1168. }
  1169. $response = $this->_get_binary_packet();
  1170. if ($response === false) {
  1171. user_error('Connection closed by server', E_USER_NOTICE);
  1172. return false;
  1173. }
  1174. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  1175. if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) {
  1176. user_error('Expected SSH_MSG_SERVICE_ACCEPT', E_USER_NOTICE);
  1177. return false;
  1178. }
  1179. // although PHP5's get_class() preserves the case, PHP4's does not
  1180. if (is_object($password) && strtolower(get_class($password)) == 'crypt_rsa') {
  1181. return $this->_privatekey_login($username, $password);
  1182. }
  1183. $packet = pack('CNa*Na*Na*CNa*',
  1184. NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
  1185. strlen('password'), 'password', 0, strlen($password), $password
  1186. );
  1187. if (!$this->_send_binary_packet($packet)) {
  1188. return false;
  1189. }
  1190. // remove the username and password from the last logged packet
  1191. if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
  1192. $packet = pack('CNa*Na*Na*CNa*',
  1193. NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection',
  1194. strlen('password'), 'password', 0, strlen('password'), 'password'
  1195. );
  1196. $this->message_log[count($this->message_log) - 1] = $packet;
  1197. }
  1198. $response = $this->_get_binary_packet();
  1199. if ($response === false) {
  1200. user_error('Connection closed by server', E_USER_NOTICE);
  1201. return false;
  1202. }
  1203. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  1204. switch ($type) {
  1205. case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: // in theory, the password can be changed
  1206. if (defined('NET_SSH2_LOGGING')) {
  1207. $this->message_number_log[count($this->message_number_log) - 1] = 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ';
  1208. }
  1209. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1210. $this->errors[] = 'SSH_MSG_USERAUTH_PASSWD_CHANGEREQ: ' . utf8_decode($this->_string_shift($response, $length));
  1211. return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1212. case NET_SSH2_MSG_USERAUTH_FAILURE:
  1213. // can we use keyboard-interactive authentication? if not then either the login is bad or the server employees
  1214. // multi-factor authentication
  1215. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1216. $auth_methods = explode(',', $this->_string_shift($response, $length));
  1217. if (in_array('keyboard-interactive', $auth_methods)) {
  1218. if ($this->_keyboard_interactive_login($username, $password)) {
  1219. $this->bitmap |= NET_SSH2_MASK_LOGIN;
  1220. return true;
  1221. }
  1222. return false;
  1223. }
  1224. return false;
  1225. case NET_SSH2_MSG_USERAUTH_SUCCESS:
  1226. $this->bitmap |= NET_SSH2_MASK_LOGIN;
  1227. return true;
  1228. }
  1229. return false;
  1230. }
  1231. /**
  1232. * Login via keyboard-interactive authentication
  1233. *
  1234. * See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator.
  1235. *
  1236. * @param String $username
  1237. * @param String $password
  1238. * @return Boolean
  1239. * @access private
  1240. */
  1241. function _keyboard_interactive_login($username, $password)
  1242. {
  1243. $packet = pack('CNa*Na*Na*Na*Na*',
  1244. NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
  1245. strlen('keyboard-interactive'), 'keyboard-interactive', 0, '', 0, ''
  1246. );
  1247. if (!$this->_send_binary_packet($packet)) {
  1248. return false;
  1249. }
  1250. return $this->_keyboard_interactive_process($password);
  1251. }
  1252. /**
  1253. * Handle the keyboard-interactive requests / responses.
  1254. *
  1255. * @param String $responses...
  1256. * @return Boolean
  1257. * @access private
  1258. */
  1259. function _keyboard_interactive_process()
  1260. {
  1261. $responses = func_get_args();
  1262. $response = $this->_get_binary_packet();
  1263. if ($response === false) {
  1264. user_error('Connection closed by server', E_USER_NOTICE);
  1265. return false;
  1266. }
  1267. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  1268. switch ($type) {
  1269. case NET_SSH2_MSG_USERAUTH_INFO_REQUEST:
  1270. // see http://tools.ietf.org/html/rfc4256#section-3.2
  1271. if (defined('NET_SSH2_LOGGING')) {
  1272. $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
  1273. 'UNKNOWN',
  1274. 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
  1275. $this->message_number_log[count($this->message_number_log) - 1]
  1276. );
  1277. }
  1278. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1279. $this->_string_shift($response, $length); // name; may be empty
  1280. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1281. $this->_string_shift($response, $length); // instruction; may be empty
  1282. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1283. $this->_string_shift($response, $length); // language tag; may be empty
  1284. extract(unpack('Nnum_prompts', $this->_string_shift($response, 4)));
  1285. /*
  1286. for ($i = 0; $i < $num_prompts; $i++) {
  1287. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1288. // prompt - ie. "Password: "; must not be empty
  1289. $this->_string_shift($response, $length);
  1290. $echo = $this->_string_shift($response) != chr(0);
  1291. }
  1292. */
  1293. /*
  1294. After obtaining the requested information from the user, the client
  1295. MUST respond with an SSH_MSG_USERAUTH_INFO_RESPONSE message.
  1296. */
  1297. // see http://tools.ietf.org/html/rfc4256#section-3.4
  1298. $packet = $logged = pack('CN', NET_SSH2_MSG_USERAUTH_INFO_RESPONSE, count($responses));
  1299. for ($i = 0; $i < count($responses); $i++) {
  1300. $packet.= pack('Na*', strlen($responses[$i]), $responses[$i]);
  1301. $logged.= pack('Na*', strlen('dummy-answer'), 'dummy-answer');
  1302. }
  1303. if (!$this->_send_binary_packet($packet)) {
  1304. return false;
  1305. }
  1306. if (defined('NET_SSH2_LOGGING')) {
  1307. $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
  1308. 'UNKNOWN',
  1309. 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE',
  1310. $this->message_number_log[count($this->message_number_log) - 1]
  1311. );
  1312. $this->message_log[count($this->message_log) - 1] = $logged;
  1313. }
  1314. /*
  1315. After receiving the response, the server MUST send either an
  1316. SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, or another
  1317. SSH_MSG_USERAUTH_INFO_REQUEST message.
  1318. */
  1319. // maybe phpseclib should force close the connection after x request / responses? unless something like that is done
  1320. // there could be an infinite loop of request / responses.
  1321. return $this->_keyboard_interactive_process();
  1322. case NET_SSH2_MSG_USERAUTH_SUCCESS:
  1323. return true;
  1324. case NET_SSH2_MSG_USERAUTH_FAILURE:
  1325. return false;
  1326. }
  1327. return false;
  1328. }
  1329. /**
  1330. * Login with an RSA private key
  1331. *
  1332. * @param String $username
  1333. * @param Crypt_RSA $password
  1334. * @return Boolean
  1335. * @access private
  1336. * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
  1337. * by sending dummy SSH_MSG_IGNORE messages.
  1338. */
  1339. function _privatekey_login($username, $privatekey)
  1340. {
  1341. // see http://tools.ietf.org/html/rfc4253#page-15
  1342. $publickey = $privatekey->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
  1343. if ($publickey === false) {
  1344. return false;
  1345. }
  1346. $publickey = array(
  1347. 'e' => $publickey['e']->toBytes(true),
  1348. 'n' => $publickey['n']->toBytes(true)
  1349. );
  1350. $publickey = pack('Na*Na*Na*',
  1351. strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey['e']), $publickey['e'], strlen($publickey['n']), $publickey['n']
  1352. );
  1353. $part1 = pack('CNa*Na*Na*',
  1354. NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
  1355. strlen('publickey'), 'publickey'
  1356. );
  1357. $part2 = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey), $publickey);
  1358. $packet = $part1 . chr(0) . $part2;
  1359. if (!$this->_send_binary_packet($packet)) {
  1360. return false;
  1361. }
  1362. $response = $this->_get_binary_packet();
  1363. if ($response === false) {
  1364. user_error('Connection closed by server', E_USER_NOTICE);
  1365. return false;
  1366. }
  1367. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  1368. switch ($type) {
  1369. case NET_SSH2_MSG_USERAUTH_FAILURE:
  1370. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1371. $this->errors[] = 'SSH_MSG_USERAUTH_FAILURE: ' . $this->_string_shift($response, $length);
  1372. return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1373. case NET_SSH2_MSG_USERAUTH_PK_OK:
  1374. // we'll just take it on faith that the public key blob and the public key algorithm name are as
  1375. // they should be
  1376. if (defined('NET_SSH2_LOGGING')) {
  1377. $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
  1378. 'UNKNOWN',
  1379. 'NET_SSH2_MSG_USERAUTH_PK_OK',
  1380. $this->message_number_log[count($this->message_number_log) - 1]
  1381. );
  1382. }
  1383. }
  1384. $packet = $part1 . chr(1) . $part2;
  1385. $privatekey->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
  1386. $signature = $privatekey->sign(pack('Na*a*', strlen($this->session_id), $this->session_id, $packet));
  1387. $signature = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($signature), $signature);
  1388. $packet.= pack('Na*', strlen($signature), $signature);
  1389. if (!$this->_send_binary_packet($packet)) {
  1390. return false;
  1391. }
  1392. $response = $this->_get_binary_packet();
  1393. if ($response === false) {
  1394. user_error('Connection closed by server', E_USER_NOTICE);
  1395. return false;
  1396. }
  1397. extract(unpack('Ctype', $this->_string_shift($response, 1)));
  1398. switch ($type) {
  1399. case NET_SSH2_MSG_USERAUTH_FAILURE:
  1400. // either the login is bad or the server employees multi-factor authentication
  1401. return false;
  1402. case NET_SSH2_MSG_USERAUTH_SUCCESS:
  1403. $this->bitmap |= NET_SSH2_MASK_LOGIN;
  1404. return true;
  1405. }
  1406. return false;
  1407. }
  1408. /**
  1409. * Execute Command
  1410. *
  1411. * If $block is set to false then Net_SSH2::_get_channel_packet(NET_SSH2_CHANNEL_EXEC) will need to be called manually.
  1412. * In all likelihood, this is not a feature you want to be taking advantage of.
  1413. *
  1414. * @param String $command
  1415. * @param optional Boolean $block
  1416. * @return String
  1417. * @access public
  1418. */
  1419. function exec($command, $block = true)
  1420. {
  1421. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1422. return false;
  1423. }
  1424. // RFC4254 defines the (client) window size as "bytes the other party can send before it must wait for the window to
  1425. // be adjusted". 0x7FFFFFFF is, at 4GB, the max size. technically, it should probably be decremented, but,
  1426. // honestly, if you're transfering more than 4GB, you probably shouldn't be using phpseclib, anyway.
  1427. // see http://tools.ietf.org/html/rfc4254#section-5.2 for more info
  1428. $this->window_size_client_to_server[NET_SSH2_CHANNEL_EXEC] = 0x7FFFFFFF;
  1429. // 0x8000 is the maximum max packet size, per http://tools.ietf.org/html/rfc4253#section-6.1, although since PuTTy
  1430. // uses 0x4000, that's what will be used here, as well.
  1431. $packet_size = 0x4000;
  1432. $packet = pack('CNa*N3',
  1433. NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_EXEC, $this->window_size_client_to_server[NET_SSH2_CHANNEL_EXEC], $packet_size);
  1434. if (!$this->_send_binary_packet($packet)) {
  1435. return false;
  1436. }
  1437. $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN;
  1438. $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
  1439. if ($response === false) {
  1440. return false;
  1441. }
  1442. // sending a pty-req SSH_MSG_CHANNEL_REQUEST message is unnecessary and, in fact, in most cases, slows things
  1443. // down. the one place where it might be desirable is if you're doing something like Net_SSH2::exec('ping localhost &').
  1444. // with a pty-req SSH_MSG_CHANNEL_REQUEST, exec() will return immediately and the ping process will then
  1445. // then immediately terminate. without such a request exec() will loop indefinitely. the ping process won't end but
  1446. // neither will your script.
  1447. // although, in theory, the size of SSH_MSG_CHANNEL_REQUEST could exceed the maximum packet size established by
  1448. // SSH_MSG_CHANNEL_OPEN_CONFIRMATION, RFC4254#section-5.1 states that the "maximum packet size" refers to the
  1449. // "maximum size of an individual data packet". ie. SSH_MSG_CHANNEL_DATA. RFC4254#section-5.2 corroborates.
  1450. $packet = pack('CNNa*CNa*',
  1451. NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command);
  1452. if (!$this->_send_binary_packet($packet)) {
  1453. return false;
  1454. }
  1455. $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST;
  1456. $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
  1457. if ($response === false) {
  1458. return false;
  1459. }
  1460. $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA;
  1461. if (!$block) {
  1462. return true;
  1463. }
  1464. $output = '';
  1465. while (true) {
  1466. $temp = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
  1467. switch (true) {
  1468. case $temp === true:
  1469. return $output;
  1470. case $temp === false:
  1471. return false;
  1472. default:
  1473. $output.= $temp;
  1474. }
  1475. }
  1476. }
  1477. /**
  1478. * Creates an interactive shell
  1479. *
  1480. * @see Net_SSH2::read()
  1481. * @see Net_SSH2::write()
  1482. * @return Boolean
  1483. * @access private
  1484. */
  1485. function _initShell()
  1486. {
  1487. $this->window_size_client_to_server[NET_SSH2_CHANNEL_SHELL] = 0x7FFFFFFF;
  1488. $packet_size = 0x4000;
  1489. $packet = pack('CNa*N3',
  1490. NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SHELL, $this->window_size_client_to_server[NET_SSH2_CHANNEL_SHELL], $packet_size);
  1491. if (!$this->_send_binary_packet($packet)) {
  1492. return false;
  1493. }
  1494. $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN;
  1495. $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
  1496. if ($response === false) {
  1497. return false;
  1498. }
  1499. $terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
  1500. $packet = pack('CNNa*CNa*N5a*',
  1501. NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100',
  1502. 80, 24, 0, 0, strlen($terminal_modes), $terminal_modes);
  1503. if (!$this->_send_binary_packet($packet)) {
  1504. return false;
  1505. }
  1506. $response = $this->_get_binary_packet();
  1507. if ($response === false) {
  1508. user_error('Connection closed by server', E_USER_NOTICE);
  1509. return false;
  1510. }
  1511. list(, $type) = unpack('C', $this->_string_shift($response, 1));
  1512. switch ($type) {
  1513. case NET_SSH2_MSG_CHANNEL_SUCCESS:
  1514. break;
  1515. case NET_SSH2_MSG_CHANNEL_FAILURE:
  1516. default:
  1517. user_error('Unable to request pseudo-terminal', E_USER_NOTICE);
  1518. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1519. }
  1520. $packet = pack('CNNa*C',
  1521. NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('shell'), 'shell', 1);
  1522. if (!$this->_send_binary_packet($packet)) {
  1523. return false;
  1524. }
  1525. $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST;
  1526. $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
  1527. if ($response === false) {
  1528. return false;
  1529. }
  1530. $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA;
  1531. $this->bitmap |= NET_SSH2_MASK_SHELL;
  1532. return true;
  1533. }
  1534. /**
  1535. * Returns the output of an interactive shell
  1536. *
  1537. * Returns when there's a match for $expect, which can take the form of a string literal or,
  1538. * if $mode == NET_SSH2_READ_REGEX, a regular expression.
  1539. *
  1540. * @see Net_SSH2::read()
  1541. * @param String $expect
  1542. * @param Integer $mode
  1543. * @return String
  1544. * @access public
  1545. */
  1546. function read($expect, $mode = NET_SSH2_READ_SIMPLE)
  1547. {
  1548. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1549. user_error('Operation disallowed prior to login()', E_USER_NOTICE);
  1550. return false;
  1551. }
  1552. if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
  1553. user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
  1554. return false;
  1555. }
  1556. $match = $expect;
  1557. while (true) {
  1558. if ($mode == NET_SSH2_READ_REGEX) {
  1559. preg_match($expect, $this->interactiveBuffer, $matches);
  1560. $match = $matches[0];
  1561. }
  1562. $pos = strpos($this->interactiveBuffer, $match);
  1563. if ($pos !== false) {
  1564. return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match));
  1565. }
  1566. $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
  1567. $this->interactiveBuffer.= $response;
  1568. }
  1569. }
  1570. /**
  1571. * Inputs a command into an interactive shell.
  1572. *
  1573. * @see Net_SSH1::interactiveWrite()
  1574. * @param String $cmd
  1575. * @return Boolean
  1576. * @access public
  1577. */
  1578. function write($cmd)
  1579. {
  1580. if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1581. user_error('Operation disallowed prior to login()', E_USER_NOTICE);
  1582. return false;
  1583. }
  1584. if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
  1585. user_error('Unable to initiate an interactive shell session', E_USER_NOTICE);
  1586. return false;
  1587. }
  1588. return $this->_send_channel_packet(NET_SSH2_CHANNEL_SHELL, $cmd);
  1589. }
  1590. /**
  1591. * Disconnect
  1592. *
  1593. * @access public
  1594. */
  1595. function disconnect()
  1596. {
  1597. $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1598. }
  1599. /**
  1600. * Destructor.
  1601. *
  1602. * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
  1603. * disconnect().
  1604. *
  1605. * @access public
  1606. */
  1607. function __destruct()
  1608. {
  1609. $this->disconnect();
  1610. }
  1611. /**
  1612. * Gets Binary Packets
  1613. *
  1614. * See '6. Binary Packet Protocol' of rfc4253 for more info.
  1615. *
  1616. * @see Net_SSH2::_send_binary_packet()
  1617. * @return String
  1618. * @access private
  1619. */
  1620. function _get_binary_packet()
  1621. {
  1622. if (feof($this->fsock)) {
  1623. user_error('Connection closed prematurely', E_USER_NOTICE);
  1624. return false;
  1625. }
  1626. $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
  1627. $raw = fread($this->fsock, $this->decrypt_block_size);
  1628. $stop = strtok(microtime(), ' ') + strtok('');
  1629. if (empty($raw)) {
  1630. return '';
  1631. }
  1632. if ($this->decrypt !== false) {
  1633. $raw = $this->decrypt->decrypt($raw);
  1634. }
  1635. extract(unpack('Npacket_length/Cpadding_length', $this->_string_shift($raw, 5)));
  1636. $remaining_length = $packet_length + 4 - $this->decrypt_block_size;
  1637. $buffer = '';
  1638. while ($remaining_length > 0) {
  1639. $temp = fread($this->fsock, $remaining_length);
  1640. $buffer.= $temp;
  1641. $remaining_length-= strlen($temp);
  1642. }
  1643. if (!empty($buffer)) {
  1644. $raw.= $this->decrypt !== false ? $this->decrypt->decrypt($buffer) : $buffer;
  1645. $buffer = $temp = '';
  1646. }
  1647. $payload = $this->_string_shift($raw, $packet_length - $padding_length - 1);
  1648. $padding = $this->_string_shift($raw, $padding_length); // should leave $raw empty
  1649. if ($this->hmac_check !== false) {
  1650. $hmac = fread($this->fsock, $this->hmac_size);
  1651. if ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) {
  1652. user_error('Invalid HMAC', E_USER_NOTICE);
  1653. return false;
  1654. }
  1655. }
  1656. //if ($this->decompress) {
  1657. // $payload = gzinflate(substr($payload, 2));
  1658. //}
  1659. $this->get_seq_no++;
  1660. if (defined('NET_SSH2_LOGGING')) {
  1661. $temp = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')';
  1662. $this->message_number_log[] = '<- ' . $temp .
  1663. ' (' . round($stop - $start, 4) . 's)';
  1664. if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
  1665. $this->message_log[] = substr($payload, 1);
  1666. }
  1667. }
  1668. return $this->_filter($payload);
  1669. }
  1670. /**
  1671. * Filter Binary Packets
  1672. *
  1673. * Because some binary packets need to be ignored...
  1674. *
  1675. * @see Net_SSH2::_get_binary_packet()
  1676. * @return String
  1677. * @access private
  1678. */
  1679. function _filter($payload)
  1680. {
  1681. switch (ord($payload[0])) {
  1682. case NET_SSH2_MSG_DISCONNECT:
  1683. $this->_string_shift($payload, 1);
  1684. extract(unpack('Nreason_code/Nlength', $this->_string_shift($payload, 8)));
  1685. $this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n" . utf8_decode($this->_string_shift($payload, $length));
  1686. $this->bitmask = 0;
  1687. return false;
  1688. case NET_SSH2_MSG_IGNORE:
  1689. $payload = $this->_get_binary_packet();
  1690. break;
  1691. case NET_SSH2_MSG_DEBUG:
  1692. $this->_string_shift($payload, 2);
  1693. extract(unpack('Nlength', $this->_string_shift($payload, 4)));
  1694. $this->errors[] = 'SSH_MSG_DEBUG: ' . utf8_decode($this->_string_shift($payload, $length));
  1695. $payload = $this->_get_binary_packet();
  1696. break;
  1697. case NET_SSH2_MSG_UNIMPLEMENTED:
  1698. return false;
  1699. case NET_SSH2_MSG_KEXINIT:
  1700. if ($this->session_id !== false) {
  1701. if (!$this->_key_exchange($payload)) {
  1702. $this->bitmask = 0;
  1703. return false;
  1704. }
  1705. $payload = $this->_get_binary_packet();
  1706. }
  1707. }
  1708. // see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in
  1709. if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
  1710. $this->_string_shift($payload, 1);
  1711. extract(unpack('Nlength', $this->_string_shift($payload, 4)));
  1712. $this->errors[] = 'SSH_MSG_USERAUTH_BANNER: ' . utf8_decode($this->_string_shift($payload, $length));
  1713. $payload = $this->_get_binary_packet();
  1714. }
  1715. // only called when we've already logged in
  1716. if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && ($this->bitmap & NET_SSH2_MASK_LOGIN)) {
  1717. switch (ord($payload[0])) {
  1718. case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4
  1719. $this->_string_shift($payload, 1);
  1720. extract(unpack('Nlength', $this->_string_shift($payload)));
  1721. $this->errors[] = 'SSH_MSG_GLOBAL_REQUEST: ' . utf8_decode($this->_string_shift($payload, $length));
  1722. if (!$this->_send_binary_packet(pack('C', NET_SSH2_MSG_REQUEST_FAILURE))) {
  1723. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1724. }
  1725. $payload = $this->_get_binary_packet();
  1726. break;
  1727. case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1
  1728. $this->_string_shift($payload, 1);
  1729. extract(unpack('N', $this->_string_shift($payload, 4)));
  1730. $this->errors[] = 'SSH_MSG_CHANNEL_OPEN: ' . utf8_decode($this->_string_shift($payload, $length));
  1731. $this->_string_shift($payload, 4); // skip over client channel
  1732. extract(unpack('Nserver_channel', $this->_string_shift($payload, 4)));
  1733. $packet = pack('CN3a*Na*',
  1734. NET_SSH2_MSG_REQUEST_FAILURE, $server_channel, NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED, 0, '', 0, '');
  1735. if (!$this->_send_binary_packet($packet)) {
  1736. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1737. }
  1738. $payload = $this->_get_binary_packet();
  1739. break;
  1740. case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST:
  1741. $payload = $this->_get_binary_packet();
  1742. }
  1743. }
  1744. return $payload;
  1745. }
  1746. /**
  1747. * Gets channel data
  1748. *
  1749. * Returns the data as a string if it's available and false if not.
  1750. *
  1751. * @param $client_channel
  1752. * @return Mixed
  1753. * @access private
  1754. */
  1755. function _get_channel_packet($client_channel, $skip_extended = false)
  1756. {
  1757. if (!empty($this->channel_buffers[$client_channel])) {
  1758. return array_shift($this->channel_buffers[$client_channel]);
  1759. }
  1760. while (true) {
  1761. $response = $this->_get_binary_packet();
  1762. if ($response === false) {
  1763. user_error('Connection closed by server', E_USER_NOTICE);
  1764. return false;
  1765. }
  1766. if (empty($response)) {
  1767. return '';
  1768. }
  1769. extract(unpack('Ctype/Nchannel', $this->_string_shift($response, 5)));
  1770. switch ($this->channel_status[$channel]) {
  1771. case NET_SSH2_MSG_CHANNEL_OPEN:
  1772. switch ($type) {
  1773. case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
  1774. extract(unpack('Nserver_channel', $this->_string_shift($response, 4)));
  1775. $this->server_channels[$client_channel] = $server_channel;
  1776. $this->_string_shift($response, 4); // skip over (server) window size
  1777. $temp = unpack('Npacket_size_client_to_server', $this->_string_shift($response, 4));
  1778. $this->packet_size_client_to_server[$client_channel] = $temp['packet_size_client_to_server'];
  1779. return true;
  1780. //case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE:
  1781. default:
  1782. user_error('Unable to open channel', E_USER_NOTICE);
  1783. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1784. }
  1785. break;
  1786. case NET_SSH2_MSG_CHANNEL_REQUEST:
  1787. switch ($type) {
  1788. case NET_SSH2_MSG_CHANNEL_SUCCESS:
  1789. return true;
  1790. //case NET_SSH2_MSG_CHANNEL_FAILURE:
  1791. default:
  1792. user_error('Unable to request pseudo-terminal', E_USER_NOTICE);
  1793. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1794. }
  1795. }
  1796. switch ($type) {
  1797. case NET_SSH2_MSG_CHANNEL_DATA:
  1798. /*
  1799. if ($client_channel == NET_SSH2_CHANNEL_EXEC) {
  1800. // SCP requires null packets, such as this, be sent. further, in the case of the ssh.com SSH server
  1801. // this actually seems to make things twice as fast. more to the point, the message right after
  1802. // SSH_MSG_CHANNEL_DATA (usually SSH_MSG_IGNORE) won't block for as long as it would have otherwise.
  1803. // in OpenSSH it slows things down but only by a couple thousandths of a second.
  1804. $this->_send_channel_packet($client_channel, chr(0));
  1805. }
  1806. */
  1807. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1808. $data = $this->_string_shift($response, $length);
  1809. if ($client_channel == $channel) {
  1810. return $data;
  1811. }
  1812. if (!isset($this->channel_buffers[$client_channel])) {
  1813. $this->channel_buffers[$client_channel] = array();
  1814. }
  1815. $this->channel_buffers[$client_channel][] = $data;
  1816. break;
  1817. case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA:
  1818. if ($skip_extended) {
  1819. break;
  1820. }
  1821. /*
  1822. if ($client_channel == NET_SSH2_CHANNEL_EXEC) {
  1823. $this->_send_channel_packet($client_channel, chr(0));
  1824. }
  1825. */
  1826. // currently, there's only one possible value for $data_type_code: NET_SSH2_EXTENDED_DATA_STDERR
  1827. extract(unpack('Ndata_type_code/Nlength', $this->_string_shift($response, 8)));
  1828. $data = $this->_string_shift($response, $length);
  1829. if ($client_channel == $channel) {
  1830. return $data;
  1831. }
  1832. if (!isset($this->channel_buffers[$client_channel])) {
  1833. $this->channel_buffers[$client_channel] = array();
  1834. }
  1835. $this->channel_buffers[$client_channel][] = $data;
  1836. break;
  1837. case NET_SSH2_MSG_CHANNEL_REQUEST:
  1838. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1839. $value = $this->_string_shift($response, $length);
  1840. switch ($value) {
  1841. case 'exit-signal':
  1842. $this->_string_shift($response, 1);
  1843. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1844. $this->errors[] = 'SSH_MSG_CHANNEL_REQUEST (exit-signal): ' . $this->_string_shift($response, $length);
  1845. $this->_string_shift($response, 1);
  1846. extract(unpack('Nlength', $this->_string_shift($response, 4)));
  1847. if ($length) {
  1848. $this->errors[count($this->errors)].= "\r\n" . $this->_string_shift($response, $length);
  1849. }
  1850. //case 'exit-status':
  1851. default:
  1852. // "Some systems may not implement signals, in which case they SHOULD ignore this message."
  1853. // -- http://tools.ietf.org/html/rfc4254#section-6.9
  1854. break;
  1855. }
  1856. break;
  1857. case NET_SSH2_MSG_CHANNEL_CLOSE:
  1858. $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
  1859. return true;
  1860. case NET_SSH2_MSG_CHANNEL_EOF:
  1861. break;
  1862. default:
  1863. user_error('Error reading channel data', E_USER_NOTICE);
  1864. return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
  1865. }
  1866. }
  1867. }
  1868. /**
  1869. * Sends Binary Packets
  1870. *
  1871. * See '6. Binary Packet Protocol' of rfc4253 for more info.
  1872. *
  1873. * @param String $data
  1874. * @see Net_SSH2::_get_binary_packet()
  1875. * @return Boolean
  1876. * @access private
  1877. */
  1878. function _send_binary_packet($data)
  1879. {
  1880. if (feof($this->fsock)) {
  1881. user_error('Connection closed prematurely', E_USER_NOTICE);
  1882. return false;
  1883. }
  1884. //if ($this->compress) {
  1885. // // the -4 removes the checksum:
  1886. // // http://php.net/function.gzcompress#57710
  1887. // $data = substr(gzcompress($data), 0, -4);
  1888. //}
  1889. // 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9
  1890. $packet_length = strlen($data) + 9;
  1891. // round up to the nearest $this->encrypt_block_size
  1892. $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size;
  1893. // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length
  1894. $padding_length = $packet_length - strlen($data) - 5;
  1895. $padding = '';
  1896. for ($i = 0; $i < $padding_length; $i++) {
  1897. $padding.= chr(crypt_random(0, 255));
  1898. }
  1899. // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself
  1900. $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);
  1901. $hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : '';
  1902. $this->send_seq_no++;
  1903. if ($this->encrypt !== false) {
  1904. $packet = $this->encrypt->encrypt($packet);
  1905. }
  1906. $packet.= $hmac;
  1907. $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
  1908. $result = strlen($packet) == fputs($this->fsock, $packet);
  1909. $stop = strtok(microtime(), ' ') + strtok('');
  1910. if (defined('NET_SSH2_LOGGING')) {
  1911. $temp = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')';
  1912. $this->message_number_log[] = '-> ' . $temp .
  1913. ' (' . round($stop - $start, 4) . 's)';
  1914. if (NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
  1915. $this->message_log[] = substr($data, 1);
  1916. }
  1917. }
  1918. return $result;
  1919. }
  1920. /**
  1921. * Sends channel data
  1922. *
  1923. * Spans multiple SSH_MSG_CHANNEL_DATAs if appropriate
  1924. *
  1925. * @param Integer $client_channel
  1926. * @param String $data
  1927. * @return Boolean
  1928. * @access private
  1929. */
  1930. function _send_channel_packet($client_channel, $data)
  1931. {
  1932. while (strlen($data) > $this->packet_size_client_to_server[$client_channel]) {
  1933. // resize the window, if appropriate
  1934. $this->window_size_client_to_server[$client_channel]-= $this->packet_size_client_to_server[$client_channel];
  1935. if ($this->window_size_client_to_server[$client_channel] < 0) {
  1936. $packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$client_channel], $this->window_size);
  1937. if (!$this->_send_binary_packet($packet)) {
  1938. return false;
  1939. }
  1940. $this->window_size_client_to_server[$client_channel]+= $this->window_size;
  1941. }
  1942. $packet = pack('CN2a*',
  1943. NET_SSH2_MSG_CHANNEL_DATA,
  1944. $this->server_channels[$client_channel],
  1945. $this->packet_size_client_to_server[$client_channel],
  1946. $this->_string_shift($data, $this->packet_size_client_to_server[$client_channel])
  1947. );
  1948. if (!$this->_send_binary_packet($packet)) {
  1949. return false;
  1950. }
  1951. }
  1952. // resize the window, if appropriate
  1953. $this->window_size_client_to_server[$client_channel]-= strlen($data);
  1954. if ($this->window_size_client_to_server[$client_channel] < 0) {
  1955. $packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$client_channel], $this->window_size);
  1956. if (!$this->_send_binary_packet($packet)) {
  1957. return false;
  1958. }
  1959. $this->window_size_client_to_server[$client_channel]+= $this->window_size;
  1960. }
  1961. return $this->_send_binary_packet(pack('CN2a*',
  1962. NET_SSH2_MSG_CHANNEL_DATA,
  1963. $this->server_channels[$client_channel],
  1964. strlen($data),
  1965. $data));
  1966. }
  1967. /**
  1968. * Closes and flushes a channel
  1969. *
  1970. * Net_SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server
  1971. * and for SFTP channels are presumably closed when the client disconnects. This functions is intended
  1972. * for SCP more than anything.
  1973. *
  1974. * @param Integer $client_channel
  1975. * @return Boolean
  1976. * @access private
  1977. */
  1978. function _close_channel($client_channel)
  1979. {
  1980. // see http://tools.ietf.org/html/rfc4254#section-5.3
  1981. $packet = pack('CN',
  1982. NET_SSH2_MSG_CHANNEL_EOF,
  1983. $this->server_channels[$client_channel]);
  1984. if (!$this->_send_binary_packet($packet)) {
  1985. return false;
  1986. }
  1987. while ($this->_get_channel_packet($client_channel) !== true);
  1988. }
  1989. /**
  1990. * Disconnect
  1991. *
  1992. * @param Integer $reason
  1993. * @return Boolean
  1994. * @access private
  1995. */
  1996. function _disconnect($reason)
  1997. {
  1998. if ($this->bitmap) {
  1999. $data = pack('CNNa*Na*', NET_SSH2_MSG_DISCONNECT, $reason, 0, '', 0, '');
  2000. $this->_send_binary_packet($data);
  2001. $this->bitmap = 0;
  2002. fclose($this->fsock);
  2003. return false;
  2004. }
  2005. }
  2006. /**
  2007. * String Shift
  2008. *
  2009. * Inspired by array_shift
  2010. *
  2011. * @param String $string
  2012. * @param optional Integer $index
  2013. * @return String
  2014. * @access private
  2015. */
  2016. function _string_shift(&$string, $index = 1)
  2017. {
  2018. $substr = substr($string, 0, $index);
  2019. $string = substr($string, $index);
  2020. return $substr;
  2021. }
  2022. /**
  2023. * Define Array
  2024. *
  2025. * Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of
  2026. * named constants from it, using the value as the name of the constant and the index as the value of the constant.
  2027. * If any of the constants that would be defined already exists, none of the constants will be defined.
  2028. *
  2029. * @param Array $array
  2030. * @access private
  2031. */
  2032. function _define_array()
  2033. {
  2034. $args = func_get_args();
  2035. foreach ($args as $arg) {
  2036. foreach ($arg as $key=>$value) {
  2037. if (!defined($value)) {
  2038. define($value, $key);
  2039. } else {
  2040. break 2;
  2041. }
  2042. }
  2043. }
  2044. }
  2045. /**
  2046. * Returns a log of the packets that have been sent and received.
  2047. *
  2048. * 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')
  2049. *
  2050. * @access public
  2051. * @return String or Array
  2052. */
  2053. function getLog()
  2054. {
  2055. if (!defined('NET_SSH2_LOGGING')) {
  2056. return false;
  2057. }
  2058. switch (NET_SSH2_LOGGING) {
  2059. case NET_SSH2_LOG_SIMPLE:
  2060. return $this->message_number_log;
  2061. break;
  2062. case NET_SSH2_LOG_COMPLEX:
  2063. return $this->_format_log($this->message_log, $this->message_number_log);
  2064. break;
  2065. default:
  2066. return false;
  2067. }
  2068. }
  2069. /**
  2070. * Formats a log for printing
  2071. *
  2072. * @param Array $message_log
  2073. * @param Array $message_number_log
  2074. * @access private
  2075. * @return String
  2076. */
  2077. function _format_log($message_log, $message_number_log)
  2078. {
  2079. static $boundary = ':', $long_width = 65, $short_width = 16;
  2080. $output = '';
  2081. for ($i = 0; $i < count($message_log); $i++) {
  2082. $output.= $message_number_log[$i] . "\r\n";
  2083. $current_log = $message_log[$i];
  2084. $j = 0;
  2085. do {
  2086. if (!empty($current_log)) {
  2087. $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
  2088. }
  2089. $fragment = $this->_string_shift($current_log, $short_width);
  2090. $hex = substr(
  2091. preg_replace(
  2092. '#(.)#es',
  2093. '"' . $boundary . '" . str_pad(dechex(ord(substr("\\1", -1))), 2, "0", STR_PAD_LEFT)',
  2094. $fragment),
  2095. strlen($boundary)
  2096. );
  2097. // replace non ASCII printable characters with dots
  2098. // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
  2099. // also replace < with a . since < messes up the output on web browsers
  2100. $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
  2101. $output.= str_pad($hex, $long_width - $short_width, ' ') . $raw . "\r\n";
  2102. $j++;
  2103. } while (!empty($current_log));
  2104. $output.= "\r\n";
  2105. }
  2106. return $output;
  2107. }
  2108. /**
  2109. * Returns all errors
  2110. *
  2111. * @return String
  2112. * @access public
  2113. */
  2114. function getErrors()
  2115. {
  2116. return $this->errors;
  2117. }
  2118. /**
  2119. * Returns the last error
  2120. *
  2121. * @return String
  2122. * @access public
  2123. */
  2124. function getLastError()
  2125. {
  2126. return $this->errors[count($this->errors) - 1];
  2127. }
  2128. /**
  2129. * Return the server identification.
  2130. *
  2131. * @return String
  2132. * @access public
  2133. */
  2134. function getServerIdentification()
  2135. {
  2136. return $this->server_identifier;
  2137. }
  2138. /**
  2139. * Return a list of the key exchange algorithms the server supports.
  2140. *
  2141. * @return Array
  2142. * @access public
  2143. */
  2144. function getKexAlgorithms()
  2145. {
  2146. return $this->kex_algorithms;
  2147. }
  2148. /**
  2149. * Return a list of the host key (public key) algorithms the server supports.
  2150. *
  2151. * @return Array
  2152. * @access public
  2153. */
  2154. function getServerHostKeyAlgorithms()
  2155. {
  2156. return $this->server_host_key_algorithms;
  2157. }
  2158. /**
  2159. * Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.
  2160. *
  2161. * @return Array
  2162. * @access public
  2163. */
  2164. function getEncryptionAlgorithmsClient2Server()
  2165. {
  2166. return $this->encryption_algorithms_client_to_server;
  2167. }
  2168. /**
  2169. * Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.
  2170. *
  2171. * @return Array
  2172. * @access public
  2173. */
  2174. function getEncryptionAlgorithmsServer2Client()
  2175. {
  2176. return $this->encryption_algorithms_server_to_client;
  2177. }
  2178. /**
  2179. * Return a list of the MAC algorithms the server supports, when receiving stuff from the client.
  2180. *
  2181. * @return Array
  2182. * @access public
  2183. */
  2184. function getMACAlgorithmsClient2Server()
  2185. {
  2186. return $this->mac_algorithms_client_to_server;
  2187. }
  2188. /**
  2189. * Return a list of the MAC algorithms the server supports, when sending stuff to the client.
  2190. *
  2191. * @return Array
  2192. * @access public
  2193. */
  2194. function getMACAlgorithmsServer2Client()
  2195. {
  2196. return $this->mac_algorithms_server_to_client;
  2197. }
  2198. /**
  2199. * Return a list of the compression algorithms the server supports, when receiving stuff from the client.
  2200. *
  2201. * @return Array
  2202. * @access public
  2203. */
  2204. function getCompressionAlgorithmsClient2Server()
  2205. {
  2206. return $this->compression_algorithms_client_to_server;
  2207. }
  2208. /**
  2209. * Return a list of the compression algorithms the server supports, when sending stuff to the client.
  2210. *
  2211. * @return Array
  2212. * @access public
  2213. */
  2214. function getCompressionAlgorithmsServer2Client()
  2215. {
  2216. return $this->compression_algorithms_server_to_client;
  2217. }
  2218. /**
  2219. * Return a list of the languages the server supports, when sending stuff to the client.
  2220. *
  2221. * @return Array
  2222. * @access public
  2223. */
  2224. function getLanguagesServer2Client()
  2225. {
  2226. return $this->languages_server_to_client;
  2227. }
  2228. /**
  2229. * Return a list of the languages the server supports, when receiving stuff from the client.
  2230. *
  2231. * @return Array
  2232. * @access public
  2233. */
  2234. function getLanguagesClient2Server()
  2235. {
  2236. return $this->languages_client_to_server;
  2237. }
  2238. /**
  2239. * Returns the server public host key.
  2240. *
  2241. * Caching this the first time you connect to a server and checking the result on subsequent connections
  2242. * is recommended. Returns false if the server signature is not signed correctly with the public host key.
  2243. *
  2244. * @return Mixed
  2245. * @access public
  2246. */
  2247. function getServerPublicHostKey()
  2248. {
  2249. $signature = $this->signature;
  2250. $server_public_host_key = $this->server_public_host_key;
  2251. extract(unpack('Nlength', $this->_string_shift($server_public_host_key, 4)));
  2252. $this->_string_shift($server_public_host_key, $length);
  2253. switch ($this->signature_format) {
  2254. case 'ssh-dss':
  2255. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2256. $p = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2257. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2258. $q = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2259. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2260. $g = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2261. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2262. $y = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2263. /* The value for 'dss_signature_blob' is encoded as a string containing
  2264. r, followed by s (which are 160-bit integers, without lengths or
  2265. padding, unsigned, and in network byte order). */
  2266. $temp = unpack('Nlength', $this->_string_shift($signature, 4));
  2267. if ($temp['length'] != 40) {
  2268. user_error('Invalid signature', E_USER_NOTICE);
  2269. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  2270. }
  2271. $r = new Math_BigInteger($this->_string_shift($signature, 20), 256);
  2272. $s = new Math_BigInteger($this->_string_shift($signature, 20), 256);
  2273. if ($r->compare($q) >= 0 || $s->compare($q) >= 0) {
  2274. user_error('Invalid signature', E_USER_NOTICE);
  2275. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  2276. }
  2277. $w = $s->modInverse($q);
  2278. $u1 = $w->multiply(new Math_BigInteger(sha1($this->exchange_hash), 16));
  2279. list(, $u1) = $u1->divide($q);
  2280. $u2 = $w->multiply($r);
  2281. list(, $u2) = $u2->divide($q);
  2282. $g = $g->modPow($u1, $p);
  2283. $y = $y->modPow($u2, $p);
  2284. $v = $g->multiply($y);
  2285. list(, $v) = $v->divide($p);
  2286. list(, $v) = $v->divide($q);
  2287. if (!$v->equals($r)) {
  2288. user_error('Bad server signature', E_USER_NOTICE);
  2289. return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
  2290. }
  2291. break;
  2292. case 'ssh-rsa':
  2293. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2294. $e = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2295. $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
  2296. $n = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
  2297. $nLength = $temp['length'];
  2298. /*
  2299. $temp = unpack('Nlength', $this->_string_shift($signature, 4));
  2300. $signature = $this->_string_shift($signature, $temp['length']);
  2301. if (!class_exists('Crypt_RSA')) {
  2302. require_once('Crypt/RSA.php');
  2303. }
  2304. $rsa = new Crypt_RSA();
  2305. $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
  2306. $rsa->loadKey(array('e' => $e, 'n' => $n), CRYPT_RSA_PUBLIC_FORMAT_RAW);
  2307. if (!$rsa->verify($this->exchange_hash, $signature)) {
  2308. user_error('Bad server signature', E_USER_NOTICE);
  2309. return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
  2310. }
  2311. */
  2312. $temp = unpack('Nlength', $this->_string_shift($signature, 4));
  2313. $s = new Math_BigInteger($this->_string_shift($signature, $temp['length']), 256);
  2314. // validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the
  2315. // following URL:
  2316. // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf
  2317. // also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source.
  2318. if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) {
  2319. user_error('Invalid signature', E_USER_NOTICE);
  2320. return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
  2321. }
  2322. $s = $s->modPow($e, $n);
  2323. $s = $s->toBytes();
  2324. $h = pack('N4H*', 0x00302130, 0x0906052B, 0x0E03021A, 0x05000414, sha1($this->exchange_hash));
  2325. $h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 3 - strlen($h)) . $h;
  2326. if ($s != $h) {
  2327. user_error('Bad server signature', E_USER_NOTICE);
  2328. return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
  2329. }
  2330. }
  2331. return $this->server_public_host_key;
  2332. }
  2333. }