Skip to content

Commit 78822fd

Browse files
committed
Land rapid7#9524, prefer 'shell' channels over 'exec' channels for ssh CommandStream
2 parents 9cbc55c + b1d0529 commit 78822fd

15 files changed

+48
-43
lines changed

lib/net/ssh/command_stream.rb

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,42 @@ module PeerInfo
1515
attr_accessor :localinfo
1616
end
1717

18-
def initialize(ssh, cmd, cleanup = false)
18+
def shell_requested(channel, success)
19+
raise "could not request ssh shell" unless success
20+
channel[:data] = ''
21+
22+
channel.on_eof do
23+
self.rsock.close rescue nil
24+
self.ssh.close rescue nil
25+
self.thread.kill
26+
end
27+
28+
channel.on_close do
29+
self.rsock.close rescue nil
30+
self.ssh.close rescue nil
31+
self.thread.kill
32+
end
33+
34+
channel.on_data do |ch,data|
35+
self.rsock.write(data)
36+
end
37+
38+
channel.on_extended_data do |ch, ctype, data|
39+
self.rsock.write(data)
40+
end
41+
42+
self.channel = channel
43+
end
44+
45+
def initialize(ssh, cmd = nil, cleanup = true)
1946

2047
self.lsock, self.rsock = Rex::Socket.tcp_socket_pair()
2148
self.lsock.extend(Rex::IO::Stream)
2249
self.lsock.extend(PeerInfo)
2350
self.rsock.extend(Rex::IO::Stream)
2451

2552
self.ssh = ssh
26-
self.thread = Thread.new(ssh,cmd,cleanup) do |rssh,rcmd,rcleanup|
53+
self.thread = Thread.new(ssh,cmd,cleanup) do |rssh, rcmd, rcleanup|
2754

2855
begin
2956
info = rssh.transport.socket.getpeername_as_array
@@ -33,32 +60,10 @@ def initialize(ssh, cmd, cleanup = false)
3360
self.lsock.localinfo = "#{info[1]}:#{info[2]}"
3461

3562
rssh.open_channel do |rch|
36-
rch.exec(rcmd) do |c, success|
37-
raise "could not execute command: #{rcmd.inspect}" unless success
38-
39-
c[:data] = ''
40-
41-
c.on_eof do
42-
self.rsock.close rescue nil
43-
self.ssh.close rescue nil
44-
self.thread.kill
45-
end
46-
47-
c.on_close do
48-
self.rsock.close rescue nil
49-
self.ssh.close rescue nil
50-
self.thread.kill
51-
end
52-
53-
c.on_data do |ch,data|
54-
self.rsock.write(data)
55-
end
56-
57-
c.on_extended_data do |ch, ctype, data|
58-
self.rsock.write(data)
59-
end
60-
61-
self.channel = c
63+
if cmd.nil?
64+
rch.send_channel_request("shell", &method(:shell_requested))
65+
else
66+
rch.exec(rsh, &method(:shell_requested))
6267
end
6368
end
6469

@@ -85,7 +90,7 @@ def initialize(ssh, cmd, cleanup = false)
8590
end
8691

8792
# Shut down the SSH session if requested
88-
if(rcleanup)
93+
if rcleanup
8994
rssh.close
9095
end
9196
end

modules/auxiliary/scanner/ssh/ssh_login.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def session_setup(result, ssh_socket)
5757
return unless ssh_socket
5858

5959
# Create a new session
60-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/sh', true)
60+
conn = Net::SSH::CommandStream.new(ssh_socket)
6161

6262
merge_me = {
6363
'USERPASS_FILE' => nil,

modules/auxiliary/scanner/ssh/ssh_login_pubkey.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def session_setup(result, ssh_socket, fingerprint)
7272
return unless ssh_socket
7373

7474
# Create a new session from the socket
75-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/sh', true)
75+
conn = Net::SSH::CommandStream.new(ssh_socket)
7676

7777
# Clean up the stored data - need to stash the keyfile into
7878
# a datastore for later reuse.

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, '/bin/sh', true)
113+
conn = Net::SSH::CommandStream.new(ssh)
114114
ssh = nil
115115
return conn
116116
end

modules/exploits/linux/ssh/ceragon_fibeair_known_privkey.rb

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

108108
# Create a new session from the socket, then dump it.
109-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/sh', true)
109+
conn = Net::SSH::CommandStream.new(ssh_socket)
110110
ssh_socket = nil
111111

112112
return conn

modules/exploits/linux/ssh/exagrid_known_privkey.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def do_login(ssh_options)
9494
if ssh_socket
9595

9696
# Create a new session from the socket, then dump it.
97-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/bash -i', true)
97+
conn = Net::SSH::CommandStream.new(ssh_socket)
9898
ssh_socket = nil
9999

100100
return conn

modules/exploits/linux/ssh/f5_bigip_known_privkey.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def do_login(user)
109109
return false unless ssh_socket
110110

111111
# Create a new session from the socket, then dump it.
112-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/sh', true)
112+
conn = Net::SSH::CommandStream.new(ssh_socket)
113113
ssh_socket = nil
114114
conn
115115
end

modules/exploits/linux/ssh/loadbalancerorg_enterprise_known_privkey.rb

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

105105
# Create a new session from the socket, then dump it.
106-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/bash', true)
106+
conn = Net::SSH::CommandStream.new(ssh_socket)
107107
ssh_socket = nil
108108

109109
return conn

modules/exploits/linux/ssh/quantum_dxi_known_privkey.rb

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

104104
# Create a new session from the socket, then dump it.
105-
conn = Net::SSH::CommandStream.new(ssh_socket, '/bin/bash', true)
105+
conn = Net::SSH::CommandStream.new(ssh_socket)
106106
ssh_socket = nil
107107

108108
return conn

modules/exploits/linux/ssh/quantum_vmpro_backdoor.rb

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

116116
if ssh
117-
conn = Net::SSH::CommandStream.new(ssh, 'shell-escape', true)
117+
conn = Net::SSH::CommandStream.new(ssh, 'shell-escape')
118118
return conn
119119
end
120120

0 commit comments

Comments
 (0)