Skip to content

Commit

Permalink
Merge branch '1.0' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Dec 28, 2023
2 parents 90de8f1 + c8e3ab9 commit 542a044
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion phpseclib/Net/SSH2.php
Expand Up @@ -1096,6 +1096,14 @@ class SSH2
*/
var $smartMFA = true;

/**
* Extra packets counter
*
* @var bool
* @access private
*/
var $extra_packets;

/**
* Default Constructor.
*
Expand Down Expand Up @@ -1502,7 +1510,7 @@ function _key_exchange($kexinit_payload_server = false)
$preferred['client_to_server']['comp'] :
$this->getSupportedCompressionAlgorithms();

$kex_algorithms = array_merge($kex_algorithms, array('ext-info-c'));
$kex_algorithms = array_merge($kex_algorithms, array('ext-info-c', 'kex-strict-c-v00@openssh.com'));

// some SSH servers have buggy implementations of some of the above algorithms
switch (true) {
Expand Down Expand Up @@ -1566,6 +1574,7 @@ function _key_exchange($kexinit_payload_server = false)
return false;
}

$this->extra_packets = 0;
$kexinit_payload_server = $this->_get_binary_packet();
if ($kexinit_payload_server === false) {
$this->bitmap = 0;
Expand All @@ -1590,6 +1599,12 @@ function _key_exchange($kexinit_payload_server = false)
}
$temp = unpack('Nlength', $this->_string_shift($response, 4));
$this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
if (in_array('kex-strict-s-v00@openssh.com', $this->kex_algorithms)) {
if ($this->session_id === false && $this->extra_packets) {
user_error('Possible Terrapin Attack detected');
return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
}

if (strlen($response) < 4) {
return false;
Expand Down Expand Up @@ -1996,6 +2011,10 @@ function _key_exchange($kexinit_payload_server = false)
return false;
}

if (in_array('kex-strict-s-v00@openssh.com', $this->kex_algorithms)) {
$this->get_seq_no = $this->send_seq_no = 0;
}

$keyBytes = pack('Na*', strlen($keyBytes), $keyBytes);

$this->encrypt = $this->_encryption_algorithm_to_crypt_instance($encrypt);
Expand Down Expand Up @@ -3766,9 +3785,11 @@ function _filter($payload, $skip_channel_filter)
$this->bitmap = 0;
return false;
case NET_SSH2_MSG_IGNORE:
$this->extra_packets++;
$payload = $this->_get_binary_packet($skip_channel_filter);
break;
case NET_SSH2_MSG_DEBUG:
$this->extra_packets++;
$this->_string_shift($payload, 2);
if (strlen($payload) < 4) {
return false;
Expand All @@ -3780,6 +3801,7 @@ function _filter($payload, $skip_channel_filter)
case NET_SSH2_MSG_UNIMPLEMENTED:
return false;
case NET_SSH2_MSG_KEXINIT:
// this is here for key re-exchanges after the initial key exchange
if ($this->session_id !== false) {
$this->send_kex_first = false;
if (!$this->_key_exchange($payload)) {
Expand Down

0 comments on commit 542a044

Please sign in to comment.