Skip to content

Commit 04e94b0

Browse files
committed
Fix meterpreter and file tests for Python v3.4 on Win
1 parent 15dc335 commit 04e94b0

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
has_winreg = False
5050

5151
if sys.version_info[0] < 3:
52+
is_str = lambda obj: issubclass(obj.__class__, str)
5253
is_bytes = lambda obj: issubclass(obj.__class__, str)
5354
bytes = lambda *args: str(*args[:1])
5455
NULL_BYTE = '\x00'
5556
else:
57+
is_str = lambda obj: issubclass(obj.__class__, __builtins__['str'])
5658
is_bytes = lambda obj: issubclass(obj.__class__, bytes)
5759
str = lambda x: __builtins__['str'](x, 'UTF-8')
5860
NULL_BYTE = bytes('\x00', 'UTF-8')
@@ -546,31 +548,6 @@ def netlink_request(req_type):
546548
sock.close()
547549
return responses
548550

549-
def _netlink_request(req_type):
550-
# See RFC 3549
551-
NLM_F_REQUEST = 0x0001
552-
NLM_F_ROOT = 0x0100
553-
NLMSG_ERROR = 0x0002
554-
NLMSG_DONE = 0x0003
555-
556-
sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)
557-
sock.bind((os.getpid(), 0))
558-
seq = int(time.time())
559-
nlmsg = struct.pack('IHHIIB15x', 32, req_type, (NLM_F_REQUEST | NLM_F_ROOT), seq, 0, socket.AF_UNSPEC)
560-
sfd = os.fdopen(sock.fileno(), 'w+b')
561-
sfd.write(nlmsg)
562-
responses = []
563-
response = cstruct_unpack(NLMSGHDR, sfd.read(ctypes.sizeof(NLMSGHDR)))
564-
while response.type != NLMSG_DONE:
565-
if response.type == NLMSG_ERROR:
566-
break
567-
response_data = sfd.read(response.len - 16)
568-
responses.append(response_data)
569-
response = cstruct_unpack(NLMSGHDR, sfd.read(ctypes.sizeof(NLMSGHDR)))
570-
sfd.close()
571-
sock.close()
572-
return responses
573-
574551
def resolve_host(hostname, family):
575552
address_info = socket.getaddrinfo(hostname, 0, family, socket.SOCK_DGRAM, socket.IPPROTO_UDP)[0]
576553
family = address_info[0]
@@ -837,7 +814,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
837814
use = ctypes.c_ulong()
838815
use.value = 0
839816
ctypes.windll.advapi32.LookupAccountSidA(None, user_tkn.Sid, username, ctypes.byref(u_len), domain, ctypes.byref(d_len), ctypes.byref(use))
840-
complete_username = ctypes.string_at(domain) + '\\' + ctypes.string_at(username)
817+
complete_username = str(ctypes.string_at(domain)) + '\\' + str(ctypes.string_at(username))
841818
k32.CloseHandle(tkn_h)
842819
parch = windll_GetNativeSystemInfo()
843820
is_wow64 = ctypes.c_ubyte()
@@ -846,7 +823,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
846823
if k32.IsWow64Process(proc_h, ctypes.byref(is_wow64)):
847824
if is_wow64.value:
848825
parch = PROCESS_ARCH_X86
849-
pgroup = ''
826+
pgroup = bytes()
850827
pgroup += tlv_pack(TLV_TYPE_PID, pe32.th32ProcessID)
851828
pgroup += tlv_pack(TLV_TYPE_PARENT_PID, pe32.th32ParentProcessID)
852829
pgroup += tlv_pack(TLV_TYPE_USER_NAME, complete_username)
@@ -902,9 +879,10 @@ def stdapi_fs_delete_file(request, response):
902879
def stdapi_fs_file_expand_path(request, response):
903880
path_tlv = packet_get_tlv(request, TLV_TYPE_FILE_PATH)['value']
904881
if has_windll:
882+
path_tlv = ctypes.create_string_buffer(bytes(path_tlv, 'UTF-8'))
905883
path_out = (ctypes.c_char * 4096)()
906-
path_out_len = ctypes.windll.kernel32.ExpandEnvironmentStringsA(path_tlv, ctypes.byref(path_out), ctypes.sizeof(path_out))
907-
result = ''.join(path_out)[:path_out_len]
884+
path_out_len = ctypes.windll.kernel32.ExpandEnvironmentStringsA(ctypes.byref(path_tlv), ctypes.byref(path_out), ctypes.sizeof(path_out))
885+
result = str(ctypes.string_at(path_out))
908886
elif path_tlv == '%COMSPEC%':
909887
result = '/bin/sh'
910888
elif path_tlv in ['%TEMP%', '%TMP%']:
@@ -1011,7 +989,7 @@ def stdapi_fs_stat(request, response):
1011989

1012990
@meterpreter.register_function
1013991
def stdapi_net_config_get_interfaces(request, response):
1014-
if hasattr(socket, 'AF_NETLINK'):
992+
if hasattr(socket, 'AF_NETLINK') and hasattr(socket, 'NETLINK_ROUTE'):
1015993
interfaces = stdapi_net_config_get_interfaces_via_netlink()
1016994
elif has_osxsc:
1017995
interfaces = stdapi_net_config_get_interfaces_via_osxsc()
@@ -1184,7 +1162,10 @@ def stdapi_net_config_get_interfaces_via_windll():
11841162
iface_info['index'] = AdapterAddresses.u.s.IfIndex
11851163
if AdapterAddresses.PhysicalAddressLength:
11861164
iface_info['hw_addr'] = ctypes.string_at(ctypes.byref(AdapterAddresses.PhysicalAddress), AdapterAddresses.PhysicalAddressLength)
1187-
iface_info['name'] = str(ctypes.wstring_at(AdapterAddresses.Description))
1165+
iface_desc = ctypes.wstring_at(AdapterAddresses.Description)
1166+
if not is_str(iface_desc):
1167+
iface_desc = str(iface_desc)
1168+
iface_info['name'] = iface_desc
11881169
iface_info['mtu'] = AdapterAddresses.Mtu
11891170
pUniAddr = AdapterAddresses.FirstUnicastAddress
11901171
while pUniAddr:

data/meterpreter/meterpreter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,17 @@ def create_response(self, request):
502502
handler = self.extension_functions[handler_name]
503503
try:
504504
if DEBUGGING:
505-
print("[*] running method {0}".format(handler_name))
505+
print('[*] running method ' + handler_name)
506506
result, resp = handler(request, resp)
507507
except Exception:
508508
if DEBUGGING:
509-
print("[-] method {0} resulted in an error".format(handler_name))
509+
print('[-] method ' + handler_name + ' resulted in an error')
510510
exc_type, exc_value, exc_traceback = sys.exc_info()
511511
traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)
512512
result = ERROR_FAILURE
513513
else:
514514
if DEBUGGING:
515-
print("[-] method {0} was requested but does not exist".format(handler_name))
515+
print('[-] method ' + handler_name + ' was requested but does not exist')
516516
result = ERROR_FAILURE
517517
resp += tlv_pack(TLV_TYPE_RESULT, result)
518518
resp = struct.pack('>I', len(resp) + 4) + resp

0 commit comments

Comments
 (0)