Path: blob/master/modules/post/windows/manage/run_as_psh.rb
19593 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Windows::Powershell7def initialize(info = {})8super(9update_info(10info,11'Name' => "Windows 'Run As' Using PowerShell",12'Description' => %q{ This module will start a process as another user using PowerShell. },13'License' => MSF_LICENSE,14'Author' => ['p3nt4'],15'Platform' => ['win'],16'SessionTypes' => ['meterpreter'],17'Compat' => {18'Meterpreter' => {19'Commands' => %w[20stdapi_sys_process_execute21]22}23},24'Notes' => {25'Stability' => [CRASH_SAFE],26'SideEffects' => [],27'Reliability' => []28}29)30)31register_options(32[33OptString.new('USER', [true, 'User to run executable as', nil]),34OptString.new('PASS', [true, 'Password of user', nil]),35OptString.new('DOMAIN', [false, 'Domain of user', '']),36OptString.new('EXE', [true, 'Executable to run', 'cmd.exe']),37OptString.new('ARGS', [false, 'Arguments', nil]),38OptString.new('PATH', [true, 'Working Directory', 'C:\\']),39OptBool.new('CHANNELIZE', [true, 'Channelize output, required for reading output or interacting', true]),40OptBool.new('INTERACTIVE', [true, 'Run interactively', true]),41OptBool.new('HIDDEN', [true, 'Hide the window', true])42]43)44end4546def run47fail_with(Failure::BadConfig, 'PowerShell is not available') unless have_powershell?4849# Variable Setup50user = datastore['user']51pass = datastore['pass']52domain = datastore['domain']53exe = datastore['exe'].gsub('\\', '\\\\\\\\')54inter = datastore['interactive']55args = datastore['args']56path = datastore['path'].gsub('\\', '\\\\\\\\')57channelized = datastore['channelize']58hidden = datastore['hidden']5960if user.include?('\\')61domain = user.split('\\')[0]62user = user.split('\\')[1]63end6465# Check if session is interactive66if !session.interacting && inter67print_error('Interactive mode can only be used in a meterpreter console')68print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")69raise 'Invalid console'70end7172# Prepare powershell script73scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "74scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\#{user}',$pw; "75scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp"76if args && args != ''77scr << " -argumentlist '#{args}' "78end7980if hidden81print_status('Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false')82scr << ' -WindowStyle hidden'83end8485scr = " -c \"#{scr}\""86# Execute script87p = client.sys.process.execute(88'powershell.exe', scr,89'Channelized' => channelized,90'Desktop' => false,91'Session' => false,92'Hidden' => true,93'Interactive' => inter,94'InMemory' => false,95'UseThreadToken' => false96)97print_status("Process #{p.pid} created.")98print_status("Channel #{p.channel.cid} created.") if p.channel99100# Process output101if inter && p.channel102client.console.interact_with_channel(p.channel)103elsif p.channel104data = p.channel.read105print_line(data) if data106end107end108end109110111