python - GetTokenInformation with ctypes -
i'm trying use gettokeninformation function ctypes. problem print none.
import winappdbg ctypes import * lpvoid = c_void_p pvoid = lpvoid psid = pvoid dword = c_uint32 class sid_and_attributes(structure): _fields_ = [ ("sid", psid), ("attributes", dword), ] class token_user(structure): _fields_ = [ ("user", sid_and_attributes),] tokenprivs = (winappdbg.win32.token_query | winappdbg.win32.token_read | winappdbg.win32.token_impersonate | winappdbg.win32.token_query_source | winappdbg.win32.token_duplicate | winappdbg.win32.token_assign_primary) hprocess = winappdbg.win32.openprocess(winappdbg.win32.process_query_information, false, winappdbg.win32.getcurrentprocessid()) htoken = winappdbg.win32.openprocesstoken(hprocess, desiredaccess = tokenprivs) tokeninformation = token_user() dwlength = dword(0) windll.advapi32.gettokeninformation(htoken, winappdbg.win32.tokenuser, byref(tokeninformation), sizeof(token_user), byref(dwlength)) print tokeninformation.user.sid p.s. i'm aware win32security.gettokeninformation exists. want use ctypes because of real process handlers.
edit:
working code:
import winappdbg ctypes import * lpvoid = c_void_p pvoid = lpvoid psid = pvoid dword = c_uint32 class sid_and_attributes(structure): _fields_ = [ ("sid", psid), ("attributes", dword), ] class token_user(structure): _fields_ = [ ("user", sid_and_attributes),] tokenprivs = (winappdbg.win32.token_query | winappdbg.win32.token_read | winappdbg.win32.token_impersonate | winappdbg.win32.token_query_source | winappdbg.win32.token_duplicate | winappdbg.win32.token_assign_primary) hprocess = winappdbg.win32.openprocess(winappdbg.win32.process_query_information, false, winappdbg.win32.getcurrentprocessid()) htoken = winappdbg.win32.openprocesstoken(hprocess, desiredaccess = tokenprivs) dwsize = dword(0) pstringsid = winappdbg.win32.lpstr() windll.advapi32.gettokeninformation(htoken, winappdbg.win32.tokenuser, none, 0, byref(dwsize)) address = windll.kernel32.localalloc(0x0040, dwsize) print "address: " + str(address) windll.advapi32.gettokeninformation(htoken, winappdbg.win32.tokenuser, address, dwsize, byref(dwsize)) print formaterror(getlasterror()) ptoken_user = cast(address, pointer(token_user)) windll.advapi32.convertsidtostringsida(ptoken_user.contents.user.sid, byref(pstringsid)) print "sid: " + pstringsid.value
although query token information class tokenuser stores token_user structure in target buffer, not contain required information itself. can see in structure's documentation, contains sid_and_attributes structure, in turn contains pointer sid , integer flags.
if added more error checking, see call gettokeninformation() not succeed, reported error code error_insufficient_buffer (122) , dwlength set 36 (definitely more sizeof(token_user), 8).
apparently function wants enough space in target buffer store sid itself, point data documented output structure token_user.
i don't know ctypes much, need create output buffer real buffer/array instead of structure, , perform casting on data. can either take easy way , create buffer large enough on first try, or call function twice, first required buffer length, second fill it.
Comments
Post a Comment