2007-07-23 05:21:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2009-06-09 04:00:38 +00:00
|
|
|
* Pure-PHP implementation of SSHv2.
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-12-03 19:04:10 +00:00
|
|
|
* Here are some examples of how to use this library:
|
2007-07-25 21:49:33 +00:00
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-06-01 21:28:49 +00:00
|
|
|
* include 'Net/SSH2.php';
|
2007-07-25 21:49:33 +00:00
|
|
|
*
|
|
|
|
* $ssh = new Net_SSH2('www.domain.tld');
|
|
|
|
* if (!$ssh->login('username', 'password')) {
|
|
|
|
* exit('Login Failed');
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* echo $ssh->exec('pwd');
|
|
|
|
* echo $ssh->exec('ls -la');
|
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2009-12-03 08:19:00 +00:00
|
|
|
* <code>
|
|
|
|
* <?php
|
2014-06-01 21:28:49 +00:00
|
|
|
* include 'Crypt/RSA.php';
|
|
|
|
* include 'Net/SSH2.php';
|
2009-12-03 08:19:00 +00:00
|
|
|
*
|
|
|
|
* $key = new Crypt_RSA();
|
|
|
|
* //$key->setPassword('whatever');
|
|
|
|
* $key->loadKey(file_get_contents('privatekey'));
|
|
|
|
*
|
|
|
|
* $ssh = new Net_SSH2('www.domain.tld');
|
|
|
|
* if (!$ssh->login('username', $key)) {
|
|
|
|
* exit('Login Failed');
|
|
|
|
* }
|
|
|
|
*
|
2011-05-08 23:53:30 +00:00
|
|
|
* echo $ssh->read('username@username:~$');
|
|
|
|
* $ssh->write("ls -la\n");
|
|
|
|
* echo $ssh->read('username@username:~$');
|
2009-12-03 08:19:00 +00:00
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
2011-04-18 12:17:40 +00:00
|
|
|
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-12-25 23:33:08 +00:00
|
|
|
*
|
2011-04-18 12:17:40 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-12-25 23:33:08 +00:00
|
|
|
*
|
2011-04-18 12:17:40 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2013-12-10 19:10:37 +00:00
|
|
|
* @category Net
|
|
|
|
* @package Net_SSH2
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-09 23:02:44 +00:00
|
|
|
* @copyright 2007 Jim Wigginton
|
2013-12-10 19:10:37 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
2007-07-23 05:21:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* Execution Bitmap Masks
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::bitmap
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-07-14 09:09:16 +00:00
|
|
|
define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001);
|
2014-04-07 04:45:25 +00:00
|
|
|
define('NET_SSH2_MASK_CONNECTED', 0x00000002);
|
|
|
|
define('NET_SSH2_MASK_LOGIN_REQ', 0x00000004);
|
|
|
|
define('NET_SSH2_MASK_LOGIN', 0x00000008);
|
|
|
|
define('NET_SSH2_MASK_SHELL', 0x00000010);
|
2014-11-06 17:58:03 +00:00
|
|
|
define('NET_SSH2_MASK_WINDOW_ADJUST', 0x00000020);
|
2007-07-25 21:49:33 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
/**#@+
|
|
|
|
* Channel constants
|
|
|
|
*
|
|
|
|
* RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer
|
|
|
|
* to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with
|
|
|
|
* a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a
|
|
|
|
* recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel
|
|
|
|
* would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet:
|
|
|
|
* The 'recipient channel' is the channel number given in the original
|
|
|
|
* open request, and 'sender channel' is the channel number allocated by
|
|
|
|
* the other side.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_channel_packet()
|
|
|
|
* @see self::_get_channel_packet()
|
2009-12-14 18:14:54 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2019-01-20 15:39:12 +00:00
|
|
|
define('NET_SSH2_CHANNEL_EXEC', 1); // PuTTy uses 0x100
|
|
|
|
define('NET_SSH2_CHANNEL_SHELL', 2);
|
|
|
|
define('NET_SSH2_CHANNEL_SUBSYSTEM', 3);
|
2017-08-29 03:37:35 +00:00
|
|
|
define('NET_SSH2_CHANNEL_AGENT_FORWARD', 4);
|
2019-01-20 15:39:12 +00:00
|
|
|
define('NET_SSH2_CHANNEL_KEEP_ALIVE', 5);
|
2009-12-14 18:14:54 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2009-03-25 22:29:42 +00:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getLog()
|
2009-03-25 22:29:42 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Returns the message numbers
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_LOG_SIMPLE', 1);
|
|
|
|
/**
|
|
|
|
* Returns the message content
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_LOG_COMPLEX', 2);
|
2012-04-30 16:25:37 +00:00
|
|
|
/**
|
|
|
|
* Outputs the content real-time
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_LOG_REALTIME', 3);
|
2012-08-28 08:04:15 +00:00
|
|
|
/**
|
|
|
|
* Dumps the content real-time to a file
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_LOG_REALTIME_FILE', 4);
|
2017-06-13 23:56:50 +00:00
|
|
|
/**
|
|
|
|
* Make sure that the log never gets larger than this
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024);
|
2009-03-25 22:29:42 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2011-02-28 05:24:09 +00:00
|
|
|
/**#@+
|
|
|
|
* @access public
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::read()
|
2011-02-28 05:24:09 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Returns when a string matching $expect exactly is found
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_READ_SIMPLE', 1);
|
|
|
|
/**
|
|
|
|
* Returns when a string matching the regular expression $expect is found
|
|
|
|
*/
|
|
|
|
define('NET_SSH2_READ_REGEX', 2);
|
2011-06-04 17:06:53 +00:00
|
|
|
/**
|
2019-04-12 02:54:37 +00:00
|
|
|
* Returns whenever a data packet is received.
|
|
|
|
*
|
|
|
|
* Some data packets may only contain a single character so it may be necessary
|
|
|
|
* to call read() multiple times when using this option
|
2011-06-04 17:06:53 +00:00
|
|
|
*/
|
2017-06-13 23:56:50 +00:00
|
|
|
define('NET_SSH2_READ_NEXT', 3);
|
2011-02-28 05:24:09 +00:00
|
|
|
/**#@-*/
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Pure-PHP implementation of SSHv2.
|
|
|
|
*
|
2013-12-11 17:33:18 +00:00
|
|
|
* @package Net_SSH2
|
2007-07-23 05:21:39 +00:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2013-12-03 18:34:41 +00:00
|
|
|
class Net_SSH2
|
|
|
|
{
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* The SSH identifier
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-12-16 19:17:10 +00:00
|
|
|
var $identifier;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Socket Object
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var object
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $fsock;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execution Bitmap
|
|
|
|
*
|
2012-08-14 17:07:18 +00:00
|
|
|
* The bits that are set represent functions that have been called already. This is used to determine
|
2007-07-23 05:21:39 +00:00
|
|
|
* if a requisite function has been successfully executed. If not, an error should be thrown.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-05-30 15:46:32 +00:00
|
|
|
var $bitmap = 0;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-12 23:02:13 +00:00
|
|
|
* Error information
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getErrors()
|
|
|
|
* @see self::getLastError()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2010-02-12 23:02:13 +00:00
|
|
|
var $errors = array();
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Server Identifier
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerIdentification()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $server_identifier = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Key Exchange Algorithms
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getKexAlgorithims()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $kex_algorithms = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
/**
|
|
|
|
* Key Exchange Algorithm
|
|
|
|
*
|
|
|
|
* @see self::getMethodsNegotiated()
|
|
|
|
* @var string|false
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $kex_algorithm = false;
|
|
|
|
|
2015-06-24 22:50:00 +00:00
|
|
|
/**
|
|
|
|
* Minimum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2015-06-24 22:50:00 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $kex_dh_group_size_min = 1536;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferred Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2015-06-24 22:50:00 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $kex_dh_group_size_preferred = 2048;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum Diffie-Hellman Group Bit Size in RFC 4419 Key Exchange Methods
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2015-06-24 22:50:00 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $kex_dh_group_size_max = 4096;
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Server Host Key Algorithms
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerHostKeyAlgorithms()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $server_host_key_algorithms = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encryption Algorithms: Client to Server
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getEncryptionAlgorithmsClient2Server()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $encryption_algorithms_client_to_server = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encryption Algorithms: Server to Client
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getEncryptionAlgorithmsServer2Client()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $encryption_algorithms_server_to_client = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MAC Algorithms: Client to Server
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getMACAlgorithmsClient2Server()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $mac_algorithms_client_to_server = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MAC Algorithms: Server to Client
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getMACAlgorithmsServer2Client()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $mac_algorithms_server_to_client = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compression Algorithms: Client to Server
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getCompressionAlgorithmsClient2Server()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $compression_algorithms_client_to_server = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compression Algorithms: Server to Client
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getCompressionAlgorithmsServer2Client()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $compression_algorithms_server_to_client = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Languages: Server to Client
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getLanguagesServer2Client()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $languages_server_to_client = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Languages: Client to Server
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getLanguagesClient2Server()
|
2015-09-06 11:28:08 +00:00
|
|
|
* @var array|false
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-08-05 01:11:34 +00:00
|
|
|
var $languages_client_to_server = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
/**
|
|
|
|
* Preferred Algorithms
|
|
|
|
*
|
|
|
|
* @see self::setPreferredAlgorithms()
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $preferred = array();
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
2009-02-16 22:22:13 +00:00
|
|
|
* Block Size for Server to Client Encryption
|
|
|
|
*
|
|
|
|
* "Note that the length of the concatenation of 'packet_length',
|
|
|
|
* 'padding_length', 'payload', and 'random padding' MUST be a multiple
|
|
|
|
* of the cipher block size or 8, whichever is larger. This constraint
|
|
|
|
* MUST be enforced, even when using stream ciphers."
|
|
|
|
*
|
|
|
|
* -- http://tools.ietf.org/html/rfc4253#section-6
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
|
|
|
* @see self::_send_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-02-16 22:22:13 +00:00
|
|
|
var $encrypt_block_size = 8;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block Size for Client to Server Encryption
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2009-02-16 22:22:13 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $decrypt_block_size = 8;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Server to Client Encryption Object
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var object
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $decrypt = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client to Server Encryption Object
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var object
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $encrypt = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Client to Server HMAC Object
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var object
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hmac_create = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server to Client HMAC Object
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var object
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hmac_check = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of server to client HMAC
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 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
|
|
|
|
* append it.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $hmac_size = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server Public Host Key
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerPublicHostKey()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $server_public_host_key;
|
|
|
|
|
|
|
|
/**
|
2016-07-31 12:41:23 +00:00
|
|
|
* Session identifier
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
|
|
|
* "The exchange hash H from the first key exchange is additionally
|
|
|
|
* used as the session identifier, which is a unique identifier for
|
|
|
|
* this connection."
|
|
|
|
*
|
|
|
|
* -- http://tools.ietf.org/html/rfc4253#section-7.2
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $session_id = false;
|
|
|
|
|
2010-10-24 01:24:30 +00:00
|
|
|
/**
|
|
|
|
* Exchange hash
|
|
|
|
*
|
|
|
|
* The current exchange hash
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2010-10-24 01:24:30 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $exchange_hash = false;
|
|
|
|
|
2008-05-26 19:42:01 +00:00
|
|
|
/**
|
|
|
|
* Message Numbers
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $message_numbers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnection Message 'reason codes' defined in RFC4253
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $disconnect_reasons = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $channel_open_failure_reasons = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminal Modes
|
|
|
|
*
|
|
|
|
* @link http://tools.ietf.org/html/rfc4254#section-8
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $terminal_modes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
|
|
|
|
*
|
|
|
|
* @link http://tools.ietf.org/html/rfc4254#section-5.2
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $channel_extended_data_type_codes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Sequence Number
|
|
|
|
*
|
|
|
|
* See 'Section 6.4. Data Integrity' of rfc4253 for more info.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $send_seq_no = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Sequence Number
|
|
|
|
*
|
|
|
|
* See 'Section 6.4. Data Integrity' of rfc4253 for more info.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $get_seq_no = 0;
|
|
|
|
|
2009-10-16 03:37:24 +00:00
|
|
|
/**
|
2009-12-14 18:14:54 +00:00
|
|
|
* Server Channels
|
2009-10-16 03:37:24 +00:00
|
|
|
*
|
2009-12-14 18:14:54 +00:00
|
|
|
* Maps client channels to server channels
|
2009-10-16 03:37:24 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_channel_packet()
|
|
|
|
* @see self::exec()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-10-16 03:37:24 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-12-14 18:14:54 +00:00
|
|
|
var $server_channels = array();
|
2009-10-16 03:37:24 +00:00
|
|
|
|
2009-08-23 03:40:50 +00:00
|
|
|
/**
|
2009-12-14 18:14:54 +00:00
|
|
|
* Channel Buffers
|
2009-08-23 03:40:50 +00:00
|
|
|
*
|
2009-12-14 18:14:54 +00:00
|
|
|
* If a client requests a packet from one channel but receives two packets from another those packets should
|
|
|
|
* be placed in a buffer
|
2009-08-23 03:40:50 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_channel_packet()
|
|
|
|
* @see self::exec()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-08-23 03:40:50 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-12-14 18:14:54 +00:00
|
|
|
var $channel_buffers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Channel Status
|
|
|
|
*
|
|
|
|
* Contains the type of the last sent message
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_channel_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-12-14 18:14:54 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $channel_status = array();
|
2009-08-23 03:40:50 +00:00
|
|
|
|
2009-09-17 03:19:20 +00:00
|
|
|
/**
|
|
|
|
* Packet Size
|
|
|
|
*
|
2009-12-14 18:14:54 +00:00
|
|
|
* Maximum packet size indexed by channel
|
2009-09-17 03:19:20 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_channel_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-09-17 03:19:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-12-14 18:14:54 +00:00
|
|
|
var $packet_size_client_to_server = array();
|
2009-09-17 03:19:20 +00:00
|
|
|
|
2009-03-25 22:29:42 +00:00
|
|
|
/**
|
|
|
|
* Message Number Log
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getLog()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-03-25 22:29:42 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $message_number_log = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message Log
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getLog()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2009-03-25 22:29:42 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $message_log = array();
|
|
|
|
|
2010-02-11 07:02:51 +00:00
|
|
|
/**
|
|
|
|
* The Window Size
|
|
|
|
*
|
2013-05-10 21:48:10 +00:00
|
|
|
* Bytes the other party can send before it must wait for the window to be adjusted (0x7FFFFFFF = 2GB)
|
2010-02-11 07:02:51 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_channel_packet()
|
|
|
|
* @see self::exec()
|
2010-02-11 07:02:51 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $window_size = 0x7FFFFFFF;
|
|
|
|
|
|
|
|
/**
|
2013-07-07 20:57:15 +00:00
|
|
|
* Window size, server to client
|
2010-02-11 07:02:51 +00:00
|
|
|
*
|
|
|
|
* Window size indexed by channel
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_channel_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2010-02-11 07:02:51 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-05-14 03:37:32 +00:00
|
|
|
var $window_size_server_to_client = array();
|
2010-02-11 07:02:51 +00:00
|
|
|
|
2013-07-07 20:57:15 +00:00
|
|
|
/**
|
|
|
|
* Window size, client to server
|
|
|
|
*
|
|
|
|
* Window size indexed by channel
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_channel_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2013-07-07 20:57:15 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $window_size_client_to_server = array();
|
|
|
|
|
2010-04-22 16:06:43 +00:00
|
|
|
/**
|
|
|
|
* Server signature
|
|
|
|
*
|
|
|
|
* Verified against $this->session_id
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerPublicHostKey()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2010-04-22 16:06:43 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $signature = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server signature format
|
|
|
|
*
|
|
|
|
* ssh-rsa or ssh-dss.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerPublicHostKey()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2010-04-22 16:06:43 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $signature_format = '';
|
|
|
|
|
2011-02-28 05:24:09 +00:00
|
|
|
/**
|
|
|
|
* Interactive Buffer
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::read()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2011-02-28 05:24:09 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2011-05-08 02:34:36 +00:00
|
|
|
var $interactiveBuffer = '';
|
2011-02-28 05:24:09 +00:00
|
|
|
|
2011-06-04 17:06:53 +00:00
|
|
|
/**
|
|
|
|
* Current log size
|
|
|
|
*
|
|
|
|
* Should never exceed NET_SSH2_LOG_MAX_SIZE
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_binary_packet()
|
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2011-06-04 17:06:53 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $log_size;
|
|
|
|
|
2012-03-03 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Timeout
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::setTimeout()
|
2012-03-03 17:49:16 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $timeout;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current Timeout
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_channel_packet()
|
2012-03-03 17:49:16 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $curTimeout;
|
|
|
|
|
2012-04-30 16:25:37 +00:00
|
|
|
/**
|
|
|
|
* Real-time log file pointer
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_append_log()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var resource
|
2012-04-30 16:25:37 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $realtime_log_file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Real-time log file size
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_append_log()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2012-04-30 16:25:37 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $realtime_log_size;
|
|
|
|
|
2012-07-04 18:36:26 +00:00
|
|
|
/**
|
|
|
|
* Has the signature been validated?
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getServerPublicHostKey()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var bool
|
2012-07-04 18:36:26 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $signature_validated = false;
|
|
|
|
|
2012-04-30 16:25:37 +00:00
|
|
|
/**
|
|
|
|
* Real-time log file wrap boolean
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_append_log()
|
2012-04-30 16:25:37 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $realtime_log_wrap;
|
|
|
|
|
2012-07-23 12:17:53 +00:00
|
|
|
/**
|
|
|
|
* Flag to suppress stderr from output
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::enableQuietMode()
|
2012-07-23 12:17:53 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $quiet_mode = false;
|
|
|
|
|
2012-11-22 19:08:30 +00:00
|
|
|
/**
|
|
|
|
* Time of first network activity
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2012-11-22 19:08:30 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $last_packet;
|
|
|
|
|
2013-01-17 18:47:42 +00:00
|
|
|
/**
|
|
|
|
* Exit status returned from ssh if any
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2013-01-17 18:47:42 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $exit_status;
|
|
|
|
|
2013-02-08 22:04:52 +00:00
|
|
|
/**
|
|
|
|
* Flag to request a PTY when using exec()
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var bool
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::enablePTY()
|
2013-02-08 22:04:52 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $request_pty = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag set while exec() is running when using enablePTY()
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var bool
|
2013-02-08 22:04:52 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $in_request_pty_exec = false;
|
|
|
|
|
2013-10-25 17:35:30 +00:00
|
|
|
/**
|
|
|
|
* Flag set after startSubsystem() is called
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var bool
|
2013-10-25 17:35:30 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $in_subsystem;
|
|
|
|
|
2013-03-21 22:18:31 +00:00
|
|
|
/**
|
|
|
|
* Contents of stdError
|
2013-04-20 03:23:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2013-03-21 22:18:31 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $stdErrorLog;
|
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
/**
|
|
|
|
* The Last Interactive Response
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_keyboard_interactive_process()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2013-04-20 03:23:06 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $last_interactive_response = '';
|
|
|
|
|
2013-04-20 19:35:08 +00:00
|
|
|
/**
|
|
|
|
* Keyboard Interactive Request / Responses
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_keyboard_interactive_process()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var array
|
2013-04-20 19:35:08 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $keyboard_requests_responses = array();
|
|
|
|
|
2013-04-28 00:58:24 +00:00
|
|
|
/**
|
|
|
|
* Banner Message
|
|
|
|
*
|
|
|
|
* Quoting from the RFC, "in some jurisdictions, sending a warning message before
|
|
|
|
* authentication may be relevant for getting legal protection."
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_filter()
|
|
|
|
* @see self::getBannerMessage()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2013-04-28 00:58:24 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $banner_message = '';
|
|
|
|
|
2013-06-07 22:21:11 +00:00
|
|
|
/**
|
|
|
|
* Did read() timeout or return normally?
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::isTimeout()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var bool
|
2013-06-07 22:21:11 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $is_timeout = false;
|
|
|
|
|
2013-12-16 17:27:12 +00:00
|
|
|
/**
|
|
|
|
* Log Boundary
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_format_log()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2013-12-16 17:27:12 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $log_boundary = ':';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log Long Width
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_format_log()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2013-12-16 17:27:12 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $log_long_width = 65;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log Short Width
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_format_log()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2013-12-16 17:27:12 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $log_short_width = 16;
|
|
|
|
|
2014-04-07 04:45:25 +00:00
|
|
|
/**
|
|
|
|
* Hostname
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
|
|
|
* @see self::_connect()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var string
|
2014-04-07 04:45:25 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $host;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Port Number
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::Net_SSH2()
|
|
|
|
* @see self::_connect()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2014-04-07 04:45:25 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $port;
|
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Number of columns for terminal window size
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getWindowColumns()
|
|
|
|
* @see self::setWindowColumns()
|
|
|
|
* @see self::setWindowSize()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $windowColumns = 80;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of columns for terminal window size
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::getWindowRows()
|
|
|
|
* @see self::setWindowRows()
|
|
|
|
* @see self::setWindowSize()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $windowRows = 24;
|
|
|
|
|
2014-10-12 04:26:46 +00:00
|
|
|
/**
|
|
|
|
* Crypto Engine
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::setCryptoEngine()
|
|
|
|
* @see self::_key_exchange()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @var int
|
2014-10-12 04:26:46 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $crypto_engine = false;
|
|
|
|
|
2014-12-30 02:44:31 +00:00
|
|
|
/**
|
|
|
|
* A System_SSH_Agent for use in the SSH2 Agent Forwarding scenario
|
|
|
|
*
|
|
|
|
* @var System_SSH_Agent
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $agent;
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
/**
|
|
|
|
* Send the identification string first?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $send_id_string_first = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the key exchange initiation packet first?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $send_kex_first = true;
|
|
|
|
|
2017-08-27 00:50:17 +00:00
|
|
|
/**
|
|
|
|
* Some versions of OpenSSH incorrectly calculate the key size
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $bad_key_size_fix = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should we try to re-connect to re-establish keys?
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $retry_connect = false;
|
|
|
|
|
2017-08-29 03:37:35 +00:00
|
|
|
/**
|
|
|
|
* Binary Packet Buffer
|
|
|
|
*
|
|
|
|
* @var string|false
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $binary_packet_buffer = false;
|
|
|
|
|
2018-05-27 14:48:44 +00:00
|
|
|
/**
|
|
|
|
* Preferred Signature Format
|
|
|
|
*
|
|
|
|
* @var string|false
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $preferred_signature_format = false;
|
|
|
|
|
2018-10-06 06:19:14 +00:00
|
|
|
/**
|
|
|
|
* Authentication Credentials
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $auth = array();
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
2015-07-17 17:30:44 +00:00
|
|
|
* $host can either be a string, representing the host, or a stream resource.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param mixed $host
|
|
|
|
* @param int $port
|
|
|
|
* @param int $timeout
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::login()
|
2007-07-23 05:21:39 +00:00
|
|
|
* @return Net_SSH2
|
|
|
|
* @access public
|
|
|
|
*/
|
2016-09-10 05:52:56 +00:00
|
|
|
function __construct($host, $port = 22, $timeout = 10)
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2013-05-13 05:41:52 +00:00
|
|
|
// Include Math_BigInteger
|
|
|
|
// Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
|
|
|
|
if (!class_exists('Math_BigInteger')) {
|
2013-12-06 00:03:34 +00:00
|
|
|
include_once 'Math/BigInteger.php';
|
2013-05-13 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!function_exists('crypt_random_string')) {
|
2013-12-06 00:03:34 +00:00
|
|
|
include_once 'Crypt/Random.php';
|
2013-05-13 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('Crypt_Hash')) {
|
2013-12-06 00:03:34 +00:00
|
|
|
include_once 'Crypt/Hash.php';
|
2013-05-13 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-12 04:40:56 +00:00
|
|
|
// include Crypt_Base so constants can be defined for setCryptoEngine()
|
|
|
|
if (!class_exists('Crypt_Base')) {
|
|
|
|
include_once 'Crypt/Base.php';
|
|
|
|
}
|
|
|
|
|
2008-05-26 19:42:01 +00:00
|
|
|
$this->message_numbers = array(
|
|
|
|
1 => 'NET_SSH2_MSG_DISCONNECT',
|
|
|
|
2 => 'NET_SSH2_MSG_IGNORE',
|
|
|
|
3 => 'NET_SSH2_MSG_UNIMPLEMENTED',
|
|
|
|
4 => 'NET_SSH2_MSG_DEBUG',
|
|
|
|
5 => 'NET_SSH2_MSG_SERVICE_REQUEST',
|
|
|
|
6 => 'NET_SSH2_MSG_SERVICE_ACCEPT',
|
|
|
|
20 => 'NET_SSH2_MSG_KEXINIT',
|
|
|
|
21 => 'NET_SSH2_MSG_NEWKEYS',
|
|
|
|
30 => 'NET_SSH2_MSG_KEXDH_INIT',
|
|
|
|
31 => 'NET_SSH2_MSG_KEXDH_REPLY',
|
|
|
|
50 => 'NET_SSH2_MSG_USERAUTH_REQUEST',
|
|
|
|
51 => 'NET_SSH2_MSG_USERAUTH_FAILURE',
|
|
|
|
52 => 'NET_SSH2_MSG_USERAUTH_SUCCESS',
|
|
|
|
53 => 'NET_SSH2_MSG_USERAUTH_BANNER',
|
|
|
|
|
|
|
|
80 => 'NET_SSH2_MSG_GLOBAL_REQUEST',
|
|
|
|
81 => 'NET_SSH2_MSG_REQUEST_SUCCESS',
|
|
|
|
82 => 'NET_SSH2_MSG_REQUEST_FAILURE',
|
|
|
|
90 => 'NET_SSH2_MSG_CHANNEL_OPEN',
|
|
|
|
91 => 'NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION',
|
|
|
|
92 => 'NET_SSH2_MSG_CHANNEL_OPEN_FAILURE',
|
|
|
|
93 => 'NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST',
|
|
|
|
94 => 'NET_SSH2_MSG_CHANNEL_DATA',
|
|
|
|
95 => 'NET_SSH2_MSG_CHANNEL_EXTENDED_DATA',
|
|
|
|
96 => 'NET_SSH2_MSG_CHANNEL_EOF',
|
|
|
|
97 => 'NET_SSH2_MSG_CHANNEL_CLOSE',
|
|
|
|
98 => 'NET_SSH2_MSG_CHANNEL_REQUEST',
|
|
|
|
99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS',
|
|
|
|
100 => 'NET_SSH2_MSG_CHANNEL_FAILURE'
|
|
|
|
);
|
|
|
|
$this->disconnect_reasons = array(
|
|
|
|
1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
|
|
|
|
2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
|
|
|
|
3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
|
|
|
|
4 => 'NET_SSH2_DISCONNECT_RESERVED',
|
|
|
|
5 => 'NET_SSH2_DISCONNECT_MAC_ERROR',
|
|
|
|
6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR',
|
|
|
|
7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE',
|
|
|
|
8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED',
|
|
|
|
9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE',
|
|
|
|
10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST',
|
|
|
|
11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION',
|
|
|
|
12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS',
|
|
|
|
13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER',
|
|
|
|
14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
|
|
|
|
15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
|
|
|
|
);
|
|
|
|
$this->channel_open_failure_reasons = array(
|
|
|
|
1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED'
|
|
|
|
);
|
|
|
|
$this->terminal_modes = array(
|
|
|
|
0 => 'NET_SSH2_TTY_OP_END'
|
|
|
|
);
|
|
|
|
$this->channel_extended_data_type_codes = array(
|
|
|
|
1 => 'NET_SSH2_EXTENDED_DATA_STDERR'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->_define_array(
|
|
|
|
$this->message_numbers,
|
|
|
|
$this->disconnect_reasons,
|
|
|
|
$this->channel_open_failure_reasons,
|
|
|
|
$this->terminal_modes,
|
2009-12-03 08:19:00 +00:00
|
|
|
$this->channel_extended_data_type_codes,
|
|
|
|
array(60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'),
|
2010-08-28 17:26:22 +00:00
|
|
|
array(60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'),
|
|
|
|
array(60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
|
2015-06-24 22:50:00 +00:00
|
|
|
61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE'),
|
|
|
|
// RFC 4419 - diffie-hellman-group-exchange-sha{1,256}
|
|
|
|
array(30 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST_OLD',
|
|
|
|
31 => 'NET_SSH2_MSG_KEXDH_GEX_GROUP',
|
|
|
|
32 => 'NET_SSH2_MSG_KEXDH_GEX_INIT',
|
|
|
|
33 => 'NET_SSH2_MSG_KEXDH_GEX_REPLY',
|
|
|
|
34 => 'NET_SSH2_MSG_KEXDH_GEX_REQUEST')
|
2008-05-26 19:42:01 +00:00
|
|
|
);
|
|
|
|
|
2015-07-17 17:30:44 +00:00
|
|
|
if (is_resource($host)) {
|
|
|
|
$this->fsock = $host;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($host)) {
|
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
}
|
2014-04-07 04:45:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 05:52:56 +00:00
|
|
|
/**
|
|
|
|
* PHP4 compatible Default Constructor.
|
|
|
|
*
|
|
|
|
* @see self::__construct()
|
|
|
|
* @param mixed $host
|
|
|
|
* @param int $port
|
|
|
|
* @param int $timeout
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function Net_SSH2($host, $port = 22, $timeout = 10)
|
|
|
|
{
|
|
|
|
$this->__construct($host, $port, $timeout);
|
|
|
|
}
|
|
|
|
|
2014-10-12 04:26:46 +00:00
|
|
|
/**
|
|
|
|
* Set Crypto Engine Mode
|
|
|
|
*
|
|
|
|
* Possible $engine values:
|
|
|
|
* CRYPT_MODE_INTERNAL, CRYPT_MODE_MCRYPT
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $engine
|
2017-08-07 05:15:50 +00:00
|
|
|
* @access public
|
2014-10-12 04:26:46 +00:00
|
|
|
*/
|
|
|
|
function setCryptoEngine($engine)
|
|
|
|
{
|
|
|
|
$this->crypto_engine = $engine;
|
|
|
|
}
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
/**
|
|
|
|
* Send Identification String First
|
|
|
|
*
|
|
|
|
* https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established,
|
|
|
|
* both sides MUST send an identification string". It does not say which side sends it first. In
|
|
|
|
* theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function sendIdentificationStringFirst()
|
|
|
|
{
|
|
|
|
$this->send_id_string_first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send Identification String Last
|
|
|
|
*
|
|
|
|
* https://tools.ietf.org/html/rfc4253#section-4.2 says "when the connection has been established,
|
|
|
|
* both sides MUST send an identification string". It does not say which side sends it first. In
|
|
|
|
* theory it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function sendIdentificationStringLast()
|
|
|
|
{
|
|
|
|
$this->send_id_string_first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send SSH_MSG_KEXINIT First
|
|
|
|
*
|
|
|
|
* https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending
|
|
|
|
* sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory
|
|
|
|
* it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function sendKEXINITFirst()
|
|
|
|
{
|
|
|
|
$this->send_kex_first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send SSH_MSG_KEXINIT Last
|
|
|
|
*
|
|
|
|
* https://tools.ietf.org/html/rfc4253#section-7.1 says "key exchange begins by each sending
|
|
|
|
* sending the [SSH_MSG_KEXINIT] packet". It does not say which side sends it first. In theory
|
|
|
|
* it shouldn't matter but it is a fact of life that some SSH servers are simply buggy
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function sendKEXINITLast()
|
|
|
|
{
|
|
|
|
$this->send_kex_first = false;
|
|
|
|
}
|
|
|
|
|
2014-04-07 04:56:21 +00:00
|
|
|
/**
|
|
|
|
* Connect to an SSHv2 server
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2014-04-07 04:56:21 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2014-04-07 04:45:25 +00:00
|
|
|
function _connect()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
if ($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_CONSTRUCTOR;
|
|
|
|
|
2015-03-19 12:53:19 +00:00
|
|
|
$this->curTimeout = $this->timeout;
|
|
|
|
|
2014-04-07 04:45:25 +00:00
|
|
|
$this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5
|
|
|
|
|
2015-07-17 17:30:44 +00:00
|
|
|
if (!is_resource($this->fsock)) {
|
|
|
|
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
|
2016-11-20 17:08:53 +00:00
|
|
|
// with stream_select a timeout of 0 means that no timeout takes place;
|
|
|
|
// with fsockopen a timeout of 0 means that you instantly timeout
|
|
|
|
// to resolve this incompatibility a timeout of 100,000 will be used for fsockopen if timeout is 0
|
|
|
|
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout == 0 ? 100000 : $this->curTimeout);
|
2015-07-17 17:30:44 +00:00
|
|
|
if (!$this->fsock) {
|
2015-07-17 18:08:24 +00:00
|
|
|
$host = $this->host . ':' . $this->port;
|
2015-07-17 17:30:44 +00:00
|
|
|
user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
|
2012-06-23 22:16:42 +00:00
|
|
|
|
2018-05-19 11:26:22 +00:00
|
|
|
if ($this->curTimeout) {
|
|
|
|
$this->curTimeout-= $elapsed;
|
|
|
|
if ($this->curTimeout < 0) {
|
|
|
|
$this->is_timeout = true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-17 17:30:44 +00:00
|
|
|
}
|
2012-06-23 22:16:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:11:57 +00:00
|
|
|
$this->identifier = $this->_generate_identifier();
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
if ($this->send_id_string_first) {
|
|
|
|
fputs($this->fsock, $this->identifier . "\r\n");
|
|
|
|
}
|
2016-11-08 15:11:57 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/* According to the SSH2 specs,
|
|
|
|
|
|
|
|
"The server MAY send other lines of data before sending the version
|
|
|
|
string. Each line SHOULD be terminated by a Carriage Return and Line
|
|
|
|
Feed. Such lines MUST NOT begin with "SSH-", and SHOULD be encoded
|
|
|
|
in ISO-10646 UTF-8 [RFC3629] (language is not specified). Clients
|
|
|
|
MUST be able to process such lines." */
|
|
|
|
$temp = '';
|
2010-02-27 23:34:46 +00:00
|
|
|
$extra = '';
|
2007-07-25 21:49:33 +00:00
|
|
|
while (!feof($this->fsock) && !preg_match('#^SSH-(\d\.\d+)#', $temp, $matches)) {
|
2007-07-23 05:21:39 +00:00
|
|
|
if (substr($temp, -2) == "\r\n") {
|
2010-02-27 23:34:46 +00:00
|
|
|
$extra.= $temp;
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = '';
|
|
|
|
}
|
2015-03-19 12:53:19 +00:00
|
|
|
|
|
|
|
if ($this->curTimeout) {
|
|
|
|
if ($this->curTimeout < 0) {
|
|
|
|
$this->is_timeout = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$read = array($this->fsock);
|
|
|
|
$write = $except = null;
|
2015-03-20 03:39:43 +00:00
|
|
|
$start = strtok(microtime(), ' ') + strtok('');
|
2015-03-19 12:53:19 +00:00
|
|
|
$sec = floor($this->curTimeout);
|
|
|
|
$usec = 1000000 * ($this->curTimeout - $sec);
|
|
|
|
// on windows this returns a "Warning: Invalid CRT parameters detected" error
|
|
|
|
// the !count() is done as a workaround for <https://bugs.php.net/42682>
|
|
|
|
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
|
|
|
|
$this->is_timeout = true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-20 03:39:43 +00:00
|
|
|
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
|
2015-03-19 12:53:19 +00:00
|
|
|
$this->curTimeout-= $elapsed;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp.= fgets($this->fsock, 255);
|
|
|
|
}
|
2009-10-17 03:49:16 +00:00
|
|
|
|
2010-05-10 16:16:35 +00:00
|
|
|
if (feof($this->fsock)) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2010-05-10 16:16:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:49:16 +00:00
|
|
|
if (defined('NET_SSH2_LOGGING')) {
|
2013-01-09 04:09:27 +00:00
|
|
|
$this->_append_log('<-', $extra . $temp);
|
|
|
|
$this->_append_log('->', $this->identifier . "\r\n");
|
2009-10-17 03:49:16 +00:00
|
|
|
}
|
|
|
|
|
2010-04-27 21:29:36 +00:00
|
|
|
$this->server_identifier = trim($temp, "\r\n");
|
2013-01-12 16:46:19 +00:00
|
|
|
if (strlen($extra)) {
|
2018-08-21 01:50:07 +00:00
|
|
|
$this->errors[] = $extra;
|
2010-02-27 23:34:46 +00:00
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2017-08-25 03:01:40 +00:00
|
|
|
if (version_compare($matches[1], '1.99', '<')) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error("Cannot connect to SSH $matches[1] servers");
|
2014-04-07 04:45:25 +00:00
|
|
|
return false;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
if (!$this->send_id_string_first) {
|
|
|
|
fputs($this->fsock, $this->identifier . "\r\n");
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
if (!$this->send_kex_first) {
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2017-08-07 05:15:50 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($response) || ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
|
|
|
|
user_error('Expected SSH_MSG_KEXINIT');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->_key_exchange($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
if ($this->send_kex_first && !$this->_key_exchange()) {
|
2014-04-07 04:45:25 +00:00
|
|
|
return false;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-21 21:41:39 +00:00
|
|
|
$this->bitmap|= NET_SSH2_MASK_CONNECTED;
|
2014-04-07 04:45:25 +00:00
|
|
|
|
|
|
|
return true;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 19:17:10 +00:00
|
|
|
/**
|
|
|
|
* Generates the SSH identifier
|
2013-12-28 19:47:24 +00:00
|
|
|
*
|
2013-12-28 17:16:09 +00:00
|
|
|
* You should overwrite this method in your own class if you want to use another identifier
|
2013-12-16 19:17:10 +00:00
|
|
|
*
|
|
|
|
* @access protected
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2013-12-16 19:17:10 +00:00
|
|
|
*/
|
|
|
|
function _generate_identifier()
|
|
|
|
{
|
2015-12-05 16:27:51 +00:00
|
|
|
$identifier = 'SSH-2.0-phpseclib_1.0';
|
2013-12-16 19:17:10 +00:00
|
|
|
|
|
|
|
$ext = array();
|
2014-12-30 04:10:26 +00:00
|
|
|
if (extension_loaded('openssl')) {
|
|
|
|
$ext[] = 'openssl';
|
|
|
|
} elseif (extension_loaded('mcrypt')) {
|
2013-12-16 19:17:10 +00:00
|
|
|
$ext[] = 'mcrypt';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extension_loaded('gmp')) {
|
|
|
|
$ext[] = 'gmp';
|
2013-12-17 17:44:37 +00:00
|
|
|
} elseif (extension_loaded('bcmath')) {
|
|
|
|
$ext[] = 'bcmath';
|
2013-12-16 19:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($ext)) {
|
|
|
|
$identifier .= ' (' . implode(', ', $ext) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $identifier;
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Key Exchange
|
|
|
|
*
|
2017-08-07 05:15:50 +00:00
|
|
|
* @param string $kexinit_payload_server optional
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-08-07 05:15:50 +00:00
|
|
|
function _key_exchange($kexinit_payload_server = false)
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2019-09-08 00:08:00 +00:00
|
|
|
$preferred = $this->preferred;
|
|
|
|
|
|
|
|
$kex_algorithms = isset($preferred['kex']) ?
|
|
|
|
$preferred['kex'] :
|
|
|
|
$this->getSupportedKEXAlgorithms();
|
|
|
|
$server_host_key_algorithms = isset($preferred['hostkey']) ?
|
|
|
|
$preferred['hostkey'] :
|
|
|
|
$this->getSupportedHostKeyAlgorithms();
|
|
|
|
$s2c_encryption_algorithms = isset($preferred['server_to_client']['crypt']) ?
|
|
|
|
$preferred['server_to_client']['crypt'] :
|
|
|
|
$this->getSupportedEncryptionAlgorithms();
|
|
|
|
$c2s_encryption_algorithms = isset($preferred['client_to_server']['crypt']) ?
|
|
|
|
$preferred['client_to_server']['crypt'] :
|
|
|
|
$this->getSupportedEncryptionAlgorithms();
|
|
|
|
$s2c_mac_algorithms = isset($preferred['server_to_client']['mac']) ?
|
|
|
|
$preferred['server_to_client']['mac'] :
|
|
|
|
$this->getSupportedMACAlgorithms();
|
|
|
|
$c2s_mac_algorithms = isset($preferred['client_to_server']['mac']) ?
|
|
|
|
$preferred['client_to_server']['mac'] :
|
|
|
|
$this->getSupportedMACAlgorithms();
|
|
|
|
$s2c_compression_algorithms = isset($preferred['server_to_client']['comp']) ?
|
|
|
|
$preferred['server_to_client']['comp'] :
|
|
|
|
$this->getSupportedCompressionAlgorithms();
|
|
|
|
$c2s_compression_algorithms = isset($preferred['client_to_server']['comp']) ?
|
|
|
|
$preferred['client_to_server']['comp'] :
|
|
|
|
$this->getSupportedCompressionAlgorithms();
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2012-09-17 07:33:03 +00:00
|
|
|
// some SSH servers have buggy implementations of some of the above algorithms
|
2017-06-02 12:46:51 +00:00
|
|
|
switch (true) {
|
|
|
|
case $this->server_identifier == 'SSH-2.0-SSHD':
|
|
|
|
case substr($this->server_identifier, 0, 13) == 'SSH-2.0-DLINK':
|
2019-09-08 00:08:00 +00:00
|
|
|
if (!isset($preferred['server_to_client']['mac'])) {
|
|
|
|
$s2c_mac_algorithms = array_values(array_diff(
|
|
|
|
$s2c_mac_algorithms,
|
|
|
|
array('hmac-sha1-96', 'hmac-md5-96')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if (!isset($preferred['client_to_server']['mac'])) {
|
|
|
|
$c2s_mac_algorithms = array_values(array_diff(
|
|
|
|
$c2s_mac_algorithms,
|
|
|
|
array('hmac-sha1-96', 'hmac-md5-96')
|
|
|
|
));
|
|
|
|
}
|
2012-09-17 07:33:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$str_kex_algorithms = implode(',', $kex_algorithms);
|
|
|
|
$str_server_host_key_algorithms = implode(',', $server_host_key_algorithms);
|
|
|
|
$encryption_algorithms_server_to_client = implode(',', $s2c_encryption_algorithms);
|
|
|
|
$encryption_algorithms_client_to_server = implode(',', $c2s_encryption_algorithms);
|
|
|
|
$mac_algorithms_server_to_client = implode(',', $s2c_mac_algorithms);
|
|
|
|
$mac_algorithms_client_to_server = implode(',', $c2s_mac_algorithms);
|
|
|
|
$compression_algorithms_server_to_client = implode(',', $s2c_compression_algorithms);
|
|
|
|
$compression_algorithms_client_to_server = implode(',', $c2s_compression_algorithms);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2012-12-16 08:20:16 +00:00
|
|
|
$client_cookie = crypt_random_string(16);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
$kexinit_payload_client = pack(
|
|
|
|
'Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN',
|
|
|
|
NET_SSH2_MSG_KEXINIT,
|
|
|
|
$client_cookie,
|
|
|
|
strlen($str_kex_algorithms),
|
|
|
|
$str_kex_algorithms,
|
|
|
|
strlen($str_server_host_key_algorithms),
|
|
|
|
$str_server_host_key_algorithms,
|
|
|
|
strlen($encryption_algorithms_client_to_server),
|
|
|
|
$encryption_algorithms_client_to_server,
|
|
|
|
strlen($encryption_algorithms_server_to_client),
|
|
|
|
$encryption_algorithms_server_to_client,
|
|
|
|
strlen($mac_algorithms_client_to_server),
|
|
|
|
$mac_algorithms_client_to_server,
|
|
|
|
strlen($mac_algorithms_server_to_client),
|
|
|
|
$mac_algorithms_server_to_client,
|
|
|
|
strlen($compression_algorithms_client_to_server),
|
|
|
|
$compression_algorithms_client_to_server,
|
|
|
|
strlen($compression_algorithms_server_to_client),
|
|
|
|
$compression_algorithms_server_to_client,
|
|
|
|
0,
|
|
|
|
'',
|
|
|
|
0,
|
|
|
|
'',
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->send_kex_first) {
|
|
|
|
if (!$this->_send_binary_packet($kexinit_payload_client)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$kexinit_payload_server = $this->_get_binary_packet();
|
|
|
|
if ($kexinit_payload_server === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2017-08-07 05:15:50 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($kexinit_payload_server) || ord($kexinit_payload_server[0]) != NET_SSH2_MSG_KEXINIT) {
|
|
|
|
user_error('Expected SSH_MSG_KEXINIT');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
$response = $kexinit_payload_server;
|
|
|
|
$this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT)
|
2009-11-26 06:41:34 +00:00
|
|
|
$server_cookie = $this->_string_shift($response, 16);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->server_host_key_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->encryption_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->encryption_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->mac_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->mac_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->compression_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->compression_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->languages_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->languages_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Cfirst_kex_packet_follows', $this->_string_shift($response, 1)));
|
2007-07-23 05:21:39 +00:00
|
|
|
$first_kex_packet_follows = $first_kex_packet_follows != 0;
|
|
|
|
|
2017-08-07 05:15:50 +00:00
|
|
|
if (!$this->send_kex_first && !$this->_send_binary_packet($kexinit_payload_client)) {
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange
|
|
|
|
// we don't initialize any crypto-objects, yet - we do that, later. for now, we need the lengths to make the
|
|
|
|
// diffie-hellman key exchange as fast as possible
|
2019-09-08 00:08:00 +00:00
|
|
|
$decrypt = $this->_array_intersect_first($s2c_encryption_algorithms, $this->encryption_algorithms_server_to_client);
|
2015-07-24 10:31:50 +00:00
|
|
|
$decryptKeyLength = $this->_encryption_algorithm_to_key_size($decrypt);
|
|
|
|
if ($decryptKeyLength === null) {
|
|
|
|
user_error('No compatible server to client encryption algorithms found');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$encrypt = $this->_array_intersect_first($c2s_encryption_algorithms, $this->encryption_algorithms_client_to_server);
|
2015-07-24 10:31:50 +00:00
|
|
|
$encryptKeyLength = $this->_encryption_algorithm_to_key_size($encrypt);
|
|
|
|
if ($encryptKeyLength === null) {
|
|
|
|
user_error('No compatible client to server encryption algorithms found');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyLength = $decryptKeyLength > $encryptKeyLength ? $decryptKeyLength : $encryptKeyLength;
|
|
|
|
|
|
|
|
// through diffie-hellman key exchange a symmetric key is obtained
|
2019-09-08 00:08:00 +00:00
|
|
|
$this->kex_algorithm = $kex_algorithm = $this->_array_intersect_first($kex_algorithms, $this->kex_algorithms);
|
2015-07-17 16:20:42 +00:00
|
|
|
if ($kex_algorithm === false) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('No compatible key exchange algorithms found');
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
2015-07-17 16:20:42 +00:00
|
|
|
if (strpos($kex_algorithm, 'diffie-hellman-group-exchange') === 0) {
|
2015-07-15 01:52:31 +00:00
|
|
|
$dh_group_sizes_packed = pack(
|
|
|
|
'NNN',
|
2015-06-24 22:50:00 +00:00
|
|
|
$this->kex_dh_group_size_min,
|
|
|
|
$this->kex_dh_group_size_preferred,
|
|
|
|
$this->kex_dh_group_size_max
|
|
|
|
);
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'Ca*',
|
2015-06-24 22:50:00 +00:00
|
|
|
NET_SSH2_MSG_KEXDH_GEX_REQUEST,
|
|
|
|
$dh_group_sizes_packed
|
|
|
|
);
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2015-06-24 22:50:00 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 22:50:00 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
|
|
|
if ($type != NET_SSH2_MSG_KEXDH_GEX_GROUP) {
|
|
|
|
user_error('Expected SSH_MSG_KEX_DH_GEX_GROUP');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 22:50:00 +00:00
|
|
|
extract(unpack('NprimeLength', $this->_string_shift($response, 4)));
|
|
|
|
$primeBytes = $this->_string_shift($response, $primeLength);
|
|
|
|
$prime = new Math_BigInteger($primeBytes, -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 22:50:00 +00:00
|
|
|
extract(unpack('NgLength', $this->_string_shift($response, 4)));
|
|
|
|
$gBytes = $this->_string_shift($response, $gLength);
|
|
|
|
$g = new Math_BigInteger($gBytes, -256);
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$exchange_hash_rfc4419 = pack(
|
|
|
|
'a*Na*Na*',
|
2015-06-24 22:50:00 +00:00
|
|
|
$dh_group_sizes_packed,
|
2015-07-15 01:52:31 +00:00
|
|
|
$primeLength,
|
|
|
|
$primeBytes,
|
|
|
|
$gLength,
|
|
|
|
$gBytes
|
2015-06-24 22:50:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$clientKexInitMessage = NET_SSH2_MSG_KEXDH_GEX_INIT;
|
|
|
|
$serverKexReplyMessage = NET_SSH2_MSG_KEXDH_GEX_REPLY;
|
|
|
|
} else {
|
2015-07-17 16:20:42 +00:00
|
|
|
switch ($kex_algorithm) {
|
2015-06-24 22:50:00 +00:00
|
|
|
// see http://tools.ietf.org/html/rfc2409#section-6.2 and
|
|
|
|
// http://tools.ietf.org/html/rfc2412, appendex E
|
|
|
|
case 'diffie-hellman-group1-sha1':
|
|
|
|
$prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
|
|
|
|
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
|
|
|
|
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
|
|
|
|
break;
|
|
|
|
// see http://tools.ietf.org/html/rfc3526#section-3
|
|
|
|
case 'diffie-hellman-group14-sha1':
|
|
|
|
$prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
|
|
|
|
'020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
|
|
|
|
'4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
|
|
|
|
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05' .
|
|
|
|
'98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB' .
|
|
|
|
'9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' .
|
|
|
|
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' .
|
|
|
|
'3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1
|
|
|
|
// the generator field element is 2 (decimal) and the hash function is sha1.
|
|
|
|
$g = new Math_BigInteger(2);
|
|
|
|
$prime = new Math_BigInteger($prime, 16);
|
|
|
|
$exchange_hash_rfc4419 = '';
|
|
|
|
$clientKexInitMessage = NET_SSH2_MSG_KEXDH_INIT;
|
|
|
|
$serverKexReplyMessage = NET_SSH2_MSG_KEXDH_REPLY;
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:20:42 +00:00
|
|
|
switch ($kex_algorithm) {
|
2015-06-24 22:50:00 +00:00
|
|
|
case 'diffie-hellman-group-exchange-sha256':
|
|
|
|
$kexHash = new Crypt_Hash('sha256');
|
2013-06-06 22:38:38 +00:00
|
|
|
break;
|
2015-06-24 22:50:00 +00:00
|
|
|
default:
|
|
|
|
$kexHash = new Crypt_Hash('sha1');
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* To increase the speed of the key exchange, both client and server may
|
|
|
|
reduce the size of their private exponents. It should be at least
|
|
|
|
twice as long as the key material that is generated from the shared
|
|
|
|
secret. For more details, see the paper by van Oorschot and Wiener
|
|
|
|
[VAN-OORSCHOT].
|
|
|
|
|
|
|
|
-- http://tools.ietf.org/html/rfc4419#section-6.2 */
|
2013-06-06 22:43:40 +00:00
|
|
|
$one = new Math_BigInteger(1);
|
2013-06-06 23:03:03 +00:00
|
|
|
$keyLength = min($keyLength, $kexHash->getLength());
|
2013-12-23 00:25:00 +00:00
|
|
|
$max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength
|
|
|
|
$max = $max->subtract($one);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-06-06 22:47:59 +00:00
|
|
|
$x = $one->random($one, $max);
|
2013-06-06 22:53:21 +00:00
|
|
|
$e = $g->modPow($x, $prime);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
$eBytes = $e->toBytes(true);
|
2015-06-24 22:50:00 +00:00
|
|
|
$data = pack('CNa*', $clientKexInitMessage, strlen($eBytes), $eBytes);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($data)) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2015-06-24 22:50:00 +00:00
|
|
|
if ($type != $serverKexReplyMessage) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Expected SSH_MSG_KEXDH_REPLY');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$this->server_public_host_key = $server_public_host_key = $this->_string_shift($response, $temp['length']);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$public_key_format = $this->_string_shift($server_public_host_key, $temp['length']);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
|
|
|
$fBytes = $this->_string_shift($response, $temp['length']);
|
|
|
|
$f = new Math_BigInteger($fBytes, -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($response, 4));
|
2010-04-22 16:06:43 +00:00
|
|
|
$this->signature = $this->_string_shift($response, $temp['length']);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($this->signature) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($this->signature, 4));
|
|
|
|
$this->signature_format = $this->_string_shift($this->signature, $temp['length']);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-06-06 22:53:21 +00:00
|
|
|
$key = $f->modPow($x, $prime);
|
2007-07-23 05:21:39 +00:00
|
|
|
$keyBytes = $key->toBytes(true);
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$this->exchange_hash = pack(
|
|
|
|
'Na*Na*Na*Na*Na*a*Na*Na*Na*',
|
|
|
|
strlen($this->identifier),
|
|
|
|
$this->identifier,
|
|
|
|
strlen($this->server_identifier),
|
|
|
|
$this->server_identifier,
|
|
|
|
strlen($kexinit_payload_client),
|
|
|
|
$kexinit_payload_client,
|
|
|
|
strlen($kexinit_payload_server),
|
|
|
|
$kexinit_payload_server,
|
|
|
|
strlen($this->server_public_host_key),
|
|
|
|
$this->server_public_host_key,
|
2015-06-24 22:50:00 +00:00
|
|
|
$exchange_hash_rfc4419,
|
2015-07-15 01:52:31 +00:00
|
|
|
strlen($eBytes),
|
|
|
|
$eBytes,
|
|
|
|
strlen($fBytes),
|
|
|
|
$fBytes,
|
|
|
|
strlen($keyBytes),
|
|
|
|
$keyBytes
|
2010-10-24 01:24:30 +00:00
|
|
|
);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$this->exchange_hash = $kexHash->hash($this->exchange_hash);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2010-10-24 01:24:30 +00:00
|
|
|
if ($this->session_id === false) {
|
|
|
|
$this->session_id = $this->exchange_hash;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 16:20:42 +00:00
|
|
|
$server_host_key_algorithm = $this->_array_intersect_first($server_host_key_algorithms, $this->server_host_key_algorithms);
|
|
|
|
if ($server_host_key_algorithm === false) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('No compatible server host key algorithms found');
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
|
|
|
|
2018-05-27 14:48:44 +00:00
|
|
|
switch ($server_host_key_algorithm) {
|
|
|
|
case 'ssh-dss':
|
|
|
|
$expected_key_format = 'ssh-dss';
|
|
|
|
break;
|
|
|
|
//case 'rsa-sha2-256':
|
|
|
|
//case 'rsa-sha2-512':
|
|
|
|
//case 'ssh-rsa':
|
|
|
|
default:
|
|
|
|
$expected_key_format = 'ssh-rsa';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($public_key_format != $expected_key_format || $this->signature_format != $server_host_key_algorithm) {
|
2019-01-17 03:15:11 +00:00
|
|
|
switch (true) {
|
|
|
|
case $this->signature_format == $server_host_key_algorithm:
|
|
|
|
case $server_host_key_algorithm != 'rsa-sha2-256' && $server_host_key_algorithm != 'rsa-sha2-512':
|
|
|
|
case $this->signature_format != 'ssh-rsa':
|
|
|
|
user_error('Server Host Key Algorithm Mismatch');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'C',
|
2007-07-23 05:21:39 +00:00
|
|
|
NET_SSH2_MSG_NEWKEYS
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2008-05-25 07:28:57 +00:00
|
|
|
if ($type != NET_SSH2_MSG_NEWKEYS) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Expected SSH_MSG_NEWKEYS');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$this->encrypt = $this->_encryption_algorithm_to_crypt_instance($encrypt);
|
|
|
|
$this->decrypt = $this->_encryption_algorithm_to_crypt_instance($decrypt);
|
2017-08-27 00:50:17 +00:00
|
|
|
|
2009-05-16 17:09:37 +00:00
|
|
|
$keyBytes = pack('Na*', strlen($keyBytes), $keyBytes);
|
|
|
|
|
|
|
|
if ($this->encrypt) {
|
2014-10-12 04:26:46 +00:00
|
|
|
if ($this->crypto_engine) {
|
2018-09-08 19:41:28 +00:00
|
|
|
$this->encrypt->setPreferredEngine($this->crypto_engine);
|
2014-10-12 04:26:46 +00:00
|
|
|
}
|
2009-05-16 17:09:37 +00:00
|
|
|
$this->encrypt->enableContinuousBuffer();
|
|
|
|
$this->encrypt->disablePadding();
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
if ($this->encrypt->getBlockLength()) {
|
|
|
|
$this->encrypt_block_size = $this->encrypt->getBlockLength() >> 3;
|
|
|
|
}
|
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'A' . $this->session_id);
|
2009-05-16 17:09:37 +00:00
|
|
|
while ($this->encrypt_block_size > strlen($iv)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
|
|
|
$this->encrypt->setIV(substr($iv, 0, $this->encrypt_block_size));
|
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$key = $kexHash->hash($keyBytes . $this->exchange_hash . 'C' . $this->session_id);
|
2009-05-16 17:09:37 +00:00
|
|
|
while ($encryptKeyLength > strlen($key)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
|
|
|
$this->encrypt->setKey(substr($key, 0, $encryptKeyLength));
|
2019-09-08 00:08:00 +00:00
|
|
|
|
|
|
|
$this->encrypt->name = $decrypt;
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->decrypt) {
|
2014-10-12 04:26:46 +00:00
|
|
|
if ($this->crypto_engine) {
|
2018-09-08 19:41:28 +00:00
|
|
|
$this->decrypt->setPreferredEngine($this->crypto_engine);
|
2014-10-12 04:26:46 +00:00
|
|
|
}
|
2009-05-16 17:09:37 +00:00
|
|
|
$this->decrypt->enableContinuousBuffer();
|
|
|
|
$this->decrypt->disablePadding();
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
if ($this->decrypt->getBlockLength()) {
|
|
|
|
$this->decrypt_block_size = $this->decrypt->getBlockLength() >> 3;
|
|
|
|
}
|
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'B' . $this->session_id);
|
2009-05-16 17:09:37 +00:00
|
|
|
while ($this->decrypt_block_size > strlen($iv)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
|
|
|
$this->decrypt->setIV(substr($iv, 0, $this->decrypt_block_size));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$key = $kexHash->hash($keyBytes . $this->exchange_hash . 'D' . $this->session_id);
|
2009-05-16 17:09:37 +00:00
|
|
|
while ($decryptKeyLength > strlen($key)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
|
|
|
$this->decrypt->setKey(substr($key, 0, $decryptKeyLength));
|
2019-09-08 00:08:00 +00:00
|
|
|
|
|
|
|
$this->decrypt->name = $decrypt;
|
2009-05-16 17:09:37 +00:00
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2010-02-09 06:10:26 +00:00
|
|
|
/* The "arcfour128" algorithm is the RC4 cipher, as described in
|
|
|
|
[SCHNEIER], using a 128-bit key. The first 1536 bytes of keystream
|
|
|
|
generated by the cipher MUST be discarded, and the first byte of the
|
|
|
|
first encrypted packet MUST be encrypted using the 1537th byte of
|
|
|
|
keystream.
|
|
|
|
|
|
|
|
-- http://tools.ietf.org/html/rfc4345#section-4 */
|
|
|
|
if ($encrypt == 'arcfour128' || $encrypt == 'arcfour256') {
|
|
|
|
$this->encrypt->encrypt(str_repeat("\0", 1536));
|
|
|
|
}
|
|
|
|
if ($decrypt == 'arcfour128' || $decrypt == 'arcfour256') {
|
|
|
|
$this->decrypt->decrypt(str_repeat("\0", 1536));
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$mac_algorithm = $this->_array_intersect_first($c2s_mac_algorithms, $this->mac_algorithms_client_to_server);
|
2015-07-17 16:20:42 +00:00
|
|
|
if ($mac_algorithm === false) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('No compatible client to server message authentication algorithms found');
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:20:42 +00:00
|
|
|
$createKeyLength = 0; // ie. $mac_algorithm == 'none'
|
|
|
|
switch ($mac_algorithm) {
|
2014-07-25 10:28:08 +00:00
|
|
|
case 'hmac-sha2-256':
|
|
|
|
$this->hmac_create = new Crypt_Hash('sha256');
|
|
|
|
$createKeyLength = 32;
|
|
|
|
break;
|
2007-07-23 05:21:39 +00:00
|
|
|
case 'hmac-sha1':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_create = new Crypt_Hash('sha1');
|
2007-07-23 05:21:39 +00:00
|
|
|
$createKeyLength = 20;
|
|
|
|
break;
|
|
|
|
case 'hmac-sha1-96':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_create = new Crypt_Hash('sha1-96');
|
2007-07-23 05:21:39 +00:00
|
|
|
$createKeyLength = 20;
|
|
|
|
break;
|
|
|
|
case 'hmac-md5':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_create = new Crypt_Hash('md5');
|
2007-07-23 05:21:39 +00:00
|
|
|
$createKeyLength = 16;
|
|
|
|
break;
|
|
|
|
case 'hmac-md5-96':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_create = new Crypt_Hash('md5-96');
|
2007-07-23 05:21:39 +00:00
|
|
|
$createKeyLength = 16;
|
|
|
|
}
|
2019-09-08 00:08:00 +00:00
|
|
|
$this->hmac_create->name = $mac_algorithm;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$mac_algorithm = $this->_array_intersect_first($s2c_mac_algorithms, $this->mac_algorithms_server_to_client);
|
2015-07-17 16:20:42 +00:00
|
|
|
if ($mac_algorithm === false) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('No compatible server to client message authentication algorithms found');
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
|
|
|
|
2009-02-16 22:22:13 +00:00
|
|
|
$checkKeyLength = 0;
|
|
|
|
$this->hmac_size = 0;
|
2015-07-17 16:20:42 +00:00
|
|
|
switch ($mac_algorithm) {
|
2014-07-25 10:28:08 +00:00
|
|
|
case 'hmac-sha2-256':
|
|
|
|
$this->hmac_check = new Crypt_Hash('sha256');
|
|
|
|
$checkKeyLength = 32;
|
|
|
|
$this->hmac_size = 32;
|
|
|
|
break;
|
2007-07-23 05:21:39 +00:00
|
|
|
case 'hmac-sha1':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_check = new Crypt_Hash('sha1');
|
2007-07-23 05:21:39 +00:00
|
|
|
$checkKeyLength = 20;
|
|
|
|
$this->hmac_size = 20;
|
|
|
|
break;
|
|
|
|
case 'hmac-sha1-96':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_check = new Crypt_Hash('sha1-96');
|
2007-07-23 05:21:39 +00:00
|
|
|
$checkKeyLength = 20;
|
|
|
|
$this->hmac_size = 12;
|
|
|
|
break;
|
|
|
|
case 'hmac-md5':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_check = new Crypt_Hash('md5');
|
2007-07-23 05:21:39 +00:00
|
|
|
$checkKeyLength = 16;
|
|
|
|
$this->hmac_size = 16;
|
|
|
|
break;
|
|
|
|
case 'hmac-md5-96':
|
2009-02-16 22:22:13 +00:00
|
|
|
$this->hmac_check = new Crypt_Hash('md5-96');
|
2007-07-23 05:21:39 +00:00
|
|
|
$checkKeyLength = 16;
|
|
|
|
$this->hmac_size = 12;
|
|
|
|
}
|
2019-09-08 00:08:00 +00:00
|
|
|
$this->hmac_check->name = $mac_algorithm;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$key = $kexHash->hash($keyBytes . $this->exchange_hash . 'E' . $this->session_id);
|
2007-07-23 05:21:39 +00:00
|
|
|
while ($createKeyLength > strlen($key)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
$this->hmac_create->setKey(substr($key, 0, $createKeyLength));
|
|
|
|
|
2013-06-06 22:38:38 +00:00
|
|
|
$key = $kexHash->hash($keyBytes . $this->exchange_hash . 'F' . $this->session_id);
|
2007-07-23 05:21:39 +00:00
|
|
|
while ($checkKeyLength > strlen($key)) {
|
2013-06-06 22:38:38 +00:00
|
|
|
$key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
$this->hmac_check->setKey(substr($key, 0, $checkKeyLength));
|
2008-05-26 19:42:01 +00:00
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$compression_algorithm = $this->_array_intersect_first($c2s_compression_algorithms, $this->compression_algorithms_client_to_server);
|
2015-07-17 16:20:42 +00:00
|
|
|
if ($compression_algorithm === false) {
|
2019-09-08 00:08:00 +00:00
|
|
|
user_error('No compatible client to server compression algorithms found');
|
2009-05-16 17:09:37 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
2019-09-08 00:08:00 +00:00
|
|
|
//$this->decompress = $compression_algorithm == 'zlib';
|
2009-05-16 17:09:37 +00:00
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
$compression_algorithm = $this->_array_intersect_first($s2c_compression_algorithms, $this->compression_algorithms_client_to_server);
|
2015-07-17 16:20:42 +00:00
|
|
|
if ($compression_algorithm === false) {
|
2019-09-08 00:08:00 +00:00
|
|
|
user_error('No compatible server to client compression algorithms found');
|
2009-05-16 17:09:37 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
2019-09-08 00:08:00 +00:00
|
|
|
//$this->compress = $compression_algorithm == 'zlib';
|
2009-05-30 16:40:31 +00:00
|
|
|
|
|
|
|
return true;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 10:31:50 +00:00
|
|
|
/**
|
|
|
|
* Maps an encryption algorithm name to the number of key bytes.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $algorithm Name of the encryption algorithm
|
2015-09-06 11:28:08 +00:00
|
|
|
* @return int|null Number of bytes as an integer or null for unknown
|
2015-07-24 10:31:50 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _encryption_algorithm_to_key_size($algorithm)
|
|
|
|
{
|
2017-08-27 00:50:17 +00:00
|
|
|
if ($this->bad_key_size_fix && $this->_bad_algorithm_candidate($algorithm)) {
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:31:50 +00:00
|
|
|
switch ($algorithm) {
|
|
|
|
case 'none':
|
|
|
|
return 0;
|
|
|
|
case 'aes128-cbc':
|
|
|
|
case 'aes128-ctr':
|
|
|
|
case 'arcfour':
|
|
|
|
case 'arcfour128':
|
|
|
|
case 'blowfish-cbc':
|
|
|
|
case 'blowfish-ctr':
|
|
|
|
case 'twofish128-cbc':
|
|
|
|
case 'twofish128-ctr':
|
|
|
|
return 16;
|
|
|
|
case '3des-cbc':
|
|
|
|
case '3des-ctr':
|
|
|
|
case 'aes192-cbc':
|
|
|
|
case 'aes192-ctr':
|
|
|
|
case 'twofish192-cbc':
|
|
|
|
case 'twofish192-ctr':
|
|
|
|
return 24;
|
|
|
|
case 'aes256-cbc':
|
|
|
|
case 'aes256-ctr':
|
|
|
|
case 'arcfour256':
|
|
|
|
case 'twofish-cbc':
|
|
|
|
case 'twofish256-cbc':
|
|
|
|
case 'twofish256-ctr':
|
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
/**
|
|
|
|
* Maps an encryption algorithm name to an instance of a subclass of
|
|
|
|
* \phpseclib\Crypt\Base.
|
|
|
|
*
|
|
|
|
* @param string $algorithm Name of the encryption algorithm
|
|
|
|
* @return mixed Instance of \phpseclib\Crypt\Base or null for unknown
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _encryption_algorithm_to_crypt_instance($algorithm)
|
|
|
|
{
|
|
|
|
switch ($algorithm) {
|
|
|
|
case '3des-cbc':
|
|
|
|
if (!class_exists('Crypt_TripleDES')) {
|
|
|
|
include_once 'Crypt/TripleDES.php';
|
|
|
|
}
|
|
|
|
return new Crypt_TripleDES();
|
|
|
|
break;
|
|
|
|
case '3des-ctr':
|
|
|
|
if (!class_exists('Crypt_TripleDES')) {
|
|
|
|
include_once 'Crypt/TripleDES.php';
|
|
|
|
}
|
|
|
|
return new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
|
|
|
|
break;
|
|
|
|
case 'aes256-cbc':
|
|
|
|
case 'aes192-cbc':
|
|
|
|
case 'aes128-cbc':
|
|
|
|
if (!class_exists('Crypt_Rijndael')) {
|
|
|
|
include_once 'Crypt/Rijndael.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Rijndael();
|
|
|
|
break;
|
|
|
|
case 'aes256-ctr':
|
|
|
|
case 'aes192-ctr':
|
|
|
|
case 'aes128-ctr':
|
|
|
|
if (!class_exists('Crypt_Rijndael')) {
|
|
|
|
include_once 'Crypt/Rijndael.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CTR);
|
|
|
|
$this->encrypt_block_size = 16; // eg. 128 / 8
|
|
|
|
break;
|
|
|
|
case 'blowfish-cbc':
|
|
|
|
if (!class_exists('Crypt_Blowfish')) {
|
|
|
|
include_once 'Crypt/Blowfish.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Blowfish();
|
|
|
|
break;
|
|
|
|
case 'blowfish-ctr':
|
|
|
|
if (!class_exists('Crypt_Blowfish')) {
|
|
|
|
include_once 'Crypt/Blowfish.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
|
|
|
|
break;
|
|
|
|
case 'twofish128-cbc':
|
|
|
|
case 'twofish192-cbc':
|
|
|
|
case 'twofish256-cbc':
|
|
|
|
case 'twofish-cbc':
|
|
|
|
if (!class_exists('Crypt_Twofish')) {
|
|
|
|
include_once 'Crypt/Twofish.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Twofish();
|
|
|
|
break;
|
|
|
|
case 'twofish128-ctr':
|
|
|
|
case 'twofish192-ctr':
|
|
|
|
case 'twofish256-ctr':
|
|
|
|
if (!class_exists('Crypt_Twofish')) {
|
|
|
|
include_once 'Crypt/Twofish.php';
|
|
|
|
}
|
|
|
|
return new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
|
|
|
|
break;
|
|
|
|
case 'arcfour':
|
|
|
|
case 'arcfour128':
|
|
|
|
case 'arcfour256':
|
|
|
|
if (!class_exists('Crypt_RC4')) {
|
|
|
|
include_once 'Crypt/RC4.php';
|
|
|
|
}
|
|
|
|
return new Crypt_RC4();
|
|
|
|
break;
|
|
|
|
case 'none':
|
|
|
|
//return new Crypt_Null();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-27 00:50:17 +00:00
|
|
|
/**
|
|
|
|
* Tests whether or not proposed algorithm has a potential for issues
|
|
|
|
*
|
|
|
|
* @link https://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/ssh2-aesctr-openssh.html
|
|
|
|
* @link https://bugzilla.mindrot.org/show_bug.cgi?id=1291
|
|
|
|
* @param string $algorithm Name of the encryption algorithm
|
|
|
|
* @return bool
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _bad_algorithm_candidate($algorithm)
|
|
|
|
{
|
|
|
|
switch ($algorithm) {
|
|
|
|
case 'arcfour256':
|
|
|
|
case 'aes192-ctr':
|
|
|
|
case 'aes256-ctr':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Login
|
|
|
|
*
|
2013-04-20 19:33:07 +00:00
|
|
|
* The $password parameter can be a plaintext password, a Crypt_RSA object or an array
|
2010-08-28 17:26:22 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param mixed $password
|
|
|
|
* @param mixed $...
|
|
|
|
* @return bool
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_login()
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
2013-04-20 19:33:07 +00:00
|
|
|
*/
|
|
|
|
function login($username)
|
2013-12-23 00:14:49 +00:00
|
|
|
{
|
|
|
|
$args = func_get_args();
|
2018-10-06 06:19:14 +00:00
|
|
|
$this->auth[] = $args;
|
2013-12-23 00:14:49 +00:00
|
|
|
return call_user_func_array(array(&$this, '_login'), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login Helper
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param mixed $password
|
|
|
|
* @param mixed $...
|
|
|
|
* @return bool
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_login_helper()
|
2013-12-23 00:14:49 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _login($username)
|
2013-04-20 19:33:07 +00:00
|
|
|
{
|
2014-04-08 13:48:12 +00:00
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) {
|
|
|
|
if (!$this->_connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:33:07 +00:00
|
|
|
$args = array_slice(func_get_args(), 1);
|
|
|
|
if (empty($args)) {
|
|
|
|
return $this->_login_helper($username);
|
|
|
|
}
|
2013-04-20 19:35:08 +00:00
|
|
|
|
2013-04-20 19:33:07 +00:00
|
|
|
foreach ($args as $arg) {
|
|
|
|
if ($this->_login_helper($username, $arg)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Login Helper
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
2013-04-20 19:33:07 +00:00
|
|
|
* @access private
|
2009-05-16 17:09:37 +00:00
|
|
|
* @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
|
2009-12-03 08:19:00 +00:00
|
|
|
* by sending dummy SSH_MSG_IGNORE messages.
|
2007-07-23 05:21:39 +00:00
|
|
|
*/
|
2013-04-20 19:33:07 +00:00
|
|
|
function _login_helper($username, $password = null)
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2014-04-07 04:45:25 +00:00
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_CONNECTED)) {
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_LOGIN_REQ)) {
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*',
|
|
|
|
NET_SSH2_MSG_SERVICE_REQUEST,
|
|
|
|
strlen('ssh-userauth'),
|
|
|
|
'ssh-userauth'
|
2013-04-20 03:23:06 +00:00
|
|
|
);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2017-08-27 00:50:17 +00:00
|
|
|
if ($this->retry_connect) {
|
|
|
|
$this->retry_connect = false;
|
|
|
|
if (!$this->_connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_login_helper($username, $password);
|
|
|
|
}
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-04-20 03:23:06 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-20 03:23:06 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) {
|
|
|
|
user_error('Expected SSH_MSG_SERVICE_ACCEPT');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN_REQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($this->last_interactive_response)) {
|
2013-04-20 19:35:08 +00:00
|
|
|
return !is_string($password) && !is_array($password) ? false : $this->_keyboard_interactive_process($password);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 08:19:00 +00:00
|
|
|
// although PHP5's get_class() preserves the case, PHP4's does not
|
2014-02-10 06:04:16 +00:00
|
|
|
if (is_object($password)) {
|
|
|
|
switch (strtolower(get_class($password))) {
|
|
|
|
case 'crypt_rsa':
|
|
|
|
return $this->_privatekey_login($username, $password);
|
|
|
|
case 'system_ssh_agent':
|
|
|
|
return $this->_ssh_agent_login($username, $password);
|
|
|
|
}
|
2009-12-03 08:19:00 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 19:35:08 +00:00
|
|
|
if (is_array($password)) {
|
|
|
|
if ($this->_keyboard_interactive_login($username, $password)) {
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-22 20:25:57 +00:00
|
|
|
if (!isset($password)) {
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*Na*Na*',
|
|
|
|
NET_SSH2_MSG_USERAUTH_REQUEST,
|
|
|
|
strlen($username),
|
|
|
|
$username,
|
|
|
|
strlen('ssh-connection'),
|
|
|
|
'ssh-connection',
|
|
|
|
strlen('none'),
|
|
|
|
'none'
|
2012-11-22 20:25:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2012-11-22 20:25:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-22 20:25:57 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_USERAUTH_SUCCESS:
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN;
|
|
|
|
return true;
|
|
|
|
//case NET_SSH2_MSG_USERAUTH_FAILURE:
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*Na*Na*CNa*',
|
|
|
|
NET_SSH2_MSG_USERAUTH_REQUEST,
|
|
|
|
strlen($username),
|
|
|
|
$username,
|
|
|
|
strlen('ssh-connection'),
|
|
|
|
'ssh-connection',
|
|
|
|
strlen('password'),
|
|
|
|
'password',
|
|
|
|
0,
|
|
|
|
strlen($password),
|
|
|
|
$password
|
2007-07-23 05:21:39 +00:00
|
|
|
);
|
|
|
|
|
2013-09-12 13:45:13 +00:00
|
|
|
// remove the username and password from the logged packet
|
|
|
|
if (!defined('NET_SSH2_LOGGING')) {
|
2013-12-03 17:54:43 +00:00
|
|
|
$logged = null;
|
2013-09-12 13:45:13 +00:00
|
|
|
} else {
|
2015-07-15 01:52:31 +00:00
|
|
|
$logged = pack(
|
|
|
|
'CNa*Na*Na*CNa*',
|
|
|
|
NET_SSH2_MSG_USERAUTH_REQUEST,
|
|
|
|
strlen('username'),
|
|
|
|
'username',
|
|
|
|
strlen('ssh-connection'),
|
|
|
|
'ssh-connection',
|
|
|
|
strlen('password'),
|
|
|
|
'password',
|
|
|
|
0,
|
|
|
|
strlen('password'),
|
|
|
|
'password'
|
2009-05-23 14:42:17 +00:00
|
|
|
);
|
2013-09-12 13:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet, $logged)) {
|
|
|
|
return false;
|
2009-05-23 14:42:17 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 21:49:33 +00:00
|
|
|
$response = $this->_get_binary_packet();
|
2007-07-23 05:21:39 +00:00
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: // in theory, the password can be changed
|
2009-12-03 08:19:00 +00:00
|
|
|
if (defined('NET_SSH2_LOGGING')) {
|
|
|
|
$this->message_number_log[count($this->message_number_log) - 1] = 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ';
|
|
|
|
}
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
2018-08-21 01:50:07 +00:00
|
|
|
$this->errors[] = 'SSH_MSG_USERAUTH_PASSWD_CHANGEREQ: ' . $this->_string_shift($response, $length);
|
2007-07-25 21:49:33 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
|
2009-12-03 08:19:00 +00:00
|
|
|
case NET_SSH2_MSG_USERAUTH_FAILURE:
|
2010-08-28 17:26:22 +00:00
|
|
|
// can we use keyboard-interactive authentication? if not then either the login is bad or the server employees
|
|
|
|
// multi-factor authentication
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$auth_methods = explode(',', $this->_string_shift($response, $length));
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-20 03:23:06 +00:00
|
|
|
extract(unpack('Cpartial_success', $this->_string_shift($response, 1)));
|
|
|
|
$partial_success = $partial_success != 0;
|
|
|
|
|
|
|
|
if (!$partial_success && in_array('keyboard-interactive', $auth_methods)) {
|
2010-09-12 22:12:45 +00:00
|
|
|
if ($this->_keyboard_interactive_login($username, $password)) {
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
2009-12-03 08:19:00 +00:00
|
|
|
case NET_SSH2_MSG_USERAUTH_SUCCESS:
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-28 17:26:22 +00:00
|
|
|
/**
|
|
|
|
* Login via keyboard-interactive authentication
|
|
|
|
*
|
|
|
|
* See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
2010-08-28 17:26:22 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _keyboard_interactive_login($username, $password)
|
|
|
|
{
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*Na*Na*Na*Na*',
|
|
|
|
NET_SSH2_MSG_USERAUTH_REQUEST,
|
|
|
|
strlen($username),
|
|
|
|
$username,
|
|
|
|
strlen('ssh-connection'),
|
|
|
|
'ssh-connection',
|
|
|
|
strlen('keyboard-interactive'),
|
|
|
|
'keyboard-interactive',
|
|
|
|
0,
|
|
|
|
'',
|
|
|
|
0,
|
|
|
|
''
|
2010-08-28 17:26:22 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_keyboard_interactive_process($password);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the keyboard-interactive requests / responses.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $responses...
|
|
|
|
* @return bool
|
2010-08-28 17:26:22 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _keyboard_interactive_process()
|
|
|
|
{
|
|
|
|
$responses = func_get_args();
|
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
if (strlen($this->last_interactive_response)) {
|
|
|
|
$response = $this->last_interactive_response;
|
|
|
|
} else {
|
|
|
|
$orig = $response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-04-20 03:23:06 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_USERAUTH_INFO_REQUEST:
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$this->_string_shift($response, $length); // name; may be empty
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$this->_string_shift($response, $length); // instruction; may be empty
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$this->_string_shift($response, $length); // language tag; may be empty
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
extract(unpack('Nnum_prompts', $this->_string_shift($response, 4)));
|
2013-04-20 19:35:08 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i < count($responses); $i++) {
|
|
|
|
if (is_array($responses[$i])) {
|
|
|
|
foreach ($responses[$i] as $key => $value) {
|
|
|
|
$this->keyboard_requests_responses[$key] = $value;
|
|
|
|
}
|
|
|
|
unset($responses[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$responses = array_values($responses);
|
|
|
|
|
|
|
|
if (isset($this->keyboard_requests_responses)) {
|
|
|
|
for ($i = 0; $i < $num_prompts; $i++) {
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-20 19:35:08 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
// prompt - ie. "Password: "; must not be empty
|
|
|
|
$prompt = $this->_string_shift($response, $length);
|
|
|
|
//$echo = $this->_string_shift($response) != chr(0);
|
|
|
|
foreach ($this->keyboard_requests_responses as $key => $value) {
|
|
|
|
if (substr($prompt, 0, strlen($key)) == $key) {
|
|
|
|
$responses[] = $value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 03:23:06 +00:00
|
|
|
// see http://tools.ietf.org/html/rfc4256#section-3.2
|
|
|
|
if (strlen($this->last_interactive_response)) {
|
|
|
|
$this->last_interactive_response = '';
|
2015-07-15 01:52:31 +00:00
|
|
|
} elseif (defined('NET_SSH2_LOGGING')) {
|
2013-04-20 03:23:06 +00:00
|
|
|
$this->message_number_log[count($this->message_number_log) - 1] = str_replace(
|
|
|
|
'UNKNOWN',
|
|
|
|
'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
|
|
|
|
$this->message_number_log[count($this->message_number_log) - 1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($responses) && $num_prompts) {
|
|
|
|
$this->last_interactive_response = $orig;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-28 17:26:22 +00:00
|
|
|
/*
|
|
|
|
After obtaining the requested information from the user, the client
|
|
|
|
MUST respond with an SSH_MSG_USERAUTH_INFO_RESPONSE message.
|
|
|
|
*/
|
|
|
|
// see http://tools.ietf.org/html/rfc4256#section-3.4
|
2010-08-29 03:27:02 +00:00
|
|
|
$packet = $logged = pack('CN', NET_SSH2_MSG_USERAUTH_INFO_RESPONSE, count($responses));
|
2010-08-28 17:26:22 +00:00
|
|
|
for ($i = 0; $i < count($responses); $i++) {
|
|
|
|
$packet.= pack('Na*', strlen($responses[$i]), $responses[$i]);
|
2010-08-29 03:27:02 +00:00
|
|
|
$logged.= pack('Na*', strlen('dummy-answer'), 'dummy-answer');
|
2010-08-28 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 13:45:13 +00:00
|
|
|
if (!$this->_send_binary_packet($packet, $logged)) {
|
2010-08-28 17:26:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-12 13:45:13 +00:00
|
|
|
if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
|
2011-02-28 05:24:09 +00:00
|
|
|
$this->message_number_log[count($this->message_number_log) - 1] = str_replace(
|
|
|
|
'UNKNOWN',
|
|
|
|
'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE',
|
|
|
|
$this->message_number_log[count($this->message_number_log) - 1]
|
|
|
|
);
|
2010-08-28 17:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
After receiving the response, the server MUST send either an
|
|
|
|
SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, or another
|
|
|
|
SSH_MSG_USERAUTH_INFO_REQUEST message.
|
|
|
|
*/
|
|
|
|
// maybe phpseclib should force close the connection after x request / responses? unless something like that is done
|
|
|
|
// there could be an infinite loop of request / responses.
|
|
|
|
return $this->_keyboard_interactive_process();
|
|
|
|
case NET_SSH2_MSG_USERAUTH_SUCCESS:
|
|
|
|
return true;
|
|
|
|
case NET_SSH2_MSG_USERAUTH_FAILURE:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 06:04:16 +00:00
|
|
|
/**
|
|
|
|
* Login with an ssh-agent provided key
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
2014-02-10 06:04:16 +00:00
|
|
|
* @param System_SSH_Agent $agent
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2014-02-10 06:04:16 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _ssh_agent_login($username, $agent)
|
|
|
|
{
|
2014-12-30 02:44:31 +00:00
|
|
|
$this->agent = $agent;
|
2014-02-10 06:04:16 +00:00
|
|
|
$keys = $agent->requestIdentities();
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if ($this->_privatekey_login($username, $key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 08:19:00 +00:00
|
|
|
/**
|
|
|
|
* Login with an RSA private key
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $username
|
2009-12-03 08:19:00 +00:00
|
|
|
* @param Crypt_RSA $password
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2009-12-03 08:19:00 +00:00
|
|
|
* @access private
|
|
|
|
* @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
|
|
|
|
* by sending dummy SSH_MSG_IGNORE messages.
|
|
|
|
*/
|
|
|
|
function _privatekey_login($username, $privatekey)
|
|
|
|
{
|
|
|
|
// see http://tools.ietf.org/html/rfc4253#page-15
|
|
|
|
$publickey = $privatekey->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
|
2009-12-06 07:26:52 +00:00
|
|
|
if ($publickey === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-03 08:19:00 +00:00
|
|
|
$publickey = array(
|
|
|
|
'e' => $publickey['e']->toBytes(true),
|
|
|
|
'n' => $publickey['n']->toBytes(true)
|
|
|
|
);
|
2015-07-15 01:52:31 +00:00
|
|
|
$publickey = pack(
|
|
|
|
'Na*Na*Na*',
|
|
|
|
strlen('ssh-rsa'),
|
|
|
|
'ssh-rsa',
|
|
|
|
strlen($publickey['e']),
|
|
|
|
$publickey['e'],
|
|
|
|
strlen($publickey['n']),
|
|
|
|
$publickey['n']
|
2009-12-03 08:19:00 +00:00
|
|
|
);
|
|
|
|
|
2019-01-16 05:41:49 +00:00
|
|
|
switch ($this->signature_format) {
|
|
|
|
case 'rsa-sha2-512':
|
|
|
|
$hash = 'sha512';
|
|
|
|
$signatureType = 'rsa-sha2-512';
|
|
|
|
break;
|
|
|
|
case 'rsa-sha2-256':
|
|
|
|
$hash = 'sha256';
|
|
|
|
$signatureType = 'rsa-sha2-256';
|
|
|
|
break;
|
|
|
|
//case 'ssh-rsa':
|
|
|
|
default:
|
|
|
|
$hash = 'sha1';
|
|
|
|
$signatureType = 'ssh-rsa';
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$part1 = pack(
|
|
|
|
'CNa*Na*Na*',
|
|
|
|
NET_SSH2_MSG_USERAUTH_REQUEST,
|
|
|
|
strlen($username),
|
|
|
|
$username,
|
|
|
|
strlen('ssh-connection'),
|
|
|
|
'ssh-connection',
|
|
|
|
strlen('publickey'),
|
|
|
|
'publickey'
|
2009-12-03 08:19:00 +00:00
|
|
|
);
|
2019-01-16 05:41:49 +00:00
|
|
|
$part2 = pack('Na*Na*', strlen($signatureType), $signatureType, strlen($publickey), $publickey);
|
2009-12-03 08:19:00 +00:00
|
|
|
|
|
|
|
$packet = $part1 . chr(0) . $part2;
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2009-12-03 08:19:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 08:19:00 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
|
|
|
|
|
|
|
switch ($type) {
|
2007-07-23 05:21:39 +00:00
|
|
|
case NET_SSH2_MSG_USERAUTH_FAILURE:
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
2010-02-12 23:02:13 +00:00
|
|
|
$this->errors[] = 'SSH_MSG_USERAUTH_FAILURE: ' . $this->_string_shift($response, $length);
|
2013-04-20 03:23:06 +00:00
|
|
|
return false;
|
2009-12-03 08:19:00 +00:00
|
|
|
case NET_SSH2_MSG_USERAUTH_PK_OK:
|
|
|
|
// we'll just take it on faith that the public key blob and the public key algorithm name are as
|
|
|
|
// they should be
|
2013-09-12 13:45:13 +00:00
|
|
|
if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
|
2011-04-18 12:17:40 +00:00
|
|
|
$this->message_number_log[count($this->message_number_log) - 1] = str_replace(
|
|
|
|
'UNKNOWN',
|
|
|
|
'NET_SSH2_MSG_USERAUTH_PK_OK',
|
|
|
|
$this->message_number_log[count($this->message_number_log) - 1]
|
|
|
|
);
|
2009-12-03 08:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$packet = $part1 . chr(1) . $part2;
|
|
|
|
$privatekey->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
|
2018-05-27 14:48:44 +00:00
|
|
|
$privatekey->setHash($hash);
|
2009-12-03 08:19:00 +00:00
|
|
|
$signature = $privatekey->sign(pack('Na*a*', strlen($this->session_id), $this->session_id, $packet));
|
2019-01-16 05:41:49 +00:00
|
|
|
$signature = pack('Na*Na*', strlen($signatureType), $signatureType, strlen($signature), $signature);
|
2009-12-03 08:19:00 +00:00
|
|
|
$packet.= pack('Na*', strlen($signature), $signature);
|
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2009-12-03 08:19:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 08:19:00 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_USERAUTH_FAILURE:
|
2012-08-14 17:07:18 +00:00
|
|
|
// either the login is bad or the server employs multi-factor authentication
|
2009-12-03 08:19:00 +00:00
|
|
|
return false;
|
2007-07-23 05:21:39 +00:00
|
|
|
case NET_SSH2_MSG_USERAUTH_SUCCESS:
|
2007-07-25 21:49:33 +00:00
|
|
|
$this->bitmap |= NET_SSH2_MASK_LOGIN;
|
2007-07-23 05:21:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-03 17:49:16 +00:00
|
|
|
/**
|
|
|
|
* Set Timeout
|
|
|
|
*
|
|
|
|
* $ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout.
|
|
|
|
* Setting $timeout to false or 0 will mean there is no timeout.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param mixed $timeout
|
2013-04-27 19:10:36 +00:00
|
|
|
* @access public
|
2012-03-03 17:49:16 +00:00
|
|
|
*/
|
|
|
|
function setTimeout($timeout)
|
|
|
|
{
|
2012-03-03 19:56:22 +00:00
|
|
|
$this->timeout = $this->curTimeout = $timeout;
|
2012-03-03 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 22:18:31 +00:00
|
|
|
/**
|
|
|
|
* Get the output from stdError
|
2013-12-25 23:33:08 +00:00
|
|
|
*
|
2013-04-27 19:12:59 +00:00
|
|
|
* @access public
|
2013-03-21 22:18:31 +00:00
|
|
|
*/
|
2013-04-27 19:10:36 +00:00
|
|
|
function getStdError()
|
|
|
|
{
|
2013-03-21 22:18:31 +00:00
|
|
|
return $this->stdErrorLog;
|
|
|
|
}
|
|
|
|
|
2007-07-25 21:49:33 +00:00
|
|
|
/**
|
|
|
|
* Execute Command
|
|
|
|
*
|
2014-11-20 04:38:19 +00:00
|
|
|
* If $callback is set to false then Net_SSH2::_get_channel_packet(NET_SSH2_CHANNEL_EXEC) will need to be called manually.
|
2010-11-30 04:34:31 +00:00
|
|
|
* In all likelihood, this is not a feature you want to be taking advantage of.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $command
|
2015-09-01 23:22:30 +00:00
|
|
|
* @param Callback $callback
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2007-07-25 21:49:33 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2013-12-03 17:54:43 +00:00
|
|
|
function exec($command, $callback = null)
|
2007-07-25 21:49:33 +00:00
|
|
|
{
|
2012-03-03 17:49:16 +00:00
|
|
|
$this->curTimeout = $this->timeout;
|
2013-06-07 22:21:11 +00:00
|
|
|
$this->is_timeout = false;
|
2013-03-21 22:18:31 +00:00
|
|
|
$this->stdErrorLog = '';
|
2012-03-03 17:49:16 +00:00
|
|
|
|
2016-12-16 16:27:56 +00:00
|
|
|
if (!$this->isAuthenticated()) {
|
2007-07-25 21:49:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-17 22:09:48 +00:00
|
|
|
if ($this->in_request_pty_exec) {
|
|
|
|
user_error('If you want to run multiple exec()\'s you will need to disable (and re-enable if appropriate) a PTY for each one.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-17 03:19:20 +00:00
|
|
|
// RFC4254 defines the (client) window size as "bytes the other party can send before it must wait for the window to
|
2013-12-25 23:33:08 +00:00
|
|
|
// be adjusted". 0x7FFFFFFF is, at 2GB, the max size. technically, it should probably be decremented, but,
|
2016-07-31 12:41:23 +00:00
|
|
|
// honestly, if you're transferring more than 2GB, you probably shouldn't be using phpseclib, anyway.
|
2009-05-16 17:09:37 +00:00
|
|
|
// see http://tools.ietf.org/html/rfc4254#section-5.2 for more info
|
2014-05-26 22:37:32 +00:00
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC] = $this->window_size;
|
2009-05-16 17:09:37 +00:00
|
|
|
// 0x8000 is the maximum max packet size, per http://tools.ietf.org/html/rfc4253#section-6.1, although since PuTTy
|
2009-09-17 03:19:20 +00:00
|
|
|
// uses 0x4000, that's what will be used here, as well.
|
2009-05-16 17:09:37 +00:00
|
|
|
$packet_size = 0x4000;
|
2007-07-25 21:49:33 +00:00
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*N3',
|
|
|
|
NET_SSH2_MSG_CHANNEL_OPEN,
|
|
|
|
strlen('session'),
|
|
|
|
'session',
|
|
|
|
NET_SSH2_CHANNEL_EXEC,
|
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC],
|
|
|
|
$packet_size
|
|
|
|
);
|
2007-07-25 21:49:33 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN;
|
2007-07-25 21:49:33 +00:00
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
|
2007-07-25 21:49:33 +00:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:04:52 +00:00
|
|
|
if ($this->request_pty === true) {
|
|
|
|
$terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*CNa*N5a*',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$this->server_channels[NET_SSH2_CHANNEL_EXEC],
|
|
|
|
strlen('pty-req'),
|
|
|
|
'pty-req',
|
|
|
|
1,
|
|
|
|
strlen('vt100'),
|
|
|
|
'vt100',
|
|
|
|
$this->windowColumns,
|
|
|
|
$this->windowRows,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
strlen($terminal_modes),
|
|
|
|
$terminal_modes
|
|
|
|
);
|
2013-02-08 22:04:52 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
|
2017-11-09 06:12:06 +00:00
|
|
|
$response = $this->_get_binary_packet();
|
2013-02-08 22:04:52 +00:00
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-02-08 22:04:52 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-08 22:04:52 +00:00
|
|
|
list(, $type) = unpack('C', $this->_string_shift($response, 1));
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_SUCCESS:
|
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_FAILURE:
|
|
|
|
default:
|
|
|
|
user_error('Unable to request pseudo-terminal');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
$this->in_request_pty_exec = true;
|
|
|
|
}
|
|
|
|
|
2010-03-22 22:01:38 +00:00
|
|
|
// sending a pty-req SSH_MSG_CHANNEL_REQUEST message is unnecessary and, in fact, in most cases, slows things
|
|
|
|
// down. the one place where it might be desirable is if you're doing something like Net_SSH2::exec('ping localhost &').
|
2010-05-16 16:10:50 +00:00
|
|
|
// with a pty-req SSH_MSG_CHANNEL_REQUEST, exec() will return immediately and the ping process will then
|
2010-03-22 22:01:38 +00:00
|
|
|
// then immediately terminate. without such a request exec() will loop indefinitely. the ping process won't end but
|
|
|
|
// neither will your script.
|
2007-07-25 21:49:33 +00:00
|
|
|
|
2009-09-17 03:19:20 +00:00
|
|
|
// although, in theory, the size of SSH_MSG_CHANNEL_REQUEST could exceed the maximum packet size established by
|
2013-12-25 23:33:08 +00:00
|
|
|
// SSH_MSG_CHANNEL_OPEN_CONFIRMATION, RFC4254#section-5.1 states that the "maximum packet size" refers to the
|
2009-09-17 03:19:20 +00:00
|
|
|
// "maximum size of an individual data packet". ie. SSH_MSG_CHANNEL_DATA. RFC4254#section-5.2 corroborates.
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*CNa*',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$this->server_channels[NET_SSH2_CHANNEL_EXEC],
|
|
|
|
strlen('exec'),
|
|
|
|
'exec',
|
|
|
|
1,
|
|
|
|
strlen($command),
|
|
|
|
$command
|
|
|
|
);
|
2014-12-30 02:44:31 +00:00
|
|
|
|
2007-07-25 21:49:33 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST;
|
|
|
|
|
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
|
2007-07-25 21:49:33 +00:00
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA;
|
2007-07-25 21:49:33 +00:00
|
|
|
|
2013-05-28 22:02:27 +00:00
|
|
|
if ($callback === false || $this->in_request_pty_exec) {
|
2010-11-30 04:34:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-25 21:49:33 +00:00
|
|
|
$output = '';
|
|
|
|
while (true) {
|
2009-12-14 18:14:54 +00:00
|
|
|
$temp = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
|
2009-05-23 14:42:17 +00:00
|
|
|
switch (true) {
|
|
|
|
case $temp === true:
|
2013-05-28 22:02:27 +00:00
|
|
|
return is_callable($callback) ? true : $output;
|
2009-05-23 14:42:17 +00:00
|
|
|
case $temp === false:
|
|
|
|
return false;
|
2007-07-25 21:49:33 +00:00
|
|
|
default:
|
2013-05-28 22:02:27 +00:00
|
|
|
if (is_callable($callback)) {
|
2014-05-26 22:37:32 +00:00
|
|
|
if (call_user_func($callback, $temp) === true) {
|
|
|
|
$this->_close_channel(NET_SSH2_CHANNEL_EXEC);
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-28 22:02:27 +00:00
|
|
|
} else {
|
|
|
|
$output.= $temp;
|
|
|
|
}
|
2007-07-25 21:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 05:24:09 +00:00
|
|
|
/**
|
|
|
|
* Creates an interactive shell
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::read()
|
|
|
|
* @see self::write()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2011-02-28 05:24:09 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _initShell()
|
|
|
|
{
|
2013-02-08 22:04:52 +00:00
|
|
|
if ($this->in_request_pty_exec === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-26 22:57:14 +00:00
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL] = $this->window_size;
|
2011-02-28 05:24:09 +00:00
|
|
|
$packet_size = 0x4000;
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*N3',
|
|
|
|
NET_SSH2_MSG_CHANNEL_OPEN,
|
|
|
|
strlen('session'),
|
|
|
|
'session',
|
|
|
|
NET_SSH2_CHANNEL_SHELL,
|
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL],
|
|
|
|
$packet_size
|
|
|
|
);
|
2011-02-28 05:24:09 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
|
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*CNa*N5a*',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$this->server_channels[NET_SSH2_CHANNEL_SHELL],
|
|
|
|
strlen('pty-req'),
|
|
|
|
'pty-req',
|
|
|
|
1,
|
|
|
|
strlen('vt100'),
|
|
|
|
'vt100',
|
|
|
|
$this->windowColumns,
|
|
|
|
$this->windowRows,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
strlen($terminal_modes),
|
|
|
|
$terminal_modes
|
|
|
|
);
|
2011-02-28 05:24:09 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_get_binary_packet();
|
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Connection closed by server');
|
2011-02-28 05:24:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-28 05:24:09 +00:00
|
|
|
list(, $type) = unpack('C', $this->_string_shift($response, 1));
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_SUCCESS:
|
2013-08-21 04:52:15 +00:00
|
|
|
// if a pty can't be opened maybe commands can still be executed
|
2011-02-28 05:24:09 +00:00
|
|
|
case NET_SSH2_MSG_CHANNEL_FAILURE:
|
2013-08-21 04:52:15 +00:00
|
|
|
break;
|
2011-02-28 05:24:09 +00:00
|
|
|
default:
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Unable to request pseudo-terminal');
|
2011-02-28 05:24:09 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*C',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$this->server_channels[NET_SSH2_CHANNEL_SHELL],
|
|
|
|
strlen('shell'),
|
|
|
|
'shell',
|
|
|
|
1
|
|
|
|
);
|
2011-02-28 05:24:09 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST;
|
|
|
|
|
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
|
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA;
|
|
|
|
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_SHELL;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:35:30 +00:00
|
|
|
/**
|
|
|
|
* Return the channel to be used with read() / write()
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::read()
|
|
|
|
* @see self::write()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return int
|
2013-10-25 17:35:30 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function _get_interactive_channel()
|
|
|
|
{
|
|
|
|
switch (true) {
|
|
|
|
case $this->in_subsystem:
|
|
|
|
return NET_SSH2_CHANNEL_SUBSYSTEM;
|
|
|
|
case $this->in_request_pty_exec:
|
|
|
|
return NET_SSH2_CHANNEL_EXEC;
|
|
|
|
default:
|
|
|
|
return NET_SSH2_CHANNEL_SHELL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 03:28:23 +00:00
|
|
|
/**
|
|
|
|
* Return an available open channel
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return int
|
2015-02-06 03:28:23 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function _get_open_channel()
|
|
|
|
{
|
|
|
|
$channel = NET_SSH2_CHANNEL_EXEC;
|
|
|
|
do {
|
2015-03-24 05:38:56 +00:00
|
|
|
if (isset($this->channel_status[$channel]) && $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_OPEN) {
|
2015-02-06 03:28:23 +00:00
|
|
|
return $channel;
|
|
|
|
}
|
|
|
|
} while ($channel++ < NET_SSH2_CHANNEL_SUBSYSTEM);
|
|
|
|
|
2015-03-24 05:38:56 +00:00
|
|
|
return false;
|
2015-02-06 03:28:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 05:24:09 +00:00
|
|
|
/**
|
|
|
|
* Returns the output of an interactive shell
|
|
|
|
*
|
|
|
|
* Returns when there's a match for $expect, which can take the form of a string literal or,
|
|
|
|
* if $mode == NET_SSH2_READ_REGEX, a regular expression.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::write()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $expect
|
|
|
|
* @param int $mode
|
|
|
|
* @return string
|
2011-02-28 05:24:09 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2012-03-05 03:13:09 +00:00
|
|
|
function read($expect = '', $mode = NET_SSH2_READ_SIMPLE)
|
2011-02-28 05:24:09 +00:00
|
|
|
{
|
2012-03-03 17:49:16 +00:00
|
|
|
$this->curTimeout = $this->timeout;
|
2013-06-07 22:21:11 +00:00
|
|
|
$this->is_timeout = false;
|
2012-03-03 17:49:16 +00:00
|
|
|
|
2016-12-16 16:27:56 +00:00
|
|
|
if (!$this->isAuthenticated()) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Operation disallowed prior to login()');
|
2011-02-28 05:24:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Unable to initiate an interactive shell session');
|
2011-02-28 05:24:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:35:30 +00:00
|
|
|
$channel = $this->_get_interactive_channel();
|
2013-02-08 22:04:52 +00:00
|
|
|
|
2017-06-13 23:56:50 +00:00
|
|
|
if ($mode == NET_SSH2_READ_NEXT) {
|
|
|
|
return $this->_get_channel_packet($channel);
|
|
|
|
}
|
|
|
|
|
2011-05-08 02:34:36 +00:00
|
|
|
$match = $expect;
|
2011-02-28 05:24:09 +00:00
|
|
|
while (true) {
|
2011-04-18 21:37:06 +00:00
|
|
|
if ($mode == NET_SSH2_READ_REGEX) {
|
2015-07-15 00:05:52 +00:00
|
|
|
preg_match($expect, substr($this->interactiveBuffer, -1024), $matches);
|
2013-01-13 16:49:03 +00:00
|
|
|
$match = isset($matches[0]) ? $matches[0] : '';
|
2011-02-28 05:24:09 +00:00
|
|
|
}
|
2013-01-12 16:46:19 +00:00
|
|
|
$pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false;
|
2011-02-28 05:24:09 +00:00
|
|
|
if ($pos !== false) {
|
2011-05-08 02:34:36 +00:00
|
|
|
return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match));
|
2011-02-28 05:24:09 +00:00
|
|
|
}
|
2013-02-08 22:04:52 +00:00
|
|
|
$response = $this->_get_channel_packet($channel);
|
2012-03-03 17:49:16 +00:00
|
|
|
if (is_bool($response)) {
|
2013-02-08 22:04:52 +00:00
|
|
|
$this->in_request_pty_exec = false;
|
2012-03-03 17:49:16 +00:00
|
|
|
return $response ? $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)) : false;
|
|
|
|
}
|
2011-02-28 05:24:09 +00:00
|
|
|
|
|
|
|
$this->interactiveBuffer.= $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inputs a command into an interactive shell.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::read()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $cmd
|
|
|
|
* @return bool
|
2011-02-28 05:24:09 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function write($cmd)
|
|
|
|
{
|
2016-12-16 16:27:56 +00:00
|
|
|
if (!$this->isAuthenticated()) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Operation disallowed prior to login()');
|
2011-02-28 05:24:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Unable to initiate an interactive shell session');
|
2011-02-28 05:24:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:35:30 +00:00
|
|
|
return $this->_send_channel_packet($this->_get_interactive_channel(), $cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a subsystem.
|
|
|
|
*
|
|
|
|
* Right now only one subsystem at a time is supported. To support multiple subsystem's stopSubsystem() could accept
|
|
|
|
* a string that contained the name of the subsystem, but at that point, only one subsystem of each type could be opened.
|
|
|
|
* To support multiple subsystem's of the same name maybe it'd be best if startSubsystem() generated a new channel id and
|
|
|
|
* returns that and then that that was passed into stopSubsystem() but that'll be saved for a future date and implemented
|
|
|
|
* if there's sufficient demand for such a feature.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::stopSubsystem()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $subsystem
|
|
|
|
* @return bool
|
2013-10-25 17:35:30 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function startSubsystem($subsystem)
|
|
|
|
{
|
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_SUBSYSTEM] = $this->window_size;
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNa*N3',
|
|
|
|
NET_SSH2_MSG_CHANNEL_OPEN,
|
|
|
|
strlen('session'),
|
|
|
|
'session',
|
|
|
|
NET_SSH2_CHANNEL_SUBSYSTEM,
|
|
|
|
$this->window_size,
|
|
|
|
0x4000
|
|
|
|
);
|
2013-10-25 17:35:30 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM);
|
|
|
|
if ($response === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CNNa*CNa*',
|
|
|
|
NET_SSH2_MSG_CHANNEL_REQUEST,
|
|
|
|
$this->server_channels[NET_SSH2_CHANNEL_SUBSYSTEM],
|
|
|
|
strlen('subsystem'),
|
|
|
|
'subsystem',
|
|
|
|
1,
|
|
|
|
strlen($subsystem),
|
|
|
|
$subsystem
|
|
|
|
);
|
2013-10-25 17:35:30 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_REQUEST;
|
|
|
|
|
|
|
|
$response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM);
|
|
|
|
|
|
|
|
if ($response === false) {
|
2015-07-15 01:52:31 +00:00
|
|
|
return false;
|
2013-10-25 17:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_DATA;
|
|
|
|
|
|
|
|
$this->bitmap |= NET_SSH2_MASK_SHELL;
|
|
|
|
$this->in_subsystem = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops a subsystem.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::startSubsystem()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2013-10-25 17:35:30 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function stopSubsystem()
|
|
|
|
{
|
|
|
|
$this->in_subsystem = false;
|
|
|
|
$this->_close_channel(NET_SSH2_CHANNEL_SUBSYSTEM);
|
|
|
|
return true;
|
2011-02-28 05:24:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 22:21:11 +00:00
|
|
|
/**
|
|
|
|
* Closes a channel
|
|
|
|
*
|
|
|
|
* If read() timed out you might want to just close the channel and have it auto-restart on the next read() call
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function reset()
|
|
|
|
{
|
2013-11-20 21:17:23 +00:00
|
|
|
$this->_close_channel($this->_get_interactive_channel());
|
2013-06-07 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is timeout?
|
|
|
|
*
|
|
|
|
* Did exec() or read() return because they timed out or because they encountered the end?
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function isTimeout()
|
|
|
|
{
|
|
|
|
return $this->is_timeout;
|
|
|
|
}
|
|
|
|
|
2008-05-15 17:40:03 +00:00
|
|
|
/**
|
|
|
|
* Disconnect
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function disconnect()
|
|
|
|
{
|
|
|
|
$this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
2012-04-30 16:25:37 +00:00
|
|
|
if (isset($this->realtime_log_file) && is_resource($this->realtime_log_file)) {
|
|
|
|
fclose($this->realtime_log_file);
|
|
|
|
}
|
2008-05-15 17:40:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 16:33:08 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
2008-05-15 17:40:03 +00:00
|
|
|
* Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
|
|
|
|
* disconnect().
|
2008-05-15 16:33:08 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function __destruct()
|
|
|
|
{
|
2008-05-15 17:40:03 +00:00
|
|
|
$this->disconnect();
|
2008-05-15 16:33:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 20:58:12 +00:00
|
|
|
/**
|
|
|
|
* Is the connection still active?
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2013-02-16 20:58:12 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function isConnected()
|
|
|
|
{
|
2014-07-20 21:03:06 +00:00
|
|
|
return (bool) ($this->bitmap & NET_SSH2_MASK_CONNECTED);
|
2013-02-16 20:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 01:08:17 +00:00
|
|
|
/**
|
|
|
|
* Have you successfully been logged in?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function isAuthenticated()
|
|
|
|
{
|
|
|
|
return (bool) ($this->bitmap & NET_SSH2_MASK_LOGIN);
|
|
|
|
}
|
|
|
|
|
2018-10-06 06:19:14 +00:00
|
|
|
/**
|
|
|
|
* Pings a server connection, or tries to reconnect if the connection has gone down
|
|
|
|
*
|
|
|
|
* Inspired by http://php.net/manual/en/mysqli.ping.php
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function ping()
|
|
|
|
{
|
|
|
|
if (!$this->isAuthenticated()) {
|
2019-09-10 22:31:43 +00:00
|
|
|
if (!empty($this->auth)) {
|
|
|
|
return $this->_reconnect();
|
|
|
|
}
|
2018-10-06 06:19:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_KEEP_ALIVE] = $this->window_size;
|
|
|
|
$packet_size = 0x4000;
|
|
|
|
$packet = pack(
|
|
|
|
'CNa*N3',
|
|
|
|
NET_SSH2_MSG_CHANNEL_OPEN,
|
|
|
|
strlen('session'),
|
|
|
|
'session',
|
|
|
|
NET_SSH2_CHANNEL_KEEP_ALIVE,
|
|
|
|
$this->window_size_server_to_client[NET_SSH2_CHANNEL_KEEP_ALIVE],
|
|
|
|
$packet_size
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!@$this->_send_binary_packet($packet)) {
|
|
|
|
return $this->_reconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[NET_SSH2_CHANNEL_KEEP_ALIVE] = NET_SSH2_MSG_CHANNEL_OPEN;
|
|
|
|
|
|
|
|
$response = @$this->_get_channel_packet(NET_SSH2_CHANNEL_KEEP_ALIVE);
|
|
|
|
if ($response !== false) {
|
|
|
|
$this->_close_channel(NET_SSH2_CHANNEL_KEEP_ALIVE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_reconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In situ reconnect method
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _reconnect()
|
|
|
|
{
|
|
|
|
$this->_reset_connection(NET_SSH2_DISCONNECT_CONNECTION_LOST);
|
|
|
|
$this->retry_connect = true;
|
|
|
|
if (!$this->_connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($this->auth as $auth) {
|
2019-03-23 23:42:40 +00:00
|
|
|
$result = call_user_func_array(array(&$this, 'login'), $auth);
|
2018-10-06 06:19:14 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-08-27 00:50:17 +00:00
|
|
|
/**
|
|
|
|
* Resets a connection for re-use
|
|
|
|
*
|
|
|
|
* @param int $reason
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _reset_connection($reason)
|
|
|
|
{
|
|
|
|
$this->_disconnect($reason);
|
|
|
|
$this->decrypt = $this->encrypt = false;
|
|
|
|
$this->decrypt_block_size = $this->encrypt_block_size = 8;
|
|
|
|
$this->hmac_check = $this->hmac_create = false;
|
|
|
|
$this->hmac_size = false;
|
|
|
|
$this->session_id = false;
|
|
|
|
$this->retry_connect = true;
|
|
|
|
$this->get_seq_no = $this->send_seq_no = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Gets Binary Packets
|
|
|
|
*
|
|
|
|
* See '6. Binary Packet Protocol' of rfc4253 for more info.
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_send_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-11-09 06:12:06 +00:00
|
|
|
function _get_binary_packet($skip_channel_filter = false)
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2012-04-24 05:01:55 +00:00
|
|
|
if (!is_resource($this->fsock) || feof($this->fsock)) {
|
2013-11-15 18:34:31 +00:00
|
|
|
$this->bitmap = 0;
|
2018-10-06 02:27:59 +00:00
|
|
|
user_error('Connection closed prematurely');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-29 19:23:25 +00:00
|
|
|
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
|
2009-02-16 22:22:13 +00:00
|
|
|
$raw = fread($this->fsock, $this->decrypt_block_size);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2013-01-12 16:46:19 +00:00
|
|
|
if (!strlen($raw)) {
|
2011-04-22 07:50:24 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
if ($this->decrypt !== false) {
|
|
|
|
$raw = $this->decrypt->decrypt($raw);
|
|
|
|
}
|
2011-10-06 12:12:56 +00:00
|
|
|
if ($raw === false) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Unable to decrypt content');
|
2011-10-06 12:12:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($raw) < 5) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Npacket_length/Cpadding_length', $this->_string_shift($raw, 5)));
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2009-02-16 22:22:13 +00:00
|
|
|
$remaining_length = $packet_length + 4 - $this->decrypt_block_size;
|
2013-01-26 06:17:23 +00:00
|
|
|
|
|
|
|
// quoting <http://tools.ietf.org/html/rfc4253#section-6.1>,
|
|
|
|
// "implementations SHOULD check that the packet length is reasonable"
|
|
|
|
// PuTTY uses 0x9000 as the actual max packet size and so to shall we
|
|
|
|
if ($remaining_length < -$this->decrypt_block_size || $remaining_length > 0x9000 || $remaining_length % $this->decrypt_block_size != 0) {
|
2019-09-08 00:08:00 +00:00
|
|
|
if (!$this->bad_key_size_fix && $this->_bad_algorithm_candidate($this->decrypt->name) && !($this->bitmap & NET_SSH2_MASK_LOGIN)) {
|
2017-08-27 00:50:17 +00:00
|
|
|
$this->bad_key_size_fix = true;
|
|
|
|
$this->_reset_connection(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-26 06:17:23 +00:00
|
|
|
user_error('Invalid size');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-16 17:09:37 +00:00
|
|
|
$buffer = '';
|
|
|
|
while ($remaining_length > 0) {
|
2009-02-16 22:22:13 +00:00
|
|
|
$temp = fread($this->fsock, $remaining_length);
|
2014-04-18 21:58:00 +00:00
|
|
|
if ($temp === false || feof($this->fsock)) {
|
|
|
|
$this->bitmap = 0;
|
2018-10-06 02:27:59 +00:00
|
|
|
user_error('Error reading from socket');
|
2014-04-18 21:58:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-05-16 17:09:37 +00:00
|
|
|
$buffer.= $temp;
|
|
|
|
$remaining_length-= strlen($temp);
|
|
|
|
}
|
2017-08-29 03:37:35 +00:00
|
|
|
|
2013-01-12 00:38:38 +00:00
|
|
|
$stop = strtok(microtime(), ' ') + strtok('');
|
2013-01-12 16:46:19 +00:00
|
|
|
if (strlen($buffer)) {
|
2009-05-16 17:09:37 +00:00
|
|
|
$raw.= $this->decrypt !== false ? $this->decrypt->decrypt($buffer) : $buffer;
|
2009-02-16 22:22:13 +00:00
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
$payload = $this->_string_shift($raw, $packet_length - $padding_length - 1);
|
|
|
|
$padding = $this->_string_shift($raw, $padding_length); // should leave $raw empty
|
|
|
|
|
|
|
|
if ($this->hmac_check !== false) {
|
|
|
|
$hmac = fread($this->fsock, $this->hmac_size);
|
2014-04-18 21:58:00 +00:00
|
|
|
if ($hmac === false || strlen($hmac) != $this->hmac_size) {
|
|
|
|
$this->bitmap = 0;
|
2018-10-06 02:27:59 +00:00
|
|
|
user_error('Error reading socket');
|
2014-04-18 21:58:00 +00:00
|
|
|
return false;
|
|
|
|
} elseif ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Invalid HMAC');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 17:09:37 +00:00
|
|
|
//if ($this->decompress) {
|
|
|
|
// $payload = gzinflate(substr($payload, 2));
|
|
|
|
//}
|
|
|
|
|
2008-05-26 19:42:01 +00:00
|
|
|
$this->get_seq_no++;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
2009-03-25 22:29:42 +00:00
|
|
|
if (defined('NET_SSH2_LOGGING')) {
|
2012-11-22 19:08:30 +00:00
|
|
|
$current = strtok(microtime(), ' ') + strtok('');
|
2012-04-30 16:25:37 +00:00
|
|
|
$message_number = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')';
|
|
|
|
$message_number = '<- ' . $message_number .
|
2012-11-22 19:08:30 +00:00
|
|
|
' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
|
2012-04-30 16:25:37 +00:00
|
|
|
$this->_append_log($message_number, $payload);
|
2012-11-22 19:08:30 +00:00
|
|
|
$this->last_packet = $current;
|
2009-03-25 22:29:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:12:06 +00:00
|
|
|
return $this->_filter($payload, $skip_channel_filter);
|
2007-07-26 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter Binary Packets
|
|
|
|
*
|
|
|
|
* Because some binary packets need to be ignored...
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2007-07-26 14:53:45 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2017-11-09 06:12:06 +00:00
|
|
|
function _filter($payload, $skip_channel_filter)
|
2007-07-26 14:53:45 +00:00
|
|
|
{
|
2007-07-23 05:21:39 +00:00
|
|
|
switch (ord($payload[0])) {
|
|
|
|
case NET_SSH2_MSG_DISCONNECT:
|
2008-05-26 19:42:01 +00:00
|
|
|
$this->_string_shift($payload, 1);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nreason_code/Nlength', $this->_string_shift($payload, 8)));
|
2018-08-21 01:50:07 +00:00
|
|
|
$this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n" . $this->_string_shift($payload, $length);
|
2013-11-15 18:34:31 +00:00
|
|
|
$this->bitmap = 0;
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
case NET_SSH2_MSG_IGNORE:
|
2017-11-12 05:08:08 +00:00
|
|
|
$payload = $this->_get_binary_packet($skip_channel_filter);
|
2007-07-23 05:21:39 +00:00
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_DEBUG:
|
|
|
|
$this->_string_shift($payload, 2);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($payload, 4)));
|
2018-08-21 01:50:07 +00:00
|
|
|
$this->errors[] = 'SSH_MSG_DEBUG: ' . $this->_string_shift($payload, $length);
|
2017-11-12 05:08:08 +00:00
|
|
|
$payload = $this->_get_binary_packet($skip_channel_filter);
|
2007-07-23 05:21:39 +00:00
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_UNIMPLEMENTED:
|
|
|
|
return false;
|
|
|
|
case NET_SSH2_MSG_KEXINIT:
|
|
|
|
if ($this->session_id !== false) {
|
2017-11-12 05:08:08 +00:00
|
|
|
$this->send_kex_first = false;
|
2007-07-23 05:21:39 +00:00
|
|
|
if (!$this->_key_exchange($payload)) {
|
2013-11-15 18:34:31 +00:00
|
|
|
$this->bitmap = 0;
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-12 05:08:08 +00:00
|
|
|
$payload = $this->_get_binary_packet($skip_channel_filter);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-25 21:49:33 +00:00
|
|
|
// 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
|
2016-12-16 16:27:56 +00:00
|
|
|
if (($this->bitmap & NET_SSH2_MASK_CONNECTED) && !$this->isAuthenticated() && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
|
2007-07-25 21:49:33 +00:00
|
|
|
$this->_string_shift($payload, 1);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($payload, 4)));
|
2018-08-21 01:50:07 +00:00
|
|
|
$this->banner_message = $this->_string_shift($payload, $length);
|
2007-07-25 21:49:33 +00:00
|
|
|
$payload = $this->_get_binary_packet();
|
|
|
|
}
|
|
|
|
|
|
|
|
// only called when we've already logged in
|
2016-12-16 16:27:56 +00:00
|
|
|
if (($this->bitmap & NET_SSH2_MASK_CONNECTED) && $this->isAuthenticated()) {
|
2007-07-25 21:49:33 +00:00
|
|
|
switch (ord($payload[0])) {
|
2017-08-29 03:37:35 +00:00
|
|
|
case NET_SSH2_MSG_CHANNEL_DATA:
|
|
|
|
case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA:
|
|
|
|
case NET_SSH2_MSG_CHANNEL_REQUEST:
|
|
|
|
case NET_SSH2_MSG_CHANNEL_CLOSE:
|
|
|
|
case NET_SSH2_MSG_CHANNEL_EOF:
|
2017-11-09 06:12:06 +00:00
|
|
|
if (!$skip_channel_filter && !empty($this->server_channels)) {
|
2017-08-29 03:37:35 +00:00
|
|
|
$this->binary_packet_buffer = $payload;
|
|
|
|
$this->_get_channel_packet(true);
|
2017-11-09 06:12:06 +00:00
|
|
|
$payload = $this->_get_binary_packet();
|
2017-08-29 03:37:35 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-07-25 21:49:33 +00:00
|
|
|
case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-04 13:22:27 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($payload, 4)));
|
2015-05-01 02:34:07 +00:00
|
|
|
$this->errors[] = 'SSH_MSG_GLOBAL_REQUEST: ' . $this->_string_shift($payload, $length);
|
2007-07-25 21:49:33 +00:00
|
|
|
|
|
|
|
if (!$this->_send_binary_packet(pack('C', NET_SSH2_MSG_REQUEST_FAILURE))) {
|
2009-05-16 17:09:37 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
2007-07-25 21:49:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:12:06 +00:00
|
|
|
$payload = $this->_get_binary_packet($skip_channel_filter);
|
2007-07-25 21:49:33 +00:00
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1
|
|
|
|
$this->_string_shift($payload, 1);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-07 20:49:49 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($payload, 4)));
|
2015-02-06 03:28:23 +00:00
|
|
|
$data = $this->_string_shift($payload, $length);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nserver_channel', $this->_string_shift($payload, 4)));
|
2015-07-15 01:52:31 +00:00
|
|
|
switch ($data) {
|
2015-02-06 03:28:23 +00:00
|
|
|
case 'auth-agent':
|
|
|
|
case 'auth-agent@openssh.com':
|
|
|
|
if (isset($this->agent)) {
|
2015-07-15 01:52:31 +00:00
|
|
|
$new_channel = NET_SSH2_CHANNEL_AGENT_FORWARD;
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-15 01:52:31 +00:00
|
|
|
extract(unpack('Nremote_window_size', $this->_string_shift($payload, 4)));
|
|
|
|
extract(unpack('Nremote_maximum_packet_size', $this->_string_shift($payload, 4)));
|
|
|
|
|
|
|
|
$this->packet_size_client_to_server[$new_channel] = $remote_window_size;
|
|
|
|
$this->window_size_server_to_client[$new_channel] = $remote_maximum_packet_size;
|
|
|
|
$this->window_size_client_to_server[$new_channel] = $this->window_size;
|
|
|
|
|
|
|
|
$packet_size = 0x4000;
|
|
|
|
|
|
|
|
$packet = pack(
|
|
|
|
'CN4',
|
|
|
|
NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION,
|
|
|
|
$server_channel,
|
|
|
|
$new_channel,
|
|
|
|
$packet_size,
|
|
|
|
$packet_size
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->server_channels[$new_channel] = $server_channel;
|
|
|
|
$this->channel_status[$new_channel] = NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION;
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-06 03:28:23 +00:00
|
|
|
}
|
2015-03-24 05:38:56 +00:00
|
|
|
break;
|
2015-02-06 03:28:23 +00:00
|
|
|
default:
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CN3a*Na*',
|
|
|
|
NET_SSH2_MSG_REQUEST_FAILURE,
|
|
|
|
$server_channel,
|
|
|
|
NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED,
|
|
|
|
0,
|
|
|
|
'',
|
|
|
|
0,
|
|
|
|
''
|
|
|
|
);
|
2007-07-25 21:49:33 +00:00
|
|
|
|
2015-02-06 03:28:23 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 06:12:06 +00:00
|
|
|
$payload = $this->_get_binary_packet($skip_channel_filter);
|
2007-07-25 21:49:33 +00:00
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST:
|
2013-07-07 20:57:15 +00:00
|
|
|
$this->_string_shift($payload, 1);
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($payload) < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-07 20:57:15 +00:00
|
|
|
extract(unpack('Nchannel', $this->_string_shift($payload, 4)));
|
|
|
|
extract(unpack('Nwindow_size', $this->_string_shift($payload, 4)));
|
2013-09-12 13:29:14 +00:00
|
|
|
$this->window_size_client_to_server[$channel]+= $window_size;
|
2013-07-07 20:57:15 +00:00
|
|
|
|
2017-11-09 06:12:06 +00:00
|
|
|
$payload = ($this->bitmap & NET_SSH2_MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet($skip_channel_filter);
|
2007-07-25 21:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $payload;
|
|
|
|
}
|
|
|
|
|
2012-07-23 12:17:53 +00:00
|
|
|
/**
|
|
|
|
* Enable Quiet Mode
|
|
|
|
*
|
|
|
|
* Suppress stderr from output
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function enableQuietMode()
|
|
|
|
{
|
|
|
|
$this->quiet_mode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable Quiet Mode
|
|
|
|
*
|
|
|
|
* Show stderr in output
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function disableQuietMode()
|
|
|
|
{
|
|
|
|
$this->quiet_mode = false;
|
|
|
|
}
|
|
|
|
|
2013-12-28 20:27:02 +00:00
|
|
|
/**
|
|
|
|
* Returns whether Quiet Mode is enabled or not
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::enableQuietMode()
|
|
|
|
* @see self::disableQuietMode()
|
2013-12-28 20:27:02 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2013-12-28 20:27:02 +00:00
|
|
|
*/
|
|
|
|
function isQuietModeEnabled()
|
|
|
|
{
|
|
|
|
return $this->quiet_mode;
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:04:52 +00:00
|
|
|
/**
|
|
|
|
* Enable request-pty when using exec()
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function enablePTY()
|
|
|
|
{
|
|
|
|
$this->request_pty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable request-pty when using exec()
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function disablePTY()
|
|
|
|
{
|
2016-12-17 22:09:48 +00:00
|
|
|
if ($this->in_request_pty_exec) {
|
|
|
|
$this->_close_channel(NET_SSH2_CHANNEL_EXEC);
|
|
|
|
$this->in_request_pty_exec = false;
|
|
|
|
}
|
2013-02-08 22:04:52 +00:00
|
|
|
$this->request_pty = false;
|
|
|
|
}
|
|
|
|
|
2013-12-28 20:27:02 +00:00
|
|
|
/**
|
|
|
|
* Returns whether request-pty is enabled or not
|
|
|
|
*
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::enablePTY()
|
|
|
|
* @see self::disablePTY()
|
2013-12-28 20:27:02 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2013-12-28 20:27:02 +00:00
|
|
|
*/
|
|
|
|
function isPTYEnabled()
|
|
|
|
{
|
|
|
|
return $this->request_pty;
|
|
|
|
}
|
|
|
|
|
2009-05-23 14:42:17 +00:00
|
|
|
/**
|
|
|
|
* Gets channel data
|
|
|
|
*
|
|
|
|
* Returns the data as a string if it's available and false if not.
|
|
|
|
*
|
2009-12-14 18:14:54 +00:00
|
|
|
* @param $client_channel
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return mixed
|
2009-05-23 14:42:17 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2011-05-08 23:53:30 +00:00
|
|
|
function _get_channel_packet($client_channel, $skip_extended = false)
|
2009-05-23 14:42:17 +00:00
|
|
|
{
|
2013-01-13 17:36:57 +00:00
|
|
|
if (!empty($this->channel_buffers[$client_channel])) {
|
2009-12-14 18:14:54 +00:00
|
|
|
return array_shift($this->channel_buffers[$client_channel]);
|
|
|
|
}
|
|
|
|
|
2009-05-23 14:42:17 +00:00
|
|
|
while (true) {
|
2017-08-29 03:37:35 +00:00
|
|
|
if ($this->binary_packet_buffer !== false) {
|
|
|
|
$response = $this->binary_packet_buffer;
|
|
|
|
$this->binary_packet_buffer = false;
|
|
|
|
} else {
|
2018-12-16 17:44:58 +00:00
|
|
|
$read = array($this->fsock);
|
|
|
|
$write = $except = null;
|
|
|
|
|
|
|
|
if (!$this->curTimeout) {
|
|
|
|
@stream_select($read, $write, $except, null);
|
|
|
|
} else {
|
2017-08-29 03:37:35 +00:00
|
|
|
if ($this->curTimeout < 0) {
|
|
|
|
$this->is_timeout = true;
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-21 01:43:05 +00:00
|
|
|
|
2017-08-29 03:37:35 +00:00
|
|
|
$read = array($this->fsock);
|
|
|
|
$write = $except = null;
|
2012-03-03 17:49:16 +00:00
|
|
|
|
2017-08-29 03:37:35 +00:00
|
|
|
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
|
|
|
|
$sec = floor($this->curTimeout);
|
|
|
|
$usec = 1000000 * ($this->curTimeout - $sec);
|
|
|
|
// on windows this returns a "Warning: Invalid CRT parameters detected" error
|
|
|
|
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
|
|
|
|
$this->is_timeout = true;
|
2019-07-12 12:36:12 +00:00
|
|
|
if ($client_channel == NET_SSH2_CHANNEL_EXEC && !$this->request_pty) {
|
|
|
|
$this->_close_channel($client_channel);
|
|
|
|
}
|
2017-08-29 03:37:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
|
|
|
|
$this->curTimeout-= $elapsed;
|
2012-03-03 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 06:12:06 +00:00
|
|
|
$response = $this->_get_binary_packet(true);
|
2017-08-29 03:37:35 +00:00
|
|
|
if ($response === false) {
|
2018-10-06 02:27:59 +00:00
|
|
|
$this->bitmap = 0;
|
2017-08-29 03:37:35 +00:00
|
|
|
user_error('Connection closed by server');
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-23 14:42:17 +00:00
|
|
|
}
|
2017-08-29 03:37:35 +00:00
|
|
|
|
2013-07-14 09:09:16 +00:00
|
|
|
if ($client_channel == -1 && $response === true) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-23 03:13:17 +00:00
|
|
|
if (!strlen($response)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
extract(unpack('Ctype', $this->_string_shift($response, 1)));
|
2013-07-18 04:21:25 +00:00
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
if ($type == NET_SSH2_MSG_CHANNEL_OPEN) {
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
} else {
|
|
|
|
extract(unpack('Nchannel', $this->_string_shift($response, 4)));
|
2013-05-14 03:37:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 02:44:31 +00:00
|
|
|
// will not be setup yet on incoming channel open request
|
|
|
|
if (isset($channel) && isset($this->channel_status[$channel]) && isset($this->window_size_server_to_client[$channel])) {
|
|
|
|
$this->window_size_server_to_client[$channel]-= strlen($response);
|
|
|
|
|
|
|
|
// resize the window, if appropriate
|
|
|
|
if ($this->window_size_server_to_client[$channel] < 0) {
|
|
|
|
$packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$channel], $this->window_size);
|
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
2009-12-14 18:14:54 +00:00
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
$this->window_size_server_to_client[$channel]+= $this->window_size;
|
|
|
|
}
|
|
|
|
|
2017-12-25 16:26:26 +00:00
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA:
|
|
|
|
/*
|
|
|
|
if ($client_channel == NET_SSH2_CHANNEL_EXEC) {
|
|
|
|
$this->_send_channel_packet($client_channel, chr(0));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// currently, there's only one possible value for $data_type_code: NET_SSH2_EXTENDED_DATA_STDERR
|
|
|
|
if (strlen($response) < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract(unpack('Ndata_type_code/Nlength', $this->_string_shift($response, 8)));
|
|
|
|
$data = $this->_string_shift($response, $length);
|
|
|
|
$this->stdErrorLog.= $data;
|
|
|
|
if ($skip_extended || $this->quiet_mode) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
if ($client_channel == $channel && $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_DATA) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
if (!isset($this->channel_buffers[$channel])) {
|
|
|
|
$this->channel_buffers[$channel] = array();
|
|
|
|
}
|
|
|
|
$this->channel_buffers[$channel][] = $data;
|
|
|
|
|
|
|
|
continue 2;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_REQUEST:
|
|
|
|
if ($this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_CLOSE) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$value = $this->_string_shift($response, $length);
|
|
|
|
switch ($value) {
|
|
|
|
case 'exit-signal':
|
|
|
|
$this->_string_shift($response, 1);
|
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
$this->errors[] = 'SSH_MSG_CHANNEL_REQUEST (exit-signal): ' . $this->_string_shift($response, $length);
|
|
|
|
$this->_string_shift($response, 1);
|
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
|
|
|
if ($length) {
|
|
|
|
$this->errors[count($this->errors)].= "\r\n" . $this->_string_shift($response, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
|
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
|
|
|
|
|
|
|
|
$this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_EOF;
|
|
|
|
|
|
|
|
continue 3;
|
|
|
|
case 'exit-status':
|
|
|
|
if (strlen($response) < 5) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract(unpack('Cfalse/Nexit_status', $this->_string_shift($response, 5)));
|
|
|
|
$this->exit_status = $exit_status;
|
2017-11-09 06:12:06 +00:00
|
|
|
|
2017-12-25 16:26:26 +00:00
|
|
|
// "The client MAY ignore these messages."
|
|
|
|
// -- http://tools.ietf.org/html/rfc4254#section-6.10
|
|
|
|
|
|
|
|
continue 3;
|
|
|
|
default:
|
|
|
|
// "Some systems may not implement signals, in which case they SHOULD ignore this message."
|
|
|
|
// -- http://tools.ietf.org/html/rfc4254#section-6.9
|
|
|
|
continue 3;
|
|
|
|
}
|
2017-11-09 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 02:44:31 +00:00
|
|
|
switch ($this->channel_status[$channel]) {
|
2015-01-12 09:13:33 +00:00
|
|
|
case NET_SSH2_MSG_CHANNEL_OPEN:
|
2014-12-30 02:44:31 +00:00
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
extract(unpack('Nserver_channel', $this->_string_shift($response, 4)));
|
|
|
|
$this->server_channels[$channel] = $server_channel;
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
extract(unpack('Nwindow_size', $this->_string_shift($response, 4)));
|
2015-06-25 04:45:14 +00:00
|
|
|
if ($window_size < 0) {
|
|
|
|
$window_size&= 0x7FFFFFFF;
|
|
|
|
$window_size+= 0x80000000;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
$this->window_size_client_to_server[$channel] = $window_size;
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-30 02:44:31 +00:00
|
|
|
$temp = unpack('Npacket_size_client_to_server', $this->_string_shift($response, 4));
|
|
|
|
$this->packet_size_client_to_server[$channel] = $temp['packet_size_client_to_server'];
|
2015-02-06 03:28:23 +00:00
|
|
|
$result = $client_channel == $channel ? true : $this->_get_channel_packet($client_channel, $skip_extended);
|
|
|
|
$this->_on_channel_open();
|
|
|
|
return $result;
|
2014-12-30 02:44:31 +00:00
|
|
|
//case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE:
|
|
|
|
default:
|
|
|
|
user_error('Unable to open channel');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_REQUEST:
|
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_SUCCESS:
|
|
|
|
return true;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_FAILURE:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
user_error('Unable to fulfill channel request');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
case NET_SSH2_MSG_CHANNEL_CLOSE:
|
|
|
|
return $type == NET_SSH2_MSG_CHANNEL_CLOSE ? true : $this->_get_channel_packet($client_channel, $skip_extended);
|
|
|
|
}
|
2009-12-14 18:14:54 +00:00
|
|
|
}
|
2009-05-23 14:42:17 +00:00
|
|
|
|
2013-07-14 09:09:16 +00:00
|
|
|
// ie. $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_DATA
|
|
|
|
|
2009-05-23 14:42:17 +00:00
|
|
|
switch ($type) {
|
|
|
|
case NET_SSH2_MSG_CHANNEL_DATA:
|
2011-02-13 07:46:01 +00:00
|
|
|
/*
|
2013-07-14 09:09:16 +00:00
|
|
|
if ($channel == NET_SSH2_CHANNEL_EXEC) {
|
2009-12-14 18:14:54 +00:00
|
|
|
// SCP requires null packets, such as this, be sent. further, in the case of the ssh.com SSH server
|
2013-12-25 23:33:08 +00:00
|
|
|
// this actually seems to make things twice as fast. more to the point, the message right after
|
2009-12-14 18:14:54 +00:00
|
|
|
// SSH_MSG_CHANNEL_DATA (usually SSH_MSG_IGNORE) won't block for as long as it would have otherwise.
|
|
|
|
// in OpenSSH it slows things down but only by a couple thousandths of a second.
|
2013-07-14 09:09:16 +00:00
|
|
|
$this->_send_channel_packet($channel, chr(0));
|
2009-12-14 18:14:54 +00:00
|
|
|
}
|
2011-02-13 07:46:01 +00:00
|
|
|
*/
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($response) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 19:06:07 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($response, 4)));
|
2009-12-14 18:14:54 +00:00
|
|
|
$data = $this->_string_shift($response, $length);
|
2014-12-30 02:44:31 +00:00
|
|
|
|
|
|
|
if ($channel == NET_SSH2_CHANNEL_AGENT_FORWARD) {
|
|
|
|
$agent_response = $this->agent->_forward_data($data);
|
|
|
|
if (!is_bool($agent_response)) {
|
|
|
|
$this->_send_channel_packet($channel, $agent_response);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-14 18:14:54 +00:00
|
|
|
if ($client_channel == $channel) {
|
|
|
|
return $data;
|
|
|
|
}
|
2013-07-14 09:09:16 +00:00
|
|
|
if (!isset($this->channel_buffers[$channel])) {
|
|
|
|
$this->channel_buffers[$channel] = array();
|
2009-05-23 14:42:17 +00:00
|
|
|
}
|
2013-07-14 09:09:16 +00:00
|
|
|
$this->channel_buffers[$channel][] = $data;
|
2009-05-23 14:42:17 +00:00
|
|
|
break;
|
|
|
|
case NET_SSH2_MSG_CHANNEL_CLOSE:
|
2012-06-11 02:52:30 +00:00
|
|
|
$this->curTimeout = 0;
|
|
|
|
|
|
|
|
if ($this->bitmap & NET_SSH2_MASK_SHELL) {
|
|
|
|
$this->bitmap&= ~NET_SSH2_MASK_SHELL;
|
|
|
|
}
|
|
|
|
if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) {
|
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
|
2016-06-29 05:30:28 +00:00
|
|
|
if ($client_channel == $channel) {
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-23 14:42:17 +00:00
|
|
|
case NET_SSH2_MSG_CHANNEL_EOF:
|
|
|
|
break;
|
|
|
|
default:
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Error reading channel data');
|
2009-05-23 14:42:17 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Sends Binary Packets
|
|
|
|
*
|
|
|
|
* See '6. Binary Packet Protocol' of rfc4253 for more info.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $data
|
|
|
|
* @param string $logged
|
2015-10-11 17:22:07 +00:00
|
|
|
* @see self::_get_binary_packet()
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return bool
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-12-03 17:54:43 +00:00
|
|
|
function _send_binary_packet($data, $logged = null)
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2012-04-24 05:01:55 +00:00
|
|
|
if (!is_resource($this->fsock) || feof($this->fsock)) {
|
2013-11-15 18:34:31 +00:00
|
|
|
$this->bitmap = 0;
|
2018-11-09 15:36:50 +00:00
|
|
|
user_error('Connection closed prematurely');
|
2007-07-23 05:21:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-16 17:09:37 +00:00
|
|
|
//if ($this->compress) {
|
|
|
|
// // the -4 removes the checksum:
|
|
|
|
// // http://php.net/function.gzcompress#57710
|
|
|
|
// $data = substr(gzcompress($data), 0, -4);
|
|
|
|
//}
|
|
|
|
|
2009-02-16 22:22:13 +00:00
|
|
|
// 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9
|
2007-07-23 05:21:39 +00:00
|
|
|
$packet_length = strlen($data) + 9;
|
2009-02-16 22:22:13 +00:00
|
|
|
// round up to the nearest $this->encrypt_block_size
|
|
|
|
$packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size;
|
2007-07-23 05:21:39 +00:00
|
|
|
// subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length
|
|
|
|
$padding_length = $packet_length - strlen($data) - 5;
|
2012-12-16 08:20:16 +00:00
|
|
|
$padding = crypt_random_string($padding_length);
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
// we subtract 4 from packet_length because the packet_length field isn't supposed to include itself
|
|
|
|
$packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);
|
|
|
|
|
2009-02-26 17:25:03 +00:00
|
|
|
$hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : '';
|
2008-05-26 19:42:01 +00:00
|
|
|
$this->send_seq_no++;
|
2007-07-23 05:21:39 +00:00
|
|
|
|
|
|
|
if ($this->encrypt !== false) {
|
|
|
|
$packet = $this->encrypt->encrypt($packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
$packet.= $hmac;
|
|
|
|
|
2009-08-29 19:23:25 +00:00
|
|
|
$start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
|
|
|
|
$result = strlen($packet) == fputs($this->fsock, $packet);
|
|
|
|
$stop = strtok(microtime(), ' ') + strtok('');
|
|
|
|
|
|
|
|
if (defined('NET_SSH2_LOGGING')) {
|
2012-11-22 19:08:30 +00:00
|
|
|
$current = strtok(microtime(), ' ') + strtok('');
|
2012-04-30 16:25:37 +00:00
|
|
|
$message_number = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')';
|
|
|
|
$message_number = '-> ' . $message_number .
|
2012-11-22 19:08:30 +00:00
|
|
|
' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
|
2013-09-12 13:45:13 +00:00
|
|
|
$this->_append_log($message_number, isset($logged) ? $logged : $data);
|
2012-11-22 19:08:30 +00:00
|
|
|
$this->last_packet = $current;
|
2009-08-29 19:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
2011-06-04 17:06:53 +00:00
|
|
|
/**
|
|
|
|
* Logs data packets
|
|
|
|
*
|
|
|
|
* Makes sure that only the last 1MB worth of packets will be logged
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $data
|
2011-06-04 17:06:53 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2012-04-30 16:25:37 +00:00
|
|
|
function _append_log($message_number, $message)
|
2011-06-04 17:06:53 +00:00
|
|
|
{
|
2013-12-26 10:45:24 +00:00
|
|
|
// remove the byte identifying the message type from all but the first two messages (ie. the identification strings)
|
|
|
|
if (strlen($message_number) > 2) {
|
|
|
|
$this->_string_shift($message);
|
|
|
|
}
|
2013-07-26 14:57:20 +00:00
|
|
|
|
2013-12-26 10:45:24 +00:00
|
|
|
switch (NET_SSH2_LOGGING) {
|
|
|
|
// useful for benchmarks
|
|
|
|
case NET_SSH2_LOG_SIMPLE:
|
|
|
|
$this->message_number_log[] = $message_number;
|
|
|
|
break;
|
|
|
|
// the most useful log for SSH2
|
|
|
|
case NET_SSH2_LOG_COMPLEX:
|
|
|
|
$this->message_number_log[] = $message_number;
|
|
|
|
$this->log_size+= strlen($message);
|
|
|
|
$this->message_log[] = $message;
|
|
|
|
while ($this->log_size > NET_SSH2_LOG_MAX_SIZE) {
|
|
|
|
$this->log_size-= strlen(array_shift($this->message_log));
|
|
|
|
array_shift($this->message_number_log);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// dump the output out realtime; packets may be interspersed with non packets,
|
|
|
|
// passwords won't be filtered out and select other packets may not be correctly
|
|
|
|
// identified
|
|
|
|
case NET_SSH2_LOG_REALTIME:
|
|
|
|
switch (PHP_SAPI) {
|
|
|
|
case 'cli':
|
|
|
|
$start = $stop = "\r\n";
|
2012-04-30 16:25:37 +00:00
|
|
|
break;
|
2013-12-26 10:45:24 +00:00
|
|
|
default:
|
|
|
|
$start = '<pre>';
|
|
|
|
$stop = '</pre>';
|
|
|
|
}
|
|
|
|
echo $start . $this->_format_log(array($message), array($message_number)) . $stop;
|
|
|
|
@flush();
|
|
|
|
@ob_flush();
|
|
|
|
break;
|
|
|
|
// basically the same thing as NET_SSH2_LOG_REALTIME with the caveat that NET_SSH2_LOG_REALTIME_FILE
|
|
|
|
// needs to be defined and that the resultant log file will be capped out at NET_SSH2_LOG_MAX_SIZE.
|
|
|
|
// the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily
|
|
|
|
// at the beginning of the file
|
|
|
|
case NET_SSH2_LOG_REALTIME_FILE:
|
|
|
|
if (!isset($this->realtime_log_file)) {
|
|
|
|
// PHP doesn't seem to like using constants in fopen()
|
|
|
|
$filename = NET_SSH2_LOG_REALTIME_FILENAME;
|
|
|
|
$fp = fopen($filename, 'w');
|
|
|
|
$this->realtime_log_file = $fp;
|
|
|
|
}
|
|
|
|
if (!is_resource($this->realtime_log_file)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$entry = $this->_format_log(array($message), array($message_number));
|
|
|
|
if ($this->realtime_log_wrap) {
|
|
|
|
$temp = "<<< START >>>\r\n";
|
|
|
|
$entry.= $temp;
|
|
|
|
fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp));
|
|
|
|
}
|
|
|
|
$this->realtime_log_size+= strlen($entry);
|
|
|
|
if ($this->realtime_log_size > NET_SSH2_LOG_MAX_SIZE) {
|
|
|
|
fseek($this->realtime_log_file, 0);
|
|
|
|
$this->realtime_log_size = strlen($entry);
|
|
|
|
$this->realtime_log_wrap = true;
|
|
|
|
}
|
|
|
|
fputs($this->realtime_log_file, $entry);
|
|
|
|
}
|
2011-06-04 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 03:19:20 +00:00
|
|
|
/**
|
|
|
|
* Sends channel data
|
|
|
|
*
|
|
|
|
* Spans multiple SSH_MSG_CHANNEL_DATAs if appropriate
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $client_channel
|
|
|
|
* @param string $data
|
|
|
|
* @return bool
|
2009-09-17 03:19:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2009-12-14 18:14:54 +00:00
|
|
|
function _send_channel_packet($client_channel, $data)
|
2009-09-17 03:19:20 +00:00
|
|
|
{
|
2014-12-06 04:01:39 +00:00
|
|
|
while (strlen($data)) {
|
2013-09-12 13:29:14 +00:00
|
|
|
if (!$this->window_size_client_to_server[$client_channel]) {
|
|
|
|
$this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
|
|
|
|
// using an invalid channel will let the buffers be built up for the valid channels
|
2014-12-06 04:01:39 +00:00
|
|
|
$this->_get_channel_packet(-1);
|
2013-09-12 13:29:14 +00:00
|
|
|
$this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
|
|
|
|
}
|
|
|
|
|
2014-12-06 04:01:39 +00:00
|
|
|
/* The maximum amount of data allowed is determined by the maximum
|
|
|
|
packet size for the channel, and the current window size, whichever
|
|
|
|
is smaller.
|
|
|
|
-- http://tools.ietf.org/html/rfc4254#section-5.2 */
|
|
|
|
$max_size = min(
|
|
|
|
$this->packet_size_client_to_server[$client_channel],
|
|
|
|
$this->window_size_client_to_server[$client_channel]
|
|
|
|
);
|
|
|
|
|
2014-03-21 07:53:43 +00:00
|
|
|
$temp = $this->_string_shift($data, $max_size);
|
2015-07-15 01:52:31 +00:00
|
|
|
$packet = pack(
|
|
|
|
'CN2a*',
|
2009-09-17 03:19:20 +00:00
|
|
|
NET_SSH2_MSG_CHANNEL_DATA,
|
2009-12-14 18:14:54 +00:00
|
|
|
$this->server_channels[$client_channel],
|
2014-03-21 07:53:43 +00:00
|
|
|
strlen($temp),
|
|
|
|
$temp
|
2009-09-17 03:19:20 +00:00
|
|
|
);
|
2014-08-30 20:26:23 +00:00
|
|
|
$this->window_size_client_to_server[$client_channel]-= strlen($temp);
|
2009-09-17 03:19:20 +00:00
|
|
|
if (!$this->_send_binary_packet($packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-14 09:09:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 04:01:39 +00:00
|
|
|
return true;
|
2009-09-17 03:19:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 07:46:01 +00:00
|
|
|
/**
|
|
|
|
* Closes and flushes a channel
|
|
|
|
*
|
|
|
|
* Net_SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server
|
|
|
|
* and for SFTP channels are presumably closed when the client disconnects. This functions is intended
|
|
|
|
* for SCP more than anything.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $client_channel
|
|
|
|
* @param bool $want_reply
|
|
|
|
* @return bool
|
2011-02-13 07:46:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2013-12-15 06:43:20 +00:00
|
|
|
function _close_channel($client_channel, $want_reply = false)
|
2011-02-13 07:46:01 +00:00
|
|
|
{
|
|
|
|
// see http://tools.ietf.org/html/rfc4254#section-5.3
|
|
|
|
|
2012-03-05 15:32:15 +00:00
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
|
2012-03-05 02:06:13 +00:00
|
|
|
|
2013-12-15 06:43:20 +00:00
|
|
|
if (!$want_reply) {
|
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
|
|
|
|
}
|
2011-02-13 07:46:01 +00:00
|
|
|
|
2012-03-03 17:49:16 +00:00
|
|
|
$this->channel_status[$client_channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
|
|
|
|
|
|
|
|
$this->curTimeout = 0;
|
|
|
|
|
2015-07-15 01:52:31 +00:00
|
|
|
while (!is_bool($this->_get_channel_packet($client_channel))) {
|
|
|
|
}
|
2012-03-03 17:49:16 +00:00
|
|
|
|
2013-12-15 06:43:20 +00:00
|
|
|
if ($want_reply) {
|
|
|
|
$this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
|
|
|
|
}
|
|
|
|
|
2012-03-03 17:49:16 +00:00
|
|
|
if ($this->bitmap & NET_SSH2_MASK_SHELL) {
|
|
|
|
$this->bitmap&= ~NET_SSH2_MASK_SHELL;
|
|
|
|
}
|
2011-02-13 07:46:01 +00:00
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Disconnect
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $reason
|
|
|
|
* @return bool
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _disconnect($reason)
|
|
|
|
{
|
2014-12-29 14:37:44 +00:00
|
|
|
if ($this->bitmap & NET_SSH2_MASK_CONNECTED) {
|
2008-05-15 17:40:03 +00:00
|
|
|
$data = pack('CNNa*Na*', NET_SSH2_MSG_DISCONNECT, $reason, 0, '', 0, '');
|
|
|
|
$this->_send_binary_packet($data);
|
|
|
|
}
|
2019-09-10 22:31:43 +00:00
|
|
|
|
|
|
|
$this->bitmap = 0;
|
|
|
|
fclose($this->fsock);
|
|
|
|
|
|
|
|
return false;
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String Shift
|
|
|
|
*
|
|
|
|
* Inspired by array_shift
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param string $string
|
|
|
|
* @param int $index
|
|
|
|
* @return string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _string_shift(&$string, $index = 1)
|
|
|
|
{
|
|
|
|
$substr = substr($string, 0, $index);
|
|
|
|
$string = substr($string, $index);
|
|
|
|
return $substr;
|
|
|
|
}
|
|
|
|
|
2008-05-26 19:42:01 +00:00
|
|
|
/**
|
|
|
|
* Define Array
|
|
|
|
*
|
|
|
|
* Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of
|
|
|
|
* named constants from it, using the value as the name of the constant and the index as the value of the constant.
|
|
|
|
* If any of the constants that would be defined already exists, none of the constants will be defined.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param array $array
|
2008-05-26 19:42:01 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _define_array()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
foreach ($args as $arg) {
|
2015-07-15 01:52:31 +00:00
|
|
|
foreach ($arg as $key => $value) {
|
2008-05-26 19:42:01 +00:00
|
|
|
if (!defined($value)) {
|
|
|
|
define($value, $key);
|
|
|
|
} else {
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-25 22:29:42 +00:00
|
|
|
/**
|
|
|
|
* Returns a log of the packets that have been sent and received.
|
|
|
|
*
|
2010-04-07 03:50:54 +00:00
|
|
|
* 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')
|
2009-03-25 22:29:42 +00:00
|
|
|
*
|
|
|
|
* @access public
|
2015-09-06 11:28:08 +00:00
|
|
|
* @return array|false|string
|
2009-03-25 22:29:42 +00:00
|
|
|
*/
|
2010-04-07 03:50:54 +00:00
|
|
|
function getLog()
|
2009-03-25 22:29:42 +00:00
|
|
|
{
|
2010-04-07 03:50:54 +00:00
|
|
|
if (!defined('NET_SSH2_LOGGING')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (NET_SSH2_LOGGING) {
|
|
|
|
case NET_SSH2_LOG_SIMPLE:
|
|
|
|
return $this->message_number_log;
|
|
|
|
case NET_SSH2_LOG_COMPLEX:
|
2017-03-02 04:14:58 +00:00
|
|
|
$log = $this->_format_log($this->message_log, $this->message_number_log);
|
|
|
|
return PHP_SAPI == 'cli' ? $log : '<pre>' . $log . '</pre>';
|
2010-04-07 03:50:54 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2009-03-25 22:29:42 +00:00
|
|
|
}
|
2010-04-07 03:50:54 +00:00
|
|
|
}
|
2009-03-25 22:29:42 +00:00
|
|
|
|
2010-04-07 03:50:54 +00:00
|
|
|
/**
|
|
|
|
* Formats a log for printing
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param array $message_log
|
|
|
|
* @param array $message_number_log
|
2010-04-07 03:50:54 +00:00
|
|
|
* @access private
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2010-04-07 03:50:54 +00:00
|
|
|
*/
|
|
|
|
function _format_log($message_log, $message_number_log)
|
|
|
|
{
|
2009-03-25 22:29:42 +00:00
|
|
|
$output = '';
|
2010-04-07 03:50:54 +00:00
|
|
|
for ($i = 0; $i < count($message_log); $i++) {
|
|
|
|
$output.= $message_number_log[$i] . "\r\n";
|
|
|
|
$current_log = $message_log[$i];
|
2010-04-24 06:40:49 +00:00
|
|
|
$j = 0;
|
2009-03-25 22:29:42 +00:00
|
|
|
do {
|
2013-01-12 16:46:19 +00:00
|
|
|
if (strlen($current_log)) {
|
2010-04-24 06:40:49 +00:00
|
|
|
$output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
|
|
|
|
}
|
2013-12-16 17:27:12 +00:00
|
|
|
$fragment = $this->_string_shift($current_log, $this->log_short_width);
|
|
|
|
$hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary));
|
2009-03-25 22:29:42 +00:00
|
|
|
// replace non ASCII printable characters with dots
|
|
|
|
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
|
2010-04-25 14:19:43 +00:00
|
|
|
// also replace < with a . since < messes up the output on web browsers
|
|
|
|
$raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
|
2013-12-16 17:27:12 +00:00
|
|
|
$output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n";
|
2010-04-24 06:40:49 +00:00
|
|
|
$j++;
|
2013-01-12 16:46:19 +00:00
|
|
|
} while (strlen($current_log));
|
2009-03-25 22:29:42 +00:00
|
|
|
$output.= "\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:27:12 +00:00
|
|
|
/**
|
|
|
|
* Helper function for _format_log
|
|
|
|
*
|
|
|
|
* For use with preg_replace_callback()
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param array $matches
|
2013-12-16 17:27:12 +00:00
|
|
|
* @access private
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2013-12-16 17:27:12 +00:00
|
|
|
*/
|
|
|
|
function _format_log_helper($matches)
|
|
|
|
{
|
|
|
|
return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
|
|
|
|
}
|
|
|
|
|
2015-02-06 03:28:23 +00:00
|
|
|
/**
|
|
|
|
* Helper function for agent->_on_channel_open()
|
|
|
|
*
|
|
|
|
* Used when channels are created to inform agent
|
|
|
|
* of said channel opening. Must be called after
|
|
|
|
* channel open confirmation received
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _on_channel_open()
|
|
|
|
{
|
|
|
|
if (isset($this->agent)) {
|
|
|
|
$this->agent->_on_channel_open($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 16:20:42 +00:00
|
|
|
/**
|
|
|
|
* Returns the first value of the intersection of two arrays or false if
|
|
|
|
* the intersection is empty. The order is defined by the first parameter.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param array $array1
|
|
|
|
* @param array $array2
|
|
|
|
* @return mixed False if intersection is empty, else intersected value.
|
2015-07-17 16:20:42 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _array_intersect_first($array1, $array2)
|
|
|
|
{
|
|
|
|
foreach ($array1 as $value) {
|
|
|
|
if (in_array($value, $array2)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
2010-02-12 23:02:13 +00:00
|
|
|
* Returns all errors
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2016-04-13 09:59:55 +00:00
|
|
|
* @return string[]
|
2010-02-12 23:02:13 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getErrors()
|
|
|
|
{
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last error
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2009-03-25 22:29:42 +00:00
|
|
|
* @access public
|
2007-07-23 05:21:39 +00:00
|
|
|
*/
|
2010-02-12 23:02:13 +00:00
|
|
|
function getLastError()
|
2007-07-23 05:21:39 +00:00
|
|
|
{
|
2015-11-19 20:11:43 +00:00
|
|
|
$count = count($this->errors);
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
return $this->errors[$count - 1];
|
|
|
|
}
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the server identification.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getServerIdentification()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->server_identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the key exchange algorithms the server supports.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getKexAlgorithms()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->kex_algorithms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the host key (public key) algorithms the server supports.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getServerHostKeyAlgorithms()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->server_host_key_algorithms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getEncryptionAlgorithmsClient2Server()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->encryption_algorithms_client_to_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getEncryptionAlgorithmsServer2Client()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->encryption_algorithms_server_to_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the MAC algorithms the server supports, when receiving stuff from the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getMACAlgorithmsClient2Server()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->mac_algorithms_client_to_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the MAC algorithms the server supports, when sending stuff to the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getMACAlgorithmsServer2Client()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->mac_algorithms_server_to_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the compression algorithms the server supports, when receiving stuff from the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getCompressionAlgorithmsClient2Server()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->compression_algorithms_client_to_server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the compression algorithms the server supports, when sending stuff to the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getCompressionAlgorithmsServer2Client()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->compression_algorithms_server_to_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the languages the server supports, when sending stuff to the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getLanguagesServer2Client()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->languages_server_to_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the languages the server supports, when receiving stuff from the client.
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return array
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getLanguagesClient2Server()
|
|
|
|
{
|
2014-08-05 01:11:34 +00:00
|
|
|
$this->_connect();
|
2014-07-25 15:03:31 +00:00
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
return $this->languages_client_to_server;
|
|
|
|
}
|
|
|
|
|
2019-09-08 00:08:00 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of algorithms the server supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function getServerAlgorithms()
|
|
|
|
{
|
|
|
|
$this->_connect();
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'kex' => $this->kex_algorithms,
|
|
|
|
'hostkey' => $this->server_host_key_algorithms,
|
|
|
|
'client_to_server' => array(
|
|
|
|
'crypt' => $this->encryption_algorithms_client_to_server,
|
|
|
|
'mac' => $this->mac_algorithms_client_to_server,
|
|
|
|
'comp' => $this->compression_algorithms_client_to_server,
|
|
|
|
'lang' => $this->languages_client_to_server
|
|
|
|
),
|
|
|
|
'server_to_client' => array(
|
|
|
|
'crypt' => $this->encryption_algorithms_server_to_client,
|
|
|
|
'mac' => $this->mac_algorithms_server_to_client,
|
|
|
|
'comp' => $this->compression_algorithms_server_to_client,
|
|
|
|
'lang' => $this->languages_server_to_client
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of KEX algorithms that phpseclib supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getSupportedKEXAlgorithms()
|
|
|
|
{
|
|
|
|
$kex_algorithms = array(
|
|
|
|
'diffie-hellman-group-exchange-sha256',// RFC 4419
|
|
|
|
'diffie-hellman-group-exchange-sha1', // RFC 4419
|
|
|
|
|
|
|
|
// Diffie-Hellman Key Agreement (DH) using integer modulo prime
|
|
|
|
// groups.
|
|
|
|
'diffie-hellman-group14-sha1', // REQUIRED
|
|
|
|
'diffie-hellman-group1-sha1', // REQUIRED
|
|
|
|
);
|
|
|
|
|
|
|
|
return $kex_algorithms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of host key algorithms that phpseclib supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getSupportedHostKeyAlgorithms()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'rsa-sha2-256', // RFC 8332
|
|
|
|
'rsa-sha2-512', // RFC 8332
|
|
|
|
'ssh-rsa', // RECOMMENDED sign Raw RSA Key
|
|
|
|
'ssh-dss' // REQUIRED sign Raw DSS Key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of symmetric key algorithms that phpseclib supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getSupportedEncryptionAlgorithms()
|
|
|
|
{
|
|
|
|
$algos = array(
|
|
|
|
// from <http://tools.ietf.org/html/rfc4345#section-4>:
|
|
|
|
'arcfour256',
|
|
|
|
'arcfour128',
|
|
|
|
|
|
|
|
//'arcfour', // OPTIONAL the ARCFOUR stream cipher with a 128-bit key
|
|
|
|
|
|
|
|
// CTR modes from <http://tools.ietf.org/html/rfc4344#section-4>:
|
|
|
|
'aes128-ctr', // RECOMMENDED AES (Rijndael) in SDCTR mode, with 128-bit key
|
|
|
|
'aes192-ctr', // RECOMMENDED AES with 192-bit key
|
|
|
|
'aes256-ctr', // RECOMMENDED AES with 256-bit key
|
|
|
|
|
|
|
|
'twofish128-ctr', // OPTIONAL Twofish in SDCTR mode, with 128-bit key
|
|
|
|
'twofish192-ctr', // OPTIONAL Twofish with 192-bit key
|
|
|
|
'twofish256-ctr', // OPTIONAL Twofish with 256-bit key
|
|
|
|
|
|
|
|
'aes128-cbc', // RECOMMENDED AES with a 128-bit key
|
|
|
|
'aes192-cbc', // OPTIONAL AES with a 192-bit key
|
|
|
|
'aes256-cbc', // OPTIONAL AES in CBC mode, with a 256-bit key
|
|
|
|
|
|
|
|
'twofish128-cbc', // OPTIONAL Twofish with a 128-bit key
|
|
|
|
'twofish192-cbc', // OPTIONAL Twofish with a 192-bit key
|
|
|
|
'twofish256-cbc',
|
|
|
|
'twofish-cbc', // OPTIONAL alias for "twofish256-cbc"
|
|
|
|
// (this is being retained for historical reasons)
|
|
|
|
|
|
|
|
'blowfish-ctr', // OPTIONAL Blowfish in SDCTR mode
|
|
|
|
|
|
|
|
'blowfish-cbc', // OPTIONAL Blowfish in CBC mode
|
|
|
|
|
|
|
|
'3des-ctr', // RECOMMENDED Three-key 3DES in SDCTR mode
|
|
|
|
|
|
|
|
'3des-cbc', // REQUIRED three-key 3DES in CBC mode
|
|
|
|
|
|
|
|
//'none' // OPTIONAL no encryption; NOT RECOMMENDED
|
|
|
|
);
|
|
|
|
|
|
|
|
$engines = array(
|
|
|
|
CRYPT_ENGINE_OPENSSL,
|
|
|
|
CRYPT_ENGINE_MCRYPT,
|
|
|
|
CRYPT_ENGINE_INTERNAL
|
|
|
|
);
|
|
|
|
|
|
|
|
$ciphers = array();
|
|
|
|
foreach ($engines as $engine) {
|
|
|
|
foreach ($algos as $algo) {
|
|
|
|
$obj = $this->_encryption_algorithm_to_crypt_instance($algo);
|
|
|
|
if (strtolower(get_class($obj)) == 'crypt_rijndael') {
|
|
|
|
$obj->setKeyLength(preg_replace('#[^\d]#', '', $algo));
|
|
|
|
}
|
|
|
|
switch ($algo) {
|
|
|
|
case 'arcfour128':
|
|
|
|
case 'arcfour256':
|
|
|
|
if ($engine == CRYPT_ENGINE_INTERNAL) {
|
|
|
|
$algos = array_diff($algos, array($algo));
|
|
|
|
$ciphers[] = $algo;
|
|
|
|
} else {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($obj->isValidEngine($engine)) {
|
|
|
|
$algos = array_diff($algos, array($algo));
|
|
|
|
$ciphers[] = $algo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ciphers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of MAC algorithms that phpseclib supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getSupportedMACAlgorithms()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
// from <http://www.ietf.org/rfc/rfc6668.txt>:
|
|
|
|
'hmac-sha2-256',// RECOMMENDED HMAC-SHA256 (digest length = key length = 32)
|
|
|
|
|
|
|
|
'hmac-sha1-96', // RECOMMENDED first 96 bits of HMAC-SHA1 (digest length = 12, key length = 20)
|
|
|
|
'hmac-sha1', // REQUIRED HMAC-SHA1 (digest length = key length = 20)
|
|
|
|
'hmac-md5-96', // OPTIONAL first 96 bits of HMAC-MD5 (digest length = 12, key length = 16)
|
|
|
|
'hmac-md5', // OPTIONAL HMAC-MD5 (digest length = key length = 16)
|
|
|
|
//'none' // OPTIONAL no MAC; NOT RECOMMENDED
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of compression algorithms that phpseclib supports
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getSupportedCompressionAlgorithms()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'none' // REQUIRED no compression
|
|
|
|
//'zlib' // OPTIONAL ZLIB (LZ77) compression
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return list of negotiated algorithms
|
|
|
|
*
|
|
|
|
* Uses the same format as https://www.php.net/ssh2-methods-negotiated
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getAlgorithmsNegotiated()
|
|
|
|
{
|
|
|
|
$this->_connect();
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'kex' => $this->kex_algorithm,
|
|
|
|
'hostkey' => $this->signature_format,
|
|
|
|
'client_to_server' => array(
|
|
|
|
'crypt' => $this->encrypt->name,
|
|
|
|
'mac' => $this->hmac_create->name,
|
|
|
|
'comp' => 'none',
|
|
|
|
),
|
|
|
|
'server_to_client' => array(
|
|
|
|
'crypt' => $this->decrypt->name,
|
|
|
|
'mac' => $this->hmac_check->name,
|
|
|
|
'comp' => 'none',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts an associative array with up to four parameters as described at
|
|
|
|
* <https://www.php.net/manual/en/function.ssh2-connect.php>
|
|
|
|
*
|
|
|
|
* @param array $methods
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setPreferredAlgorithms($methods)
|
|
|
|
{
|
|
|
|
$preferred = $methods;
|
|
|
|
|
|
|
|
if (isset($preferred['kex'])) {
|
|
|
|
$preferred['kex'] = array_intersect(
|
|
|
|
$preferred['kex'],
|
|
|
|
$this->getSupportedKEXAlgorithms()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($preferred['hostkey'])) {
|
|
|
|
$preferred['hostkey'] = array_intersect(
|
|
|
|
$preferred['hostkey'],
|
|
|
|
$this->getSupportedHostKeyAlgorithms()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = array('client_to_server', 'server_to_client');
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
if (isset($preferred[$key])) {
|
|
|
|
$a = &$preferred[$key];
|
|
|
|
if (isset($a['crypt'])) {
|
|
|
|
$a['crypt'] = array_intersect(
|
|
|
|
$a['crypt'],
|
|
|
|
$this->getSupportedEncryptionAlgorithms()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isset($a['comp'])) {
|
|
|
|
$a['comp'] = array_intersect(
|
|
|
|
$a['comp'],
|
|
|
|
$this->getSupportedCompressionAlgorithms()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isset($a['mac'])) {
|
|
|
|
$a['mac'] = array_intersect(
|
|
|
|
$a['mac'],
|
|
|
|
$this->getSupportedMACAlgorithms()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = array(
|
|
|
|
'kex',
|
|
|
|
'hostkey',
|
|
|
|
'client_to_server/crypt',
|
|
|
|
'client_to_server/comp',
|
|
|
|
'client_to_server/mac',
|
|
|
|
'server_to_client/crypt',
|
|
|
|
'server_to_client/comp',
|
|
|
|
'server_to_client/mac',
|
|
|
|
);
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$p = $preferred;
|
|
|
|
$m = $methods;
|
|
|
|
|
|
|
|
$subkeys = explode('/', $key);
|
|
|
|
foreach ($subkeys as $subkey) {
|
|
|
|
if (!isset($p[$subkey])) {
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
$p = $p[$subkey];
|
|
|
|
$m = $m[$subkey];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($p) != count($m)) {
|
|
|
|
$diff = array_diff($m, $p);
|
|
|
|
$msg = count($diff) == 1 ?
|
|
|
|
' is not a supported algorithm' :
|
|
|
|
' are not supported algorithms';
|
|
|
|
user_error(implode(', ', $diff) . $msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->preferred = $preferred;
|
|
|
|
}
|
|
|
|
|
2013-04-28 00:58:24 +00:00
|
|
|
/**
|
|
|
|
* Returns the banner message.
|
|
|
|
*
|
|
|
|
* Quoting from the RFC, "in some jurisdictions, sending a warning message before
|
|
|
|
* authentication may be relevant for getting legal protection."
|
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return string
|
2013-04-28 00:58:24 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getBannerMessage()
|
|
|
|
{
|
|
|
|
return $this->banner_message;
|
|
|
|
}
|
|
|
|
|
2007-07-23 05:21:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the server public host key.
|
|
|
|
*
|
|
|
|
* Caching this the first time you connect to a server and checking the result on subsequent connections
|
2010-04-22 16:06:43 +00:00
|
|
|
* is recommended. Returns false if the server signature is not signed correctly with the public host key.
|
2007-07-23 05:21:39 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return mixed
|
2007-07-23 05:21:39 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getServerPublicHostKey()
|
|
|
|
{
|
2014-06-16 15:19:34 +00:00
|
|
|
if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) {
|
|
|
|
if (!$this->_connect()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 16:06:43 +00:00
|
|
|
$signature = $this->signature;
|
|
|
|
$server_public_host_key = $this->server_public_host_key;
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
extract(unpack('Nlength', $this->_string_shift($server_public_host_key, 4)));
|
|
|
|
$this->_string_shift($server_public_host_key, $length);
|
|
|
|
|
2012-07-04 18:36:26 +00:00
|
|
|
if ($this->signature_validated) {
|
|
|
|
return $this->bitmap ?
|
|
|
|
$this->signature_format . ' ' . base64_encode($this->server_public_host_key) :
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->signature_validated = true;
|
|
|
|
|
2010-04-22 16:06:43 +00:00
|
|
|
switch ($this->signature_format) {
|
|
|
|
case 'ssh-dss':
|
2013-08-31 05:22:47 +00:00
|
|
|
$zero = new Math_BigInteger();
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$p = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$q = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$g = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$y = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
|
|
|
|
|
|
|
|
/* The value for 'dss_signature_blob' is encoded as a string containing
|
|
|
|
r, followed by s (which are 160-bit integers, without lengths or
|
|
|
|
padding, unsigned, and in network byte order). */
|
|
|
|
$temp = unpack('Nlength', $this->_string_shift($signature, 4));
|
|
|
|
if ($temp['length'] != 40) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Invalid signature');
|
2010-04-22 16:06:43 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = new Math_BigInteger($this->_string_shift($signature, 20), 256);
|
|
|
|
$s = new Math_BigInteger($this->_string_shift($signature, 20), 256);
|
|
|
|
|
2013-08-31 05:22:47 +00:00
|
|
|
switch (true) {
|
|
|
|
case $r->equals($zero):
|
|
|
|
case $r->compare($q) >= 0:
|
|
|
|
case $s->equals($zero):
|
|
|
|
case $s->compare($q) >= 0:
|
|
|
|
user_error('Invalid signature');
|
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
2010-04-22 16:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$w = $s->modInverse($q);
|
|
|
|
|
2010-10-24 01:24:30 +00:00
|
|
|
$u1 = $w->multiply(new Math_BigInteger(sha1($this->exchange_hash), 16));
|
2010-04-22 16:06:43 +00:00
|
|
|
list(, $u1) = $u1->divide($q);
|
|
|
|
|
|
|
|
$u2 = $w->multiply($r);
|
|
|
|
list(, $u2) = $u2->divide($q);
|
|
|
|
|
|
|
|
$g = $g->modPow($u1, $p);
|
|
|
|
$y = $y->modPow($u2, $p);
|
|
|
|
|
|
|
|
$v = $g->multiply($y);
|
|
|
|
list(, $v) = $v->divide($p);
|
|
|
|
list(, $v) = $v->divide($q);
|
|
|
|
|
|
|
|
if (!$v->equals($r)) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Bad server signature');
|
2010-04-22 16:06:43 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ssh-rsa':
|
2018-05-27 14:48:44 +00:00
|
|
|
case 'rsa-sha2-256':
|
|
|
|
case 'rsa-sha2-512':
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
|
|
|
$e = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($server_public_host_key) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
|
2014-11-06 17:58:03 +00:00
|
|
|
$rawN = $this->_string_shift($server_public_host_key, $temp['length']);
|
|
|
|
$n = new Math_BigInteger($rawN, -256);
|
|
|
|
$nLength = strlen(ltrim($rawN, "\0"));
|
2010-04-22 16:06:43 +00:00
|
|
|
|
|
|
|
/*
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($signature) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($signature, 4));
|
|
|
|
$signature = $this->_string_shift($signature, $temp['length']);
|
|
|
|
|
|
|
|
if (!class_exists('Crypt_RSA')) {
|
2013-12-06 00:03:34 +00:00
|
|
|
include_once 'Crypt/RSA.php';
|
2010-04-22 16:06:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$rsa = new Crypt_RSA();
|
2018-05-27 14:48:44 +00:00
|
|
|
switch ($this->signature_format) {
|
|
|
|
case 'rsa-sha2-512':
|
|
|
|
$hash = 'sha512';
|
|
|
|
break;
|
|
|
|
case 'rsa-sha2-256':
|
|
|
|
$hash = 'sha256';
|
|
|
|
break;
|
|
|
|
//case 'ssh-rsa':
|
|
|
|
default:
|
|
|
|
$hash = 'sha1';
|
|
|
|
}
|
|
|
|
$rsa->setHash($hash);
|
2010-04-22 16:06:43 +00:00
|
|
|
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
|
|
|
|
$rsa->loadKey(array('e' => $e, 'n' => $n), CRYPT_RSA_PUBLIC_FORMAT_RAW);
|
2010-10-24 01:24:30 +00:00
|
|
|
if (!$rsa->verify($this->exchange_hash, $signature)) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Bad server signature');
|
2010-04-22 16:06:43 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-10-23 03:13:17 +00:00
|
|
|
if (strlen($signature) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-22 16:06:43 +00:00
|
|
|
$temp = unpack('Nlength', $this->_string_shift($signature, 4));
|
|
|
|
$s = new Math_BigInteger($this->_string_shift($signature, $temp['length']), 256);
|
|
|
|
|
|
|
|
// validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the
|
|
|
|
// following URL:
|
|
|
|
// ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf
|
|
|
|
|
|
|
|
// also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source.
|
|
|
|
|
|
|
|
if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Invalid signature');
|
2010-04-22 16:06:43 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s = $s->modPow($e, $n);
|
|
|
|
$s = $s->toBytes();
|
|
|
|
|
2018-05-27 14:48:44 +00:00
|
|
|
switch ($this->signature_format) {
|
|
|
|
case 'rsa-sha2-512':
|
|
|
|
$hash = 'sha512';
|
|
|
|
break;
|
|
|
|
case 'rsa-sha2-256':
|
|
|
|
$hash = 'sha256';
|
|
|
|
break;
|
|
|
|
//case 'ssh-rsa':
|
|
|
|
default:
|
|
|
|
$hash = 'sha1';
|
|
|
|
}
|
|
|
|
$hashObj = new Crypt_Hash($hash);
|
|
|
|
switch ($this->signature_format) {
|
|
|
|
case 'rsa-sha2-512':
|
|
|
|
$h = pack('N5a*', 0x00305130, 0x0D060960, 0x86480165, 0x03040203, 0x05000440, $hashObj->hash($this->exchange_hash));
|
|
|
|
break;
|
|
|
|
case 'rsa-sha2-256':
|
|
|
|
$h = pack('N5a*', 0x00303130, 0x0D060960, 0x86480165, 0x03040201, 0x05000420, $hashObj->hash($this->exchange_hash));
|
|
|
|
break;
|
|
|
|
//case 'ssh-rsa':
|
|
|
|
default:
|
|
|
|
$hash = 'sha1';
|
|
|
|
$h = pack('N4a*', 0x00302130, 0x0906052B, 0x0E03021A, 0x05000414, $hashObj->hash($this->exchange_hash));
|
|
|
|
}
|
2014-11-06 17:58:03 +00:00
|
|
|
$h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 2 - strlen($h)) . $h;
|
2010-04-22 16:06:43 +00:00
|
|
|
|
|
|
|
if ($s != $h) {
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Bad server signature');
|
2010-04-22 16:06:43 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
|
|
|
|
}
|
2012-07-04 18:36:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-01-09 04:09:27 +00:00
|
|
|
user_error('Unsupported signature format');
|
2012-07-04 18:36:26 +00:00
|
|
|
return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
|
2010-04-22 16:06:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-04 18:36:26 +00:00
|
|
|
return $this->signature_format . ' ' . base64_encode($this->server_public_host_key);
|
2007-07-23 05:21:39 +00:00
|
|
|
}
|
2013-01-17 18:47:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the exit status of an SSH command or false.
|
|
|
|
*
|
2015-09-06 11:28:08 +00:00
|
|
|
* @return false|int
|
2013-01-17 18:47:42 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getExitStatus()
|
|
|
|
{
|
|
|
|
if (is_null($this->exit_status)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->exit_status;
|
|
|
|
}
|
2014-06-22 19:30:52 +00:00
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of columns for the terminal window size.
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return int
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getWindowColumns()
|
|
|
|
{
|
|
|
|
return $this->windowColumns;
|
|
|
|
}
|
2014-06-22 19:30:52 +00:00
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of rows for the terminal window size.
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @return int
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function getWindowRows()
|
|
|
|
{
|
|
|
|
return $this->windowRows;
|
|
|
|
}
|
2014-06-22 19:30:52 +00:00
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Sets the number of columns for the terminal window size.
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $value
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setWindowColumns($value)
|
|
|
|
{
|
|
|
|
$this->windowColumns = $value;
|
|
|
|
}
|
2014-06-22 19:30:52 +00:00
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Sets the number of rows for the terminal window size.
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $value
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setWindowRows($value)
|
|
|
|
{
|
|
|
|
$this->windowRows = $value;
|
|
|
|
}
|
2014-06-22 19:30:52 +00:00
|
|
|
|
2014-06-20 10:04:17 +00:00
|
|
|
/**
|
|
|
|
* Sets the number of columns and rows for the terminal window size.
|
2014-07-20 21:03:06 +00:00
|
|
|
*
|
2015-09-01 23:37:54 +00:00
|
|
|
* @param int $columns
|
|
|
|
* @param int $rows
|
2014-06-20 10:04:17 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setWindowSize($columns = 80, $rows = 24)
|
|
|
|
{
|
|
|
|
$this->windowColumns = $columns;
|
|
|
|
$this->windowRows = $rows;
|
|
|
|
}
|
2013-05-08 06:52:18 +00:00
|
|
|
}
|