Skip to content

Commit 2ec4577

Browse files
authored
Merge pull request #20180 from smashery/powershell-impersonate-warning
Warn user if they are using PowerShell with impersonation
2 parents 9e90b5d + 904f4b6 commit 2ec4577

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

lib/rex/post/meterpreter/extensions/powershell/powershell.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def import_file(opts={})
5353
request.add_tlv(TLV_TYPE_POWERSHELL_ASSEMBLY_SIZE, binary.length)
5454
request.add_tlv(TLV_TYPE_POWERSHELL_ASSEMBLY, binary)
5555
client.send_request(request)
56-
return true
56+
return { loaded: true }
5757
end
5858

59-
return false
59+
return { loaded: false }
6060
end
6161

6262
def session_remove(opts={})
@@ -75,7 +75,14 @@ def execute_string(opts={})
7575
request.add_tlv(TLV_TYPE_POWERSHELL_SESSIONID, opts[:session_id]) if opts[:session_id]
7676

7777
response = client.send_request(request)
78-
return response.get_tlv_value(TLV_TYPE_POWERSHELL_RESULT)
78+
result = {}
79+
handle = client.sys.config.get_token_handle()
80+
if handle != 0
81+
result[:warning] = 'Impersonation will not apply to PowerShell.'
82+
end
83+
84+
result[:output] = response.get_tlv_value(TLV_TYPE_POWERSHELL_RESULT)
85+
return result
7986
end
8087

8188
def shell(opts={})
@@ -87,7 +94,16 @@ def shell(opts={})
8794
if channel_id.nil?
8895
raise Exception, "We did not get a channel back!"
8996
end
90-
Rex::Post::Meterpreter::Channels::Pools::StreamPool.new(client, channel_id, 'powershell_psh', CHANNEL_FLAG_SYNCHRONOUS, response)
97+
98+
result = {}
99+
handle = client.sys.config.get_token_handle()
100+
if handle != 0
101+
result[:warning] = 'Impersonation will not apply to PowerShell.'
102+
end
103+
104+
result[:channel] = Rex::Post::Meterpreter::Channels::Pools::StreamPool.new(client, channel_id, 'powershell_psh', CHANNEL_FLAG_SYNCHRONOUS, response)
105+
106+
result
91107
end
92108

93109
end

lib/rex/post/meterpreter/extensions/stdapi/command_ids.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ module Stdapi
130130
COMMAND_ID_STDAPI_SYS_PROCESS_SET_TERM_SIZE = EXTENSION_ID_STDAPI + 118
131131
COMMAND_ID_STDAPI_SYS_PROCESS_MEMORY_SEARCH = EXTENSION_ID_STDAPI + 119
132132
COMMAND_ID_STDAPI_SYS_CONFIG_UPDATE_TOKEN = EXTENSION_ID_STDAPI + 120
133+
COMMAND_ID_STDAPI_SYS_CONFIG_GET_TOKEN_HANDLE = EXTENSION_ID_STDAPI + 121
133134

134135

135136
end; end; end; end; end

lib/rex/post/meterpreter/extensions/stdapi/sys/config.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ def update_token(token_handle)
176176
end
177177

178178
#
179+
# Gets the current impersonation token
180+
#
181+
def get_token_handle()
182+
req = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_GET_TOKEN_HANDLE)
183+
res = client.send_request(req)
184+
res.get_tlv_value(TLV_TYPE_HANDLE)
185+
end
186+
187+
#
179188
# Enables all possible privileges
180189
#
181190
def getprivs

lib/rex/post/meterpreter/ui/console/command_dispatcher/powershell.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ def cmd_powershell_shell(*args)
9898
end
9999
}
100100

101-
channel = client.powershell.shell(opts)
101+
result = client.powershell.shell(opts)
102+
103+
channel = result[:channel]
104+
105+
if result[:warning].present?
106+
print_warning(result[:warning])
107+
end
108+
102109
shell.interact_with_channel(channel)
103110
end
104111

@@ -144,12 +151,17 @@ def cmd_powershell_import(*args)
144151
}
145152

146153
result = client.powershell.import_file(opts)
147-
if result.nil? || result == false
154+
155+
if result[:warning].present?
156+
print_warning(result[:warning])
157+
end
158+
159+
if result[:loaded] == false
148160
print_error('File failed to load. The file must end in ".ps1" or ".dll".')
149-
elsif result == true || result.empty?
161+
elsif result[:loaded] == true || result[:output].empty?
150162
print_good("File successfully imported. No result was returned.")
151163
else
152-
print_good("File successfully imported. Result:\n#{result}")
164+
print_good("File successfully imported. Result:\n#{result[:output]}")
153165
end
154166
end
155167

@@ -186,7 +198,10 @@ def cmd_powershell_execute(*args)
186198
}
187199

188200
result = client.powershell.execute_string(opts)
189-
print_good("Command execution completed:\n#{result}")
201+
if result[:warning].present?
202+
print_warning(result[:warning])
203+
end
204+
print_good("Command execution completed:\n#{result[:output]}")
190205
end
191206

192207
end

0 commit comments

Comments
 (0)