Skip to content

Commit a580055

Browse files
committed
Land rapid7#7730, add module to run exe as another user via powershell
2 parents ac4eae2 + d31846c commit a580055

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Overview
2+
This module will start a process as another user using powershell.
3+
By default, it will start an interactive cmd as the target user.
4+
5+
## Module Options
6+
- **USER** - The use to run the program as.
7+
- **PASS** - The user's password
8+
- **DOMAIN** - The domain of the user
9+
- **EXE** - The program to run (default cmd.exe)
10+
- **ARGS** - The program arguments
11+
- **PATH** - The path to run the program in (default C:\\)
12+
- **CHANNELIZE** - Channelize the output, required to read output or interact
13+
- **INTERACT** - Interact with program
14+
- **HIDDEN** - Hide the console window
15+
16+
## Module Process
17+
The process will use the Start-Process command of powershell to run a process as another user.
18+
19+
## Limitations
20+
- Requires Powershell
21+
- Hidden Mode does not work with older powershell versions
22+
- Interactive mode needs to be run from a meterpreter console
23+
24+
## Examples
25+
26+
`
27+
meterpreter > getuid
28+
Server username: NT AUTHORITY\SYSTEM
29+
meterpreter > run post/windows/manage/run_as_psh user=test pass=mypassword
30+
31+
[*] Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false
32+
[*] Process 1672 created.
33+
[*] Channel 30 created.
34+
Microsoft Windows [Version 10.0.14393]
35+
(c) 2016 Microsoft Corporation. All rights reserved.
36+
37+
C:\\>whoami
38+
whoami
39+
my-pc\test
40+
41+
C:\\>
42+
43+
meterpreter > run post/windows/manage/run_as_psh user=test pass=mypassword hidden=false channelize=false interactive=false exe=cmd path=C:\\\\windows args="/c start notepad"
44+
45+
[*] Process 9768 created.
46+
meterpreter >
47+
48+
`
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)