### Install PolyHook 2.0 via vcpkg Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/README.md Installs PolyHook 2.0 using the vcpkg package manager for both x86 and x64 architectures. This method simplifies dependency management and integration. ```shell λ git clone https://github.com/Microsoft/vcpkg.git λ cd vcpkg λ .\bootstrap-vcpkg.bat -disableMetrics λ (as admin) .\vcpkg integrate install For x86: λ vcpkg.exe install polyhook2:x86-windows-static polyhook2:x86-windows For x64: λ vcpkg.exe install polyhook2:x64-windows-static polyhook2:x64-windows ``` -------------------------------- ### Install PolyHook 2.0 Targets Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake command installs the PolyHook 2.0 targets, including runtime, library, and archive components. It specifies the export name for the targets and their respective installation destinations. ```cmake install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets RUNTIME DESTINATION "bin" LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib") ``` -------------------------------- ### Install Core Headers (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Installs the core PolyHook headers to the 'include/polyhook2' directory. ```cmake set(POLYHOOK_CORE_HEADERS ${PROJECT_SOURCE_DIR}/polyhook2/Enums.hpp ${PROJECT_SOURCE_DIR}/polyhook2/IHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Instruction.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Misc.hpp ${PROJECT_SOURCE_DIR}/polyhook2/UID.hpp ${PROJECT_SOURCE_DIR}/polyhook2/ErrorLog.hpp ${PROJECT_SOURCE_DIR}/polyhook2/MemProtector.hpp ${PROJECT_SOURCE_DIR}/polyhook2/MemAccessor.hpp ${PROJECT_SOURCE_DIR}/polyhook2/FBAllocator.hpp ${PROJECT_SOURCE_DIR}/polyhook2/RangeAllocator.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Tests/TestEffectTracker.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Tests/StackCanary.hpp ${PROJECT_SOURCE_DIR}/polyhook2/EventDispatcher.hpp ${PROJECT_SOURCE_DIR}/polyhook2/PolyHookOs.hpp ${PROJECT_SOURCE_DIR}/polyhook2/PolyHookOsIncludes.hpp ) install(FILES ${POLYHOOK_CORE_HEADERS} DESTINATION include/polyhook2) ``` -------------------------------- ### Configure Include Directories (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Sets up include directories for the project, including build interface and installation paths for headers. ```cmake target_include_directories(${PROJECT_NAME} PUBLIC $ INTERFACE $ ) ``` -------------------------------- ### Basic CMake Project Setup and OS Detection Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Initializes the CMake build system, sets the project name, converts it to lowercase, and includes helper modules. It then determines the operating system (Windows, macOS, or Linux) and sets a corresponding variable. ```cmake cmake_minimum_required(VERSION 3.15) project(PolyHook_2) string(TOLOWER ${PROJECT_NAME} LOWERCASE_PROJECT_NAME) include(CMakePackageConfigHelpers) if(WIN32) set(POLYHOOK_OS "windows") elseif(APPLE) set(POLYHOOK_OS "macos") elseif(UNIX) set(POLYHOOK_OS "linux") endif() ``` -------------------------------- ### Install Exported Targets for PolyHook 2.0 Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake command installs the exported targets for the PolyHook 2.0 project. It defines a namespace for the targets and specifies their installation destination within the package configuration. ```cmake install( EXPORT ${PROJECT_NAME}-targets NAMESPACE ${PROJECT_NAME}:: DESTINATION "lib/${PROJECT_NAME}" ) ``` -------------------------------- ### Configure Package Configuration File Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake command configures a package configuration file, generating a version for the PolyHook 2.0 project. It takes an input template and an output file name, specifying the installation destination. ```cmake configure_package_config_file( "${LOWERCASE_PROJECT_NAME}-config.cmake.in" "${LOWERCASE_PROJECT_NAME}-config.cmake" ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${LOWERCASE_PROJECT_NAME}-config.cmake" DESTINATION "lib/${PROJECT_NAME}" ) ``` -------------------------------- ### Build PolyHook 2.0 with Visual Studio CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/README.md Builds PolyHook 2.0 using Visual Studio's CMake integration. Automatically handles building and linking of dependencies like Capstone, Zydis, and asmjit. Does not require running the install target. ```shell # Open VS 2022 # File -> Open -> CMake... # Load the project and start CMake generation. # Then goto CMake -> Build All or CMake -> Build. # Set startup item and release mode to use the play button. ``` -------------------------------- ### Zydis Dependency Handling (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Configures the project to use Zydis, either by finding an external installation or by building it from source. Links Zydis libraries and sets include directories. ```cmake if (POLYHOOK_USE_EXTERNAL_ZYDIS) find_library(ZYDIS_LIBRARY NAMES Zydis) find_library(ZYCORE_LIBRARY NAMES Zycore) find_path(ZYDIS_INCLUDE_DIR NAMES Zydis/Zydis.h) find_path(ZYCORE_INCLUDE_DIR NAMES Zycore/Zycore.h) target_link_libraries(${PROJECT_NAME} PUBLIC ${ZYDIS_LIBRARY}) target_link_libraries(${PROJECT_NAME} PUBLIC ${ZYCORE_LIBRARY}) target_include_directories(${PROJECT_NAME} PUBLIC ${ZYDIS_INCLUDE_DIR}) target_include_directories(${PROJECT_NAME} PUBLIC ${ZYCORE_INCLUDE_DIR}) find_package(zydis REQUIRED) target_link_libraries(${PROJECT_NAME} PUBLIC Zydis::Zydis) else() target_link_libraries(${PROJECT_NAME} PUBLIC $) target_include_directories(${PROJECT_NAME} PUBLIC $) target_include_directories(${PROJECT_NAME} PUBLIC $) target_include_directories(${PROJECT_NAME} PUBLIC $) endif() target_sources(${PROJECT_NAME} PRIVATE "${PROJECT_SOURCE_DIR}/sources/ZydisDisassembler.cpp") install(FILES ${PROJECT_SOURCE_DIR}/polyhook2/ZydisDisassembler.hpp DESTINATION include/polyhook2) ``` -------------------------------- ### Intercepting and Spoofing Return Value Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/docs/index.md Shows an example of capturing the return value of the original function, inspecting it, and potentially spoofing it before returning from the callback. ```cpp HANDLE WINAPI hCreateMutexExA( _In_opt_ LPSECURITY_ATTRIBUTES lpMutexAttributes, _In_opt_ LPCSTR lpName, _In_ DWORD dwFlags, _In_ DWORD dwDesiredAccess ) { auto returnVal = PLH::FnCast(oCreateMutexExA, &CreateMutexExA)(lpMutexAttributes, "fake name", dwFlags, dwDesiredAccess); if (returnVal) { print("Return value is non-zero!"); return 0; // spoof zero } return returnVal; } ``` -------------------------------- ### asmjit Link Function (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt A CMake function to handle linking with asmjit, supporting both external installations and building from source. ```cmake function(link_asmjit) if (POLYHOOK_USE_EXTERNAL_ASMJIT) find_library(ASMJIT_LIBRARY NAMES asmjit) find_path(ASMJIT_INCLUDE_DIR NAMES asmjit/asmjit.h) target_link_libraries(${PROJECT_NAME} PRIVATE ${ASMJIT_LIBRARY}) target_include_directories(${PROJECT_NAME} PUBLIC ${ASMJIT_INCLUDE_DIR}) find_package(asmjit REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE asmjit::asmjit) else() target_link_libraries(${PROJECT_NAME} PRIVATE $) target_include_directories(${PROJECT_NAME} PUBLIC "$") endif() endfunction() ``` -------------------------------- ### Zydis Dependency Configuration Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Configures the Zydis library and its dependency Zycore for building. It sets build options for shared libraries and disables examples and tests. It then adds the subdirectories for Zycore and Zydis and sets MSVC runtime library properties for both targets if applicable. ```cmake # # Zydis # function(set_zycore_options) set(ZYCORE_BUILD_SHARED_LIB ${POLYHOOK_BUILD_SHARED_ZYDIS} CACHE BOOL "" FORCE) set(ZYCORE_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(ZYCORE_BUILD_TESTS OFF CACHE BOOL "" FORCE) endfunction() function(set_zydis_options) set(ZYDIS_BUILD_SHARED_LIB ${POLYHOOK_BUILD_SHARED_ZYDIS} CACHE BOOL "" FORCE) set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "" FORCE) set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(ZYDIS_BUILD_DOXYGEN OFF CACHE BOOL "" FORCE) endfunction() if(NOT POLYHOOK_USE_EXTERNAL_ZYDIS) set_zycore_options() add_subdirectory(zydis/dependencies/zycore) set_zydis_options() add_subdirectory(zydis) if(MSVC) if(POLYHOOK_BUILD_STATIC_RUNTIME) set_target_properties(Zycore PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") set_target_properties(Zydis PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") else() set_target_properties(Zycore PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") set_target_properties(Zydis PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() endif() endif() ``` -------------------------------- ### Configure Virtual Table (Virtuals) Feature in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake configuration sets up the virtual table hooking features for Polyhook 2.0. It installs headers for VTableSwapHook and VFuncSwapHook, includes their source files, and conditionally compiles corresponding unit tests based on build type. ```cmake #Feature/Virtuals if(POLYHOOK_FEATURE_VIRTUALS) set(POLYHOOK_VIRTUAL_HEADERS ${PROJECT_SOURCE_DIR}/polyhook2/Virtuals/VTableSwapHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Virtuals/VFuncSwapHook.hpp) install(FILES ${POLYHOOK_VIRTUAL_HEADERS} DESTINATION include/polyhook2/Virtuals) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/VTableSwapHook.cpp ${PROJECT_SOURCE_DIR}/sources/VFuncSwapHook.cpp) # only build tests if making exe if(NOT POLYHOOK_BUILD_DLL) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestVTableSwapHook.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestVFuncSwapHook.cpp) endif() endif() ``` -------------------------------- ### Configure Exception Handling Feature in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake configuration enables the exception handling features for Polyhook 2.0. It installs headers for VEH hooks, breakpoint hooks, and hardware breakpoint hooks, and includes their corresponding source files. Unit tests for these exception handling mechanisms are also conditionally compiled. ```cmake #Feature/Exception if(POLYHOOK_FEATURE_EXCEPTION) set(POLYHOOK_EXCEPTION_HEADERS ${PROJECT_SOURCE_DIR}/polyhook2/Exceptions/AVehHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Exceptions/BreakPointHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Exceptions/HWBreakPointHook.hpp) install(FILES ${POLYHOOK_EXCEPTION_HEADERS} DESTINATION include/polyhook2/Exceptions) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/AVehHook.cpp ${PROJECT_SOURCE_DIR}/sources/BreakPointHook.cpp ${PROJECT_SOURCE_DIR}/sources/HWBreakPointHook.cpp) # only build tests if making exe if(NOT POLYHOOK_BUILD_DLL) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestBreakpointHook.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestHWBreakpointHook.cpp) endif() endif() ``` -------------------------------- ### Configure Detour Feature in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This snippet configures the Detour feature for Polyhook 2.0. It links against the asmjit library, installs detour headers, and includes source files for detour implementations. Unit tests for detours are conditionally compiled based on the target architecture (x64/x86) and if the project is not building a DLL. ```cmake find_package(asmtk REQUIRED) target_link_libraries(${PROJECT_NAME} PRIVATE asmjit::asmtk) else() target_link_libraries(${PROJECT_NAME} PUBLIC $) target_include_directories(${PROJECT_NAME} PUBLIC "$") endif() set(POLYHOOK_DETOUR_HEADERS ${PROJECT_SOURCE_DIR}/polyhook2/Detour/ADetour.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Detour/NatDetour.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Detour/x64Detour.hpp ${PROJECT_SOURCE_DIR}/polyhook2/Detour/x86Detour.hpp) install(FILES ${POLYHOOK_DETOUR_HEADERS} DESTINATION include/polyhook2/Detour) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/ADetour.cpp ${PROJECT_SOURCE_DIR}/sources/x64Detour.cpp ${PROJECT_SOURCE_DIR}/sources/x86Detour.cpp) # only build tests if making exe if(NOT POLYHOOK_BUILD_DLL) if(CMAKE_SIZEOF_VOID_P EQUAL 8) #64-bit target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourx64.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourTranslationx64.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourSchemex64.cpp) elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) #32-bit target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourx86.cpp) endif() endif() ``` -------------------------------- ### Configure Portable Executable (PE) Feature in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake snippet configures the Portable Executable (PE) features for Polyhook 2.0. It installs headers for EAT hooks, IAT hooks, and PEB manipulation, and includes the corresponding C++ source files. Unit tests for PE manipulation are also conditionally compiled. ```cmake #Feature/PE if(POLYHOOK_FEATURE_PE) set(POLYHOOK_PE_HEADERS ${PROJECT_SOURCE_DIR}/polyhook2/PE/EatHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/PE/IatHook.hpp ${PROJECT_SOURCE_DIR}/polyhook2/PE/PEB.hpp) install(FILES ${POLYHOOK_PE_HEADERS} DESTINATION include/polyhook2/PE) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/EatHook.cpp ${PROJECT_SOURCE_DIR}/sources/IatHook.cpp) # only build tests if making exe if(NOT POLYHOOK_BUILD_DLL) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestEatHook.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestIatHook.cpp) endif() endif() ``` -------------------------------- ### Configure Inline Hooking (ILCallback) in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This snippet enables the inline hooking (ILCallback) feature in Polyhook 2.0. It links against asmjit, installs the ILCallback header, and adds the ILCallback source file. Unit tests for ILCallback are conditionally compiled based on architecture and build type. ```cmake #Feature/Inlinentd if(POLYHOOK_FEATURE_INLINENTD) link_asmjit() install(FILES ${PROJECT_SOURCE_DIR}/polyhook2/Detour/ILCallback.hpp DESTINATION include/polyhook2/Detour) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/ILCallback.cpp) # only build tests if making exe if(NOT POLYHOOK_BUILD_DLL) if(CMAKE_SIZEOF_VOID_P EQUAL 8) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourNoTDx64.cpp) elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDetourNoTDx86.cpp) endif() endif() endif() ``` -------------------------------- ### Build PolyHook 2.0 Manually with CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/README.md Builds PolyHook 2.0 from source using CMake. Supports both dynamic (shared) and static library builds. Requires recursive cloning of submodules. ```shell λ git clone --recursive https://github.com/stevemk14ebr/PolyHook_2_0.git λ cd PolyHook_2_0 λ git submodule update --init --recursive λ (dynamic build) cmake -B"./_build" -DCMAKE_INSTALL_PREFIX="./_install/" -DPOLYHOOK_BUILD_SHARED_LIB=ON λ (static build) cmake -B"./_build" -DCMAKE_INSTALL_PREFIX="./_install/" -DPOLYHOOK_BUILD_SHARED_LIB=OFF λ cmake --build "./_build" --config Release --target install ``` -------------------------------- ### Include Main and OS-Specific Unit Tests in CMake Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt This CMake snippet includes the main test file and OS-specific unit test files for Polyhook 2.0. These sources are added to the project only when the Polyhook library is not being built as a DLL. ```cmake #Tests if(NOT POLYHOOK_BUILD_DLL) target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/MainTests.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestDisassembler.cpp ${PROJECT_SOURCE_DIR}/UnitTests/${POLYHOOK_OS}/TestMemProtector.cpp) endif() ``` -------------------------------- ### Include AsmTk and AsmJit Subdirectories Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Conditionally includes the AsmTk and AsmJit subdirectories for building. It sets static/shared library flags based on project options and calls a function to configure MSVC runtime properties for the 'asmjit' target. It also sets runtime library properties for the 'asmtk' target. ```cmake if(POLYHOOK_FEATURE_DETOURS AND NOT POLYHOOK_USE_EXTERNAL_ASMTK) if(NOT POLYHOOK_USE_EXTERNAL_ASMJIT) set(ASMJIT_DIR "${PROJECT_SOURCE_DIR}/asmjit") endif() # asmjit and asmtk do not use `option` if(POLYHOOK_BUILD_SHARED_ASMTK) set(ASMTK_STATIC OFF) set(ASMJIT_STATIC OFF) else() set(ASMTK_STATIC ON) set(ASMJIT_STATIC ON) endif() add_subdirectory(asmtk) add_asmjit_properties() if(MSVC) if(POLYHOOK_BUILD_STATIC_RUNTIME) set_target_properties(asmtk PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") else() set_target_properties(asmtk PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() endif() endif() # # ASMJIT # if(POLYHOOK_FEATURE_INLINENTD AND NOT POLYHOOK_USE_EXTERNAL_ASMJIT) # Avoid including asmjit again if it was already included if(NOT TARGET asmjit) # asmjit doesn't use `option` if(POLYHOOK_BUILD_SHARED_ASMJIT) set(ASMJIT_STATIC OFF) else() set(ASMJIT_STATIC ON) endif() add_subdirectory(asmjit) add_asmjit_properties() endif() endif() ``` -------------------------------- ### Detours Feature Configuration (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Enables the Detours feature, which involves linking with asmjit and potentially asmtk if used externally. ```cmake if(POLYHOOK_FEATURE_DETOURS) link_asmjit() if (POLYHOOK_USE_EXTERNAL_ASMTK) find_library(ASMTK_LIBRARY NAMES asmtk) find_path(ASMTK_INCLUDE_DIR NAMES asmtk/asmtk.h) target_link_libraries(${PROJECT_NAME} PUBLIC ${ASMTK_LIBRARY}) target_include_directories(${PROJECT_NAME} PUBLIC ${ASMTK_INCLUDE_DIR}) endif() endif() ``` -------------------------------- ### Build Library or Executable (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Configures the project to build either a shared or static library, or an executable based on the POLYHOOK_BUILD_DLL flag. Sets Windows export symbols for shared libraries. ```cmake if(POLYHOOK_BUILD_DLL) if(POLYHOOK_BUILD_SHARED_LIB) add_library(${PROJECT_NAME} SHARED) set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) else() add_library(${PROJECT_NAME} STATIC) endif() else() add_executable(${PROJECT_NAME}) endif() ``` -------------------------------- ### Polyhook Build and Feature Options Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Defines CMake options to control the build process and features of Polyhook. These include options for building DLLs, shared libraries for various components (Polyhook, AsmTk, AsmJit, Zydis), using external libraries, and enabling specific hooking functionalities like exception handling, PE, detours, inline hooks, and virtual tables. ```cmake # # Options # option(POLYHOOK_BUILD_DLL "Build dll & lib instead of tests" ON) option(POLYHOOK_BUILD_SHARED_LIB "Build polyhook2 as shared libary" OFF) option(POLYHOOK_BUILD_SHARED_ASMTK "Build asmtk as shared libary" OFF) option(POLYHOOK_BUILD_SHARED_ASMJIT "Build asmjit as shared libary" ${POLYHOOK_BUILD_SHARED_ASMTK}) option(POLYHOOK_BUILD_SHARED_ZYDIS "Build zydis as shared libary" OFF) option(POLYHOOK_USE_EXTERNAL_ASMTK "Use external asmtk libary" OFF) option(POLYHOOK_USE_EXTERNAL_ASMJIT "Use external asmjit libary" ${POLYHOOK_USE_EXTERNAL_ASMTK}) option(POLYHOOK_USE_EXTERNAL_ZYDIS "Use external zydis libary" OFF) if(MSVC) option(POLYHOOK_BUILD_STATIC_RUNTIME "Use static runtime" ON) endif() if(WIN32) option(POLYHOOK_FEATURE_EXCEPTION "Implement all exception hooking functionality" ON) option(POLYHOOK_FEATURE_PE "Implement all win pe hooking functionality" ON) else() set(POLYHOOK_FEATURE_EXCEPTION OFF) set(POLYHOOK_FEATURE_PE OFF) endif() option(POLYHOOK_FEATURE_DETOURS "Implement detour functionality" ON) option(POLYHOOK_FEATURE_INLINENTD "Support inline hooks without specifying typedefs by generating callback stubs at runtime with AsmJit" ON) option(POLYHOOK_FEATURE_VIRTUALS "Implement all virtual table hooking functionality" ON) ``` -------------------------------- ### Define Project Sources (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Specifies the source files for the PolyHook project, excluding Zydis-related files initially. ```cmake target_sources(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/sources/MemProtector.cpp ${PROJECT_SOURCE_DIR}/sources/MemAccessor.cpp ${PROJECT_SOURCE_DIR}/sources/TestEffectTracker.cpp ${PROJECT_SOURCE_DIR}/sources/StackCanary.cpp ${PROJECT_SOURCE_DIR}/sources/FBAllocator.cpp ${PROJECT_SOURCE_DIR}/sources/RangeAllocator.cpp ${PROJECT_SOURCE_DIR}/sources/ErrorLog.cpp ${PROJECT_SOURCE_DIR}/sources/UID.cpp ${PROJECT_SOURCE_DIR}/sources/Misc.cpp ${PROJECT_SOURCE_DIR}/sources/PolyHookOs.cpp ) ``` -------------------------------- ### Polyhook 2.0 Detour Overloads Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/docs/index.md Defines the two available overloads for the Detour constructor in Polyhook 2.0, one accepting uint64_t addresses and the other char* addresses. ```cpp Detour(const uint64_t fnAddress, const uint64_t fnCallback, uint64_t* userTrampVar, PLH::ADisassembler& dis) Detour(const char* fnAddress, const char* fnCallback, uint64_t* userTrampVar, PLH::ADisassembler& dis) ``` -------------------------------- ### AsmJit/AsmTk Property Configuration for MSVC Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt A CMake function to set MSVC runtime library properties for targets like 'asmjit'. It configures the runtime library based on whether static linking is enabled, ensuring compatibility with the build configuration. ```cmake function(add_asmjit_properties) if(MSVC) if(POLYHOOK_BUILD_STATIC_RUNTIME) set_target_properties(asmjit PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") else() set_target_properties(asmjit PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() endif() endfunction() ``` -------------------------------- ### MemAccessor for Memory Operations (C++) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/docs/index.md The `MemAccessor` class, inherited by `IHook`, provides routines for memory reading and writing, crucial for cross-architecture support. It offers both safe and unsafe variants for memory access. ```C++ class MemAccessor { public: // Safe variants for potentially invalid memory virtual uint64_t readMem(uint64_t address, uint8_t* buffer, size_t size) = 0; virtual void writeMem(uint64_t address, const uint8_t* buffer, size_t size) = 0; // Unsafe variants for controlled memory virtual uint64_t readMemUnsafe(uint64_t address, uint8_t* buffer, size_t size) = 0; virtual void writeMemUnsafe(uint64_t address, const uint8_t* buffer, size_t size) = 0; protected: MemAccessor() = default; }; // Note: Prefer uint64_t for memory addresses over char* or void*. ``` -------------------------------- ### Conditional Compile Options (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Applies the '-fpermissive' compile option on UNIX systems when not building a DLL. ```cmake if(NOT POLYHOOK_BUILD_DLL) target_compile_options(${PROJECT_NAME} PRIVATE $:-fpermissive> ) endif() ``` -------------------------------- ### IHook Base Class Functionality (C++) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/docs/index.md The `IHook` base class provides core functionality for all hooking methods. It is non-copyable and manages the hook's lifecycle through `hook()`, `unhook()`, and `getType()` methods. Derived classes must manage the scope of `IHook` instances, often using smart pointers. ```C++ class IHook { public: virtual ~IHook() = default; virtual void hook() = 0; virtual void unhook() = 0; virtual HookType getType() const = 0; protected: IHook() = default; }; // Example usage with smart pointers: // std::unique_ptr myHook = std::make_unique(...); // myHook->hook(); // ... // myHook->unhook(); ``` -------------------------------- ### Set C++ Standard and Compiler Flags (CMake) Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/CMakeLists.txt Sets the C++ standard to 20 and enforces its usage. Also configures MSVC specific compile flags, including multi-processor compilation and debugging information. ```cmake set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20) set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD_REQUIRED ON) if(MSVC) if(POLYHOOK_BUILD_STATIC_RUNTIME) set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") else() set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() if(MSVC) set(COMPILE_FLAGS_PLH "/W4 /Z7") if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(COMPILE_FLAGS_PLH "/MP ${COMPILE_FLAGS_PLH}") endif() set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS_PLH}) target_link_libraries(${PROJECT_NAME} PRIVATE optimized -DEBUG) # mhhm ya pdbs endif() endif() ``` -------------------------------- ### Invoking Original Function with PLH::FnCast Source: https://github.com/stevemk14ebr/polyhook_2_0/blob/master/docs/index.md Demonstrates how to use PLH::FnCast to safely cast a trampoline pointer to the correct function signature and invoke the original hooked function from within a callback. ```cpp uint64_t oCreateMutexExA = 0; HANDLE WINAPI hCreateMutexExA( _In_opt_ LPSECURITY_ATTRIBUTES lpMutexAttributes, _In_opt_ LPCSTR lpName, _In_ DWORD dwFlags, _In_ DWORD dwDesiredAccess ) { printf("kernel32!CreateMutexExA Name:%s", lpName); return PLH::FnCast(oCreateMutexExA, &CreateMutexExA)(lpMutexAttributes, "fake name", dwFlags, dwDesiredAccess); } PLH::x64Detour detour((char*)&CreateMutexExA, (char*)&hCreateMutexExA, &oCreateMutexExA, dis); detour.hook(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.