From 59621034f550ff7e7ff934c4cc778f48629c3a9b Mon Sep 17 00:00:00 2001 From: Clement Rouault Date: Fri, 11 Dec 2015 09:54:43 +0100 Subject: [PATCH] Add some play-code for wintrust/registry/wmi --- registry.py | 82 +++++++++++++++++++++ wintrust.py | 69 ++++++++++++++++++ wmi.py | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 358 insertions(+) create mode 100644 registry.py create mode 100644 wintrust.py create mode 100644 wmi.py diff --git a/registry.py b/registry.py new file mode 100644 index 0000000..f5c3a34 --- /dev/null +++ b/registry.py @@ -0,0 +1,82 @@ +import _winreg +import windows +import itertools +import collections + + +class ExpectWindowsError(object): + def __init__(self, errornumber): + self.errornumber = errornumber + + def __enter__(self): + pass + + def __exit__(self, etype, e, tb): + return (etype == WindowsError and e.winerror == self.errornumber) + + +KeyValue = collections.namedtuple("KeyValue", ["name", "value", "type"]) + +class PyHKey(object): + def __init__(self, surkey, name, sam=_winreg.KEY_READ): + self.surkey = surkey + self._phkey = None + self.name = name + self.fullname = self.surkey.fullname + "\\" + self.name if self.name else self.surkey.name + self.sam = sam + + def __repr__(self): + return ''.format(self.fullname) + + @property + def phkey(self): + if self._phkey is not None: + return self._phkey + print("OPEN <{0}, {1}>".format(self.surkey.phkey, self.name)) + self._phkey = _winreg.OpenKeyEx(self.surkey.phkey, self.name, 0, self.sam) + return self._phkey + + @property + def subkeys(self): + res = [] + with ExpectWindowsError(259): + for i in itertools.count(): + res.append(_winreg.EnumKey(self.phkey, i)) + return [PyHKey(self, n) for n in res] + + @property + def values(self): + res = [] + with ExpectWindowsError(259): + for i in itertools.count(): + res.append(_winreg.EnumValue(self.phkey, i)) + return [KeyValue(*r) for r in res] + + def open_subkey(self, name): + return PyHKey(self, name, self.sam) + + def reopen(self, new_sam): + return PyHKey(self.surkey, self.name, new_sam) + + __getitem__ = open_subkey + +class DummyPHKEY(object): + def __init__(self, phkey, name): + self.phkey = phkey + self.name = name + + +HKEY_LOCAL_MACHINE = PyHKey(DummyPHKEY(_winreg.HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE"), "", _winreg.KEY_READ) + +HKEY_CLASSES_ROOT = PyHKey(DummyPHKEY(_winreg.HKEY_CLASSES_ROOT, "HKEY_CLASSES_ROOT"), "", _winreg.KEY_READ ) + +HKEY_CURRENT_USER = PyHKey(DummyPHKEY(_winreg.HKEY_CURRENT_USER, "HKEY_CURRENT_USER"), "", _winreg.KEY_READ) + +HKEY_DYN_DATA = PyHKey(DummyPHKEY(_winreg.HKEY_DYN_DATA, "HKEY_DYN_DATA"), "", _winreg.KEY_READ) + +HKEY_PERFORMANCE_DATA = PyHKey(DummyPHKEY(_winreg.HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA"), "", _winreg.KEY_READ) + +HKEY_USERS = PyHKey(DummyPHKEY(_winreg.HKEY_USERS, "HKEY_USERS"), "", _winreg.KEY_READ ) + + +HKEY_CURRENT_USER[r"Software\Microsoft\Windows\CurrentVersion\Run"].values diff --git a/wintrust.py b/wintrust.py new file mode 100644 index 0000000..3c5b529 --- /dev/null +++ b/wintrust.py @@ -0,0 +1,69 @@ +import ctypes +import struct +import windows +from windows.generated_def.winstructs import * + +IID_PACK = "".format(repr(filename))) + file_data = WINTRUST_FILE_INFO() + file_data.cbStruct = ctypes.sizeof(WINTRUST_FILE_INFO) + file_data.pcwszFilePath = filename + file_data.hFile = None + file_data.pgKnownSubject = None + + WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2 + + win_trust_data = WINTRUST_DATA() + + win_trust_data.cbStruct = ctypes.sizeof(WINTRUST_DATA) + win_trust_data.pPolicyCallbackData = None + win_trust_data.pSIPClientData = None + win_trust_data.dwUIChoice = WTD_UI_NONE + win_trust_data.fdwRevocationChecks = WTD_REVOKE_NONE + win_trust_data.dwUnionChoice = WTD_CHOICE_FILE + win_trust_data.dwStateAction = WTD_STATEACTION_VERIFY + win_trust_data.hWVTStateData = None + win_trust_data.pwszURLReference = None + win_trust_data.dwUIContext = 0 + win_trust_data.tmp_union.pFile = ctypes.pointer(file_data) + + WinVerifyTrust = ctypes.WinDLL("wintrust").WinVerifyTrust + + x = WinVerifyTrust(None, ctypes.byref(WVTPolicyGUID), ctypes.byref(win_trust_data)) + + win_trust_data.dwStateAction = WTD_STATEACTION_CLOSE + + WinVerifyTrust(None, ctypes.byref(WVTPolicyGUID), ctypes.byref(win_trust_data)) + + return x & 0xffffffff \ No newline at end of file diff --git a/wmi.py b/wmi.py new file mode 100644 index 0000000..166fd2d --- /dev/null +++ b/wmi.py @@ -0,0 +1,207 @@ +import windows +import ctypes +from ctypes.wintypes import * +from windows.generated_def.winstructs import * +import struct +import functools + + +# Move simple_com from LKD to windows ? + +IID_PACK = "