add Handle.infos + Process.open_token

This commit is contained in:
Clement Rouault
2017-04-04 10:28:04 +02:00
parent 5431b57560
commit 4183646b28
5 changed files with 58 additions and 16 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
from windows.generated_def import X509_ASN_ENCODING, PKCS_7_ASN_ENCODING
DEFAULT_ENCODING = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
# Keep other import here so sub-crypto file can import windows.crypto.DEFAULT_ENCODING
# Keep other imports here so sub-crypto file can import windows.crypto.DEFAULT_ENCODING
from windows.crypto.certificate import *
from windows.crypto.encrypt_decrypt import *
+1 -1
View File
@@ -10,4 +10,4 @@ class ECRYPT_DATA_BLOB(CRYPT_DATA_BLOB):
@property
def data(self):
return bytearray(self.pbData[:self.cbData])
return bytearray(self.pbData[:self.cbData])
+14 -1
View File
@@ -34,11 +34,15 @@ class Handle(SYSTEM_HANDLE):
:type: :class:`str`"""
return self._get_object_type()
@property
def infos(self):
"""TODO: DOC"""
return self._get_object_basic_infos()
def _get_object_name(self):
lh = self.local_handle
size_needed = DWORD()
yyy = ctypes.c_buffer(0x1000)
size_needed = DWORD()
winproxy.NtQueryObject(lh, ObjectNameInformation, ctypes.byref(yyy), ctypes.sizeof(yyy), ctypes.byref(size_needed))
return WinUnicodeString.from_buffer_copy(yyy[:size_needed.value]).str
@@ -58,6 +62,15 @@ class Handle(SYSTEM_HANDLE):
xxx = EPUBLIC_OBJECT_TYPE_INFORMATION.from_buffer_copy(buffer)
return xxx.TypeName.str
def _get_object_basic_infos(self):
pass
lh = self.local_handle
size_needed = DWORD()
basic_infos = PUBLIC_OBJECT_BASIC_INFORMATION()
winproxy.NtQueryObject(lh, ObjectBasicInformation, ctypes.byref(basic_infos), ctypes.sizeof(basic_infos), ctypes.byref(size_needed))
return basic_infos
#PUBLIC_OBJECT_BASIC_INFORMATION
@windows.utils.fixedpropety
def local_handle(self):
"""A local copy of the handle, acquired with ``DuplicateHandle``
+41 -12
View File
@@ -59,7 +59,8 @@ class AutoHandle(object):
def __del__(self):
if hasattr(self, "_handle") and self._handle:
dbgprint("Closing Handle {0} for {1}".format(hex(self._handle), self), "HANDLE")
# Prevent some bug where dbgprint might be None when __del__ is called in a closing process
dbgprint("Closing Handle {0} for {1}".format(hex(self._handle), self), "HANDLE") if dbgprint is not None else None
self._close_function(self._handle)
@@ -262,6 +263,17 @@ class WinThread(THREADENTRY32, AutoHandle):
_get_thread_id = _get_thread_id_manual
# def token(self):
# """The token of the process
#
# :type: :class:`Token`
# """
# token_handle = HANDLE()
# winproxy.OpenThreadToken(self.handle, TOKEN_QUERY, False, byref(token_handle))
# return Token(token_handle.value)
class DeadThread(AutoHandle):
"""An already dead thread (returned only by API returning a new thread if thread die before being returned)"""
def __init__(self, handle, tid=None):
@@ -620,16 +632,25 @@ class Process(AutoHandle):
return TimeInfo(creation, exit, kernel, user)
@utils.fixedpropety
def token(self):
"""The token of the process
:type: :class:`Token`
"""
def open_token(self, flags=TOKEN_QUERY):
token_handle = HANDLE()
winproxy.OpenProcessToken(self.handle, TOKEN_QUERY, byref(token_handle))
winproxy.OpenProcessToken(self.handle, flags, byref(token_handle))
return Token(token_handle.value)
token = property(open_token)
# @utils.fixedpropety
# def token(self):
# """The token of the process
#
# :type: :class:`Token`
# """
# token_handle = HANDLE()
# winproxy.OpenProcessToken(self.handle, TOKEN_QUERY, byref(token_handle))
# return Token(token_handle.value)
class CurrentThread(AutoHandle):
"""The current thread"""
@property #It's not a fixedpropety because executing thread might change
@@ -662,6 +683,10 @@ class CurrentThread(AutoHandle):
"""Raise :class:`ValueError` to prevent deadlock :D"""
raise ValueError("wait() on current thread")
#def token(self):
# GetCurrentThreadToken()
class CurrentProcess(Process):
"""The current process"""
@@ -1042,17 +1067,16 @@ class Token(AutoHandle):
@property
def integrity(self):
"""Return the integrity level of a process
"""Return the integrity level of a token
:type: :class:`int`
"""
buffer_size = self.get_required_information_size(TokenIntegrityLevel)
buffer = ctypes.c_buffer(buffer_size)
self.get_informations(TokenIntegrityLevel, buffer)
sid = ctypes.cast(buffer, POINTER(TOKEN_MANDATORY_LABEL))[0].Label.Sid
count = winproxy.GetSidSubAuthorityCount(sid)
integrity = winproxy.GetSidSubAuthority(sid, ord(count[0]) - 1)[0]
integrity = winproxy.GetSidSubAuthority(sid, count[0] - 1)[0]
return know_integrity_level_mapper.get(integrity, integrity)
@property
@@ -1099,10 +1123,15 @@ class Token(AutoHandle):
cbsize = DWORD()
try:
winproxy.GetTokenInformation(self.handle, info_type, None, 0, ctypes.byref(cbsize))
except WindowsError:
pass
except WindowsError as e:
if not e.winerror == ERROR_INSUFFICIENT_BUFFER:
raise
return cbsize.value
#TODO: TEST + DOC
def set_informations(self, info_type, infos):
return winproxy.SetTokenInformation(self.handle, info_type, ctypes.byref(infos), ctypes.sizeof(infos))
def transform_ctypes_fields(struct, replacement):
return [(name, replacement.get(name, type)) for name, type in struct._fields_]
+1 -1
View File
@@ -43,7 +43,7 @@ class PyHKey(object):
try:
self._phkey = _winreg.OpenKeyEx(self.surkey.phkey, self.name, 0, self.sam)
except WindowsError as e:
raise WindowsError("Could not open registry key <{0}>".format(self.fullname))
raise WindowsError("Could not open registry key <{0}> ({1})".format(self.fullname, e))
return self._phkey