2016-12-20 03:38:19 +00:00
|
|
|
##
|
2016-12-22 00:13:53 +00:00
|
|
|
# This module requires Metasploit: http://metasploit.com/download
|
|
|
|
# Current source: https://github.com/rapid7/metasploit-framework
|
2016-12-20 03:38:19 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core/post/windows/powershell'
|
|
|
|
|
|
|
|
class MetasploitModule < Msf::Post
|
2016-12-22 00:13:53 +00:00
|
|
|
include Msf::Post::Windows::Powershell
|
|
|
|
def initialize(info = {})
|
|
|
|
super(
|
|
|
|
update_info(
|
|
|
|
info,
|
|
|
|
'Name' => 'Windows \'Run As\' Using Powershell',
|
|
|
|
'Description' => %q( This module will start a process as another user using powershell. ),
|
|
|
|
'License' => MSF_LICENSE,
|
2017-03-01 02:38:15 +00:00
|
|
|
'Author' => ['p3nt4'],
|
|
|
|
'Platform' => ['win'],
|
|
|
|
'SessionTypes' => ['meterpreter']
|
2016-12-22 00:13:53 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
OptString.new('USER', [true, 'User to run executable as', nil]),
|
|
|
|
OptString.new('PASS', [true, 'Password of user', nil]),
|
|
|
|
OptString.new('DOMAIN', [false, 'Domain of user', '']),
|
|
|
|
OptString.new('EXE', [true, 'Executable to run', 'cmd.exe']),
|
|
|
|
OptString.new('ARGS', [false, 'Arguments', nil]),
|
|
|
|
OptString.new('PATH', [true, 'Working Directory', 'C:\\']),
|
|
|
|
OptBool.new('CHANNELIZE', [true, 'Chanelize output, required for reading output or interracting', true]),
|
|
|
|
OptBool.new('INTERACTIVE', [true, 'Run interactively', true]),
|
|
|
|
OptBool.new('HIDDEN', [true, 'Hide the window', true])
|
2017-05-03 20:42:21 +00:00
|
|
|
])
|
2016-12-22 00:13:53 +00:00
|
|
|
end
|
2016-12-20 03:38:19 +00:00
|
|
|
|
2016-12-22 00:13:53 +00:00
|
|
|
def run
|
|
|
|
raise "Powershell is required" if !have_powershell?
|
|
|
|
# Variable Setup
|
|
|
|
user = datastore['user']
|
|
|
|
pass = datastore['pass']
|
|
|
|
domain = datastore['domain']
|
2017-03-01 02:38:15 +00:00
|
|
|
exe = datastore['exe'].gsub('\\', '\\\\\\\\')
|
2016-12-22 00:13:53 +00:00
|
|
|
inter = datastore['interactive']
|
|
|
|
args = datastore['args']
|
2017-03-01 02:38:15 +00:00
|
|
|
path = datastore['path'].gsub('\\', '\\\\\\\\')
|
2016-12-22 00:13:53 +00:00
|
|
|
channelized = datastore['channelize']
|
|
|
|
hidden = datastore['hidden']
|
2017-03-01 02:38:15 +00:00
|
|
|
if user.include? '\\'
|
|
|
|
domain = user.split('\\')[0]
|
|
|
|
user = user.split('\\')[1]
|
|
|
|
end
|
2016-12-22 00:13:53 +00:00
|
|
|
# Check if session is interactive
|
2017-03-01 02:38:15 +00:00
|
|
|
if !session.interacting && inter
|
|
|
|
print_error('Interactive mode can only be used in a meterpreter console')
|
2016-12-22 00:13:53 +00:00
|
|
|
print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")
|
|
|
|
raise 'Invalide console'
|
|
|
|
end
|
|
|
|
# Prepare powershell script
|
|
|
|
scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "
|
2017-03-01 02:38:15 +00:00
|
|
|
scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\#{user}',$pw; "
|
2016-12-22 00:13:53 +00:00
|
|
|
scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp"
|
2017-03-01 02:38:15 +00:00
|
|
|
if args && args != ''
|
2016-12-22 00:13:53 +00:00
|
|
|
scr << " -argumentlist '#{args}' "
|
|
|
|
end
|
|
|
|
if hidden
|
2017-03-01 02:38:15 +00:00
|
|
|
print_status('Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false')
|
|
|
|
scr << ' -WindowStyle hidden'
|
2016-12-22 00:13:53 +00:00
|
|
|
end
|
|
|
|
scr = " -c \"#{scr}\""
|
|
|
|
# Execute script
|
|
|
|
p = client.sys.process.execute("powershell.exe", scr,
|
|
|
|
'Channelized' => channelized,
|
|
|
|
'Desktop' => false,
|
|
|
|
'Session' => false,
|
|
|
|
'Hidden' => true,
|
|
|
|
'Interactive' => inter,
|
|
|
|
'InMemory' => false,
|
|
|
|
'UseThreadToken' => false)
|
|
|
|
print_status("Process #{p.pid} created.")
|
2017-03-01 02:38:15 +00:00
|
|
|
print_status("Channel #{p.channel.cid} created.") if p.channel
|
2016-12-22 00:13:53 +00:00
|
|
|
# Process output
|
2017-03-01 02:38:15 +00:00
|
|
|
if inter && p.channel
|
2016-12-22 00:13:53 +00:00
|
|
|
client.console.interact_with_channel(p.channel)
|
|
|
|
elsif p.channel
|
2017-03-01 02:38:15 +00:00
|
|
|
data = p.channel.read
|
2016-12-22 00:13:53 +00:00
|
|
|
print_line(data) if data
|
2016-12-20 03:38:19 +00:00
|
|
|
end
|
2016-12-22 00:13:53 +00:00
|
|
|
end
|
2016-12-20 03:38:19 +00:00
|
|
|
end
|