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_psh.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::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)25)26register_options(27[28OptString.new('USER', [true, 'User to run executable as', nil]),29OptString.new('PASS', [true, 'Password of user', nil]),30OptString.new('DOMAIN', [false, 'Domain of user', '']),31OptString.new('EXE', [true, 'Executable to run', 'cmd.exe']),32OptString.new('ARGS', [false, 'Arguments', nil]),33OptString.new('PATH', [true, 'Working Directory', 'C:\\']),34OptBool.new('CHANNELIZE', [true, 'Chanelize output, required for reading output or interracting', true]),35OptBool.new('INTERACTIVE', [true, 'Run interactively', true]),36OptBool.new('HIDDEN', [true, 'Hide the window', true])37]38)39end4041def run42raise 'Powershell is required' if !have_powershell?4344# Variable Setup45user = datastore['user']46pass = datastore['pass']47domain = datastore['domain']48exe = datastore['exe'].gsub('\\', '\\\\\\\\')49inter = datastore['interactive']50args = datastore['args']51path = datastore['path'].gsub('\\', '\\\\\\\\')52channelized = datastore['channelize']53hidden = datastore['hidden']54if user.include? '\\'55domain = user.split('\\')[0]56user = user.split('\\')[1]57end58# Check if session is interactive59if !session.interacting && inter60print_error('Interactive mode can only be used in a meterpreter console')61print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")62raise 'Invalide console'63end64# Prepare powershell script65scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "66scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\#{user}',$pw; "67scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp"68if args && args != ''69scr << " -argumentlist '#{args}' "70end71if hidden72print_status('Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false')73scr << ' -WindowStyle hidden'74end75scr = " -c \"#{scr}\""76# Execute script77p = client.sys.process.execute('powershell.exe', scr,78'Channelized' => channelized,79'Desktop' => false,80'Session' => false,81'Hidden' => true,82'Interactive' => inter,83'InMemory' => false,84'UseThreadToken' => false)85print_status("Process #{p.pid} created.")86print_status("Channel #{p.channel.cid} created.") if p.channel87# Process output88if inter && p.channel89client.console.interact_with_channel(p.channel)90elsif p.channel91data = p.channel.read92print_line(data) if data93end94end95end969798