SSH2: updates to dealing with extraneous channel packets

This commit is contained in:
terrafrost 2017-11-09 00:12:06 -06:00
parent ffcff461c4
commit 0afde6ea43

View File

@ -2829,7 +2829,7 @@ class Net_SSH2
return false; return false;
} }
$response = $this->_get_binary_packet(true); $response = $this->_get_binary_packet();
if ($response === false) { if ($response === false) {
user_error('Connection closed by server'); user_error('Connection closed by server');
return false; return false;
@ -3312,7 +3312,7 @@ class Net_SSH2
* @return string * @return string
* @access private * @access private
*/ */
function _get_binary_packet($filter_channel_packets = false) function _get_binary_packet($skip_channel_filter = false)
{ {
if (!is_resource($this->fsock) || feof($this->fsock)) { if (!is_resource($this->fsock) || feof($this->fsock)) {
user_error('Connection closed prematurely'); user_error('Connection closed prematurely');
@ -3402,7 +3402,7 @@ class Net_SSH2
$this->last_packet = $current; $this->last_packet = $current;
} }
return $this->_filter($payload, $filter_channel_packets); return $this->_filter($payload, $skip_channel_filter);
} }
/** /**
@ -3414,7 +3414,7 @@ class Net_SSH2
* @return string * @return string
* @access private * @access private
*/ */
function _filter($payload, $filter_channel_packets) function _filter($payload, $skip_channel_filter)
{ {
switch (ord($payload[0])) { switch (ord($payload[0])) {
case NET_SSH2_MSG_DISCONNECT: case NET_SSH2_MSG_DISCONNECT:
@ -3469,10 +3469,10 @@ class Net_SSH2
case NET_SSH2_MSG_CHANNEL_REQUEST: case NET_SSH2_MSG_CHANNEL_REQUEST:
case NET_SSH2_MSG_CHANNEL_CLOSE: case NET_SSH2_MSG_CHANNEL_CLOSE:
case NET_SSH2_MSG_CHANNEL_EOF: case NET_SSH2_MSG_CHANNEL_EOF:
if ($filter_channel_packets) { if (!$skip_channel_filter && !empty($this->server_channels)) {
$this->binary_packet_buffer = $payload; $this->binary_packet_buffer = $payload;
$this->_get_channel_packet(true); $this->_get_channel_packet(true);
$payload = $this->_get_binary_packet(true); $payload = $this->_get_binary_packet();
} }
break; break;
case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4 case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4
@ -3486,7 +3486,7 @@ class Net_SSH2
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
} }
$payload = $this->_get_binary_packet(); $payload = $this->_get_binary_packet($skip_channel_filter);
break; break;
case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1 case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1
$this->_string_shift($payload, 1); $this->_string_shift($payload, 1);
@ -3549,7 +3549,7 @@ class Net_SSH2
return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
} }
} }
$payload = $this->_get_binary_packet(); $payload = $this->_get_binary_packet($skip_channel_filter);
break; break;
case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST: case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST:
$this->_string_shift($payload, 1); $this->_string_shift($payload, 1);
@ -3560,7 +3560,7 @@ class Net_SSH2
extract(unpack('Nwindow_size', $this->_string_shift($payload, 4))); extract(unpack('Nwindow_size', $this->_string_shift($payload, 4)));
$this->window_size_client_to_server[$channel]+= $window_size; $this->window_size_client_to_server[$channel]+= $window_size;
$payload = ($this->bitmap & NET_SSH2_MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet(); $payload = ($this->bitmap & NET_SSH2_MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet($skip_channel_filter);
} }
} }
@ -3684,7 +3684,7 @@ class Net_SSH2
$this->curTimeout-= $elapsed; $this->curTimeout-= $elapsed;
} }
$response = $this->_get_binary_packet(); $response = $this->_get_binary_packet(true);
if ($response === false) { if ($response === false) {
user_error('Connection closed by server'); user_error('Connection closed by server');
return false; return false;
@ -3725,6 +3725,33 @@ class Net_SSH2
$this->window_size_server_to_client[$channel]+= $this->window_size; $this->window_size_server_to_client[$channel]+= $this->window_size;
} }
if ($type == 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) {
break;
}
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;
}
switch ($this->channel_status[$channel]) { switch ($this->channel_status[$channel]) {
case NET_SSH2_MSG_CHANNEL_OPEN: case NET_SSH2_MSG_CHANNEL_OPEN:
switch ($type) { switch ($type) {
@ -3799,30 +3826,6 @@ class Net_SSH2
break; break;
} }
if ($client_channel == $channel) {
return $data;
}
if (!isset($this->channel_buffers[$channel])) {
$this->channel_buffers[$channel] = array();
}
$this->channel_buffers[$channel][] = $data;
break;
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) {
break;
}
if ($client_channel == $channel) { if ($client_channel == $channel) {
return $data; return $data;
} }