mirror of
https://github.com/hakril/PythonForWindows
synced 2026-06-08 14:31:45 +00:00
cosmetic: fucking linter :(
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
||||
from pythonutils import *
|
||||
from winutils import *
|
||||
from winutils import *
|
||||
|
||||
@@ -4,6 +4,7 @@ import ctypes
|
||||
|
||||
def fixedpropety(f):
|
||||
cache_name = "_" + f.__name__
|
||||
|
||||
def prop(self):
|
||||
try:
|
||||
return getattr(self, cache_name)
|
||||
@@ -12,7 +13,8 @@ def fixedpropety(f):
|
||||
return getattr(self, cache_name)
|
||||
return property(prop)
|
||||
|
||||
|
||||
def swallow_ctypes_copy(ctypes_object):
|
||||
new_copy = type(ctypes_object)()
|
||||
ctypes.memmove(ctypes.byref(new_copy), ctypes.byref(ctypes_object), ctypes.sizeof(new_copy))
|
||||
return new_copy
|
||||
return new_copy
|
||||
|
||||
+28
-16
@@ -1,7 +1,6 @@
|
||||
import ctypes
|
||||
import msvcrt
|
||||
import os
|
||||
import copy
|
||||
import sys
|
||||
import code
|
||||
|
||||
@@ -13,13 +12,15 @@ from ..generated_def.winstructs import *
|
||||
|
||||
# Function resolution !
|
||||
def get_func_addr(dll_name, func_name):
|
||||
dll = ctypes.WinDLL(dll_name)
|
||||
# Load the DLL
|
||||
ctypes.WinDLL(dll_name)
|
||||
modules = windows.current_process.peb.modules
|
||||
if not dll_name.lower().endswith(".dll"):
|
||||
dll_name += ".dll"
|
||||
mod = [x for x in modules if x.name == dll_name][0]
|
||||
return mod.pe.exports[func_name]
|
||||
|
||||
|
||||
def get_remote_func_addr(target, dll_name, func_name):
|
||||
name_modules = [m for m in target.peb.modules if m.name == dll_name]
|
||||
if not len(name_modules):
|
||||
@@ -27,27 +28,31 @@ def get_remote_func_addr(target, dll_name, func_name):
|
||||
mod = name_modules[0]
|
||||
return mod.pe.exports[func_name]
|
||||
|
||||
|
||||
def is_wow_64(hProcess):
|
||||
try:
|
||||
fnIsWow64Process = get_func_addr("kernel32.dll", "IsWow64Process")
|
||||
fnIsWow64Process = get_func_addr("kernel32.dll", "IsWow64Process")
|
||||
except winproxy.Kernel32Error:
|
||||
return False
|
||||
IsWow64Process = ctypes.WINFUNCTYPE(BOOL, HANDLE, ctypes.POINTER(BOOL))(fnIsWow64Process)
|
||||
IsWow64Process = ctypes.WINFUNCTYPE(BOOL, HANDLE, ctypes.POINTER(BOOL))(fnIsWow64Process)
|
||||
Wow64Process = BOOL()
|
||||
res = IsWow64Process(hProcess, ctypes.byref(Wow64Process))
|
||||
if res:
|
||||
return bool(Wow64Process)
|
||||
raise ctypes.WinError()
|
||||
|
||||
|
||||
def create_file_from_handle(handle, mode="r"):
|
||||
"""Return a Python :class:`file` arround a windows HANDLE"""
|
||||
fd = msvcrt.open_osfhandle(handle, os.O_TEXT)
|
||||
return os.fdopen(fd, mode, 0)
|
||||
|
||||
|
||||
def get_handle_from_file(f):
|
||||
"""Get the windows HANDLE of a python :class:`file`"""
|
||||
return msvcrt.get_osfhandle(f.fileno())
|
||||
|
||||
|
||||
def create_console():
|
||||
"""Create a new console displaying STDOUT
|
||||
Useful in injection of GUI process"""
|
||||
@@ -64,6 +69,7 @@ def create_console():
|
||||
console_stderr = create_file_from_handle(stderr_handle, "w")
|
||||
sys.stderr = console_stderr
|
||||
|
||||
|
||||
def create_process(path, show_windows=False):
|
||||
proc_info = PROCESS_INFORMATION()
|
||||
lpStartupInfo = None
|
||||
@@ -75,7 +81,8 @@ def create_process(path, show_windows=False):
|
||||
windows.winproxy.CreateProcessA(path, lpProcessInformation=ctypes.byref(proc_info), lpStartupInfo=lpStartupInfo)
|
||||
proc = [p for p in windows.system.processes if p.pid == proc_info.dwProcessId][0]
|
||||
return proc
|
||||
|
||||
|
||||
|
||||
def enable_privilege(lpszPrivilege, bEnablePrivilege):
|
||||
"""Enable of disable a privilege: enable_privilege(SE_DEBUG_NAME, True)"""
|
||||
tp = TOKEN_PRIVILEGES()
|
||||
@@ -94,21 +101,21 @@ def enable_privilege(lpszPrivilege, bEnablePrivilege):
|
||||
winproxy.CloseHandle(hToken)
|
||||
if winproxy.GetLastError() == windef.ERROR_NOT_ALL_ASSIGNED:
|
||||
raise ValueError("Failed to get privilege {0}".format(lpszPrivilege))
|
||||
return True
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def check_is_elevated():
|
||||
"""Return True if process is Admin"""
|
||||
tp = TOKEN_PRIVILEGES()
|
||||
hToken = HANDLE()
|
||||
elevation = TOKEN_ELEVATION()
|
||||
cbsize = DWORD()
|
||||
|
||||
bcsize = sizeof(elevation)
|
||||
winproxy.OpenProcessToken(winproxy.GetCurrentProcess(), TOKEN_ALL_ACCESS, byref(hToken))
|
||||
winproxy.GetTokenInformation(hToken, TokenElevation, byref(elevation), sizeof(elevation), byref(cbsize))
|
||||
winproxy.CloseHandle(hToken)
|
||||
return elevation.TokenIsElevated
|
||||
|
||||
|
||||
|
||||
def check_debug():
|
||||
"""Check that kernel is in debug mode
|
||||
beware if NOUMEX (https://msdn.microsoft.com/en-us/library/windows/hardware/ff556253(v=vs.85).aspx#_______noumex______)"""
|
||||
@@ -119,31 +126,34 @@ def check_debug():
|
||||
winproxy.RegOpenKeyExA(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control", 0, KEY_READ, byref(hkresult))
|
||||
winproxy.RegGetValueA(hkresult, None, "SystemStartOptions", RRF_RT_REG_SZ, None, byref(bufferres), byref(cbsize))
|
||||
winproxy.RegCloseKey(hkresult)
|
||||
|
||||
|
||||
control = bufferres[:]
|
||||
if "DEBUG" not in control:
|
||||
#print "[-] Enable debug boot!"
|
||||
#print "> bcdedit /debug on"
|
||||
# print "[-] Enable debug boot!"
|
||||
# print "> bcdedit /debug on"
|
||||
return False
|
||||
if "DEBUG=NOUMEX" not in control:
|
||||
pass
|
||||
#print "[*] Warning noumex not set!"
|
||||
#print "> bcdedit /set noumex on"
|
||||
# print "[*] Warning noumex not set!"
|
||||
# print "> bcdedit /set noumex on"
|
||||
return True
|
||||
|
||||
|
||||
class FixedInteractiveConsole(code.InteractiveConsole):
|
||||
def raw_input(self, prompt=">>>"):
|
||||
sys.stdout.write(prompt)
|
||||
return raw_input("")
|
||||
|
||||
|
||||
def pop_shell():
|
||||
"""Pop a console with an InterativeConsole"""
|
||||
create_console()
|
||||
FixedInteractiveConsole(locals()).interact()
|
||||
|
||||
|
||||
def get_kernel_modules():
|
||||
cbsize = DWORD()
|
||||
|
||||
|
||||
winproxy.NtQuerySystemInformation(SystemModuleInformation, None, 0, byref(cbsize))
|
||||
raw_buffer = (cbsize.value * c_char)()
|
||||
buffer = SYSTEM_MODULE_INFORMATION.from_address(ctypes.addressof(raw_buffer))
|
||||
@@ -151,6 +161,7 @@ def get_kernel_modules():
|
||||
modules = (SYSTEM_MODULE * buffer.ModulesCount).from_address(addressof(buffer) + SYSTEM_MODULE_INFORMATION.Modules.offset)
|
||||
return list(modules)
|
||||
|
||||
|
||||
class VirtualProtected(object):
|
||||
"""A context manager usable like `VirtualProtect` that will restore the old protection at exit
|
||||
|
||||
@@ -175,6 +186,7 @@ class VirtualProtected(object):
|
||||
winproxy.VirtualProtect(self.addr, self.size, self.old_protect.value, ctypes.byref(self.old_protect))
|
||||
return False
|
||||
|
||||
|
||||
class DisableWow64FsRedirection(object):
|
||||
"""A context manager that disable the Wow64 Fs Redirection"""
|
||||
def __enter__(self):
|
||||
|
||||
Reference in New Issue
Block a user