Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/msf/base/sessions/ssh_command_shell_bind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def initialize(client, cid, ssh_channel, params)

ssh_channel.on_eof do |_ch|
dlog('ssh_channel#on_eof shutting down the socket')
rsock.shutdown(Socket::SHUT_WR)
lsock.shutdown(Socket::SHUT_WR)
end

lsock.extend(SocketInterface)
Expand Down Expand Up @@ -141,14 +141,15 @@ def write(buf, length = nil)
# Represents an SSH reverse port forward.
# Will receive connection messages back from the SSH server,
# whereupon a TcpClientChannel will be opened

class TcpServerChannel
include Rex::IO::StreamServer

def initialize(params, client, host, port)
attr_reader :params

def initialize(params, client)
@params = params
@client = client
@host = host
@port = port
@channels = []
@closed = false
@mutex = Mutex.new
Expand Down Expand Up @@ -181,7 +182,7 @@ def closed?

def close
if !closed?
@closed = @client.stop_server_channel(@host, @port)
@closed = @client.stop_server_channel(@params.localhost, @params.localport)
end
end

Expand Down Expand Up @@ -305,21 +306,23 @@ def create_server_channel(params)
timed_out = false
@ssh_connection.send_global_request('tcpip-forward', :string, params.localhost, :long, params.localport) do |success, response|
mutex.synchronize {
remote_port = params.localport
remote_port = response.read_long if remote_port == 0
if params.localport == 0
params.localport = response.read_long
end

if success
if timed_out
# We're not using the port; clean it up
elog("Remote forwarding on #{params.localhost}:#{params.localport} succeeded after timeout. Stopping channel to clean up dangling port")
stop_server_channel(params.localhost, remote_port)
elog("Remote forwarding on #{Rex::Socket.to_authority(params.localhost, params.localport)} succeeded after timeout, stopping channel to clean up dangling port")
stop_server_channel(params.localhost, params.localport)
else
dlog("Remote forwarding from #{params.localhost} established on port #{remote_port}")
key = [params.localhost, remote_port]
msf_channel = TcpServerChannel.new(params, self, params.localhost, remote_port)
dlog("Remote forwarding from #{params.localhost} established on port #{params.localport}")
key = [params.localhost, params.localport]
msf_channel = TcpServerChannel.new(params, self)
@server_channels[key] = msf_channel
end
else
elog("Remote forwarding failed on #{params.localhost}:#{params.localport}")
elog("Remote forwarding failed on #{Rex::Socket.to_authority(params.localhost, params.localport)}")
end
condition.signal
}
Expand Down Expand Up @@ -364,7 +367,7 @@ def create_client_channel(params)
condition = ConditionVariable.new
opened = false
ssh_channel = @ssh_connection.open_channel('direct-tcpip', :string, params.peerhost, :long, params.peerport, :string, params.localhost, :long, params.localport) do |_|
dlog("new direct-tcpip channel opened to #{Rex::Socket.is_ipv6?(params.peerhost) ? '[' + params.peerhost + ']' : params.peerhost}:#{params.peerport}")
dlog("new direct-tcpip channel opened to #{Rex::Socket.to_authority(params.peerhost, params.peerport)}")
opened = true
mutex.synchronize do
condition.signal
Expand Down