2019-06-04 19:54:21 -04:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
|
|
|
|
module Msf
|
2021-07-23 11:12:46 -04:00
|
|
|
module Handler
|
|
|
|
|
###
|
|
|
|
|
#
|
|
|
|
|
# This handler implements the SSH tunneling interface.
|
|
|
|
|
#
|
|
|
|
|
###
|
|
|
|
|
module ReverseSsh
|
|
|
|
|
include Msf::Handler
|
|
|
|
|
include Msf::Handler::Reverse
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns the string representation of the handler type
|
|
|
|
|
#
|
|
|
|
|
def self.handler_type
|
|
|
|
|
return 'reverse_ssh'
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
#
|
|
|
|
|
# Returns the connection-described general handler type, in this case
|
|
|
|
|
# 'tunnel'.
|
|
|
|
|
#
|
|
|
|
|
def self.general_handler_type
|
|
|
|
|
'tunnel'
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# Initializes the reverse SSH handler and ads the options that are required
|
|
|
|
|
# for all reverse SSH payloads, like version string and auth params.
|
|
|
|
|
#
|
|
|
|
|
def initialize(info = {})
|
|
|
|
|
super
|
|
|
|
|
register_options([Opt::LPORT(22)])
|
|
|
|
|
register_advanced_options(
|
|
|
|
|
[
|
|
|
|
|
OptString.new('Ssh::Version', [
|
|
|
|
|
true,
|
|
|
|
|
'The SSH version string to provide',
|
2021-06-28 12:11:01 -05:00
|
|
|
default_version_string
|
2021-07-23 11:12:46 -04:00
|
|
|
])
|
|
|
|
|
], Msf::Handler::ReverseSsh
|
|
|
|
|
)
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# A URI describing where we are listening
|
|
|
|
|
#
|
|
|
|
|
# @param addr [String] the address that
|
|
|
|
|
# @return [String] A URI of the form +ssh://host:port/+
|
|
|
|
|
def listener_uri(addr = datastore['ReverseListenerBindAddress'])
|
|
|
|
|
addr = datastore['LHOST'] if addr.nil? || addr.empty?
|
|
|
|
|
uri_host = Rex::Socket.is_ipv6?(addr) ? "[#{addr}]" : addr
|
|
|
|
|
"ssh://#{uri_host}:#{bind_port}"
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# Create an Ssh listener
|
|
|
|
|
#
|
|
|
|
|
# @return [void]
|
|
|
|
|
def setup_handler
|
2022-08-24 10:42:09 +01:00
|
|
|
# The current SSH server implementation does not support OpenSSL 3
|
|
|
|
|
if OpenSSL::OPENSSL_LIBRARY_VERSION.start_with? 'OpenSSL 3'
|
|
|
|
|
raise RuntimeError, "ReverseSSH failed to load. OpenSSL version #{OpenSSL::VERSION} not supported."
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
local_addr = nil
|
|
|
|
|
local_port = bind_port
|
2019-06-04 19:54:21 -04:00
|
|
|
ex = false
|
|
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
ssh_opts = Rex::Proto::Ssh::Connection.default_options
|
|
|
|
|
ssh_opts['local_version'] = datastore['Ssh::Version']
|
|
|
|
|
|
|
|
|
|
# Start the SSH server service on this host/port
|
|
|
|
|
bind_addresses.each do |ip|
|
|
|
|
|
self.service = Rex::ServiceManager.start(Rex::Proto::Ssh::Server,
|
|
|
|
|
local_port, ip,
|
|
|
|
|
{
|
|
|
|
|
'Msf' => framework,
|
|
|
|
|
'MsfExploit' => self
|
|
|
|
|
},
|
|
|
|
|
comm,
|
|
|
|
|
ssh_opts)
|
|
|
|
|
local_addr = ip
|
|
|
|
|
rescue StandardError
|
|
|
|
|
ex = $!
|
|
|
|
|
print_error("Handler failed to bind to #{ip}:#{local_port}")
|
|
|
|
|
else
|
|
|
|
|
ex = false
|
|
|
|
|
break
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
service.on_client_connect_proc = proc { |cli| init_fd_client(cli) }
|
|
|
|
|
raise ex if ex
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
print_status("Started SSH reverse handler on #{listener_uri(local_addr)}")
|
|
|
|
|
|
|
|
|
|
if datastore['IgnoreUnknownPayloads']
|
|
|
|
|
print_status('Handler is ignoring unknown payloads')
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# Stops the handler & service
|
|
|
|
|
#
|
|
|
|
|
# @return [void]
|
|
|
|
|
def stop_handler
|
|
|
|
|
if service && (sessions == 0)
|
|
|
|
|
Rex::ServiceManager.stop_service(service)
|
|
|
|
|
end
|
2019-06-04 19:54:21 -04:00
|
|
|
end
|
|
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
def init_fd_client(cli)
|
2022-08-03 21:43:32 +01:00
|
|
|
Timeout.timeout(25) do
|
2021-07-23 11:12:46 -04:00
|
|
|
sleep 0.02 while cli.connection.open_channel_keys.empty?
|
|
|
|
|
fdc = Rex::Proto::Ssh::ChannelFD.new(cli)
|
|
|
|
|
service.clients.push(fdc)
|
|
|
|
|
create_session(fdc)
|
2019-06-22 21:36:42 -04:00
|
|
|
end
|
2021-07-23 11:12:46 -04:00
|
|
|
rescue Timeout::Error
|
|
|
|
|
elog("Unable to find channel FDs for client #{cli}")
|
2019-06-22 21:36:42 -04:00
|
|
|
end
|
|
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
def create_session(ssh, opts = {})
|
|
|
|
|
# If there is a parent payload, then use that in preference.
|
|
|
|
|
s = Sessions::SshCommandShellReverse.new(ssh, opts)
|
|
|
|
|
# Pass along the framework context
|
|
|
|
|
s.framework = framework
|
2019-06-22 21:36:42 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# Associate this system with the original exploit
|
|
|
|
|
# and any relevant information
|
|
|
|
|
s.set_from_exploit(assoc_exploit)
|
2019-06-22 21:36:42 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
# If the session is valid, register it with the framework and
|
|
|
|
|
# notify any waiters we may have.
|
|
|
|
|
if s
|
|
|
|
|
register_session(s)
|
|
|
|
|
end
|
2019-06-22 21:36:42 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
return s
|
|
|
|
|
end
|
2019-06-22 21:36:42 -04:00
|
|
|
|
2021-07-23 11:12:46 -04:00
|
|
|
#
|
|
|
|
|
# Always wait at least 5 seconds for this payload (due to channel delays)
|
|
|
|
|
#
|
|
|
|
|
def wfs_delay
|
|
|
|
|
datastore['WfsDelay'] > 4 ? datastore['WfsDelay'] : 5
|
|
|
|
|
end
|
|
|
|
|
attr_accessor :service # :nodoc:
|
2021-06-28 12:11:01 -05:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def default_version_string
|
2022-08-24 10:42:09 +01:00
|
|
|
default_version_string = 'SSH-2.0-OpenSSH_5.3p1'
|
|
|
|
|
|
2023-10-10 15:26:24 +01:00
|
|
|
# The current SSH server implementation does not support OpenSSL 3 or windows
|
|
|
|
|
return default_version_string if OpenSSL::OPENSSL_LIBRARY_VERSION.start_with?('OpenSSL 3') || ::Gem.win_platform?
|
2022-08-24 10:42:09 +01:00
|
|
|
|
2021-06-28 12:11:01 -05:00
|
|
|
require 'rex/proto/ssh/connection'
|
|
|
|
|
Rex::Proto::Ssh::Connection.default_options['local_version']
|
2022-07-19 18:29:58 +01:00
|
|
|
rescue OpenSSL::OpenSSLError => e
|
2022-07-14 12:05:01 +01:00
|
|
|
print_error("ReverseSSH handler did not load with OpenSSL version #{OpenSSL::VERSION}")
|
|
|
|
|
elog(e)
|
2022-08-24 10:42:09 +01:00
|
|
|
default_version_string
|
2021-06-28 12:11:01 -05:00
|
|
|
rescue LoadError => e
|
2022-07-14 12:05:01 +01:00
|
|
|
print_error('ReverseSSH handler did not load as PTY access is not available on all platforms.')
|
2021-06-28 12:11:01 -05:00
|
|
|
elog(e)
|
2022-08-24 10:42:09 +01:00
|
|
|
default_version_string
|
2021-06-28 12:11:01 -05:00
|
|
|
end
|
2021-07-23 11:12:46 -04:00
|
|
|
end
|
2019-06-22 21:36:42 -04:00
|
|
|
end
|
2019-09-15 01:14:04 -04:00
|
|
|
end
|