Skip to content

Commit 759b67c

Browse files
authored
Fix ru_as_psh with domain accounts
The current versions has too many escape backslashes, as a result, running run_as_psh for domain users does not work. Also added support for DOMAIN\\User format in the USER parameter.
1 parent 4882927 commit 759b67c

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

modules/post/windows/manage/run_as_psh.rb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def initialize(info = {})
1616
'Name' => 'Windows \'Run As\' Using Powershell',
1717
'Description' => %q( This module will start a process as another user using powershell. ),
1818
'License' => MSF_LICENSE,
19-
'Author' => [ 'p3nt4' ],
20-
'Platform' => [ 'win' ],
21-
'SessionTypes' => [ 'meterpreter' ]
19+
'Author' => ['p3nt4'],
20+
'Platform' => ['win'],
21+
'SessionTypes' => ['meterpreter']
2222
)
2323
)
2424
register_options(
@@ -41,28 +41,32 @@ def run
4141
user = datastore['user']
4242
pass = datastore['pass']
4343
domain = datastore['domain']
44-
exe = datastore['exe'].gsub("\\", "\\\\\\\\")
44+
exe = datastore['exe'].gsub('\\', '\\\\\\\\')
4545
inter = datastore['interactive']
4646
args = datastore['args']
47-
path = datastore['path'].gsub("\\", "\\\\\\\\")
47+
path = datastore['path'].gsub('\\', '\\\\\\\\')
4848
channelized = datastore['channelize']
4949
hidden = datastore['hidden']
50+
if user.include? '\\'
51+
domain = user.split('\\')[0]
52+
user = user.split('\\')[1]
53+
end
5054
# Check if session is interactive
51-
if (!session.interacting and inter)
52-
print_error("Interactive mode can only be used in a meterpreter console")
55+
if !session.interacting && inter
56+
print_error('Interactive mode can only be used in a meterpreter console')
5357
print_error("Use 'run post/windows/manage/run_as_psh USER=x PASS=X EXE=X' or 'SET INTERACTIVE false'")
5458
raise 'Invalide console'
5559
end
5660
# Prepare powershell script
5761
scr = "$pw = convertto-securestring '#{pass}' -asplaintext -force; "
58-
scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\\\#{user}',$pw; "
62+
scr << "$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist '#{domain}\\#{user}',$pw; "
5963
scr << "Start-process '#{exe}' -WorkingDirectory '#{path}' -Credential $pp"
60-
if (args and args != '')
64+
if args && args != ''
6165
scr << " -argumentlist '#{args}' "
6266
end
6367
if hidden
64-
print_status("Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false")
65-
scr << " -WindowStyle hidden"
68+
print_status('Hidden mode may not work on older powershell versions, if it fails, try HIDDEN=false')
69+
scr << ' -WindowStyle hidden'
6670
end
6771
scr = " -c \"#{scr}\""
6872
# Execute script
@@ -75,12 +79,12 @@ def run
7579
'InMemory' => false,
7680
'UseThreadToken' => false)
7781
print_status("Process #{p.pid} created.")
78-
print_status("Channel #{p.channel.cid} created.") if (p.channel)
82+
print_status("Channel #{p.channel.cid} created.") if p.channel
7983
# Process output
80-
if (inter and p.channel)
84+
if inter && p.channel
8185
client.console.interact_with_channel(p.channel)
8286
elsif p.channel
83-
data = p.channel.read()
87+
data = p.channel.read
8488
print_line(data) if data
8589
end
8690
end

0 commit comments

Comments
 (0)