49
49
has_winreg = False
50
50
51
51
if sys .version_info [0 ] < 3 :
52
+ is_str = lambda obj : issubclass (obj .__class__ , str )
52
53
is_bytes = lambda obj : issubclass (obj .__class__ , str )
53
54
bytes = lambda * args : str (* args [:1 ])
54
55
NULL_BYTE = '\x00 '
55
56
else :
57
+ is_str = lambda obj : issubclass (obj .__class__ , __builtins__ ['str' ])
56
58
is_bytes = lambda obj : issubclass (obj .__class__ , bytes )
57
59
str = lambda x : __builtins__ ['str' ](x , 'UTF-8' )
58
60
NULL_BYTE = bytes ('\x00 ' , 'UTF-8' )
@@ -546,31 +548,6 @@ def netlink_request(req_type):
546
548
sock .close ()
547
549
return responses
548
550
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
-
574
551
def resolve_host (hostname , family ):
575
552
address_info = socket .getaddrinfo (hostname , 0 , family , socket .SOCK_DGRAM , socket .IPPROTO_UDP )[0 ]
576
553
family = address_info [0 ]
@@ -837,7 +814,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
837
814
use = ctypes .c_ulong ()
838
815
use .value = 0
839
816
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 ) )
841
818
k32 .CloseHandle (tkn_h )
842
819
parch = windll_GetNativeSystemInfo ()
843
820
is_wow64 = ctypes .c_ubyte ()
@@ -846,7 +823,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
846
823
if k32 .IsWow64Process (proc_h , ctypes .byref (is_wow64 )):
847
824
if is_wow64 .value :
848
825
parch = PROCESS_ARCH_X86
849
- pgroup = ''
826
+ pgroup = bytes ()
850
827
pgroup += tlv_pack (TLV_TYPE_PID , pe32 .th32ProcessID )
851
828
pgroup += tlv_pack (TLV_TYPE_PARENT_PID , pe32 .th32ParentProcessID )
852
829
pgroup += tlv_pack (TLV_TYPE_USER_NAME , complete_username )
@@ -902,9 +879,10 @@ def stdapi_fs_delete_file(request, response):
902
879
def stdapi_fs_file_expand_path (request , response ):
903
880
path_tlv = packet_get_tlv (request , TLV_TYPE_FILE_PATH )['value' ]
904
881
if has_windll :
882
+ path_tlv = ctypes .create_string_buffer (bytes (path_tlv , 'UTF-8' ))
905
883
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 ))
908
886
elif path_tlv == '%COMSPEC%' :
909
887
result = '/bin/sh'
910
888
elif path_tlv in ['%TEMP%' , '%TMP%' ]:
@@ -1011,7 +989,7 @@ def stdapi_fs_stat(request, response):
1011
989
1012
990
@meterpreter .register_function
1013
991
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' ) :
1015
993
interfaces = stdapi_net_config_get_interfaces_via_netlink ()
1016
994
elif has_osxsc :
1017
995
interfaces = stdapi_net_config_get_interfaces_via_osxsc ()
@@ -1184,7 +1162,10 @@ def stdapi_net_config_get_interfaces_via_windll():
1184
1162
iface_info ['index' ] = AdapterAddresses .u .s .IfIndex
1185
1163
if AdapterAddresses .PhysicalAddressLength :
1186
1164
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
1188
1169
iface_info ['mtu' ] = AdapterAddresses .Mtu
1189
1170
pUniAddr = AdapterAddresses .FirstUnicastAddress
1190
1171
while pUniAddr :
0 commit comments