Skip to content

Instantly share code, notes, and snippets.

@SyuTingSong
Created November 14, 2023 07:14
Show Gist options
  • Save SyuTingSong/350c0944c4a11b28194fda38b2c521d7 to your computer and use it in GitHub Desktop.
Save SyuTingSong/350c0944c4a11b28194fda38b2c521d7 to your computer and use it in GitHub Desktop.
log packet_id and buffer
diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php
index cdf0bec..caa42ac 100644
--- a/phpseclib/Net/SFTP.php
+++ b/phpseclib/Net/SFTP.php
@@ -598,7 +598,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_VERSION) {
throw new \UnexpectedValueException('Expected NET_SFTP_VERSION. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
$this->use_request_id = true;
@@ -667,7 +667,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
if ($status != NET_SFTP_STATUS_OK) {
@@ -880,7 +880,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
}
@@ -955,7 +955,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS' .
- 'Got packet type: ' . $this->packet_type);
+ 'Got packet type: ' . $this->get_packet_type_name($response));
}
if (!$this->close_handle($handle)) {
@@ -1107,7 +1107,7 @@ class SFTP extends SSH2
return $status;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
$this->update_stat_cache($dir, []);
@@ -1162,7 +1162,7 @@ class SFTP extends SSH2
break 2;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
}
@@ -1517,7 +1517,7 @@ class SFTP extends SSH2
}
throw new \UnexpectedValueException('Expected NET_SFTP_ATTRS or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
/**
@@ -1584,7 +1584,7 @@ class SFTP extends SSH2
break;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
return $this->setstat($filename, $attr, false);
@@ -1707,7 +1707,7 @@ class SFTP extends SSH2
}
throw new \UnexpectedValueException('Expected NET_SFTP_ATTRS or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
/**
@@ -1755,7 +1755,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -1868,7 +1868,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_NAME or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($count) = Strings::unpackSSH2('N', $response);
@@ -1936,7 +1936,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -1996,7 +1996,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -2035,7 +2035,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -2158,7 +2158,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
@@ -2300,7 +2300,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -2329,7 +2329,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
list($status) = Strings::unpackSSH2('N', $response);
@@ -2385,7 +2385,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
if (is_resource($local_file)) {
@@ -2478,7 +2478,7 @@ class SFTP extends SSH2
return false;
} else {
throw new \UnexpectedValueException('Expected NET_SFTP_DATA or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
}
$response = null;
@@ -2540,7 +2540,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
// if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
@@ -2725,7 +2725,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected NET_SFTP_HANDLE or NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
}
@@ -2752,7 +2752,7 @@ class SFTP extends SSH2
return false;
default:
throw new \UnexpectedValueException('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
}
@@ -2992,7 +2992,7 @@ class SFTP extends SSH2
$response = $this->get_sftp_packet();
if ($this->packet_type != NET_SFTP_STATUS) {
throw new \UnexpectedValueException('Expected NET_SFTP_STATUS. '
- . 'Got packet type: ' . $this->packet_type);
+ . 'Got packet type: ' . $this->get_packet_type_name($response));
}
// if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
@@ -3278,12 +3278,30 @@ class SFTP extends SSH2
$stop = microtime(true);
if (defined('NET_SFTP_LOGGING')) {
- $packet_type = '-> ' . self::$packet_types[$type] .
+ $packet_type = '-> ' .
+ ($this->use_request_id ? ('[' . str_pad(strval($request_id), 2, '0', STR_PAD_LEFT) . ']') : '') .
+ self::$packet_types[$type] . $this->get_detail($type, $data) .
' (' . round($stop - $start, 4) . 's)';
$this->append_log($packet_type, $data);
}
}
+ private function get_detail($type, $data)
+ {
+ return match ($type) {
+ NET_SFTP_OPENDIR,
+ NET_SFTP_READDIR,
+ NET_SFTP_OPEN,
+ NET_SFTP_READ,
+ NET_SFTP_CLOSE => '(' . Strings::unpackSSH2('s', $data)[0] . ')',
+ NET_SFTP_HANDLE => '(' . substr($data, 4) . ')',
+ NET_SFTP_NAME => '(' . Strings::unpackSSH2('Ns', $data)[1]. ')',
+ NET_SFTP_STATUS => '(' . substr(self::$status_codes[unpack('N', $data)[1] ?? 'A'] ?? 'NET_SFTP_STATUS_UNKNOWN', 16) . ')',
+ NET_SFTP_DATA => '(' . unpack('N', $data)[1] . 'bytes)',
+ default => '',
+ };
+ }
+
/**
* Resets a connection for re-use
*
@@ -3297,6 +3315,15 @@ class SFTP extends SSH2
$this->requestBuffer = [];
}
+ private function get_packet_type_name($packet)
+ {
+ if (is_bool($this->packet_type)) {
+ return $this->packet_type ? 'true' : 'false';
+ }
+
+ return self::$packet_types[$this->packet_type] ?? 'NET_SFTP_UNKNOWN';
+ }
+
/**
* Receives SFTP Packets
*
@@ -3317,6 +3344,15 @@ class SFTP extends SSH2
$this->packet_type = $this->requestBuffer[$request_id]['packet_type'];
$temp = $this->requestBuffer[$request_id]['packet'];
unset($this->requestBuffer[$request_id]);
+
+ if (defined('NET_SFTP_LOGGING')) {
+ $packet_type = '-< ' .
+ '[' . str_pad(strval($request_id), 2, '0', STR_PAD_LEFT) . ']' .
+ $this->get_packet_type_name($temp) . $this->get_detail($this->packet_type, $temp) .
+ ' (0s)' . '[buffer]';
+ $this->append_log($packet_type, $temp);
+ }
+
return $temp;
}
@@ -3382,8 +3418,11 @@ class SFTP extends SSH2
$packet = Strings::shift($this->packet_buffer, $length);
if (defined('NET_SFTP_LOGGING')) {
- $packet_type = '<- ' . self::$packet_types[$this->packet_type] .
- ' (' . round($stop - $start, 4) . 's)';
+ $packet_type = '<- ' .
+ ($this->use_request_id && isset($packet_id) ? ('[' . str_pad(strval($packet_id), 2, '0', STR_PAD_LEFT) . ']') : '') .
+ $this->get_packet_type_name($packet) . $this->get_detail($this->packet_type, $packet) .
+ ' (' . round($stop - $start, 4) . 's)' .
+ (isset($request_id) && $this->use_request_id && $packet_id != $request_id ? '[buffer]' : '');
$this->append_log($packet_type, $packet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment