为什么 "sock.recv(1024)" 在 "while True:" 之外 return 服务 运行 在端口上,就像 nmap 一样?
Why does "sock.recv(1024)" outside "while True:" return the service running on the port, like nmap does?
我很困惑为什么 sock.recv()
returns 特定端口上的服务名称 运行。这就是我的意思。
当您在 python 中编码并连接到服务器时,您通常会在 while True
循环中执行此操作以接收另一台计算机发送的任何内容。但是当你只是自发地做一次 recv()
时,对服务器说 192.168.55.3
,在 port 22
即 运行 ssh
, sock.recv(1024)
returns 类似 ssh version xxx
的应用程序名称
我不明白这背后的机制,希望有人能解释一下。
我确实做了一些研究,但找不到太多。
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((interface, port))
sock.connect((host, host_port))
print(sock.recv(100))
它returns像这样b'SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu1\n
在某些网络协议中,服务器会在建立连接后立即向客户端发送消息。此消息可能会识别协议版本、软件名称 运行 服务、机器名称及其运行的操作系统等
消息的目的可以多种多样,从让客户端决定它是否连接到正确的服务器并能够与其通信,到简单的 vanity/advertising。
执行此操作的协议包括 SSH、SMTP 和 FTP。尝试连接到不同的端口,你会得到截然不同的结果
您正在连接到端口 22,该端口用于 SSH(安全 Shell)协议。
根据 RFC 4253: The Secure Shell (SSH) Transport Layer Protocol, Section 4: "Connection Setup":
SSH works over any 8-bit clean, binary-transparent transport. The underlying transport SHOULD protect against transmission errors, as such errors cause the SSH connection to terminate. The client initiates the connection.
4.1. Use over TCP/IP
When used over TCP/IP, the server normally listens for connections on port 22. This port number has been registered with the IANA, and has been officially assigned for SSH.
4.2. Protocol Version Exchange
When the connection has been established, both sides MUST send an identification string. This identification string MUST be
SSH-protoversion-softwareversion SP comments CR LF
Since the protocol being defined in this set of documents is version 2.0, the 'protoversion' MUST be "2.0". The 'comments' string is OPTIONAL. If the 'comments' string is included, a 'space' character (denoted above as SP, ASCII 32) MUST separate the 'softwareversion' and 'comments' strings. The identification MUST be terminated by a single Carriage Return (CR) and a single Line Feed (LF) character (ASCII 13 and 10, respectively). Implementers who wish to maintain Ylonen & Lonvick Standards Track [Page 4] RFC 4253 SSH Transport Layer Protocol January 2006 compatibility with older, undocumented versions of this protocol may want to process the identification string without expecting the presence of the carriage return character for reasons described in Section 5 of this document. The null character MUST NOT be sent. The maximum length of the string is 255 characters, including the Carriage Return and Line Feed.
The part of the identification string preceding the Carriage Return and Line Feed is used in the Diffie-Hellman key exchange (see Section 8).
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. Such lines MAY be silently ignored, or MAY be displayed to the client user. If they are displayed, control character filtering, as discussed in [SSH-ARCH], SHOULD be used. The primary use of this feature is to allow TCP- wrappers to display an error message before disconnecting.
Both the 'protoversion' and 'softwareversion' strings MUST consist of printable US-ASCII characters, with the exception of whitespace characters and the minus sign (-). The 'softwareversion' string is primarily used to trigger compatibility extensions and to indicate the capabilities of an implementation. The 'comments' string SHOULD contain additional information that might be useful in solving user problems. As such, an example of a valid identification string is
SSH-2.0-billsSSH_3.6.3q3<CR><LF>
This identification string does not contain the optional 'comments' string and is thus terminated by a CR and LF immediately after the 'softwareversion' string.
Key exchange will begin immediately after sending this identifier. All packets following the identification string SHALL use the binary packet protocol, which is described in Section 6.
您看到的打印出来的是服务器的标识字符串:
SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu1
其中:
- protoversion =
2.0
- 软件版本 =
OpenSSH_4.7p1
- 评论=
Debian-8ubuntu1
我很困惑为什么 sock.recv()
returns 特定端口上的服务名称 运行。这就是我的意思。
当您在 python 中编码并连接到服务器时,您通常会在 while True
循环中执行此操作以接收另一台计算机发送的任何内容。但是当你只是自发地做一次 recv()
时,对服务器说 192.168.55.3
,在 port 22
即 运行 ssh
, sock.recv(1024)
returns 类似 ssh version xxx
的应用程序名称
我不明白这背后的机制,希望有人能解释一下。
我确实做了一些研究,但找不到太多。
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((interface, port))
sock.connect((host, host_port))
print(sock.recv(100))
它returns像这样b'SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu1\n
在某些网络协议中,服务器会在建立连接后立即向客户端发送消息。此消息可能会识别协议版本、软件名称 运行 服务、机器名称及其运行的操作系统等
消息的目的可以多种多样,从让客户端决定它是否连接到正确的服务器并能够与其通信,到简单的 vanity/advertising。
执行此操作的协议包括 SSH、SMTP 和 FTP。尝试连接到不同的端口,你会得到截然不同的结果
您正在连接到端口 22,该端口用于 SSH(安全 Shell)协议。
根据 RFC 4253: The Secure Shell (SSH) Transport Layer Protocol, Section 4: "Connection Setup":
SSH works over any 8-bit clean, binary-transparent transport. The underlying transport SHOULD protect against transmission errors, as such errors cause the SSH connection to terminate. The client initiates the connection.
4.1. Use over TCP/IP
When used over TCP/IP, the server normally listens for connections on port 22. This port number has been registered with the IANA, and has been officially assigned for SSH.
4.2. Protocol Version Exchange
When the connection has been established, both sides MUST send an identification string. This identification string MUST be
SSH-protoversion-softwareversion SP comments CR LF
Since the protocol being defined in this set of documents is version 2.0, the 'protoversion' MUST be "2.0". The 'comments' string is OPTIONAL. If the 'comments' string is included, a 'space' character (denoted above as SP, ASCII 32) MUST separate the 'softwareversion' and 'comments' strings. The identification MUST be terminated by a single Carriage Return (CR) and a single Line Feed (LF) character (ASCII 13 and 10, respectively). Implementers who wish to maintain Ylonen & Lonvick Standards Track [Page 4] RFC 4253 SSH Transport Layer Protocol January 2006 compatibility with older, undocumented versions of this protocol may want to process the identification string without expecting the presence of the carriage return character for reasons described in Section 5 of this document. The null character MUST NOT be sent. The maximum length of the string is 255 characters, including the Carriage Return and Line Feed.
The part of the identification string preceding the Carriage Return and Line Feed is used in the Diffie-Hellman key exchange (see Section 8).
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. Such lines MAY be silently ignored, or MAY be displayed to the client user. If they are displayed, control character filtering, as discussed in [SSH-ARCH], SHOULD be used. The primary use of this feature is to allow TCP- wrappers to display an error message before disconnecting.
Both the 'protoversion' and 'softwareversion' strings MUST consist of printable US-ASCII characters, with the exception of whitespace characters and the minus sign (-). The 'softwareversion' string is primarily used to trigger compatibility extensions and to indicate the capabilities of an implementation. The 'comments' string SHOULD contain additional information that might be useful in solving user problems. As such, an example of a valid identification string is
SSH-2.0-billsSSH_3.6.3q3<CR><LF>
This identification string does not contain the optional 'comments' string and is thus terminated by a CR and LF immediately after the 'softwareversion' string.
Key exchange will begin immediately after sending this identifier. All packets following the identification string SHALL use the binary packet protocol, which is described in Section 6.
您看到的打印出来的是服务器的标识字符串:
SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu1
其中:
- protoversion =
2.0
- 软件版本 =
OpenSSH_4.7p1
- 评论=
Debian-8ubuntu1