From 10a6e7705fd584bfcbf9bcbabb3189323ff600c9 Mon Sep 17 00:00:00 2001 From: Petr Benes Date: Mon, 15 Oct 2018 23:16:51 +0200 Subject: [PATCH] add ARM & ARM64 support --- DetoursNT.sln | 28 +++++ DetoursNT/DetoursNT.cpp | 26 +++-- DetoursNT/DetoursNT.h | 9 ++ DetoursNT/DetoursNT.vcxproj | 166 ++++++++++++++++++++++++++- README.md | 2 +- Sample/Sample.vcxproj | 148 +++++++++++++++++++++++- SampleHookDLL/SampleHookDLL.vcxproj | 170 +++++++++++++++++++++++++++- 7 files changed, 533 insertions(+), 16 deletions(-) diff --git a/DetoursNT.sln b/DetoursNT.sln index b35bb68..cbf79ba 100644 --- a/DetoursNT.sln +++ b/DetoursNT.sln @@ -11,32 +11,60 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sample", "Sample\Sample.vcx EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|ARM = Debug|ARM + Debug|ARM64 = Debug|ARM64 Debug|x64 = Debug|x64 Debug|x86 = Debug|x86 + Release|ARM = Release|ARM + Release|ARM64 = Release|ARM64 Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|ARM.ActiveCfg = Debug|ARM + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|ARM.Build.0 = Debug|ARM + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|ARM64.Build.0 = Debug|ARM64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|x64.ActiveCfg = Debug|x64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|x64.Build.0 = Debug|x64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|x86.ActiveCfg = Debug|Win32 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Debug|x86.Build.0 = Debug|Win32 + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|ARM.ActiveCfg = Release|ARM + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|ARM.Build.0 = Release|ARM + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|ARM64.ActiveCfg = Release|ARM64 + {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|ARM64.Build.0 = Release|ARM64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|x64.ActiveCfg = Release|x64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|x64.Build.0 = Release|x64 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|x86.ActiveCfg = Release|Win32 {C78B9003-FC49-4BBF-8F29-52FAD48BB58A}.Release|x86.Build.0 = Release|Win32 + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|ARM.ActiveCfg = Debug|ARM + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|ARM.Build.0 = Debug|ARM + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|ARM64.Build.0 = Debug|ARM64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|x64.ActiveCfg = Debug|x64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|x64.Build.0 = Debug|x64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|x86.ActiveCfg = Debug|Win32 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Debug|x86.Build.0 = Debug|Win32 + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|ARM.ActiveCfg = Release|ARM + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|ARM.Build.0 = Release|ARM + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|ARM64.ActiveCfg = Release|ARM64 + {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|ARM64.Build.0 = Release|ARM64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|x64.ActiveCfg = Release|x64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|x64.Build.0 = Release|x64 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|x86.ActiveCfg = Release|Win32 {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B}.Release|x86.Build.0 = Release|Win32 + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|ARM.ActiveCfg = Debug|ARM + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|ARM.Build.0 = Debug|ARM + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|ARM64.Build.0 = Debug|ARM64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|x64.ActiveCfg = Debug|x64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|x64.Build.0 = Debug|x64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|x86.ActiveCfg = Debug|Win32 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Debug|x86.Build.0 = Debug|Win32 + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|ARM.ActiveCfg = Release|ARM + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|ARM.Build.0 = Release|ARM + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|ARM64.ActiveCfg = Release|ARM64 + {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|ARM64.Build.0 = Release|ARM64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|x64.ActiveCfg = Release|x64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|x64.Build.0 = Release|x64 {113B23FE-32D7-4D79-8A74-119BC11C25E2}.Release|x86.ActiveCfg = Release|Win32 diff --git a/DetoursNT/DetoursNT.cpp b/DetoursNT/DetoursNT.cpp index dbf3fd3..1445a31 100644 --- a/DetoursNT/DetoursNT.cpp +++ b/DetoursNT/DetoursNT.cpp @@ -1,8 +1,17 @@ +// +// Workaround for VS2017 to force this library to +// be compilable on ARM. +// + #define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1 #include #include "DetoursNT.h" +// +// Exception handling. +// + // #define DETOURSNT_NO_EH #ifdef __cplusplus @@ -189,7 +198,7 @@ LdrGetProcedureAddress( OUT PVOID *ProcedureAddress ); -#ifdef _M_AMD64 +#if defined(_M_AMD64) || defined(_M_ARM64) || defined(_M_ARM) EXCEPTION_DISPOSITION __cdecl @@ -201,14 +210,7 @@ __C_specific_handler( ) { #if 0 - typedef EXCEPTION_DISPOSITION (__cdecl * pfn__C_specific_handler)( - _In_ struct _EXCEPTION_RECORD* ExceptionRecord, - _In_ void* EstablisherFrame, - _Inout_ struct _CONTEXT* ContextRecord, - _Inout_ struct _DISPATCHER_CONTEXT* DispatcherContext - ); - - static pfn__C_specific_handler Procedure = NULL; + static decltype(__C_specific_handler)* Procedure = nullptr; if (!Procedure) { @@ -235,9 +237,9 @@ __C_specific_handler( #endif } -#endif // _M_IA64 +#endif // defined(_M_AMD64) || defined(_M_ARM64) || defined(_M_ARM) -#if _M_IX86 +#if defined(_M_IX86) int _callnewh( @@ -265,7 +267,7 @@ _except_handler3( return (EXCEPTION_DISPOSITION)0; } -#endif // _M_IX86 +#endif // defined(_M_IX86) #endif // DETOURSNT_NO_EH diff --git a/DetoursNT/DetoursNT.h b/DetoursNT/DetoursNT.h index 8df4030..d849133 100644 --- a/DetoursNT/DetoursNT.h +++ b/DetoursNT/DetoursNT.h @@ -1,5 +1,10 @@ #pragma once +// +// Workaround for VS2017 to force this library to +// be compilable on ARM. +// + #define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1 #include @@ -9,6 +14,10 @@ extern "C" { #pragma region CRT +// +// __ImageBase is automatically provided by the linker. +// + extern IMAGE_DOS_HEADER __ImageBase; #pragma intrinsic(strlen) diff --git a/DetoursNT/DetoursNT.vcxproj b/DetoursNT/DetoursNT.vcxproj index 78ce803..39c0264 100644 --- a/DetoursNT/DetoursNT.vcxproj +++ b/DetoursNT/DetoursNT.vcxproj @@ -1,10 +1,26 @@  + + Debug + ARM + + + Debug + ARM64 + Debug Win32 + + Release + ARM + + + Release + ARM64 + Release Win32 @@ -37,7 +53,7 @@ {C78B9003-FC49-4BBF-8F29-52FAD48BB58A} Win32Proj DetoursNT - 8.1 + 10.0.17763.0 @@ -46,6 +62,12 @@ v141 Unicode + + StaticLibrary + true + v141 + Unicode + StaticLibrary false @@ -53,12 +75,25 @@ true Unicode + + StaticLibrary + false + v141 + true + Unicode + StaticLibrary true v141 Unicode + + StaticLibrary + true + v141 + Unicode + StaticLibrary false @@ -66,6 +101,13 @@ true Unicode + + StaticLibrary + false + v141 + true + Unicode + @@ -74,36 +116,68 @@ + + + + + + + + + + + + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + @@ -125,6 +199,27 @@ Default + + + + + Level3 + Disabled + MultiThreadedDebug + false + Default + false + $(ProjectDir)DetoursNT.h + false + true + + + Windows + true + ntdll.lib + Default + + @@ -146,6 +241,27 @@ Default + + + + + Level3 + Disabled + MultiThreadedDebug + false + Default + false + $(ProjectDir)DetoursNT.h + false + true + + + Windows + true + ntdll.lib + Default + + Level3 @@ -170,6 +286,30 @@ ntdll.lib + + + Level3 + + + MaxSpeed + true + true + MultiThreaded + false + $(ProjectDir)DetoursNT.h + false + false + true + + + Windows + true + true + true + Default + ntdll.lib + + Level3 @@ -194,6 +334,30 @@ Default + + + Level3 + + + MaxSpeed + true + true + MultiThreaded + false + $(ProjectDir)DetoursNT.h + false + false + true + + + Windows + true + true + true + ntdll.lib + Default + + diff --git a/README.md b/README.md index d8a8a7f..fd294a6 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ This hooking DLL only hooks `NtTestAlert` function for demonstrative purposes. I - This implementation intentionally crashes on SEH exceptions which occur inside of Detours. This is because SEH handlers are usually located in CRT (which is ommited here). -- Only x86 and x64 is currently supported. +- Supported architectures are: x86, x64, ARM (32-bit) and ARM64. ### License diff --git a/Sample/Sample.vcxproj b/Sample/Sample.vcxproj index 1b0011e..de0358a 100644 --- a/Sample/Sample.vcxproj +++ b/Sample/Sample.vcxproj @@ -1,10 +1,26 @@ + + Debug + ARM + + + Debug + ARM64 + Debug Win32 + + Release + ARM + + + Release + ARM64 + Release Win32 @@ -23,7 +39,7 @@ {113B23FE-32D7-4D79-8A74-119BC11C25E2} Win32Proj Sample - 8.1 + 10.0.17763.0 @@ -32,6 +48,12 @@ v141 Unicode + + Application + true + v141 + Unicode + Application false @@ -39,12 +61,25 @@ true Unicode + + Application + false + v141 + true + Unicode + Application true v141 Unicode + + Application + true + v141 + Unicode + Application false @@ -52,6 +87,13 @@ true Unicode + + Application + false + v141 + true + Unicode + @@ -60,36 +102,68 @@ + + + + + + + + + + + + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + Level3 @@ -106,6 +180,22 @@ ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + + + Console + true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + Level3 @@ -122,6 +212,22 @@ ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + + + Console + true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + Level3 @@ -142,6 +248,26 @@ ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + + + Console + true + true + true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + Level3 @@ -162,6 +288,26 @@ ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + + + Console + true + true + true + ntdll.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + diff --git a/SampleHookDLL/SampleHookDLL.vcxproj b/SampleHookDLL/SampleHookDLL.vcxproj index 58e399f..06631fa 100644 --- a/SampleHookDLL/SampleHookDLL.vcxproj +++ b/SampleHookDLL/SampleHookDLL.vcxproj @@ -1,10 +1,26 @@  + + Debug + ARM + + + Debug + ARM64 + Debug Win32 + + Release + ARM + + + Release + ARM64 + Release Win32 @@ -30,7 +46,7 @@ {6266D1B7-DD83-4A79-B7F3-DA8099BBAB1B} Win32Proj SampleHookDLL - 8.1 + 10.0.17763.0 @@ -39,6 +55,12 @@ v141 Unicode + + DynamicLibrary + true + v141 + Unicode + DynamicLibrary false @@ -46,12 +68,25 @@ true Unicode + + DynamicLibrary + false + v141 + true + Unicode + DynamicLibrary true v141 Unicode + + DynamicLibrary + true + v141 + Unicode + DynamicLibrary false @@ -59,6 +94,13 @@ true Unicode + + DynamicLibrary + false + v141 + true + Unicode + @@ -67,36 +109,68 @@ + + + + + + + + + + + + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + true $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + true + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + false $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + + false + $(SolutionDir)Bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)Bin\Obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\ + @@ -119,6 +193,28 @@ Default + + + + + Level3 + Disabled + MultiThreadedDebug + false + Default + false + false + true + $(SolutionDir)Detours\src;%(AdditionalIncludeDirectories) + + + Windows + true + NtDllMain + ntdll.lib + Default + + @@ -141,6 +237,28 @@ Default + + + + + Level3 + Disabled + MultiThreadedDebug + false + Default + false + false + true + $(SolutionDir)Detours\src;%(AdditionalIncludeDirectories) + + + Windows + true + NtDllMain + ntdll.lib + Default + + Level3 @@ -166,6 +284,31 @@ ntdll.lib + + + Level3 + + + MaxSpeed + true + true + MultiThreaded + false + false + false + true + $(SolutionDir)Detours\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + NtDllMain + Default + ntdll.lib + + Level3 @@ -191,6 +334,31 @@ Default + + + Level3 + + + MaxSpeed + true + true + MultiThreaded + false + false + false + true + $(SolutionDir)Detours\src;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + NtDllMain + ntdll.lib + Default + +