net-ssh 4.2.0 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.dockerignore +6 -0
- data/.github/config/rubocop_linter_action.yml +4 -0
- data/.github/workflows/ci-with-docker.yml +44 -0
- data/.github/workflows/ci.yml +87 -0
- data/.github/workflows/rubocop.yml +13 -0
- data/.gitignore +7 -0
- data/.rubocop.yml +19 -2
- data/.rubocop_todo.yml +619 -667
- data/CHANGES.txt +110 -1
- data/Dockerfile +27 -0
- data/Dockerfile.openssl3 +17 -0
- data/Gemfile +3 -7
- data/{Gemfile.norbnacl → Gemfile.noed25519} +3 -1
- data/Manifest +4 -5
- data/README.md +293 -0
- data/Rakefile +45 -29
- data/appveyor.yml +8 -6
- data/docker-compose.yml +23 -0
- data/lib/net/ssh/authentication/agent.rb +248 -223
- data/lib/net/ssh/authentication/certificate.rb +178 -164
- data/lib/net/ssh/authentication/constants.rb +17 -15
- data/lib/net/ssh/authentication/ed25519.rb +141 -116
- data/lib/net/ssh/authentication/ed25519_loader.rb +28 -28
- data/lib/net/ssh/authentication/key_manager.rb +79 -36
- data/lib/net/ssh/authentication/methods/abstract.rb +62 -47
- data/lib/net/ssh/authentication/methods/hostbased.rb +34 -37
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +3 -3
- data/lib/net/ssh/authentication/methods/none.rb +16 -19
- data/lib/net/ssh/authentication/methods/password.rb +15 -16
- data/lib/net/ssh/authentication/methods/publickey.rb +96 -55
- data/lib/net/ssh/authentication/pageant.rb +468 -465
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
- data/lib/net/ssh/authentication/session.rb +131 -122
- data/lib/net/ssh/buffer.rb +385 -332
- data/lib/net/ssh/buffered_io.rb +150 -151
- data/lib/net/ssh/config.rb +316 -239
- data/lib/net/ssh/connection/channel.rb +635 -613
- data/lib/net/ssh/connection/constants.rb +29 -29
- data/lib/net/ssh/connection/event_loop.rb +104 -95
- data/lib/net/ssh/connection/keepalive.rb +55 -51
- data/lib/net/ssh/connection/session.rb +614 -611
- data/lib/net/ssh/connection/term.rb +125 -123
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +194 -108
- data/lib/net/ssh/known_hosts.rb +212 -134
- data/lib/net/ssh/loggable.rb +50 -49
- data/lib/net/ssh/packet.rb +83 -79
- data/lib/net/ssh/prompt.rb +51 -51
- data/lib/net/ssh/proxy/command.rb +105 -91
- data/lib/net/ssh/proxy/errors.rb +12 -10
- data/lib/net/ssh/proxy/http.rb +81 -81
- data/lib/net/ssh/proxy/https.rb +37 -36
- data/lib/net/ssh/proxy/jump.rb +49 -48
- data/lib/net/ssh/proxy/socks4.rb +2 -6
- data/lib/net/ssh/proxy/socks5.rb +14 -17
- data/lib/net/ssh/service/forward.rb +365 -362
- data/lib/net/ssh/test/channel.rb +145 -143
- data/lib/net/ssh/test/extensions.rb +131 -127
- data/lib/net/ssh/test/kex.rb +34 -32
- data/lib/net/ssh/test/local_packet.rb +46 -44
- data/lib/net/ssh/test/packet.rb +87 -84
- data/lib/net/ssh/test/remote_packet.rb +32 -30
- data/lib/net/ssh/test/script.rb +155 -155
- data/lib/net/ssh/test/socket.rb +49 -48
- data/lib/net/ssh/test.rb +82 -80
- data/lib/net/ssh/transport/algorithms.rb +433 -364
- data/lib/net/ssh/transport/cipher_factory.rb +95 -91
- data/lib/net/ssh/transport/constants.rb +32 -24
- data/lib/net/ssh/transport/ctr.rb +37 -15
- data/lib/net/ssh/transport/hmac/abstract.rb +81 -63
- data/lib/net/ssh/transport/hmac/md5.rb +0 -2
- data/lib/net/ssh/transport/hmac/md5_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/none.rb +0 -2
- data/lib/net/ssh/transport/hmac/ripemd160.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac.rb +14 -12
- data/lib/net/ssh/transport/identity_cipher.rb +54 -52
- data/lib/net/ssh/transport/kex/abstract.rb +130 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +33 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha256.rb +11 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +112 -217
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -63
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +36 -90
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
- data/lib/net/ssh/transport/kex.rb +15 -12
- data/lib/net/ssh/transport/key_expander.rb +24 -21
- data/lib/net/ssh/transport/openssl.rb +158 -133
- data/lib/net/ssh/transport/packet_stream.rb +223 -191
- data/lib/net/ssh/transport/server_version.rb +55 -56
- data/lib/net/ssh/transport/session.rb +306 -259
- data/lib/net/ssh/transport/state.rb +178 -176
- data/lib/net/ssh/verifiers/accept_new.rb +33 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
- data/lib/net/ssh/verifiers/always.rb +58 -0
- data/lib/net/ssh/verifiers/never.rb +19 -0
- data/lib/net/ssh/version.rb +55 -53
- data/lib/net/ssh.rb +47 -34
- data/net-ssh-public_cert.pem +18 -19
- data/net-ssh.gemspec +12 -11
- data/support/ssh_tunnel_bug.rb +5 -5
- data.tar.gz.sig +0 -0
- metadata +78 -73
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -51
- data/Gemfile.norbnacl.lock +0 -41
- data/README.rdoc +0 -169
- data/lib/net/ssh/ruby_compat.rb +0 -24
- data/lib/net/ssh/verifiers/lenient.rb +0 -30
- data/lib/net/ssh/verifiers/null.rb +0 -12
- data/lib/net/ssh/verifiers/secure.rb +0 -52
- data/lib/net/ssh/verifiers/strict.rb +0 -24
- data/support/arcfour_check.rb +0 -20
|
@@ -1,248 +1,280 @@
|
|
|
1
1
|
require 'net/ssh/buffered_io'
|
|
2
2
|
require 'net/ssh/errors'
|
|
3
3
|
require 'net/ssh/packet'
|
|
4
|
-
require 'net/ssh/ruby_compat'
|
|
5
4
|
require 'net/ssh/transport/cipher_factory'
|
|
6
5
|
require 'net/ssh/transport/hmac'
|
|
7
6
|
require 'net/ssh/transport/state'
|
|
8
7
|
|
|
8
|
+
module Net
|
|
9
|
+
module SSH
|
|
10
|
+
module Transport
|
|
11
|
+
# A module that builds additional functionality onto the Net::SSH::BufferedIo
|
|
12
|
+
# module. It adds SSH encryption, compression, and packet validation, as
|
|
13
|
+
# per the SSH2 protocol. It also adds an abstraction for polling packets,
|
|
14
|
+
# to allow for both blocking and non-blocking reads.
|
|
15
|
+
module PacketStream
|
|
16
|
+
PROXY_COMMAND_HOST_IP = '<no hostip for proxy command>'.freeze
|
|
9
17
|
|
|
10
|
-
|
|
18
|
+
include BufferedIo
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# to allow for both blocking and non-blocking reads.
|
|
16
|
-
module PacketStream
|
|
17
|
-
PROXY_COMMAND_HOST_IP = '<no hostip for proxy command>'.freeze
|
|
18
|
-
|
|
19
|
-
include BufferedIo
|
|
20
|
-
|
|
21
|
-
def self.extended(object)
|
|
22
|
-
object.__send__(:initialize_ssh)
|
|
23
|
-
end
|
|
20
|
+
def self.extended(object)
|
|
21
|
+
object.__send__(:initialize_ssh)
|
|
22
|
+
end
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
begin
|
|
45
|
-
Socket.getnameinfo(sockaddr, Socket::NI_NAMEREQD).first
|
|
46
|
-
rescue
|
|
47
|
-
begin
|
|
48
|
-
Socket.getnameinfo(sockaddr).first
|
|
49
|
-
rescue
|
|
24
|
+
# The map of "hints" that can be used to modify the behavior of the packet
|
|
25
|
+
# stream. For instance, when authentication succeeds, an "authenticated"
|
|
26
|
+
# hint is set, which is used to determine whether or not to compress the
|
|
27
|
+
# data when using the "delayed" compression algorithm.
|
|
28
|
+
attr_reader :hints
|
|
29
|
+
|
|
30
|
+
# The server state object, which encapsulates the algorithms used to interpret
|
|
31
|
+
# packets coming from the server.
|
|
32
|
+
attr_reader :server
|
|
33
|
+
|
|
34
|
+
# The client state object, which encapsulates the algorithms used to build
|
|
35
|
+
# packets to send to the server.
|
|
36
|
+
attr_reader :client
|
|
37
|
+
|
|
38
|
+
# The name of the client (local) end of the socket, as reported by the
|
|
39
|
+
# socket.
|
|
40
|
+
def client_name
|
|
41
|
+
@client_name ||= begin
|
|
42
|
+
sockaddr = getsockname
|
|
50
43
|
begin
|
|
51
|
-
Socket.
|
|
52
|
-
rescue
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
Socket.getnameinfo(sockaddr, Socket::NI_NAMEREQD).first
|
|
45
|
+
rescue StandardError
|
|
46
|
+
begin
|
|
47
|
+
Socket.getnameinfo(sockaddr).first
|
|
48
|
+
rescue StandardError
|
|
49
|
+
begin
|
|
50
|
+
Socket.gethostbyname(Socket.gethostname).first
|
|
51
|
+
rescue StandardError
|
|
52
|
+
lwarn { "the client ipaddr/name could not be determined" }
|
|
53
|
+
"unknown"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
55
56
|
end
|
|
56
57
|
end
|
|
57
58
|
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
# The IP address of the peer (remote) end of the socket, as reported by
|
|
61
|
+
# the socket.
|
|
62
|
+
def peer_ip
|
|
63
|
+
@peer_ip ||=
|
|
64
|
+
if respond_to?(:getpeername)
|
|
65
|
+
addr = getpeername
|
|
66
|
+
Socket.getnameinfo(addr, Socket::NI_NUMERICHOST | Socket::NI_NUMERICSERV).first
|
|
67
|
+
else
|
|
68
|
+
PROXY_COMMAND_HOST_IP
|
|
69
|
+
end
|
|
70
70
|
end
|
|
71
|
-
end
|
|
72
71
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
# Returns true if the IO is available for reading, and false otherwise.
|
|
73
|
+
def available_for_read?
|
|
74
|
+
result = IO.select([self], nil, nil, 0)
|
|
75
|
+
result && result.first.any?
|
|
76
|
+
end
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
78
|
+
# Returns the next full packet. If the mode parameter is :nonblock (the
|
|
79
|
+
# default), then this will return immediately, whether a packet is
|
|
80
|
+
# available or not, and will return nil if there is no packet ready to be
|
|
81
|
+
# returned. If the mode parameter is :block, then this method will block
|
|
82
|
+
# until a packet is available or timeout seconds have passed.
|
|
83
|
+
def next_packet(mode = :nonblock, timeout = nil)
|
|
84
|
+
case mode
|
|
85
|
+
when :nonblock then
|
|
86
|
+
packet = poll_next_packet
|
|
87
|
+
return packet if packet
|
|
88
|
+
|
|
89
|
+
if available_for_read?
|
|
90
|
+
if fill <= 0
|
|
91
|
+
result = poll_next_packet
|
|
92
|
+
if result.nil?
|
|
93
|
+
raise Net::SSH::Disconnect, "connection closed by remote host"
|
|
94
|
+
else
|
|
95
|
+
return result
|
|
96
|
+
end
|
|
97
|
+
end
|
|
97
98
|
end
|
|
98
|
-
|
|
99
|
-
end
|
|
100
|
-
poll_next_packet
|
|
99
|
+
poll_next_packet
|
|
101
100
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
when :block then
|
|
102
|
+
loop do
|
|
103
|
+
packet = poll_next_packet
|
|
104
|
+
return packet if packet
|
|
106
105
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
106
|
+
result = IO.select([self], nil, nil, timeout)
|
|
107
|
+
raise Net::SSH::ConnectionTimeout, "timeout waiting for next packet" unless result
|
|
108
|
+
raise Net::SSH::Disconnect, "connection closed by remote host" if fill <= 0
|
|
109
|
+
end
|
|
111
110
|
|
|
112
|
-
|
|
113
|
-
raise
|
|
111
|
+
else
|
|
112
|
+
raise ArgumentError, "expected :block or :nonblock, got #{mode.inspect}"
|
|
114
113
|
end
|
|
115
114
|
end
|
|
116
115
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
# Enqueues a packet to be sent, and blocks until the entire packet is
|
|
117
|
+
# sent.
|
|
118
|
+
def send_packet(payload)
|
|
119
|
+
enqueue_packet(payload)
|
|
120
|
+
wait_for_pending_sends
|
|
121
|
+
end
|
|
121
122
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
123
|
+
# Enqueues a packet to be sent, but does not immediately send the packet.
|
|
124
|
+
# The given payload is pre-processed according to the algorithms specified
|
|
125
|
+
# in the client state (compression, cipher, and hmac).
|
|
126
|
+
def enqueue_packet(payload)
|
|
127
|
+
# try to compress the packet
|
|
128
|
+
payload = client.compress(payload)
|
|
128
129
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
# in the client state (compression, cipher, and hmac).
|
|
132
|
-
def enqueue_packet(payload)
|
|
133
|
-
# try to compress the packet
|
|
134
|
-
payload = client.compress(payload)
|
|
130
|
+
# the length of the packet, minus the padding
|
|
131
|
+
actual_length = (client.hmac.etm ? 0 : 4) + payload.bytesize + 1
|
|
135
132
|
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
# compute the padding length
|
|
134
|
+
padding_length = client.block_size - (actual_length % client.block_size)
|
|
135
|
+
padding_length += client.block_size if padding_length < 4
|
|
138
136
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
padding_length += client.block_size if padding_length < 4
|
|
137
|
+
# compute the packet length (sans the length field itself)
|
|
138
|
+
packet_length = payload.bytesize + padding_length + 1
|
|
142
139
|
|
|
143
|
-
|
|
144
|
-
|
|
140
|
+
if packet_length < 16
|
|
141
|
+
padding_length += client.block_size
|
|
142
|
+
packet_length = payload.bytesize + padding_length + 1
|
|
143
|
+
end
|
|
145
144
|
|
|
146
|
-
|
|
147
|
-
padding_length += client.block_size
|
|
148
|
-
packet_length = payload.bytesize + padding_length + 1
|
|
149
|
-
end
|
|
145
|
+
padding = Array.new(padding_length) { rand(256) }.pack("C*")
|
|
150
146
|
|
|
151
|
-
|
|
147
|
+
if client.hmac.etm
|
|
148
|
+
debug { "using encrypt-then-mac" }
|
|
152
149
|
|
|
153
|
-
|
|
154
|
-
|
|
150
|
+
# Encrypt padding_length, payload, and padding. Take MAC
|
|
151
|
+
# from the unencrypted packet_lenght and the encrypted
|
|
152
|
+
# data.
|
|
153
|
+
length_data = [packet_length].pack("N")
|
|
155
154
|
|
|
156
|
-
|
|
157
|
-
message = encrypted_data + mac
|
|
155
|
+
unencrypted_data = [padding_length, payload, padding].pack("CA*A*")
|
|
158
156
|
|
|
159
|
-
|
|
160
|
-
enqueue(message)
|
|
157
|
+
encrypted_data = client.update_cipher(unencrypted_data) << client.final_cipher
|
|
161
158
|
|
|
162
|
-
|
|
159
|
+
mac_data = length_data + encrypted_data
|
|
163
160
|
|
|
164
|
-
|
|
165
|
-
end
|
|
161
|
+
mac = client.hmac.digest([client.sequence_number, mac_data].pack("NA*"))
|
|
166
162
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
client.cleanup
|
|
171
|
-
server.cleanup
|
|
172
|
-
end
|
|
163
|
+
message = mac_data + mac
|
|
164
|
+
else
|
|
165
|
+
unencrypted_data = [packet_length, padding_length, payload, padding].pack("NCA*A*")
|
|
173
166
|
|
|
174
|
-
|
|
175
|
-
# client or server state objects, see State#needs_rekey?), this will
|
|
176
|
-
# yield. Otherwise, this does nothing.
|
|
177
|
-
def if_needs_rekey?
|
|
178
|
-
if client.needs_rekey? || server.needs_rekey?
|
|
179
|
-
yield
|
|
180
|
-
client.reset! if client.needs_rekey?
|
|
181
|
-
server.reset! if server.needs_rekey?
|
|
182
|
-
end
|
|
183
|
-
end
|
|
167
|
+
mac = client.hmac.digest([client.sequence_number, unencrypted_data].pack("NA*"))
|
|
184
168
|
|
|
185
|
-
|
|
169
|
+
encrypted_data = client.update_cipher(unencrypted_data) << client.final_cipher
|
|
186
170
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
@client = State.new(self, :client)
|
|
193
|
-
@packet = nil
|
|
194
|
-
initialize_buffered_io
|
|
195
|
-
end
|
|
171
|
+
message = encrypted_data + mac
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
debug { "queueing packet nr #{client.sequence_number} type #{payload.getbyte(0)} len #{packet_length}" }
|
|
175
|
+
enqueue(message)
|
|
196
176
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
# decipher it
|
|
209
|
-
@packet = Net::SSH::Buffer.new(server.update_cipher(data))
|
|
210
|
-
@packet_length = @packet.read_long
|
|
177
|
+
client.increment(packet_length)
|
|
178
|
+
|
|
179
|
+
self
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Performs any pending cleanup necessary on the IO and its associated
|
|
183
|
+
# state objects. (See State#cleanup).
|
|
184
|
+
def cleanup
|
|
185
|
+
client.cleanup
|
|
186
|
+
server.cleanup
|
|
211
187
|
end
|
|
212
188
|
|
|
213
|
-
|
|
214
|
-
|
|
189
|
+
# If the IO object requires a rekey operation (as indicated by either its
|
|
190
|
+
# client or server state objects, see State#needs_rekey?), this will
|
|
191
|
+
# yield. Otherwise, this does nothing.
|
|
192
|
+
def if_needs_rekey?
|
|
193
|
+
if client.needs_rekey? || server.needs_rekey?
|
|
194
|
+
yield
|
|
195
|
+
client.reset! if client.needs_rekey?
|
|
196
|
+
server.reset! if server.needs_rekey?
|
|
197
|
+
end
|
|
198
|
+
end
|
|
215
199
|
|
|
216
|
-
|
|
200
|
+
protected
|
|
217
201
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
@
|
|
202
|
+
# Called when this module is used to extend an object. It initializes
|
|
203
|
+
# the states and generally prepares the object for use as a packet stream.
|
|
204
|
+
def initialize_ssh
|
|
205
|
+
@hints = {}
|
|
206
|
+
@server = State.new(self, :server)
|
|
207
|
+
@client = State.new(self, :client)
|
|
208
|
+
@packet = nil
|
|
209
|
+
initialize_buffered_io
|
|
222
210
|
end
|
|
223
211
|
|
|
224
|
-
#
|
|
225
|
-
#
|
|
226
|
-
|
|
212
|
+
# Tries to read the next packet. If there is insufficient data to read
|
|
213
|
+
# an entire packet, this returns immediately, otherwise the packet is
|
|
214
|
+
# read, post-processed according to the cipher, hmac, and compression
|
|
215
|
+
# algorithms specified in the server state object, and returned as a
|
|
216
|
+
# new Packet object.
|
|
217
|
+
# rubocop:disable Metrics/AbcSize
|
|
218
|
+
def poll_next_packet
|
|
219
|
+
aad_length = server.hmac.etm ? 4 : 0
|
|
220
|
+
|
|
221
|
+
if @packet.nil?
|
|
222
|
+
minimum = server.block_size < 4 ? 4 : server.block_size
|
|
223
|
+
return nil if available < minimum + aad_length
|
|
224
|
+
|
|
225
|
+
data = read_available(minimum + aad_length)
|
|
226
|
+
|
|
227
|
+
# decipher it
|
|
228
|
+
if server.hmac.etm
|
|
229
|
+
@packet_length = data.unpack("N").first
|
|
230
|
+
@mac_data = data
|
|
231
|
+
@packet = Net::SSH::Buffer.new(server.update_cipher(data[aad_length..-1]))
|
|
232
|
+
else
|
|
233
|
+
@packet = Net::SSH::Buffer.new(server.update_cipher(data))
|
|
234
|
+
@packet_length = @packet.read_long
|
|
235
|
+
end
|
|
236
|
+
end
|
|
227
237
|
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
need = @packet_length + 4 - aad_length - server.block_size
|
|
239
|
+
raise Net::SSH::Exception, "padding error, need #{need} block #{server.block_size}" if need % server.block_size != 0
|
|
240
|
+
|
|
241
|
+
return nil if available < need + server.hmac.mac_length
|
|
242
|
+
|
|
243
|
+
if need > 0
|
|
244
|
+
# read the remainder of the packet and decrypt it.
|
|
245
|
+
data = read_available(need)
|
|
246
|
+
@mac_data += data if server.hmac.etm
|
|
247
|
+
@packet.append(server.update_cipher(data))
|
|
248
|
+
end
|
|
230
249
|
|
|
231
|
-
|
|
250
|
+
# get the hmac from the tail of the packet (if one exists), and
|
|
251
|
+
# then validate it.
|
|
252
|
+
real_hmac = read_available(server.hmac.mac_length) || ""
|
|
232
253
|
|
|
233
|
-
|
|
234
|
-
|
|
254
|
+
@packet.append(server.final_cipher)
|
|
255
|
+
padding_length = @packet.read_byte
|
|
235
256
|
|
|
236
|
-
|
|
237
|
-
payload = server.decompress(payload)
|
|
257
|
+
payload = @packet.read(@packet_length - padding_length - 1)
|
|
238
258
|
|
|
239
|
-
|
|
259
|
+
my_computed_hmac = if server.hmac.etm
|
|
260
|
+
server.hmac.digest([server.sequence_number, @mac_data].pack("NA*"))
|
|
261
|
+
else
|
|
262
|
+
server.hmac.digest([server.sequence_number, @packet.content].pack("NA*"))
|
|
263
|
+
end
|
|
264
|
+
raise Net::SSH::Exception, "corrupted hmac detected #{server.hmac.class}" if real_hmac != my_computed_hmac
|
|
240
265
|
|
|
241
|
-
|
|
242
|
-
|
|
266
|
+
# try to decompress the payload, in case compression is active
|
|
267
|
+
payload = server.decompress(payload)
|
|
243
268
|
|
|
244
|
-
|
|
269
|
+
debug { "received packet nr #{server.sequence_number} type #{payload.getbyte(0)} len #{@packet_length}" }
|
|
270
|
+
|
|
271
|
+
server.increment(@packet_length)
|
|
272
|
+
@packet = nil
|
|
273
|
+
|
|
274
|
+
return Packet.new(payload)
|
|
275
|
+
end
|
|
245
276
|
end
|
|
277
|
+
# rubocop:enable Metrics/AbcSize
|
|
278
|
+
end
|
|
246
279
|
end
|
|
247
|
-
|
|
248
|
-
end; end; end
|
|
280
|
+
end
|
|
@@ -2,77 +2,76 @@ require 'net/ssh/errors'
|
|
|
2
2
|
require 'net/ssh/loggable'
|
|
3
3
|
require 'net/ssh/version'
|
|
4
4
|
|
|
5
|
-
module Net
|
|
5
|
+
module Net
|
|
6
|
+
module SSH
|
|
7
|
+
module Transport
|
|
8
|
+
# Negotiates the SSH protocol version and trades information about server
|
|
9
|
+
# and client. This is never used directly--it is always called by the
|
|
10
|
+
# transport layer as part of the initialization process of the transport
|
|
11
|
+
# layer.
|
|
12
|
+
#
|
|
13
|
+
# Note that this class also encapsulates the negotiated version, and acts as
|
|
14
|
+
# the authoritative reference for any queries regarding the version in effect.
|
|
15
|
+
class ServerVersion
|
|
16
|
+
include Loggable
|
|
6
17
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
# transport layer as part of the initialization process of the transport
|
|
10
|
-
# layer.
|
|
11
|
-
#
|
|
12
|
-
# Note that this class also encapsulates the negotiated version, and acts as
|
|
13
|
-
# the authoritative reference for any queries regarding the version in effect.
|
|
14
|
-
class ServerVersion
|
|
15
|
-
include Loggable
|
|
18
|
+
# The SSH version string as reported by Net::SSH
|
|
19
|
+
PROTO_VERSION = "SSH-2.0-Ruby/Net::SSH_#{Net::SSH::Version::CURRENT} #{RUBY_PLATFORM}"
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
# Any header text sent by the server prior to sending the version.
|
|
22
|
+
attr_reader :header
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
# The version string reported by the server.
|
|
25
|
+
attr_reader :version
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
# Instantiates a new ServerVersion and immediately (and synchronously)
|
|
28
|
+
# negotiates the SSH protocol in effect, using the given socket.
|
|
29
|
+
def initialize(socket, logger, timeout = nil)
|
|
30
|
+
@header = String.new
|
|
31
|
+
@version = nil
|
|
32
|
+
@logger = logger
|
|
33
|
+
negotiate!(socket, timeout)
|
|
34
|
+
end
|
|
25
35
|
|
|
26
|
-
|
|
27
|
-
# negotiates the SSH protocol in effect, using the given socket.
|
|
28
|
-
def initialize(socket, logger, timeout = nil)
|
|
29
|
-
@header = ""
|
|
30
|
-
@version = nil
|
|
31
|
-
@logger = logger
|
|
32
|
-
negotiate!(socket, timeout)
|
|
33
|
-
end
|
|
36
|
+
private
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
# Negotiates the SSH protocol to use, via the given socket. If the server
|
|
39
|
+
# reports an incompatible SSH version (e.g., SSH1), this will raise an
|
|
40
|
+
# exception.
|
|
41
|
+
def negotiate!(socket, timeout)
|
|
42
|
+
info { "negotiating protocol version" }
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def negotiate!(socket, timeout)
|
|
41
|
-
info { "negotiating protocol version" }
|
|
44
|
+
debug { "local is `#{PROTO_VERSION}'" }
|
|
45
|
+
socket.write "#{PROTO_VERSION}\r\n"
|
|
46
|
+
socket.flush
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
socket.write "#{PROTO_VERSION}\r\n"
|
|
45
|
-
socket.flush
|
|
48
|
+
raise Net::SSH::ConnectionTimeout, "timeout during server version negotiating" if timeout && !IO.select([socket], nil, nil, timeout)
|
|
46
49
|
|
|
47
|
-
if timeout && !IO.select([socket], nil, nil, timeout)
|
|
48
|
-
raise Net::SSH::ConnectionTimeout, "timeout during server version negotiating"
|
|
49
|
-
end
|
|
50
|
-
loop do
|
|
51
|
-
@version = ""
|
|
52
50
|
loop do
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
@version = String.new
|
|
52
|
+
loop do
|
|
53
|
+
begin
|
|
54
|
+
b = socket.readpartial(1)
|
|
55
|
+
raise Net::SSH::Disconnect, "connection closed by remote host" if b.nil?
|
|
56
|
+
rescue EOFError
|
|
57
|
+
raise Net::SSH::Disconnect, "connection closed by remote host"
|
|
58
|
+
end
|
|
59
|
+
@version << b
|
|
60
|
+
break if b == "\n"
|
|
58
61
|
end
|
|
59
|
-
@version
|
|
60
|
-
|
|
62
|
+
break if @version.match(/^SSH-/)
|
|
63
|
+
|
|
64
|
+
@header << @version
|
|
61
65
|
end
|
|
62
|
-
break if @version.match(/^SSH-/)
|
|
63
|
-
@header << @version
|
|
64
|
-
end
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
@version.chomp!
|
|
68
|
+
debug { "remote is `#{@version}'" }
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
raise Net::SSH::Exception, "incompatible SSH version `#{@version}'"
|
|
71
|
-
end
|
|
70
|
+
raise Net::SSH::Exception, "incompatible SSH version `#{@version}'" unless @version.match(/^SSH-(1\.99|2\.0)-/)
|
|
72
71
|
|
|
73
|
-
|
|
74
|
-
raise Net::SSH::ConnectionTimeout, "timeout during client version negotiating"
|
|
72
|
+
raise Net::SSH::ConnectionTimeout, "timeout during client version negotiating" if timeout && !IO.select(nil, [socket], nil, timeout)
|
|
75
73
|
end
|
|
76
74
|
end
|
|
75
|
+
end
|
|
77
76
|
end
|
|
78
|
-
end
|
|
77
|
+
end
|