From 8e257f9678ebe00d882faf35565e55f3d8c8048f Mon Sep 17 00:00:00 2001 From: hakril Date: Thu, 16 Jul 2020 23:37:41 +0200 Subject: [PATCH] Added some code resulting from exploration in COM/remote ctypes --- windows/com.py | 41 ++++++++++++++++++++++++++++++++++++++++- windows/remotectypes.py | 15 +++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/windows/com.py b/windows/com.py index 4d72c67..60f2e38 100644 --- a/windows/com.py +++ b/windows/com.py @@ -29,13 +29,46 @@ def init(): except WindowsError as e: t = e.winerror if t: - return t + return t & 0xffffffff return initsecurity() def initsecurity(): # Should take some parameters.. return winproxy.CoInitializeSecurity(0, -1, None, 0, 0, RPC_C_IMP_LEVEL_IMPERSONATE, 0,0,0) +class Dispatch(interfaces.IDispatch): + def TypeInfoCount(self): + count = gdef.UINT() + self.GetTypeInfoCount(count) + return count + + def type_info(self, idx): + type_info = TypeInfo() + self.GetTypeInfo(idx, 0, type_info) + return type_info + +class TypeInfo(interfaces.ITypeInfo): + def func(self, idx): + res = gdef.LPFUNCDESC() + self.GetFuncDesc(idx, res) + return res + + def attr(self): + res = gdef.LPTYPEATTR() + self.GetTypeAttr(res) + return res + + def names(self, memid): + size = gdef.UINT() + x = (gdef.BSTR * 10)(*tuple(gdef.BSTR() for i in range(10))) + self.GetNames(memid, x, 10, size) + return x[:size.value] + + def docu(self, id): + res = gdef.BSTR() + self.GetDocumentation(id, res, None, None, None) + return res + def create_instance(clsiid, targetinterface, custom_iid=None, context=CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER): """A simple wrapper around ``CoCreateInstance ``""" if custom_iid is None: @@ -196,6 +229,12 @@ class Variant(VARIANT): attr = self.QUICK_CHECK_TYPE[rawvt] if attr is None: return None + if attr == "punkVal": + # Quick hack for COM interface type + # Do something clean with CHECK_TYPE ? + x = gdef.IUnknown(self.punkVal) + x.AddRef() + return x return getattr(self, attr) def guess_type_and_set_value(self, value): diff --git a/windows/remotectypes.py b/windows/remotectypes.py index 34b4e55..6794ee4 100644 --- a/windows/remotectypes.py +++ b/windows/remotectypes.py @@ -301,6 +301,21 @@ class RemoteStructurePointer32(Remote_c_void_p32): @property def contents(self): + # What if we have a non-struct pointer + # Like a Ptr(DWORD) we would like to get the underlying value + realtype = self.real_pointer_type._sub_ctypes_ + # if _SimpleCData in realtype.__bases__: + # A ctypes original value. + # Returnthe real pointed value + # Kind of a tricks for now compared to the real ctypes behavior + # import pdb;pdb.set_trace() + # if not self.value: + # return None + # targetptr = self.target.read_ptr(self.value) + # if not targetptr: + # return None + # ss = self.target.read_memory(targetptr, ctypes.sizeof(realtype)) + # return realtype.from_buffer(bytearray(ss)).value remote_pointed_type = transform_type_to_remote32bits(self.real_pointer_type._sub_ctypes_) return remote_pointed_type(self.raw_value, self.target)