From e156b05e0f871ffd9761f75181eafe5bc783c6ed Mon Sep 17 00:00:00 2001 From: clement rouault Date: Mon, 9 May 2022 10:23:47 +0200 Subject: [PATCH] Improving SecurityDescriptor API/functions --- .../definitions/defines/security.txt | 4 +- .../definitions/functions/security.txt | 33 +++- windows/generated_def/meta.py | 4 + windows/generated_def/windef.py | 1 + windows/generated_def/winfuncs.py | 184 +++++++++--------- windows/security.py | 80 +++++++- windows/winproxy/apis/advapi32.py | 16 ++ 7 files changed, 228 insertions(+), 94 deletions(-) diff --git a/ctypes_generation/definitions/defines/security.txt b/ctypes_generation/definitions/defines/security.txt index ccf3380..3bb1043 100644 --- a/ctypes_generation/definitions/defines/security.txt +++ b/ctypes_generation/definitions/defines/security.txt @@ -121,4 +121,6 @@ #define PROTECTED_DACL_SECURITY_INFORMATION (0x80000000L) #define PROTECTED_SACL_SECURITY_INFORMATION (0x40000000L) #define UNPROTECTED_DACL_SECURITY_INFORMATION (0x20000000L) -#define UNPROTECTED_SACL_SECURITY_INFORMATION (0x10000000L) \ No newline at end of file +#define UNPROTECTED_SACL_SECURITY_INFORMATION (0x10000000L) + +#define SECURITY_DESCRIPTOR_REVISION 1 \ No newline at end of file diff --git a/ctypes_generation/definitions/functions/security.txt b/ctypes_generation/definitions/functions/security.txt index a526719..6b0303d 100644 --- a/ctypes_generation/definitions/functions/security.txt +++ b/ctypes_generation/definitions/functions/security.txt @@ -71,6 +71,11 @@ BOOL WINAPI GetSecurityDescriptorOwner( _Out_ LPBOOL lpbOwnerDefaulted ); +BOOL WINAPI SetSecurityDescriptorOwner( + [in, out] PSECURITY_DESCRIPTOR pSecurityDescriptor, + [in, optional] PSID pOwner, + [in] BOOL bOwnerDefaulted +); DWORD WINAPI GetSecurityDescriptorRMControl( _In_ PSECURITY_DESCRIPTOR SecurityDescriptor, @@ -307,4 +312,30 @@ DWORD BuildSecurityDescriptorW( PSECURITY_DESCRIPTOR pOldSD, PULONG pSizeNewSD, PSECURITY_DESCRIPTOR *pNewSD -); \ No newline at end of file +); + + +BOOL WINAPI MakeAbsoluteSD( + [in] PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor, + [out, optional] PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor, + [in, out] LPDWORD lpdwAbsoluteSecurityDescriptorSize, + [out, optional] PACL pDacl, + [in, out] LPDWORD lpdwDaclSize, + [out, optional] PACL pSacl, + [in, out] LPDWORD lpdwSaclSize, + [out, optional] PSID pOwner, + [in, out] LPDWORD lpdwOwnerSize, + [out, optional] PSID pPrimaryGroup, + [in, out] LPDWORD lpdwPrimaryGroupSize +); + +BOOL WINAPI MakeSelfRelativeSD( + [in] PSECURITY_DESCRIPTOR pAbsoluteSecurityDescriptor, + [out, optional] PSECURITY_DESCRIPTOR pSelfRelativeSecurityDescriptor, + [in, out] LPDWORD lpdwBufferLength +); + +BOOL WINAPI InitializeSecurityDescriptor( + [out] PSECURITY_DESCRIPTOR pSecurityDescriptor, + [in] DWORD dwRevision +); diff --git a/windows/generated_def/meta.py b/windows/generated_def/meta.py index cc3eeaa..e7a2ba2 100644 --- a/windows/generated_def/meta.py +++ b/windows/generated_def/meta.py @@ -1988,6 +1988,7 @@ windef = set(['ABOVE_NORMAL_PRIORITY_CLASS', 'SECTION_MAP_READ', 'SECTION_MAP_WRITE', 'SECTION_QUERY', + 'SECURITY_DESCRIPTOR_REVISION', 'SECURITY_MANDATORY_HIGH_RID', 'SECURITY_MANDATORY_LOW_RID', 'SECURITY_MANDATORY_MEDIUM_PLUS_RID', @@ -13900,6 +13901,8 @@ functions = set(['AccessCheck', 'LsaNtStatusToWinError', 'LsaOpenPolicy', 'LsaQueryInformationPolicy', + 'MakeAbsoluteSD', + 'MakeSelfRelativeSD', 'MapGenericMask', 'MapViewOfFile', 'MessageBoxA', @@ -14083,6 +14086,7 @@ functions = set(['AccessCheck', 'SetPriorityClass', 'SetProcessMitigationPolicy', 'SetSecurityDescriptorDacl', + 'SetSecurityDescriptorOwner', 'SetSecurityInfo', 'SetStdHandle', 'SetTcpEntry', diff --git a/windows/generated_def/windef.py b/windows/generated_def/windef.py index c96ae58..0dcdd07 100644 --- a/windows/generated_def/windef.py +++ b/windows/generated_def/windef.py @@ -872,6 +872,7 @@ PROTECTED_DACL_SECURITY_INFORMATION = make_flag("PROTECTED_DACL_SECURITY_INFORMA PROTECTED_SACL_SECURITY_INFORMATION = make_flag("PROTECTED_SACL_SECURITY_INFORMATION", ( 0x40000000 )) UNPROTECTED_DACL_SECURITY_INFORMATION = make_flag("UNPROTECTED_DACL_SECURITY_INFORMATION", ( 0x20000000 )) UNPROTECTED_SACL_SECURITY_INFORMATION = make_flag("UNPROTECTED_SACL_SECURITY_INFORMATION", ( 0x10000000 )) +SECURITY_DESCRIPTOR_REVISION = make_flag("SECURITY_DESCRIPTOR_REVISION", 1) SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL = make_flag("SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL", 1) SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY = make_flag("SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY", 2) SERVICE_TRIGGER_TYPE_DOMAIN_JOIN = make_flag("SERVICE_TRIGGER_TYPE_DOMAIN_JOIN", 3) diff --git a/windows/generated_def/winfuncs.py b/windows/generated_def/winfuncs.py index 180971f..270e35d 100644 --- a/windows/generated_def/winfuncs.py +++ b/windows/generated_def/winfuncs.py @@ -1440,6 +1440,71 @@ SetNamedPipeHandleStateParams = ((1, 'hNamedPipe'), (1, 'lpMode'), (1, 'lpMaxCol PeekNamedPipePrototype = WINFUNCTYPE(BOOL, HANDLE, LPVOID, DWORD, LPDWORD, LPDWORD, LPDWORD) PeekNamedPipeParams = ((1, 'hNamedPipe'), (1, 'lpBuffer'), (1, 'nBufferSize'), (1, 'lpBytesRead'), (1, 'lpTotalBytesAvail'), (1, 'lpBytesLeftThisMessage')) +#def CreateToolhelp32Snapshot(dwFlags, th32ProcessID): +# return CreateToolhelp32Snapshot.ctypes_function(dwFlags, th32ProcessID) +CreateToolhelp32SnapshotPrototype = WINFUNCTYPE(HANDLE, DWORD, DWORD) +CreateToolhelp32SnapshotParams = ((1, 'dwFlags'), (1, 'th32ProcessID')) + +#def Thread32First(hSnapshot, lpte): +# return Thread32First.ctypes_function(hSnapshot, lpte) +Thread32FirstPrototype = WINFUNCTYPE(BOOL, HANDLE, LPTHREADENTRY32) +Thread32FirstParams = ((1, 'hSnapshot'), (1, 'lpte')) + +#def Thread32Next(hSnapshot, lpte): +# return Thread32Next.ctypes_function(hSnapshot, lpte) +Thread32NextPrototype = WINFUNCTYPE(BOOL, HANDLE, LPTHREADENTRY32) +Thread32NextParams = ((1, 'hSnapshot'), (1, 'lpte')) + +#def Process32First(hSnapshot, lppe): +# return Process32First.ctypes_function(hSnapshot, lppe) +Process32FirstPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32) +Process32FirstParams = ((1, 'hSnapshot'), (1, 'lppe')) + +#def Process32Next(hSnapshot, lppe): +# return Process32Next.ctypes_function(hSnapshot, lppe) +Process32NextPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32) +Process32NextParams = ((1, 'hSnapshot'), (1, 'lppe')) + +#def Process32FirstW(hSnapshot, lppe): +# return Process32FirstW.ctypes_function(hSnapshot, lppe) +Process32FirstWPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32W) +Process32FirstWParams = ((1, 'hSnapshot'), (1, 'lppe')) + +#def Process32NextW(hSnapshot, lppe): +# return Process32NextW.ctypes_function(hSnapshot, lppe) +Process32NextWPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32W) +Process32NextWParams = ((1, 'hSnapshot'), (1, 'lppe')) + +#def GetProcAddress(hModule, lpProcName): +# return GetProcAddress.ctypes_function(hModule, lpProcName) +GetProcAddressPrototype = WINFUNCTYPE(FARPROC, HMODULE, LPCSTR) +GetProcAddressParams = ((1, 'hModule'), (1, 'lpProcName')) + +#def LoadLibraryA(lpFileName): +# return LoadLibraryA.ctypes_function(lpFileName) +LoadLibraryAPrototype = WINFUNCTYPE(HMODULE, LPCSTR) +LoadLibraryAParams = ((1, 'lpFileName'),) + +#def LoadLibraryW(lpFileName): +# return LoadLibraryW.ctypes_function(lpFileName) +LoadLibraryWPrototype = WINFUNCTYPE(HMODULE, LPCWSTR) +LoadLibraryWParams = ((1, 'lpFileName'),) + +#def LoadLibraryExA(lpLibFileName, hFile, dwFlags): +# return LoadLibraryExA.ctypes_function(lpLibFileName, hFile, dwFlags) +LoadLibraryExAPrototype = WINFUNCTYPE(HMODULE, LPCSTR, HANDLE, DWORD) +LoadLibraryExAParams = ((1, 'lpLibFileName'), (1, 'hFile'), (1, 'dwFlags')) + +#def LoadLibraryExW(lpLibFileName, hFile, dwFlags): +# return LoadLibraryExW.ctypes_function(lpLibFileName, hFile, dwFlags) +LoadLibraryExWPrototype = WINFUNCTYPE(HMODULE, LPCWSTR, HANDLE, DWORD) +LoadLibraryExWParams = ((1, 'lpLibFileName'), (1, 'hFile'), (1, 'dwFlags')) + +#def FreeLibrary(hLibModule): +# return FreeLibrary.ctypes_function(hLibModule) +FreeLibraryPrototype = WINFUNCTYPE(BOOL, HMODULE) +FreeLibraryParams = ((1, 'hLibModule'),) + #def RegQueryValueExA(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData): # return RegQueryValueExA.ctypes_function(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData) RegQueryValueExAPrototype = WINFUNCTYPE(LSTATUS, HKEY, LPCSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD) @@ -1650,6 +1715,11 @@ GetSecurityDescriptorLengthParams = ((1, 'pSecurityDescriptor'),) GetSecurityDescriptorOwnerPrototype = WINFUNCTYPE(BOOL, PSECURITY_DESCRIPTOR, POINTER(PSID), LPBOOL) GetSecurityDescriptorOwnerParams = ((1, 'pSecurityDescriptor'), (1, 'pOwner'), (1, 'lpbOwnerDefaulted')) +#def SetSecurityDescriptorOwner(pSecurityDescriptor, pOwner, bOwnerDefaulted): +# return SetSecurityDescriptorOwner.ctypes_function(pSecurityDescriptor, pOwner, bOwnerDefaulted) +SetSecurityDescriptorOwnerPrototype = WINFUNCTYPE(BOOL, PSECURITY_DESCRIPTOR, PSID, BOOL) +SetSecurityDescriptorOwnerParams = ((1, 'pSecurityDescriptor'), (1, 'pOwner'), (1, 'bOwnerDefaulted')) + #def GetSecurityDescriptorRMControl(SecurityDescriptor, RMControl): # return GetSecurityDescriptorRMControl.ctypes_function(SecurityDescriptor, RMControl) GetSecurityDescriptorRMControlPrototype = WINFUNCTYPE(DWORD, PSECURITY_DESCRIPTOR, PUCHAR) @@ -1800,6 +1870,21 @@ AddAccessDeniedAceExParams = ((1, 'pAcl'), (1, 'dwAceRevision'), (1, 'AceFlags') BuildSecurityDescriptorWPrototype = WINFUNCTYPE(DWORD, PTRUSTEE_W, PTRUSTEE_W, ULONG, PEXPLICIT_ACCESS_W, ULONG, PEXPLICIT_ACCESS_W, PSECURITY_DESCRIPTOR, PULONG, POINTER(PSECURITY_DESCRIPTOR)) BuildSecurityDescriptorWParams = ((1, 'pOwner'), (1, 'pGroup'), (1, 'cCountOfAccessEntries'), (1, 'pListOfAccessEntries'), (1, 'cCountOfAuditEntries'), (1, 'pListOfAuditEntries'), (1, 'pOldSD'), (1, 'pSizeNewSD'), (1, 'pNewSD')) +#def MakeAbsoluteSD(pSelfRelativeSecurityDescriptor, pAbsoluteSecurityDescriptor, lpdwAbsoluteSecurityDescriptorSize, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pPrimaryGroup, lpdwPrimaryGroupSize): +# return MakeAbsoluteSD.ctypes_function(pSelfRelativeSecurityDescriptor, pAbsoluteSecurityDescriptor, lpdwAbsoluteSecurityDescriptorSize, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pPrimaryGroup, lpdwPrimaryGroupSize) +MakeAbsoluteSDPrototype = WINFUNCTYPE(BOOL, PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD, PACL, LPDWORD, PACL, LPDWORD, PSID, LPDWORD, PSID, LPDWORD) +MakeAbsoluteSDParams = ((1, 'pSelfRelativeSecurityDescriptor'), (1, 'pAbsoluteSecurityDescriptor'), (1, 'lpdwAbsoluteSecurityDescriptorSize'), (1, 'pDacl'), (1, 'lpdwDaclSize'), (1, 'pSacl'), (1, 'lpdwSaclSize'), (1, 'pOwner'), (1, 'lpdwOwnerSize'), (1, 'pPrimaryGroup'), (1, 'lpdwPrimaryGroupSize')) + +#def MakeSelfRelativeSD(pAbsoluteSecurityDescriptor, pSelfRelativeSecurityDescriptor, lpdwBufferLength): +# return MakeSelfRelativeSD.ctypes_function(pAbsoluteSecurityDescriptor, pSelfRelativeSecurityDescriptor, lpdwBufferLength) +MakeSelfRelativeSDPrototype = WINFUNCTYPE(BOOL, PSECURITY_DESCRIPTOR, PSECURITY_DESCRIPTOR, LPDWORD) +MakeSelfRelativeSDParams = ((1, 'pAbsoluteSecurityDescriptor'), (1, 'pSelfRelativeSecurityDescriptor'), (1, 'lpdwBufferLength')) + +#def InitializeSecurityDescriptor(pSecurityDescriptor, dwRevision): +# return InitializeSecurityDescriptor.ctypes_function(pSecurityDescriptor, dwRevision) +InitializeSecurityDescriptorPrototype = WINFUNCTYPE(BOOL, PSECURITY_DESCRIPTOR, DWORD) +InitializeSecurityDescriptorParams = ((1, 'pSecurityDescriptor'), (1, 'dwRevision')) + #def OpenSCManagerA(lpMachineName, lpDatabaseName, dwDesiredAccess): # return OpenSCManagerA.ctypes_function(lpMachineName, lpDatabaseName, dwDesiredAccess) OpenSCManagerAPrototype = WINFUNCTYPE(SC_HANDLE, LPCSTR, LPCSTR, DWORD) @@ -2840,21 +2925,6 @@ TerminateProcessParams = ((1, 'hProcess'), (1, 'uExitCode')) GetLastErrorPrototype = WINFUNCTYPE(DWORD) GetLastErrorParams = () -#def GetCurrentProcess(): -# return GetCurrentProcess.ctypes_function() -GetCurrentProcessPrototype = WINFUNCTYPE(HANDLE) -GetCurrentProcessParams = () - -#def CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile): -# return CreateFileA.ctypes_function(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile) -CreateFileAPrototype = WINFUNCTYPE(HANDLE, LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE) -CreateFileAParams = ((1, 'lpFileName'), (1, 'dwDesiredAccess'), (1, 'dwShareMode'), (1, 'lpSecurityAttributes'), (1, 'dwCreationDisposition'), (1, 'dwFlagsAndAttributes'), (1, 'hTemplateFile')) - -#def CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile): -# return CreateFileW.ctypes_function(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile) -CreateFileWPrototype = WINFUNCTYPE(HANDLE, LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE) -CreateFileWParams = ((1, 'lpFileName'), (1, 'dwDesiredAccess'), (1, 'dwShareMode'), (1, 'lpSecurityAttributes'), (1, 'dwCreationDisposition'), (1, 'dwFlagsAndAttributes'), (1, 'hTemplateFile')) - #def LdrLoadDll(PathToFile, Flags, ModuleFileName, ModuleHandle): # return LdrLoadDll.ctypes_function(PathToFile, Flags, ModuleFileName, ModuleHandle) LdrLoadDllPrototype = WINFUNCTYPE(NTSTATUS, LPCWSTR, PVOID, PUNICODE_STRING, PHANDLE) @@ -3020,70 +3090,20 @@ WriteProcessMemoryParams = ((1, 'hProcess'), (1, 'lpBaseAddress'), (1, 'lpBuffer NtWow64WriteVirtualMemory64Prototype = WINFUNCTYPE(NTSTATUS, HANDLE, ULONG64, LPVOID, ULONG64, PULONG64) NtWow64WriteVirtualMemory64Params = ((1, 'hProcess'), (1, 'lpBaseAddress'), (1, 'lpBuffer'), (1, 'nSize'), (1, 'lpNumberOfBytesWritten')) -#def CreateToolhelp32Snapshot(dwFlags, th32ProcessID): -# return CreateToolhelp32Snapshot.ctypes_function(dwFlags, th32ProcessID) -CreateToolhelp32SnapshotPrototype = WINFUNCTYPE(HANDLE, DWORD, DWORD) -CreateToolhelp32SnapshotParams = ((1, 'dwFlags'), (1, 'th32ProcessID')) +#def GetCurrentProcess(): +# return GetCurrentProcess.ctypes_function() +GetCurrentProcessPrototype = WINFUNCTYPE(HANDLE) +GetCurrentProcessParams = () -#def Thread32First(hSnapshot, lpte): -# return Thread32First.ctypes_function(hSnapshot, lpte) -Thread32FirstPrototype = WINFUNCTYPE(BOOL, HANDLE, LPTHREADENTRY32) -Thread32FirstParams = ((1, 'hSnapshot'), (1, 'lpte')) +#def CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile): +# return CreateFileA.ctypes_function(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile) +CreateFileAPrototype = WINFUNCTYPE(HANDLE, LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE) +CreateFileAParams = ((1, 'lpFileName'), (1, 'dwDesiredAccess'), (1, 'dwShareMode'), (1, 'lpSecurityAttributes'), (1, 'dwCreationDisposition'), (1, 'dwFlagsAndAttributes'), (1, 'hTemplateFile')) -#def Thread32Next(hSnapshot, lpte): -# return Thread32Next.ctypes_function(hSnapshot, lpte) -Thread32NextPrototype = WINFUNCTYPE(BOOL, HANDLE, LPTHREADENTRY32) -Thread32NextParams = ((1, 'hSnapshot'), (1, 'lpte')) - -#def Process32First(hSnapshot, lppe): -# return Process32First.ctypes_function(hSnapshot, lppe) -Process32FirstPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32) -Process32FirstParams = ((1, 'hSnapshot'), (1, 'lppe')) - -#def Process32Next(hSnapshot, lppe): -# return Process32Next.ctypes_function(hSnapshot, lppe) -Process32NextPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32) -Process32NextParams = ((1, 'hSnapshot'), (1, 'lppe')) - -#def Process32FirstW(hSnapshot, lppe): -# return Process32FirstW.ctypes_function(hSnapshot, lppe) -Process32FirstWPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32W) -Process32FirstWParams = ((1, 'hSnapshot'), (1, 'lppe')) - -#def Process32NextW(hSnapshot, lppe): -# return Process32NextW.ctypes_function(hSnapshot, lppe) -Process32NextWPrototype = WINFUNCTYPE(BOOL, HANDLE, LPPROCESSENTRY32W) -Process32NextWParams = ((1, 'hSnapshot'), (1, 'lppe')) - -#def GetProcAddress(hModule, lpProcName): -# return GetProcAddress.ctypes_function(hModule, lpProcName) -GetProcAddressPrototype = WINFUNCTYPE(FARPROC, HMODULE, LPCSTR) -GetProcAddressParams = ((1, 'hModule'), (1, 'lpProcName')) - -#def LoadLibraryA(lpFileName): -# return LoadLibraryA.ctypes_function(lpFileName) -LoadLibraryAPrototype = WINFUNCTYPE(HMODULE, LPCSTR) -LoadLibraryAParams = ((1, 'lpFileName'),) - -#def LoadLibraryW(lpFileName): -# return LoadLibraryW.ctypes_function(lpFileName) -LoadLibraryWPrototype = WINFUNCTYPE(HMODULE, LPCWSTR) -LoadLibraryWParams = ((1, 'lpFileName'),) - -#def LoadLibraryExA(lpLibFileName, hFile, dwFlags): -# return LoadLibraryExA.ctypes_function(lpLibFileName, hFile, dwFlags) -LoadLibraryExAPrototype = WINFUNCTYPE(HMODULE, LPCSTR, HANDLE, DWORD) -LoadLibraryExAParams = ((1, 'lpLibFileName'), (1, 'hFile'), (1, 'dwFlags')) - -#def LoadLibraryExW(lpLibFileName, hFile, dwFlags): -# return LoadLibraryExW.ctypes_function(lpLibFileName, hFile, dwFlags) -LoadLibraryExWPrototype = WINFUNCTYPE(HMODULE, LPCWSTR, HANDLE, DWORD) -LoadLibraryExWParams = ((1, 'lpLibFileName'), (1, 'hFile'), (1, 'dwFlags')) - -#def FreeLibrary(hLibModule): -# return FreeLibrary.ctypes_function(hLibModule) -FreeLibraryPrototype = WINFUNCTYPE(BOOL, HMODULE) -FreeLibraryParams = ((1, 'hLibModule'),) +#def CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile): +# return CreateFileW.ctypes_function(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile) +CreateFileWPrototype = WINFUNCTYPE(HANDLE, LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE) +CreateFileWParams = ((1, 'lpFileName'), (1, 'dwDesiredAccess'), (1, 'dwShareMode'), (1, 'lpSecurityAttributes'), (1, 'dwCreationDisposition'), (1, 'dwFlagsAndAttributes'), (1, 'hTemplateFile')) #def OpenProcessToken(ProcessHandle, DesiredAccess, TokenHandle): # return OpenProcessToken.ctypes_function(ProcessHandle, DesiredAccess, TokenHandle) @@ -3295,16 +3315,6 @@ WaitForSingleObjectParams = ((1, 'hHandle'), (1, 'dwMilliseconds')) GetThreadIdPrototype = WINFUNCTYPE(DWORD, HANDLE) GetThreadIdParams = ((1, 'Thread'),) -#def LoadLibraryExA(lpFileName, hFile, dwFlags): -# return LoadLibraryExA.ctypes_function(lpFileName, hFile, dwFlags) -LoadLibraryExAPrototype = WINFUNCTYPE(HMODULE, LPCSTR, HANDLE, DWORD) -LoadLibraryExAParams = ((1, 'lpFileName'), (1, 'hFile'), (1, 'dwFlags')) - -#def LoadLibraryExW(lpFileName, hFile, dwFlags): -# return LoadLibraryExW.ctypes_function(lpFileName, hFile, dwFlags) -LoadLibraryExWPrototype = WINFUNCTYPE(HMODULE, LPCWSTR, HANDLE, DWORD) -LoadLibraryExWParams = ((1, 'lpFileName'), (1, 'hFile'), (1, 'dwFlags')) - #def DeviceIoControl(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped): # return DeviceIoControl.ctypes_function(hDevice, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, lpOverlapped) DeviceIoControlPrototype = WINFUNCTYPE(BOOL, HANDLE, DWORD, LPVOID, DWORD, LPVOID, DWORD, LPDWORD, LPOVERLAPPED) diff --git a/windows/security.py b/windows/security.py index 23b2817..9308430 100644 --- a/windows/security.py +++ b/windows/security.py @@ -715,6 +715,12 @@ class SecurityDescriptor(gdef.PSECURITY_DESCRIPTOR): _close_function = winproxy.LocalFree + @classmethod + def create(cls): + buff = windows.utils.BUFFER(gdef.SECURITY_DESCRIPTOR, 1)() + winproxy.InitializeSecurityDescriptor(buff, gdef.SECURITY_DESCRIPTOR_REVISION) + return buff.cast(windows.security.SecurityDescriptor) + @property def control(self): """The security descriptor control @@ -737,8 +743,7 @@ class SecurityDescriptor(gdef.PSECURITY_DESCRIPTOR): winproxy.GetSecurityDescriptorControl(self, control, lpdwRevision) return lpdwRevision.value - @property - def owner(self): + def get_owner(self): """The owner of the security descriptor :type: :class:`~windows.generated_def.winstructs.PSID` or ``None`` @@ -749,6 +754,11 @@ class SecurityDescriptor(gdef.PSECURITY_DESCRIPTOR): # Return None of owner is NULL return owner or None + def set_owner(self, new_owner): + winproxy.SetSecurityDescriptorOwner(self, new_owner, bOwnerDefaulted=False) + + owner = property(get_owner, set_owner) + @property def group(self): """The group of the security descriptor @@ -943,11 +953,66 @@ class SecurityDescriptor(gdef.PSECURITY_DESCRIPTOR): __str__ = to_string + def isrelative(self): + """[WIP] api may change""" + return bool(self.control & gdef.SE_SELF_RELATIVE) - # TST + def make_absolute(self): + """[WIP] api may change""" + # Assert relative ? + abs_descriptor_size = gdef.DWORD(0) + dacl_size = gdef.DWORD(0) + sacl_size = gdef.DWORD(0) + owner_size = gdef.DWORD(0) + prim_groupe_size = gdef.DWORD(0) + + try: + windows.winproxy.MakeAbsoluteSD(self, None, abs_descriptor_size, + None, + dacl_size, + None, + sacl_size, + None, + owner_size, + None, + prim_groupe_size) + except WindowsError as e: + if e.winerror != gdef.ERROR_INSUFFICIENT_BUFFER: + raise + + abs_sd = windows.utils.BUFFER(gdef.BYTE, abs_descriptor_size.value)().cast(SecurityDescriptor) + # dacl = ctypes.create_string_buffer(dacl_size.value) + dacl = windows.utils.BUFFER(gdef.BYTE, dacl_size.value)() + sacl = windows.utils.BUFFER(gdef.BYTE, sacl_size.value)() + owner = ctypes.create_string_buffer(owner_size.value) + prim_groupe = ctypes.create_string_buffer(prim_groupe_size.value) + + windows.winproxy.MakeAbsoluteSD(self, abs_sd, abs_descriptor_size, + dacl.cast(gdef.PACL), + dacl_size, + sacl.cast(gdef.PACL), + sacl_size, + owner, + owner_size, + prim_groupe, + prim_groupe_size) + + abs_sd._save = [dacl, sacl, owner, prim_groupe] + return abs_sd + + def make_relative(self): + """[WIP] api may change""" + srsd_size = gdef.DWORD(0) + try: + windows.winproxy.MakeSelfRelativeSD(self, None, srsd_size) + except WindowsError as e: + if e.winerror != gdef.ERROR_INSUFFICIENT_BUFFER: + raise + + srsd = windows.utils.BUFFER(gdef.BYTE, srsd_size.value)().cast(SecurityDescriptor) + windows.winproxy.MakeSelfRelativeSD(self, srsd, srsd_size) + return srsd - # def relative(self): - # return bool(self.control & gdef.SE_SELF_RELATIVE) # If we want auto-free we need to handle relf-relative SD # We need to keep-track of sub-object of the SD @@ -958,6 +1023,11 @@ class SecurityDescriptor(gdef.PSECURITY_DESCRIPTOR): # print("FREE SELF") # self._close_function(self) + def __repr__(self): + if (self.isrelative()): + return "<{0} (self relative format) at {1:#x}>".format(type(self).__name__, id(self)) + return "<{0} (absolute format) at {1:#x}>".format(type(self).__name__, id(self)) + def explain(self, type=None): if type is None: type = getattr(self, "obj_type", None) diff --git a/windows/winproxy/apis/advapi32.py b/windows/winproxy/apis/advapi32.py index 41b3bdc..682af75 100644 --- a/windows/winproxy/apis/advapi32.py +++ b/windows/winproxy/apis/advapi32.py @@ -196,6 +196,10 @@ def SetNamedSecurityInfoW(pObjectName, ObjectType, SecurityInfo, psidOwner, psid return SetNamedSecurityInfoW.ctypes_function(pObjectName, ObjectType, SecurityInfo, psidOwner, psidGroup, pDacl, pSacl) +@Advapi32Proxy() +def InitializeSecurityDescriptor(pSecurityDescriptor, dwRevision): + return InitializeSecurityDescriptor.ctypes_function(pSecurityDescriptor, dwRevision) + @Advapi32Proxy() def IsValidSecurityDescriptor(pSecurityDescriptor): return IsValidSecurityDescriptor.ctypes_function(pSecurityDescriptor) @@ -232,6 +236,10 @@ def GetSecurityDescriptorControl(pSecurityDescriptor, pControl, lpdwRevision): def GetSecurityDescriptorOwner(pSecurityDescriptor, pOwner, lpbOwnerDefaulted): return GetSecurityDescriptorOwner.ctypes_function(pSecurityDescriptor, pOwner, lpbOwnerDefaulted) +@Advapi32Proxy() +def SetSecurityDescriptorOwner(pSecurityDescriptor, pOwner, bOwnerDefaulted): + return SetSecurityDescriptorOwner.ctypes_function(pSecurityDescriptor, pOwner, bOwnerDefaulted) + @Advapi32Proxy() def GetSecurityDescriptorGroup(pSecurityDescriptor, pGroup, lpbGroupDefaulted): return GetSecurityDescriptorGroup.ctypes_function(pSecurityDescriptor, pGroup, lpbGroupDefaulted) @@ -240,6 +248,14 @@ def GetSecurityDescriptorGroup(pSecurityDescriptor, pGroup, lpbGroupDefaulted): def GetSecurityDescriptorSacl(pSecurityDescriptor, lpbSaclPresent, pSacl, lpbSaclDefaulted): return GetSecurityDescriptorSacl.ctypes_function(pSecurityDescriptor, lpbSaclPresent, pSacl, lpbSaclDefaulted) +@Advapi32Proxy() +def MakeAbsoluteSD(pSelfRelativeSecurityDescriptor, pAbsoluteSecurityDescriptor, lpdwAbsoluteSecurityDescriptorSize, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pPrimaryGroup, lpdwPrimaryGroupSize): + return MakeAbsoluteSD.ctypes_function(pSelfRelativeSecurityDescriptor, pAbsoluteSecurityDescriptor, lpdwAbsoluteSecurityDescriptorSize, pDacl, lpdwDaclSize, pSacl, lpdwSaclSize, pOwner, lpdwOwnerSize, pPrimaryGroup, lpdwPrimaryGroupSize) + +@Advapi32Proxy() +def MakeSelfRelativeSD(pAbsoluteSecurityDescriptor, pSelfRelativeSecurityDescriptor, lpdwBufferLength): + return MakeSelfRelativeSD.ctypes_function(pAbsoluteSecurityDescriptor, pSelfRelativeSecurityDescriptor, lpdwBufferLength) + # ACE - ACL @Advapi32Proxy()