这里提到了一个非常酷的功能:
Prevent user process from being killed with "End Process" from Process Explorer
有没有人知道如何将这个C ++代码翻译成Python(或重新编辑它,以便它至少在C / C ++中编译,假设它就是它):
static const bool ProtectProcess()
{
HANDLE hProcess = GetCurrentProcess();
EXPLICIT_ACCESS denyAccess = {0};
DWORD dwAccessPermissions = GENERIC_WRITE|PROCESS_ALL_ACCESS|WRITE_DAC|DELETE|WRITE_OWNER|READ_CONTROL;
BuildExplicitAccessWithName( &denyAccess, _T("CURRENT_USER"), dwAccessPermissions, DENY_ACCESS, NO_INHERITANCE );
PACL pTempDacl = NULL;
DWORD dwErr = 0;
dwErr = SetEntriesInAcl( 1, &denyAccess, NULL, &pTempDacl );
// check dwErr...
dwErr = SetSecurityInfo( hProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pTempDacl, NULL );
// check dwErr...
LocalFree( pTempDacl );
CloseHandle( hProcess );
return dwErr == ERROR_SUCCESS;
}
答案 0 :(得分:2)
这是您发布的代码的相当粗略的ctypes翻译。它甚至似乎工作!请注意,我删除了对CloseHandle
的调用,这是完全错误的。你不应该在伪句柄上调用CloseHandle
,这是GetCurrentProcess
返回的。
from ctypes import *
from ctypes.wintypes import *
from win32con import *
class TRUSTEE(Structure):
pass
TRUSTEE._fields_ = (
('pMultipleTrustee', POINTER(TRUSTEE)),
('MultipleTrusteeOperation', c_int),
('TrusteeForm', c_int),
('TrusteeType', c_int),
('ptstrName', LPSTR)
)
class EXPLICIT_ACCESS(Structure):
_fields_ = (
('grfAccessPermissions', DWORD),
('grfAccessMode', c_int),
('grfInheritance', DWORD),
('Trustee', TRUSTEE)
)
GetCurrentProcess = windll.kernel32.GetCurrentProcess
GetCurrentProcess.restype = HANDLE
hProcess = GetCurrentProcess()
denyAccess = EXPLICIT_ACCESS()
dwAccessPermissions = DWORD(GENERIC_WRITE|PROCESS_ALL_ACCESS|WRITE_DAC|DELETE|WRITE_OWNER|READ_CONTROL);
BuildExplicitAccessWithName = windll.advapi32.BuildExplicitAccessWithNameA
BuildExplicitAccessWithName.restype = None
DENY_ACCESS = 3
NO_INHERITANCE = 0
BuildExplicitAccessWithName(byref(denyAccess), 'CURRENT_USER', dwAccessPermissions, DENY_ACCESS, NO_INHERITANCE)
SetEntriesInAcl = windll.advapi32.SetEntriesInAclA
SetEntriesInAcl.restype = DWORD
SetEntriesInAcl.argtypes = (ULONG, POINTER(EXPLICIT_ACCESS), c_voidp, POINTER(c_voidp))
pTempDacl = c_voidp()
dwErr = SetEntriesInAcl(1, byref(denyAccess), None, byref(pTempDacl));
SetSecurityInfo = windll.advapi32.SetSecurityInfo
SetSecurityInfo.restype = DWORD
SetSecurityInfo.argtypes = (HANDLE, c_int, DWORD, c_voidp, c_voidp, c_voidp, c_voidp)
SE_KERNEL_OBJECT = 6
dwErr = SetSecurityInfo(hProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, None, None, pTempDacl, None);
LocalFree = windll.kernel32.LocalFree
LocalFree.restype = c_voidp
LocalFree.argtypes = (c_voidp,)
LocalFree(pTempDacl)
答案 1 :(得分:1)
如何使用ctypes?您也可以尝试pywin32。您也可以尝试使用IronPython。对于ActivePython,有win32api。
另外,我不知道你为什么要实现这一目标的原因,这意味着可能有一些更优雅的解决方案。