Skip to content

Commit 566e12b

Browse files
committed
Add error_callback to SSH Command Stream
1 parent 358e79b commit 566e12b

24 files changed

+32
-29
lines changed

lib/msf/base/sessions/aws_instance_connect_command_shell_bind.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def desc
7676
end
7777

7878
def bootstrap(datastore = {}, handler = nil)
79-
@ssh_command_stream = Net::SSH::CommandStream.new(ssh_connection, session: self)
79+
@ssh_command_stream = Net::SSH::CommandStream.new(ssh_connection, session: self, logger: self)
8080

8181
@ssh_command_stream.verify_channel
8282
# set remote_window_size to 32 which seems to help stability

lib/msf/base/sessions/ssh_command_shell_bind.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def bootstrap(datastore = {}, handler = nil)
243243
# shells accessed through SSH may respond to the echo command issued for verification as expected
244244
datastore['AutoVerifySession'] &= @platform.blank?
245245

246-
@rstream = Net::SSH::CommandStream.new(ssh_connection, session: self).lsock
246+
@rstream = Net::SSH::CommandStream.new(ssh_connection, session: self, logger: self).lsock
247247
super
248248

249249
@info = "SSH #{username} @ #{@peer_info}"

lib/net/ssh/command_stream.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Net::SSH::CommandStream
44

5-
attr_accessor :channel, :thread, :error, :ssh, :session
5+
attr_accessor :channel, :thread, :error, :ssh, :session, :logger
66
attr_accessor :lsock, :rsock, :monitor
77

88
module PeerInfo
@@ -13,9 +13,8 @@ module PeerInfo
1313

1414
def shell_requested(channel, success)
1515
unless success
16-
error = Net::SSH::ChannelRequestFailed, 'Shell/exec channel request failed'
16+
error = Net::SSH::ChannelRequestFailed.new('Shell/exec channel request failed')
1717
handle_error(error: error)
18-
raise error
1918
end
2019

2120
self.channel = channel
@@ -42,8 +41,9 @@ def shell_requested(channel, success)
4241
end
4342
end
4443

45-
def initialize(ssh, cmd = nil, pty: false, cleanup: false, session: nil)
44+
def initialize(ssh, cmd = nil, pty: false, cleanup: false, session: nil, logger: nil)
4645
self.session = session
46+
self.logger = logger
4747
self.lsock, self.rsock = Rex::Socket.tcp_socket_pair()
4848
self.lsock.extend(Rex::IO::Stream)
4949
self.lsock.extend(PeerInfo)
@@ -79,7 +79,6 @@ def initialize(ssh, cmd = nil, pty: false, cleanup: false, session: nil)
7979
channel.on_open_failed do |ch, code, desc|
8080
error = Net::SSH::ChannelOpenFailed.new(code, 'Session channel open failed')
8181
handle_error(error: error)
82-
raise error
8382
end
8483

8584
self.monitor = Thread.new do
@@ -109,7 +108,6 @@ def initialize(ssh, cmd = nil, pty: false, cleanup: false, session: nil)
109108
rssh.close
110109
end
111110
end
112-
self.thread.abort_on_exception = true
113111
rescue ::StandardError => e
114112
# XXX: This won't be set UNTIL there's a failure from a thread
115113
handle_error(error: e)
@@ -129,6 +127,11 @@ def verify_channel
129127

130128
def handle_error(error: nil)
131129
self.error = error if error
130+
131+
if self.logger
132+
self.logger.print_error("SSH Command Stream encountered an error: #{self.error} (Server Version: #{self.ssh.transport.server_version.version})")
133+
end
134+
132135
cleanup
133136
end
134137

modules/auxiliary/scanner/ssh/eaton_xpert_backdoor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def run_host(ip)
8484
info: version
8585
)
8686

87-
shell = Net::SSH::CommandStream.new(ssh)
87+
shell = Net::SSH::CommandStream.new(ssh, logger: self)
8888

8989
# XXX: Wait for CommandStream to log a channel request failure
9090
sleep 0.1

modules/auxiliary/scanner/ssh/fortinet_backdoor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run_host(ip)
7777
info: version
7878
)
7979

80-
shell = Net::SSH::CommandStream.new(ssh)
80+
shell = Net::SSH::CommandStream.new(ssh, logger: self)
8181

8282
# XXX: Wait for CommandStream to log a channel request failure
8383
sleep 0.1

modules/auxiliary/scanner/ssh/libssh_auth_bypass.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def run_host(ip)
120120
info: version
121121
)
122122

123-
shell = Net::SSH::CommandStream.new(ssh, datastore['CMD'], pty: datastore['SPAWN_PTY'])
123+
shell = Net::SSH::CommandStream.new(ssh, datastore['CMD'], pty: datastore['SPAWN_PTY'], logger: self)
124124

125125
# XXX: Wait for CommandStream to log a channel request failure
126126
sleep 0.1

modules/exploits/apple_ios/ssh/cydia_default_ssh.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def do_login(user, pass)
110110
end
111111

112112
if ssh
113-
conn = Net::SSH::CommandStream.new(ssh)
113+
conn = Net::SSH::CommandStream.new(ssh, logger: self)
114114
ssh = nil
115115
return conn
116116
end

modules/exploits/freebsd/http/junos_phprc_auto_prepend_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def ssh_login
349349
end
350350

351351
if ssh
352-
Net::SSH::CommandStream.new(ssh)
352+
Net::SSH::CommandStream.new(ssh, logger: self)
353353
end
354354
end
355355

modules/exploits/linux/http/ubiquiti_airos_file_upload.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def ssh_login
156156
private: private_key,
157157
private_type: :ssh_key
158158
)
159-
return Net::SSH::CommandStream.new(ssh)
159+
return Net::SSH::CommandStream.new(ssh, logger: self)
160160
end
161161

162162
nil

modules/exploits/linux/ssh/ceragon_fibeair_known_privkey.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def do_login(user)
111111
if ssh_socket
112112

113113
# Create a new session from the socket, then dump it.
114-
conn = Net::SSH::CommandStream.new(ssh_socket)
114+
conn = Net::SSH::CommandStream.new(ssh_socket, logger: self)
115115
ssh_socket = nil
116116

117117
return conn

0 commit comments

Comments
 (0)