diff --git a/CHANGELOG b/CHANGELOG index 4bdd129..357d5cc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -23,4 +23,10 @@ Since 0.2: * Can retrieve 64b context of syswow thread via context_syswow/set_syswow_context * Debugger handle breakpoint in 64b part of syswow process * ReadSyswow64Process is now a Process (allows to parse exports of remote PEB64 of syswow process) - * Added Debugger.detach \ No newline at end of file + * Added Debugger.detach + + +Since 0.3: + + * Rewrote pe_parse to minimize closure (allowing the refcount to dell all ref to WinProcess when the debugger detach it) + * Fix debugger.detach / handling on exit_process \ No newline at end of file diff --git a/TODO b/TODO index a26d945..31ea754 100644 --- a/TODO +++ b/TODO @@ -18,6 +18,8 @@ TODO: - registry - test ! + - Clean pe_parse.py + Documentation * verif samples diff --git a/ctypes_generation/definitions/winstruct.txt b/ctypes_generation/definitions/winstruct.txt index bdfa458..92341e1 100644 --- a/ctypes_generation/definitions/winstruct.txt +++ b/ctypes_generation/definitions/winstruct.txt @@ -2724,3 +2724,39 @@ typedef struct _CERT_CHAIN_ENGINE_CONFIG { DWORD MaximumCachedCertificates; DWORD CycleDetectionModulus; } CERT_CHAIN_ENGINE_CONFIG, *PCERT_CHAIN_ENGINE_CONFIG; + + +typedef struct _SYSTEMTIME { + WORD wYear; + WORD wMonth; + WORD wDayOfWeek; + WORD wDay; + WORD wHour; + WORD wMinute; + WORD wSecond; + WORD wMilliseconds; +} SYSTEMTIME, *PSYSTEMTIME; + + +typedef struct _CERT_EXTENSIONS { + DWORD cExtension; + PCERT_EXTENSION rgExtension; +} CERT_EXTENSIONS, *PCERT_EXTENSIONS; + +typedef struct _CRYPT_KEY_PROV_PARAM { + DWORD dwParam; + BYTE *pbData; + DWORD cbData; + DWORD dwFlags; +} CRYPT_KEY_PROV_PARAM, *PCRYPT_KEY_PROV_PARAM; + + +typedef struct _CRYPT_KEY_PROV_INFO { + LPWSTR pwszContainerName; + LPWSTR pwszProvName; + DWORD dwProvType; + DWORD dwFlags; + DWORD cProvParam; + PCRYPT_KEY_PROV_PARAM rgProvParam; + DWORD dwKeySpec; +} CRYPT_KEY_PROV_INFO, *PCRYPT_KEY_PROV_INFO; \ No newline at end of file diff --git a/ctypes_generation/definitions/wintrust_crypt_func.txt b/ctypes_generation/definitions/wintrust_crypt_func.txt index 3f099ed..159a1ca 100644 --- a/ctypes_generation/definitions/wintrust_crypt_func.txt +++ b/ctypes_generation/definitions/wintrust_crypt_func.txt @@ -1,6 +1,6 @@ BOOL WINAPI CryptQueryObject( _In_ DWORD dwObjectType, - _In_ void *pvObject, + _In_ VOID *pvObject, _In_ DWORD dwExpectedContentTypeFlags, _In_ DWORD dwExpectedFormatTypeFlags, _In_ DWORD dwFlags, @@ -16,7 +16,7 @@ BOOL WINAPI CryptMsgGetParam( _In_ HCRYPTMSG hCryptMsg, _In_ DWORD dwParamType, _In_ DWORD dwIndex, - _Out_ void *pvData, + _Out_ VOID *pvData, _Inout_ DWORD *pcbData ); @@ -27,7 +27,7 @@ BOOL WINAPI CryptDecodeObject( _In_ BYTE *pbEncoded, _In_ DWORD cbEncoded, _In_ DWORD dwFlags, - _Out_ void *pvStructInfo, + _Out_ VOID *pvStructInfo, _Inout_ DWORD *pcbStructInfo ); @@ -37,7 +37,7 @@ PCCERT_CONTEXT WINAPI CertFindCertificateInStore( _In_ DWORD dwCertEncodingType, _In_ DWORD dwFindFlags, _In_ DWORD dwFindType, - _In_ void *pvFindPara, + _In_ VOID *pvFindPara, _In_ PCCERT_CONTEXT pPrevCertContext ); @@ -45,7 +45,7 @@ DWORD WINAPI CertGetNameStringA( _In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, - _In_ void *pvTypePara, + _In_ VOID *pvTypePara, _Out_ LPCSTR pszNameString, _In_ DWORD cchNameString ); @@ -54,7 +54,7 @@ DWORD WINAPI CertGetNameStringW( _In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, - _In_ void *pvTypePara, + _In_ VOID *pvTypePara, _Out_ LPWSTR pszNameString, _In_ DWORD cchNameString ); @@ -70,3 +70,112 @@ BOOL WINAPI CertGetCertificateChain( _In_ LPVOID pvReserved, _Out_ PCCERT_CHAIN_CONTEXT *ppChainContext ); + +PCCERT_CONTEXT WINAPI CertCreateSelfSignCertificate( + _In_opt_ HCRYPTPROV_OR_NCRYPT_KEY_HANDLE hCryptProvOrNCryptKey, + _In_ PCERT_NAME_BLOB pSubjectIssuerBlob, + _In_ DWORD dwFlags, + _In_opt_ PCRYPT_KEY_PROV_INFO pKeyProvInfo, + _In_opt_ PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm, + _In_opt_ PSYSTEMTIME pStartTime, + _In_opt_ PSYSTEMTIME pEndTime, + _In_opt_ PCERT_EXTENSIONS pExtensions +); + + +BOOL WINAPI CertStrToNameA( + _In_ DWORD dwCertEncodingType, + _In_ LPCSTR pszX500, + _In_ DWORD dwStrType, + _In_opt_ VOID *pvReserved, + _Out_ BYTE *pbEncoded, + _Inout_ DWORD *pcbEncoded, + _Out_opt_ LPCSTR *ppszError +); + +BOOL WINAPI CertStrToNameW( + _In_ DWORD dwCertEncodingType, + _In_ LPWSTR pszX500, + _In_ DWORD dwStrType, + _In_opt_ VOID *pvReserved, + _Out_ BYTE *pbEncoded, + _Inout_ DWORD *pcbEncoded, + _Out_opt_ LPWSTR *ppszError +); + +HCERTSTORE WINAPI CertOpenStore( + _In_ LPCSTR lpszStoreProvider, + _In_ DWORD dwMsgAndCertEncodingType, + _In_ HCRYPTPROV_LEGACY hCryptProv, + _In_ DWORD dwFlags, + _In_ VOID *pvPara +); + +BOOL WINAPI CertAddCertificateContextToStore( + _In_ HCERTSTORE hCertStore, + _In_ PCCERT_CONTEXT pCertContext, + _In_ DWORD dwAddDisposition, + _Out_opt_ PCCERT_CONTEXT *ppStoreContext +); + + +BOOL WINAPI PFXExportCertStoreEx( + _In_ HCERTSTORE hStore, + _Inout_ CRYPT_DATA_BLOB *pPFX, + _In_ LPCWSTR szPassword, + _In_ VOID *pvPara, + _In_ DWORD dwFlags +); + +HCERTSTORE WINAPI PFXImportCertStore( + _In_ CRYPT_DATA_BLOB *pPFX, + _In_ LPCWSTR szPassword, + _In_ DWORD dwFlags +); + + +BOOL WINAPI CryptGenKey( + _In_ HCRYPTPROV hProv, + _In_ ALG_ID Algid, + _In_ DWORD dwFlags, + _Out_ HCRYPTKEY *phKey +); + +BOOL WINAPI CryptAcquireContextA( + _Out_ HCRYPTPROV *phProv, + _In_ LPCSTR pszContainer, + _In_ LPCSTR pszProvider, + _In_ DWORD dwProvType, + _In_ DWORD dwFlags +); + +BOOL WINAPI CryptAcquireContextW( + _Out_ HCRYPTPROV *phProv, + _In_ LPWSTR pszContainer, + _In_ LPWSTR pszProvider, + _In_ DWORD dwProvType, + _In_ DWORD dwFlags +); + + +BOOL WINAPI CryptExportKey( + _In_ HCRYPTKEY hKey, + _In_ HCRYPTKEY hExpKey, + _In_ DWORD dwBlobType, + _In_ DWORD dwFlags, + _Out_ BYTE *pbData, + _Inout_ DWORD *pdwDataLen +); + + +BOOL WINAPI CertGetCertificateContextProperty( + _In_ PCCERT_CONTEXT pCertContext, + _In_ DWORD dwPropId, + _Out_ VOID *pvData, + _Inout_ DWORD *pcbData +); + +DWORD WINAPI CertEnumCertificateContextProperties( + _In_ PCCERT_CONTEXT pCertContext, + _In_ DWORD dwPropId +); \ No newline at end of file diff --git a/ctypes_generation/generate.py b/ctypes_generation/generate.py index 0fe17bf..3a548d3 100644 --- a/ctypes_generation/generate.py +++ b/ctypes_generation/generate.py @@ -17,6 +17,9 @@ dedent = textwrap.dedent TYPE_EQUIVALENCE = [ + # BYTE is defined in ctypes.wintypes as c_byte but who wants + # BYTE to be signed ? (from MSDN: ) + ('BYTE', 'c_ubyte'), ('PWSTR', 'LPWSTR'), ('PCWSTR', 'LPWSTR'), ('SIZE_T', 'c_ulong'), @@ -80,6 +83,12 @@ TYPE_EQUIVALENCE = [ ('ACCESS_MASK', 'DWORD'), ('REGSAM', 'ACCESS_MASK'), ('SECURITY_CONTEXT_TRACKING_MODE', 'BOOLEAN'), + ('HCRYPTPROV_OR_NCRYPT_KEY_HANDLE', 'PULONG'), + ('HCRYPTPROV_LEGACY', 'PULONG'), + ('HCRYPTKEY', 'PULONG'), + ('HCRYPTPROV', 'PULONG'), + ('HCRYPTHASH', 'PULONG'), + ('ALG_ID', 'UINT'), ("DISPID", "LONG"), ("MEMBERID", "DISPID"), ('PSECURITY_DESCRIPTOR', 'PVOID'), @@ -158,6 +167,7 @@ class CtypesGenerator(object): self.parse() self.data += self.PARSER(open(filename).read()).parse() self.analyse(self.data) + self.check_dependances() class InitialDefGenerator(CtypesGenerator): PARSER = def_parser.WinDefParser @@ -283,6 +293,7 @@ class StructGenerator(CtypesGenerator): self.data[0].extend(new_data[0]) self.data[1].extend(new_data[1]) self.analyse(self.data) + self.check_dependances() class FuncGenerator(CtypesGenerator): PARSER = func_parser.WinFuncParser diff --git a/windows/generated_def/winfuncs.py b/windows/generated_def/winfuncs.py index 5f6eeb5..ff7ab88 100644 --- a/windows/generated_def/winfuncs.py +++ b/windows/generated_def/winfuncs.py @@ -6,7 +6,7 @@ from ctypes.wintypes import * from winstructs import * -functions = ['ExitProcess', 'TerminateProcess', 'GetLastError', 'GetCurrentProcess', 'CreateFileA', 'CreateFileW', 'NtCreateFile', 'LdrLoadDll', 'NtQuerySystemInformation', 'NtQueryInformationProcess', 'NtQueryVirtualMemory', 'NtCreateThreadEx', 'NtQueryInformationThread', 'GetExitCodeThread', 'GetExitCodeProcess', 'VirtualAlloc', 'VirtualAllocEx', 'NtProtectVirtualMemory', 'VirtualFree', 'VirtualFreeEx', 'VirtualProtect', 'VirtualProtectEx', 'VirtualQuery', 'VirtualQueryEx', 'QueryWorkingSet', 'QueryWorkingSetEx', 'GetModuleFileNameA', 'GetModuleFileNameW', 'CreateThread', 'CreateRemoteThread', 'VirtualProtect', 'CreateProcessA', 'CreateProcessW', 'GetThreadContext', 'NtGetContextThread', 'SetThreadContext', 'NtSetContextThread', 'OpenThread', 'OpenProcess', 'CloseHandle', 'ReadProcessMemory', 'NtWow64ReadVirtualMemory64', 'WriteProcessMemory', 'NtWow64WriteVirtualMemory64', 'CreateToolhelp32Snapshot', 'Thread32First', 'Thread32Next', 'Process32First', 'Process32Next', 'Process32FirstW', 'Process32NextW', 'GetProcAddress', 'LoadLibraryA', 'LoadLibraryW', 'OpenProcessToken', 'LookupPrivilegeValueA', 'LookupPrivilegeValueW', 'AdjustTokenPrivileges', 'FindResourceA', 'FindResourceW', 'SizeofResource', 'LoadResource', 'LockResource', 'GetVersionExA', 'GetVersionExW', 'GetVersion', 'GetCurrentThread', 'GetCurrentThreadId', 'GetCurrentProcessorNumber', 'AllocConsole', 'FreeConsole', 'GetStdHandle', 'SetStdHandle', 'SetThreadAffinityMask', 'ReadFile', 'WriteFile', 'GetExtendedTcpTable', 'GetExtendedUdpTable', 'SetTcpEntry', 'AddVectoredContinueHandler', 'AddVectoredExceptionHandler', 'TerminateThread', 'ExitThread', 'RemoveVectoredExceptionHandler', 'ResumeThread', 'SuspendThread', 'WaitForSingleObject', 'GetThreadId', 'LoadLibraryExA', 'LoadLibraryExW', 'SymInitialize', 'SymFromName', 'SymLoadModuleEx', 'SymSetOptions', 'SymGetTypeInfo', 'DeviceIoControl', 'GetTokenInformation', 'RegOpenKeyExA', 'RegOpenKeyExW', 'RegGetValueA', 'RegGetValueW', 'RegCloseKey', 'Wow64DisableWow64FsRedirection', 'Wow64RevertWow64FsRedirection', 'Wow64EnableWow64FsRedirection', 'Wow64GetThreadContext', 'SetConsoleCtrlHandler', 'WinVerifyTrust', 'GlobalAlloc', 'GlobalFree', 'GlobalUnlock', 'GlobalLock', 'OpenClipboard', 'EmptyClipboard', 'CloseClipboard', 'SetClipboardData', 'GetClipboardData', 'EnumClipboardFormats', 'GetClipboardFormatNameA', 'GetClipboardFormatNameW', 'WinVerifyTrust', 'OpenProcessToken', 'OpenThreadToken', 'GetTokenInformation', 'SetTokenInformation', 'GetSidIdentifierAuthority', 'GetSidSubAuthority', 'GetSidSubAuthorityCount', 'DebugBreak', 'WaitForDebugEvent', 'ContinueDebugEvent', 'DebugActiveProcess', 'DebugActiveProcessStop', 'DebugSetProcessKillOnExit', 'DebugBreakProcess', 'GetProcessId', 'Wow64SetThreadContext', 'GetMappedFileNameW', 'GetMappedFileNameA', 'RtlInitString', 'RtlInitUnicodeString', 'RtlAnsiStringToUnicodeString', 'OpenEventA', 'OpenEventW', 'NtOpenEvent', 'NtAlpcCreatePort', 'NtAlpcConnectPort', 'NtAlpcConnectPortEx', 'NtAlpcAcceptConnectPort', 'AlpcInitializeMessageAttribute', 'AlpcGetMessageAttribute', 'NtAlpcSendWaitReceivePort', 'lstrcmpA', 'lstrcmpW', 'CreateFileMappingA', 'CreateFileMappingW', 'MapViewOfFile', 'OpenSCManagerA', 'OpenSCManagerW', 'EnumServicesStatusExA', 'EnumServicesStatusExW', 'EnumWindows', 'GetWindowTextA', 'GetWindowTextW', 'GetWindowModuleFileNameA', 'GetWindowModuleFileNameW', 'CryptCATAdminCalcHashFromFileHandle', 'CryptCATAdminEnumCatalogFromHash', 'CryptCATAdminAcquireContext', 'CryptCATCatalogInfoFromContext', 'CryptCATAdminReleaseCatalogContext', 'CryptCATAdminReleaseContext', 'GetLogicalDriveStringsA', 'GetLogicalDriveStringsW', 'GetVolumeInformationA', 'GetVolumeInformationW', 'GetVolumeNameForVolumeMountPointA', 'GetVolumeNameForVolumeMountPointW', 'GetDriveTypeA', 'GetDriveTypeW', 'QueryDosDeviceA', 'QueryDosDeviceW', 'NtQueryObject', 'DuplicateHandle', 'GetModuleBaseNameA', 'GetModuleBaseNameW', 'GetProcessImageFileNameA', 'GetProcessImageFileNameW', 'GetFileVersionInfoA', 'GetFileVersionInfoW', 'GetFileVersionInfoSizeA', 'GetFileVersionInfoSizeW', 'VerQueryValueA', 'VerQueryValueW', 'GetSystemMetrics', 'GetComputerNameA', 'GetComputerNameW', 'LookupAccountSidA', 'LookupAccountSidW', 'CoInitializeEx', 'CoInitializeSecurity', 'CoCreateInstance', 'GetInterfaceInfo', 'GetIfTable', 'GetIpAddrTable', 'NtOpenDirectoryObject', 'NtQueryDirectoryObject', 'NtQuerySymbolicLinkObject', 'NtOpenSymbolicLinkObject', 'GetProcessTimes', 'GetShortPathNameA', 'GetShortPathNameW', 'GetLongPathNameA', 'GetLongPathNameW', 'GetProcessDEPPolicy', 'CryptQueryObject', 'CryptMsgGetParam', 'CryptDecodeObject', 'CertFindCertificateInStore', 'CertGetNameStringA', 'CertGetNameStringW', 'CertGetCertificateChain'] +functions = ['ExitProcess', 'TerminateProcess', 'GetLastError', 'GetCurrentProcess', 'CreateFileA', 'CreateFileW', 'NtCreateFile', 'LdrLoadDll', 'NtQuerySystemInformation', 'NtQueryInformationProcess', 'NtQueryVirtualMemory', 'NtCreateThreadEx', 'NtQueryInformationThread', 'GetExitCodeThread', 'GetExitCodeProcess', 'VirtualAlloc', 'VirtualAllocEx', 'NtProtectVirtualMemory', 'VirtualFree', 'VirtualFreeEx', 'VirtualProtect', 'VirtualProtectEx', 'VirtualQuery', 'VirtualQueryEx', 'QueryWorkingSet', 'QueryWorkingSetEx', 'GetModuleFileNameA', 'GetModuleFileNameW', 'CreateThread', 'CreateRemoteThread', 'VirtualProtect', 'CreateProcessA', 'CreateProcessW', 'GetThreadContext', 'NtGetContextThread', 'SetThreadContext', 'NtSetContextThread', 'OpenThread', 'OpenProcess', 'CloseHandle', 'ReadProcessMemory', 'NtWow64ReadVirtualMemory64', 'WriteProcessMemory', 'NtWow64WriteVirtualMemory64', 'CreateToolhelp32Snapshot', 'Thread32First', 'Thread32Next', 'Process32First', 'Process32Next', 'Process32FirstW', 'Process32NextW', 'GetProcAddress', 'LoadLibraryA', 'LoadLibraryW', 'OpenProcessToken', 'LookupPrivilegeValueA', 'LookupPrivilegeValueW', 'AdjustTokenPrivileges', 'FindResourceA', 'FindResourceW', 'SizeofResource', 'LoadResource', 'LockResource', 'GetVersionExA', 'GetVersionExW', 'GetVersion', 'GetCurrentThread', 'GetCurrentThreadId', 'GetCurrentProcessorNumber', 'AllocConsole', 'FreeConsole', 'GetStdHandle', 'SetStdHandle', 'SetThreadAffinityMask', 'ReadFile', 'WriteFile', 'GetExtendedTcpTable', 'GetExtendedUdpTable', 'SetTcpEntry', 'AddVectoredContinueHandler', 'AddVectoredExceptionHandler', 'TerminateThread', 'ExitThread', 'RemoveVectoredExceptionHandler', 'ResumeThread', 'SuspendThread', 'WaitForSingleObject', 'GetThreadId', 'LoadLibraryExA', 'LoadLibraryExW', 'SymInitialize', 'SymFromName', 'SymLoadModuleEx', 'SymSetOptions', 'SymGetTypeInfo', 'DeviceIoControl', 'GetTokenInformation', 'RegOpenKeyExA', 'RegOpenKeyExW', 'RegGetValueA', 'RegGetValueW', 'RegCloseKey', 'Wow64DisableWow64FsRedirection', 'Wow64RevertWow64FsRedirection', 'Wow64EnableWow64FsRedirection', 'Wow64GetThreadContext', 'SetConsoleCtrlHandler', 'WinVerifyTrust', 'GlobalAlloc', 'GlobalFree', 'GlobalUnlock', 'GlobalLock', 'OpenClipboard', 'EmptyClipboard', 'CloseClipboard', 'SetClipboardData', 'GetClipboardData', 'EnumClipboardFormats', 'GetClipboardFormatNameA', 'GetClipboardFormatNameW', 'WinVerifyTrust', 'OpenProcessToken', 'OpenThreadToken', 'GetTokenInformation', 'SetTokenInformation', 'GetSidIdentifierAuthority', 'GetSidSubAuthority', 'GetSidSubAuthorityCount', 'DebugBreak', 'WaitForDebugEvent', 'ContinueDebugEvent', 'DebugActiveProcess', 'DebugActiveProcessStop', 'DebugSetProcessKillOnExit', 'DebugBreakProcess', 'GetProcessId', 'Wow64SetThreadContext', 'GetMappedFileNameW', 'GetMappedFileNameA', 'RtlInitString', 'RtlInitUnicodeString', 'RtlAnsiStringToUnicodeString', 'OpenEventA', 'OpenEventW', 'NtOpenEvent', 'NtAlpcCreatePort', 'NtAlpcConnectPort', 'NtAlpcConnectPortEx', 'NtAlpcAcceptConnectPort', 'AlpcInitializeMessageAttribute', 'AlpcGetMessageAttribute', 'NtAlpcSendWaitReceivePort', 'lstrcmpA', 'lstrcmpW', 'CreateFileMappingA', 'CreateFileMappingW', 'MapViewOfFile', 'OpenSCManagerA', 'OpenSCManagerW', 'EnumServicesStatusExA', 'EnumServicesStatusExW', 'EnumWindows', 'GetWindowTextA', 'GetWindowTextW', 'GetWindowModuleFileNameA', 'GetWindowModuleFileNameW', 'CryptCATAdminCalcHashFromFileHandle', 'CryptCATAdminEnumCatalogFromHash', 'CryptCATAdminAcquireContext', 'CryptCATCatalogInfoFromContext', 'CryptCATAdminReleaseCatalogContext', 'CryptCATAdminReleaseContext', 'GetLogicalDriveStringsA', 'GetLogicalDriveStringsW', 'GetVolumeInformationA', 'GetVolumeInformationW', 'GetVolumeNameForVolumeMountPointA', 'GetVolumeNameForVolumeMountPointW', 'GetDriveTypeA', 'GetDriveTypeW', 'QueryDosDeviceA', 'QueryDosDeviceW', 'NtQueryObject', 'DuplicateHandle', 'GetModuleBaseNameA', 'GetModuleBaseNameW', 'GetProcessImageFileNameA', 'GetProcessImageFileNameW', 'GetFileVersionInfoA', 'GetFileVersionInfoW', 'GetFileVersionInfoSizeA', 'GetFileVersionInfoSizeW', 'VerQueryValueA', 'VerQueryValueW', 'GetSystemMetrics', 'GetComputerNameA', 'GetComputerNameW', 'LookupAccountSidA', 'LookupAccountSidW', 'CoInitializeEx', 'CoInitializeSecurity', 'CoCreateInstance', 'GetInterfaceInfo', 'GetIfTable', 'GetIpAddrTable', 'NtOpenDirectoryObject', 'NtQueryDirectoryObject', 'NtQuerySymbolicLinkObject', 'NtOpenSymbolicLinkObject', 'GetProcessTimes', 'GetShortPathNameA', 'GetShortPathNameW', 'GetLongPathNameA', 'GetLongPathNameW', 'GetProcessDEPPolicy', 'CryptQueryObject', 'CryptMsgGetParam', 'CryptDecodeObject', 'CertFindCertificateInStore', 'CertGetNameStringA', 'CertGetNameStringW', 'CertGetCertificateChain', 'CertCreateSelfSignCertificate', 'CertStrToNameA', 'CertStrToNameW', 'CertOpenStore', 'CertAddCertificateContextToStore', 'PFXExportCertStoreEx', 'PFXImportCertStore', 'CryptGenKey', 'CryptAcquireContextA', 'CryptAcquireContextW', 'CryptExportKey', 'CertGetCertificateContextProperty', 'CertEnumCertificateContextProperties'] #def ExitProcess(uExitCode): @@ -1086,35 +1086,100 @@ GetProcessDEPPolicyParams = ((1, 'hProcess'), (1, 'lpFlags'), (1, 'lpPermanent') #def CryptQueryObject(dwObjectType, pvObject, dwExpectedContentTypeFlags, dwExpectedFormatTypeFlags, dwFlags, pdwMsgAndCertEncodingType, pdwContentType, pdwFormatType, phCertStore, phMsg, ppvContext): # return CryptQueryObject.ctypes_function(dwObjectType, pvObject, dwExpectedContentTypeFlags, dwExpectedFormatTypeFlags, dwFlags, pdwMsgAndCertEncodingType, pdwContentType, pdwFormatType, phCertStore, phMsg, ppvContext) -CryptQueryObjectPrototype = WINFUNCTYPE(BOOL, DWORD, PVOID, DWORD, DWORD, DWORD, POINTER(DWORD), POINTER(DWORD), POINTER(DWORD), POINTER(HCERTSTORE), POINTER(HCRYPTMSG), POINTER(PVOID)) +CryptQueryObjectPrototype = WINFUNCTYPE(BOOL, DWORD, POINTER(VOID), DWORD, DWORD, DWORD, POINTER(DWORD), POINTER(DWORD), POINTER(DWORD), POINTER(HCERTSTORE), POINTER(HCRYPTMSG), POINTER(PVOID)) CryptQueryObjectParams = ((1, 'dwObjectType'), (1, 'pvObject'), (1, 'dwExpectedContentTypeFlags'), (1, 'dwExpectedFormatTypeFlags'), (1, 'dwFlags'), (1, 'pdwMsgAndCertEncodingType'), (1, 'pdwContentType'), (1, 'pdwFormatType'), (1, 'phCertStore'), (1, 'phMsg'), (1, 'ppvContext')) #def CryptMsgGetParam(hCryptMsg, dwParamType, dwIndex, pvData, pcbData): # return CryptMsgGetParam.ctypes_function(hCryptMsg, dwParamType, dwIndex, pvData, pcbData) -CryptMsgGetParamPrototype = WINFUNCTYPE(BOOL, HCRYPTMSG, DWORD, DWORD, PVOID, POINTER(DWORD)) +CryptMsgGetParamPrototype = WINFUNCTYPE(BOOL, HCRYPTMSG, DWORD, DWORD, POINTER(VOID), POINTER(DWORD)) CryptMsgGetParamParams = ((1, 'hCryptMsg'), (1, 'dwParamType'), (1, 'dwIndex'), (1, 'pvData'), (1, 'pcbData')) #def CryptDecodeObject(dwCertEncodingType, lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo): # return CryptDecodeObject.ctypes_function(dwCertEncodingType, lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, pcbStructInfo) -CryptDecodeObjectPrototype = WINFUNCTYPE(BOOL, DWORD, LPCSTR, POINTER(BYTE), DWORD, DWORD, PVOID, POINTER(DWORD)) +CryptDecodeObjectPrototype = WINFUNCTYPE(BOOL, DWORD, LPCSTR, POINTER(BYTE), DWORD, DWORD, POINTER(VOID), POINTER(DWORD)) CryptDecodeObjectParams = ((1, 'dwCertEncodingType'), (1, 'lpszStructType'), (1, 'pbEncoded'), (1, 'cbEncoded'), (1, 'dwFlags'), (1, 'pvStructInfo'), (1, 'pcbStructInfo')) #def CertFindCertificateInStore(hCertStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCertContext): # return CertFindCertificateInStore.ctypes_function(hCertStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCertContext) -CertFindCertificateInStorePrototype = WINFUNCTYPE(PCCERT_CONTEXT, HCERTSTORE, DWORD, DWORD, DWORD, PVOID, PCCERT_CONTEXT) +CertFindCertificateInStorePrototype = WINFUNCTYPE(PCCERT_CONTEXT, HCERTSTORE, DWORD, DWORD, DWORD, POINTER(VOID), PCCERT_CONTEXT) CertFindCertificateInStoreParams = ((1, 'hCertStore'), (1, 'dwCertEncodingType'), (1, 'dwFindFlags'), (1, 'dwFindType'), (1, 'pvFindPara'), (1, 'pPrevCertContext')) #def CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, pszNameString, cchNameString): # return CertGetNameStringA.ctypes_function(pCertContext, dwType, dwFlags, pvTypePara, pszNameString, cchNameString) -CertGetNameStringAPrototype = WINFUNCTYPE(DWORD, PCCERT_CONTEXT, DWORD, DWORD, PVOID, LPCSTR, DWORD) +CertGetNameStringAPrototype = WINFUNCTYPE(DWORD, PCCERT_CONTEXT, DWORD, DWORD, POINTER(VOID), LPCSTR, DWORD) CertGetNameStringAParams = ((1, 'pCertContext'), (1, 'dwType'), (1, 'dwFlags'), (1, 'pvTypePara'), (1, 'pszNameString'), (1, 'cchNameString')) #def CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, pszNameString, cchNameString): # return CertGetNameStringW.ctypes_function(pCertContext, dwType, dwFlags, pvTypePara, pszNameString, cchNameString) -CertGetNameStringWPrototype = WINFUNCTYPE(DWORD, PCCERT_CONTEXT, DWORD, DWORD, PVOID, LPWSTR, DWORD) +CertGetNameStringWPrototype = WINFUNCTYPE(DWORD, PCCERT_CONTEXT, DWORD, DWORD, POINTER(VOID), LPWSTR, DWORD) CertGetNameStringWParams = ((1, 'pCertContext'), (1, 'dwType'), (1, 'dwFlags'), (1, 'pvTypePara'), (1, 'pszNameString'), (1, 'cchNameString')) #def CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, ppChainContext): # return CertGetCertificateChain.ctypes_function(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, ppChainContext) CertGetCertificateChainPrototype = WINFUNCTYPE(BOOL, HCERTCHAINENGINE, PCCERT_CONTEXT, LPFILETIME, HCERTSTORE, PCERT_CHAIN_PARA, DWORD, LPVOID, POINTER(PCCERT_CHAIN_CONTEXT)) CertGetCertificateChainParams = ((1, 'hChainEngine'), (1, 'pCertContext'), (1, 'pTime'), (1, 'hAdditionalStore'), (1, 'pChainPara'), (1, 'dwFlags'), (1, 'pvReserved'), (1, 'ppChainContext')) + +#def CertCreateSelfSignCertificate(hCryptProvOrNCryptKey, pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime, pEndTime, pExtensions): +# return CertCreateSelfSignCertificate.ctypes_function(hCryptProvOrNCryptKey, pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime, pEndTime, pExtensions) +CertCreateSelfSignCertificatePrototype = WINFUNCTYPE(PCCERT_CONTEXT, HCRYPTPROV_OR_NCRYPT_KEY_HANDLE, PCERT_NAME_BLOB, DWORD, PCRYPT_KEY_PROV_INFO, PCRYPT_ALGORITHM_IDENTIFIER, PSYSTEMTIME, PSYSTEMTIME, PCERT_EXTENSIONS) +CertCreateSelfSignCertificateParams = ((1, 'hCryptProvOrNCryptKey'), (1, 'pSubjectIssuerBlob'), (1, 'dwFlags'), (1, 'pKeyProvInfo'), (1, 'pSignatureAlgorithm'), (1, 'pStartTime'), (1, 'pEndTime'), (1, 'pExtensions')) + +#def CertStrToNameA(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError): +# return CertStrToNameA.ctypes_function(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError) +CertStrToNameAPrototype = WINFUNCTYPE(BOOL, DWORD, LPCSTR, DWORD, POINTER(VOID), POINTER(BYTE), POINTER(DWORD), POINTER(LPCSTR)) +CertStrToNameAParams = ((1, 'dwCertEncodingType'), (1, 'pszX500'), (1, 'dwStrType'), (1, 'pvReserved'), (1, 'pbEncoded'), (1, 'pcbEncoded'), (1, 'ppszError')) + +#def CertStrToNameW(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError): +# return CertStrToNameW.ctypes_function(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError) +CertStrToNameWPrototype = WINFUNCTYPE(BOOL, DWORD, LPWSTR, DWORD, POINTER(VOID), POINTER(BYTE), POINTER(DWORD), POINTER(LPWSTR)) +CertStrToNameWParams = ((1, 'dwCertEncodingType'), (1, 'pszX500'), (1, 'dwStrType'), (1, 'pvReserved'), (1, 'pbEncoded'), (1, 'pcbEncoded'), (1, 'ppszError')) + +#def CertOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara): +# return CertOpenStore.ctypes_function(lpszStoreProvider, dwMsgAndCertEncodingType, hCryptProv, dwFlags, pvPara) +CertOpenStorePrototype = WINFUNCTYPE(HCERTSTORE, LPCSTR, DWORD, HCRYPTPROV_LEGACY, DWORD, POINTER(VOID)) +CertOpenStoreParams = ((1, 'lpszStoreProvider'), (1, 'dwMsgAndCertEncodingType'), (1, 'hCryptProv'), (1, 'dwFlags'), (1, 'pvPara')) + +#def CertAddCertificateContextToStore(hCertStore, pCertContext, dwAddDisposition, ppStoreContext): +# return CertAddCertificateContextToStore.ctypes_function(hCertStore, pCertContext, dwAddDisposition, ppStoreContext) +CertAddCertificateContextToStorePrototype = WINFUNCTYPE(BOOL, HCERTSTORE, PCCERT_CONTEXT, DWORD, POINTER(PCCERT_CONTEXT)) +CertAddCertificateContextToStoreParams = ((1, 'hCertStore'), (1, 'pCertContext'), (1, 'dwAddDisposition'), (1, 'ppStoreContext')) + +#def PFXExportCertStoreEx(hStore, pPFX, szPassword, pvPara, dwFlags): +# return PFXExportCertStoreEx.ctypes_function(hStore, pPFX, szPassword, pvPara, dwFlags) +PFXExportCertStoreExPrototype = WINFUNCTYPE(BOOL, HCERTSTORE, POINTER(CRYPT_DATA_BLOB), LPCWSTR, POINTER(VOID), DWORD) +PFXExportCertStoreExParams = ((1, 'hStore'), (1, 'pPFX'), (1, 'szPassword'), (1, 'pvPara'), (1, 'dwFlags')) + +#def PFXImportCertStore(pPFX, szPassword, dwFlags): +# return PFXImportCertStore.ctypes_function(pPFX, szPassword, dwFlags) +PFXImportCertStorePrototype = WINFUNCTYPE(HCERTSTORE, POINTER(CRYPT_DATA_BLOB), LPCWSTR, DWORD) +PFXImportCertStoreParams = ((1, 'pPFX'), (1, 'szPassword'), (1, 'dwFlags')) + +#def CryptGenKey(hProv, Algid, dwFlags, phKey): +# return CryptGenKey.ctypes_function(hProv, Algid, dwFlags, phKey) +CryptGenKeyPrototype = WINFUNCTYPE(BOOL, HCRYPTPROV, ALG_ID, DWORD, POINTER(HCRYPTKEY)) +CryptGenKeyParams = ((1, 'hProv'), (1, 'Algid'), (1, 'dwFlags'), (1, 'phKey')) + +#def CryptAcquireContextA(phProv, pszContainer, pszProvider, dwProvType, dwFlags): +# return CryptAcquireContextA.ctypes_function(phProv, pszContainer, pszProvider, dwProvType, dwFlags) +CryptAcquireContextAPrototype = WINFUNCTYPE(BOOL, POINTER(HCRYPTPROV), LPCSTR, LPCSTR, DWORD, DWORD) +CryptAcquireContextAParams = ((1, 'phProv'), (1, 'pszContainer'), (1, 'pszProvider'), (1, 'dwProvType'), (1, 'dwFlags')) + +#def CryptAcquireContextW(phProv, pszContainer, pszProvider, dwProvType, dwFlags): +# return CryptAcquireContextW.ctypes_function(phProv, pszContainer, pszProvider, dwProvType, dwFlags) +CryptAcquireContextWPrototype = WINFUNCTYPE(BOOL, POINTER(HCRYPTPROV), LPWSTR, LPWSTR, DWORD, DWORD) +CryptAcquireContextWParams = ((1, 'phProv'), (1, 'pszContainer'), (1, 'pszProvider'), (1, 'dwProvType'), (1, 'dwFlags')) + +#def CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen): +# return CryptExportKey.ctypes_function(hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen) +CryptExportKeyPrototype = WINFUNCTYPE(BOOL, HCRYPTKEY, HCRYPTKEY, DWORD, DWORD, POINTER(BYTE), POINTER(DWORD)) +CryptExportKeyParams = ((1, 'hKey'), (1, 'hExpKey'), (1, 'dwBlobType'), (1, 'dwFlags'), (1, 'pbData'), (1, 'pdwDataLen')) + +#def CertGetCertificateContextProperty(pCertContext, dwPropId, pvData, pcbData): +# return CertGetCertificateContextProperty.ctypes_function(pCertContext, dwPropId, pvData, pcbData) +CertGetCertificateContextPropertyPrototype = WINFUNCTYPE(BOOL, PCCERT_CONTEXT, DWORD, POINTER(VOID), POINTER(DWORD)) +CertGetCertificateContextPropertyParams = ((1, 'pCertContext'), (1, 'dwPropId'), (1, 'pvData'), (1, 'pcbData')) + +#def CertEnumCertificateContextProperties(pCertContext, dwPropId): +# return CertEnumCertificateContextProperties.ctypes_function(pCertContext, dwPropId) +CertEnumCertificateContextPropertiesPrototype = WINFUNCTYPE(DWORD, PCCERT_CONTEXT, DWORD) +CertEnumCertificateContextPropertiesParams = ((1, 'pCertContext'), (1, 'dwPropId')) diff --git a/windows/generated_def/winstructs.py b/windows/generated_def/winstructs.py index f272607..9b8654f 100644 --- a/windows/generated_def/winstructs.py +++ b/windows/generated_def/winstructs.py @@ -4,6 +4,7 @@ from ctypes import * from ctypes.wintypes import * from windef import * +BYTE = c_ubyte PWSTR = LPWSTR PCWSTR = LPWSTR SIZE_T = c_ulong @@ -66,6 +67,12 @@ PHKEY = POINTER(HKEY) ACCESS_MASK = DWORD REGSAM = ACCESS_MASK SECURITY_CONTEXT_TRACKING_MODE = BOOLEAN +HCRYPTPROV_OR_NCRYPT_KEY_HANDLE = PULONG +HCRYPTPROV_LEGACY = PULONG +HCRYPTKEY = PULONG +HCRYPTPROV = PULONG +HCRYPTHASH = PULONG +ALG_ID = UINT DISPID = LONG MEMBERID = DISPID PSECURITY_DESCRIPTOR = PVOID @@ -3374,6 +3381,51 @@ class _CERT_CHAIN_ENGINE_CONFIG(Structure): CERT_CHAIN_ENGINE_CONFIG = _CERT_CHAIN_ENGINE_CONFIG PCERT_CHAIN_ENGINE_CONFIG = POINTER(_CERT_CHAIN_ENGINE_CONFIG) +class _SYSTEMTIME(Structure): + _fields_ = [ + ("wYear", WORD), + ("wMonth", WORD), + ("wDayOfWeek", WORD), + ("wDay", WORD), + ("wHour", WORD), + ("wMinute", WORD), + ("wSecond", WORD), + ("wMilliseconds", WORD), + ] +SYSTEMTIME = _SYSTEMTIME +PSYSTEMTIME = POINTER(_SYSTEMTIME) + +class _CERT_EXTENSIONS(Structure): + _fields_ = [ + ("cExtension", DWORD), + ("rgExtension", PCERT_EXTENSION), + ] +PCERT_EXTENSIONS = POINTER(_CERT_EXTENSIONS) +CERT_EXTENSIONS = _CERT_EXTENSIONS + +class _CRYPT_KEY_PROV_PARAM(Structure): + _fields_ = [ + ("dwParam", DWORD), + ("pbData", POINTER(BYTE)), + ("cbData", DWORD), + ("dwFlags", DWORD), + ] +CRYPT_KEY_PROV_PARAM = _CRYPT_KEY_PROV_PARAM +PCRYPT_KEY_PROV_PARAM = POINTER(_CRYPT_KEY_PROV_PARAM) + +class _CRYPT_KEY_PROV_INFO(Structure): + _fields_ = [ + ("pwszContainerName", LPWSTR), + ("pwszProvName", LPWSTR), + ("dwProvType", DWORD), + ("dwFlags", DWORD), + ("cProvParam", DWORD), + ("rgProvParam", PCRYPT_KEY_PROV_PARAM), + ("dwKeySpec", DWORD), + ] +CRYPT_KEY_PROV_INFO = _CRYPT_KEY_PROV_INFO +PCRYPT_KEY_PROV_INFO = POINTER(_CRYPT_KEY_PROV_INFO) + class tagRECT(Structure): _fields_ = [ ("left", LONG), diff --git a/windows/winproxy.py b/windows/winproxy.py index 531a39e..f6af9f2 100644 --- a/windows/winproxy.py +++ b/windows/winproxy.py @@ -69,6 +69,8 @@ def kernel32_error_check(func_name, result, func, args): raise Kernel32Error(func_name) return args +null_is_fail_error_check = kernel32_error_check + def kernel32_zero_check(func_name, result, func, args): """raise Kernel32Error if result is NOT 0""" @@ -188,6 +190,10 @@ class VersionProxy(ApiProxy): APIDLL = "version" default_error_check = staticmethod(kernel32_error_check) +class Crypt32Proxy(ApiProxy): + APIDLL = "crypt32" + default_error_check = staticmethod(null_is_fail_error_check) + #class OptionalExport(object): # """used 'around' a Proxy decorator # Should be used for export that are not available everywhere (ntdll internals | 32/64 bits stuff) @@ -991,6 +997,22 @@ def CryptCATAdminReleaseContext(hCatAdmin, dwFlags): return CryptCATAdminReleaseContext.ctypes_function(hCatAdmin, dwFlags) +## Crypto API ## +@Crypt32Proxy('CertStrToNameA') +def CertStrToNameA(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError): + return CertStrToNameA.ctypes_function(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError) + + +@Crypt32Proxy('CertStrToNameW') +def CertStrToNameW(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError): + return CertStrToNameW.ctypes_function(dwCertEncodingType, pszX500, dwStrType, pvReserved, pbEncoded, pcbEncoded, ppszError) + + +@Crypt32Proxy('CertCreateSelfSignCertificate') +def CertCreateSelfSignCertificate(hCryptProvOrNCryptKey, pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime, pEndTime, pExtensions): + return CertCreateSelfSignCertificate.ctypes_function(hCryptProvOrNCryptKey, pSubjectIssuerBlob, dwFlags, pKeyProvInfo, pSignatureAlgorithm, pStartTime, pEndTime, pExtensions) + + # ## User32 stuff ## # EnumWindows = TransparentUser32Proxy('EnumWindows')