@@ -215,6 +215,9 @@ class SYSTEM_INFO(ctypes.Structure):
215
215
("wProcessorLevel" , ctypes .c_uint16 ),
216
216
("wProcessorRevision" , ctypes .c_uint16 )]
217
217
218
+ class TOKEN_USER (ctypes .Structure ):
219
+ _fields_ = [("User" , SID_AND_ATTRIBUTES )]
220
+
218
221
#
219
222
# Linux Structures
220
223
#
@@ -364,6 +367,7 @@ class RTATTR(ctypes.Structure):
364
367
TLV_TYPE_OS_NAME = TLV_META_TYPE_STRING | 1041
365
368
TLV_TYPE_USER_NAME = TLV_META_TYPE_STRING | 1042
366
369
TLV_TYPE_ARCHITECTURE = TLV_META_TYPE_STRING | 1043
370
+ TLV_TYPE_SID = TLV_META_TYPE_STRING | 1045
367
371
368
372
##
369
373
# Environment
@@ -525,6 +529,36 @@ def get_stat_buffer(path):
525
529
st_buf += struct .pack ('<II' , blksize , blocks )
526
530
return st_buf
527
531
532
+ def get_token_user (handle ):
533
+ TOKEN_QUERY = 0x0008
534
+ TokenUser = 1
535
+ advapi32 = ctypes .windll .advapi32
536
+ advapi32 .OpenProcessToken .argtypes = [ctypes .c_void_p , ctypes .c_uint32 , ctypes .POINTER (ctypes .c_void_p )]
537
+
538
+ token_handle = ctypes .c_void_p ()
539
+ if not advapi32 .OpenProcessToken (handle , TOKEN_QUERY , ctypes .byref (token_handle )):
540
+ return None
541
+ token_user_buffer = (ctypes .c_byte * 4096 )()
542
+ dw_returned = ctypes .c_uint32 ()
543
+ result = advapi32 .GetTokenInformation (token_handle , TokenUser , ctypes .byref (token_user_buffer ), ctypes .sizeof (token_user_buffer ), ctypes .byref (dw_returned ))
544
+ ctypes .windll .kernel32 .CloseHandle (token_handle )
545
+ if not result :
546
+ return None
547
+ return cstruct_unpack (TOKEN_USER , token_user_buffer )
548
+
549
+ def get_username_from_token (token_user ):
550
+ user = (ctypes .c_char * 512 )()
551
+ domain = (ctypes .c_char * 512 )()
552
+ user_len = ctypes .c_uint32 ()
553
+ user_len .value = ctypes .sizeof (user )
554
+ domain_len = ctypes .c_uint32 ()
555
+ domain_len .value = ctypes .sizeof (domain )
556
+ use = ctypes .c_ulong ()
557
+ use .value = 0
558
+ if not ctypes .windll .advapi32 .LookupAccountSidA (None , token_user .User .Sid , user , ctypes .byref (user_len ), domain , ctypes .byref (domain_len ), ctypes .byref (use )):
559
+ return None
560
+ return str (ctypes .string_at (domain )) + '\\ ' + str (ctypes .string_at (user ))
561
+
528
562
def netlink_request (req_type ):
529
563
import select
530
564
# See RFC 3549
@@ -632,11 +666,6 @@ def channel_open_stdapi_net_tcp_server(request, response):
632
666
response += tlv_pack (TLV_TYPE_CHANNEL_ID , channel_id )
633
667
return ERROR_SUCCESS , response
634
668
635
- @meterpreter .register_function
636
- def stdapi_sys_config_getuid (request , response ):
637
- response += tlv_pack (TLV_TYPE_USER_NAME , getpass .getuser ())
638
- return ERROR_SUCCESS , response
639
-
640
669
@meterpreter .register_function
641
670
def stdapi_sys_config_getenv (request , response ):
642
671
for env_var in packet_enum_tlvs (request , TLV_TYPE_ENV_VARIABLE ):
@@ -649,6 +678,32 @@ def stdapi_sys_config_getenv(request, response):
649
678
response += tlv_pack (TLV_TYPE_ENV_GROUP , pgroup )
650
679
return ERROR_SUCCESS , response
651
680
681
+ @meterpreter .register_function_windll
682
+ def stdapi_sys_config_getsid (request , response ):
683
+ token = get_token_user (ctypes .windll .kernel32 .GetCurrentProcess ())
684
+ if not token :
685
+ return ERROR_FAILURE , response
686
+ sid_str = ctypes .c_char_p ()
687
+ if not ctypes .windll .advapi32 .ConvertSidToStringSidA (token .User .Sid , ctypes .byref (sid_str )):
688
+ return ERROR_FAILURE , response
689
+ sid_str = str (ctypes .string_at (sid_str ))
690
+ response += tlv_pack (TLV_TYPE_SID , sid_str )
691
+ return ERROR_SUCCESS , response
692
+
693
+ @meterpreter .register_function
694
+ def stdapi_sys_config_getuid (request , response ):
695
+ if has_windll :
696
+ token = get_token_user (ctypes .windll .kernel32 .GetCurrentProcess ())
697
+ if not token :
698
+ return ERROR_FAILURE , response
699
+ username = get_username_from_token (token )
700
+ if not username :
701
+ return ERROR_FAILURE , response
702
+ else :
703
+ username = getpass .getuser ()
704
+ response += tlv_pack (TLV_TYPE_USER_NAME , username )
705
+ return ERROR_SUCCESS , response
706
+
652
707
@meterpreter .register_function
653
708
def stdapi_sys_config_sysinfo (request , response ):
654
709
uname_info = platform .uname ()
@@ -821,26 +876,10 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
821
876
exe_path = ctypes .string_at (exe_path )
822
877
else :
823
878
exe_path = ''
824
- complete_username = ''
825
- tkn_h = ctypes .c_long ()
826
- tkn_len = ctypes .c_uint32 ()
827
- if ctypes .windll .advapi32 .OpenProcessToken (proc_h , TOKEN_QUERY , ctypes .byref (tkn_h )):
828
- ctypes .windll .advapi32 .GetTokenInformation (tkn_h , TokenUser , None , 0 , ctypes .byref (tkn_len ))
829
- buf = (ctypes .c_ubyte * tkn_len .value )()
830
- if ctypes .windll .advapi32 .GetTokenInformation (tkn_h , TokenUser , ctypes .byref (buf ), ctypes .sizeof (buf ), ctypes .byref (tkn_len )):
831
- user_tkn = SID_AND_ATTRIBUTES ()
832
- ctypes .memmove (ctypes .byref (user_tkn ), buf , ctypes .sizeof (user_tkn ))
833
- username = (ctypes .c_char * 512 )()
834
- domain = (ctypes .c_char * 512 )()
835
- u_len = ctypes .c_uint32 ()
836
- u_len .value = ctypes .sizeof (username )
837
- d_len = ctypes .c_uint32 ()
838
- d_len .value = ctypes .sizeof (domain )
839
- use = ctypes .c_ulong ()
840
- use .value = 0
841
- ctypes .windll .advapi32 .LookupAccountSidA (None , user_tkn .Sid , username , ctypes .byref (u_len ), domain , ctypes .byref (d_len ), ctypes .byref (use ))
842
- complete_username = str (ctypes .string_at (domain )) + '\\ ' + str (ctypes .string_at (username ))
843
- k32 .CloseHandle (tkn_h )
879
+ process_username = ''
880
+ process_token_user = get_token_user (proc_h )
881
+ if process_token_user :
882
+ process_username = get_username_from_token (process_token_user ) or ''
844
883
parch = windll_GetNativeSystemInfo ()
845
884
is_wow64 = ctypes .c_ubyte ()
846
885
is_wow64 .value = 0
@@ -851,7 +890,7 @@ def stdapi_sys_process_get_processes_via_windll(request, response):
851
890
pgroup = bytes ()
852
891
pgroup += tlv_pack (TLV_TYPE_PID , pe32 .th32ProcessID )
853
892
pgroup += tlv_pack (TLV_TYPE_PARENT_PID , pe32 .th32ParentProcessID )
854
- pgroup += tlv_pack (TLV_TYPE_USER_NAME , complete_username )
893
+ pgroup += tlv_pack (TLV_TYPE_USER_NAME , process_username )
855
894
pgroup += tlv_pack (TLV_TYPE_PROCESS_NAME , pe32 .szExeFile )
856
895
pgroup += tlv_pack (TLV_TYPE_PROCESS_PATH , exe_path )
857
896
pgroup += tlv_pack (TLV_TYPE_PROCESS_ARCH , parch )
0 commit comments