@@ -9,16 +9,18 @@ def GetTokenSid(hToken):
99 dwSize = DWORD (0 )
1010 pStringSid = LPSTR ()
1111 TokenUser = 1
12+
1213 if GetTokenInformation (hToken , TokenUser , byref (TOKEN_USER ()), 0 , byref (dwSize )) == 0 :
1314 address = LocalAlloc (0x0040 , dwSize )
1415 if address :
1516 GetTokenInformation (hToken , TokenUser , address , dwSize , byref (dwSize ))
1617 pToken_User = cast (address , POINTER (TOKEN_USER ))
17- ConvertSidToStringSidA (pToken_User .contents .User .Sid , byref (pStringSid ))
18- if pStringSid :
19- sid = pStringSid .value
20- LocalFree (address )
21- return sid
18+ if pToken_User .contents .User .Sid :
19+ ConvertSidToStringSidA (pToken_User .contents .User .Sid , byref (pStringSid ))
20+ if pStringSid :
21+ sid = pStringSid .value
22+ LocalFree (address )
23+ return sid
2224 return False
2325
2426def EnablePrivilege (privilegeStr , hToken = None ):
@@ -28,7 +30,7 @@ def EnablePrivilege(privilegeStr, hToken=None):
2830 if not hToken :
2931 return False
3032
31- hProcess = OpenProcess (PROCESS_QUERY_INFORMATION , False , GetCurrentProcessId ())
33+ hProcess = OpenProcess (PROCESS_QUERY_INFORMATION , False , os . getpid ())
3234 if not hProcess :
3335 return False
3436
@@ -43,7 +45,7 @@ def EnablePrivilege(privilegeStr, hToken=None):
4345 e = GetLastError ()
4446 if e != 0 :
4547 return False
46-
48+
4749 SE_PRIVILEGE_ENABLED = 0x00000002
4850 laa = LUID_AND_ATTRIBUTES (privilege_id , SE_PRIVILEGE_ENABLED )
4951 tp = TOKEN_PRIVILEGES (1 , laa )
@@ -75,11 +77,11 @@ def ListSids():
7577 hToken = HANDLE (INVALID_HANDLE_VALUE )
7678 if not hToken :
7779 continue
78-
80+
7981 OpenProcessToken (hProcess , tokenprivs , byref (hToken ))
8082 if not hToken :
8183 continue
82-
84+
8385 token_sid = GetTokenSid (hToken )
8486 if not token_sid :
8587 continue
@@ -160,7 +162,6 @@ def impersonate_sid(sid, close=True):
160162global_ref = None
161163def impersonate_sid_long_handle (* args , ** kwargs ):
162164 global global_ref
163-
164165 hTokendupe = impersonate_sid (* args , ** kwargs )
165166 if not hTokendupe :
166167 return False
0 commit comments