line 100, in <module>
ReturnLength)
TypeError: an integer is required
from cffi import FFI
import inspect
import ctypes
ffi = FFI()
# Unicode is necessary to use the LPCTSTR type
ffi.set_unicode(True)
Kernel32 = ffi.dlopen("Kernel32.dll")
advapi32 = ffi.dlopen("Advapi32.dll")
# Define TOKEN_PRIVILEGE, LUID_AND_ATTRIBUTES and LUID structures for later use
ffi.cdef('''
int ANYSIZE_ARRAY = 1;
typedef struct _LUID {
DWORD LowPart;
LONG HighPart;
} LUID, *PLUID;
typedef struct _LUID_AND_ATTRIBUTES {
LUID Luid;
DWORD Attributes;
} LUID_AND_ATTRIBUTES, *PLUID_AND_ATTRIBUTES;
typedef struct _TOKEN_PRIVILEGES {
DWORD PrivilegeCount;
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];
} TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES;
typedef PVOID HANDLE;
typedef HANDLE *PHANDLE;
''')
# Define LookupPrivilegeValue and look up privilege value for SE_MANAGE_VOLUME_NAME
ffi.cdef("""
BOOL LookupPrivilegeValueW(LPCTSTR lpSystemName,
LPCTSTR lpName,
PLUID lpLUID);
""")
LUID = ffi.new("PLUID")
privilegeName = "SeManageVolumePrivilege"
advapi32.LookupPrivilegeValueW(ffi.NULL, privilegeName, LUID)
print(privilegeName, "HighPart", LUID[0].HighPart, "LowPart", LUID[0].LowPart)
# Make privilege token to hand to AdjustTokenPrivileges
SE_PRIVILEGE_ENABLED = 0x00000002 # in WinNT.h
tp = ffi.new("PTOKEN_PRIVILEGES")
tp[0].PrivilegeCount = 1
tp[0].Privileges[0].Luid = LUID[0]
tp[0].Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
# we can print the whole struct \o/
print("tp PrivilegeCount", tp[0].PrivilegeCount)
print("tp LUID_AND_ATTRIBUTES Attributes", tp[0].Privileges[0].Attributes)
print("tp LUID_AND_ATTRIBUTES LUID HighPart", tp[0].Privileges[0].Luid.HighPart, "LowPart", tp[0].Privileges[0].Luid.LowPart)
# Retreive pseudo handle for current process
ffi.cdef("""
HANDLE GetCurrentProcess(void);
""")
pHandle = Kernel32.GetCurrentProcess()
print(pHandle)
# Open Privilege Token for current process
ffi.cdef("""
BOOL OpenProcessToken(HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle);
""")
TOKEN_ADJUST_PRIVILEGES = 0x20
TOKEN_QUERY = 0x8
hToken = ffi.new("PHANDLE")
advapi32.OpenProcessToken(pHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, hToken)
print(ffi.getwinerror())
# Maken PTOKEN_PRIVILEGES to hold the old privileges
oldTP = ffi.new("PTOKEN_PRIVILEGES")
tp_buffer = ffi.buffer(oldTP)
ReturnLength = ffi.new("PDWORD")
print(ReturnLength)
print("ReturnLength: ", ReturnLength)
print("tp_buffer: ", tp_buffer)
# Adjust the process' privileges
ffi.cdef("""
BOOL AdjustTokenPrivileges(HANDLE TokenHandle,
BOOL DisableAllPrivileges,
PTOKEN_PRIVILEGES NewState,
DWORD BufferLength,
PTOKEN_PRIVILEGES PreviousState,
PDWORD ReturnLength);
""")
advapi32.AdjustTokenPrivileges(hToken,
False,
tp,
tp_buffer,
oldTP,
ReturnLength)
--
-- python-cffi: To unsubscribe from this group, send email to python-cffi...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/python-cffi?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "python-cffi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-cffi/R89P4CrdWzc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-cffi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.