CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Windows/W32Util/IatHook.h
Views: 1401
// This file contains code from1// https://github.com/stevemk14ebr/PolyHook_2_0/blob/master/sources/IatHook.cpp2// which is licensed under the MIT License.3// See PolyHook_2_0-LICENSE for more information.45#pragma once67#include "Common/CommonWindows.h"8#include <cstdint>9#include <winnt.h>1011template <typename T, typename T1, typename T2>12constexpr T RVA2VA(T1 base, T2 rva)13{14return reinterpret_cast<T>(reinterpret_cast<ULONG_PTR>(base) + rva);15}1617template <typename T>18constexpr T DataDirectoryFromModuleBase(void *moduleBase, size_t entryID)19{20auto dosHdr = reinterpret_cast<PIMAGE_DOS_HEADER>(moduleBase);21auto ntHdr = RVA2VA<PIMAGE_NT_HEADERS>(moduleBase, dosHdr->e_lfanew);22auto dataDir = ntHdr->OptionalHeader.DataDirectory;23return RVA2VA<T>(moduleBase, dataDir[entryID].VirtualAddress);24}2526inline PIMAGE_THUNK_DATA FindAddressByName(void *moduleBase, PIMAGE_THUNK_DATA impName, PIMAGE_THUNK_DATA impAddr, const char *funcName)27{28for (; impName->u1.Ordinal; ++impName, ++impAddr)29{30if (IMAGE_SNAP_BY_ORDINAL(impName->u1.Ordinal))31continue;3233auto import = RVA2VA<PIMAGE_IMPORT_BY_NAME>(moduleBase, impName->u1.AddressOfData);34if (strcmp(import->Name, funcName) != 0)35continue;36return impAddr;37}38return nullptr;39}4041inline PIMAGE_THUNK_DATA FindAddressByOrdinal(void *moduleBase, PIMAGE_THUNK_DATA impName, PIMAGE_THUNK_DATA impAddr, uint16_t ordinal)42{43for (; impName->u1.Ordinal; ++impName, ++impAddr)44{45if (IMAGE_SNAP_BY_ORDINAL(impName->u1.Ordinal) && IMAGE_ORDINAL(impName->u1.Ordinal) == ordinal)46return impAddr;47}48return nullptr;49}5051inline PIMAGE_THUNK_DATA FindIatThunkInModule(void *moduleBase, const char *dllName, const char *funcName)52{53auto imports = DataDirectoryFromModuleBase<PIMAGE_IMPORT_DESCRIPTOR>(moduleBase, IMAGE_DIRECTORY_ENTRY_IMPORT);54for (; imports->Name; ++imports)55{56if (_stricmp(RVA2VA<LPCSTR>(moduleBase, imports->Name), dllName) != 0)57continue;5859auto origThunk = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->OriginalFirstThunk);60auto thunk = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->FirstThunk);61return FindAddressByName(moduleBase, origThunk, thunk, funcName);62}63return nullptr;64}6566inline PIMAGE_THUNK_DATA FindDelayLoadThunkInModule(void *moduleBase, const char *dllName, const char *funcName)67{68auto imports = DataDirectoryFromModuleBase<PIMAGE_DELAYLOAD_DESCRIPTOR>(moduleBase, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);69for (; imports->DllNameRVA; ++imports)70{71if (_stricmp(RVA2VA<LPCSTR>(moduleBase, imports->DllNameRVA), dllName) != 0)72continue;7374auto impName = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->ImportNameTableRVA);75auto impAddr = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->ImportAddressTableRVA);76return FindAddressByName(moduleBase, impName, impAddr, funcName);77}78return nullptr;79}8081inline PIMAGE_THUNK_DATA FindDelayLoadThunkInModule(void *moduleBase, const char *dllName, uint16_t ordinal)82{83auto imports = DataDirectoryFromModuleBase<PIMAGE_DELAYLOAD_DESCRIPTOR>(moduleBase, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);84for (; imports->DllNameRVA; ++imports)85{86if (_stricmp(RVA2VA<LPCSTR>(moduleBase, imports->DllNameRVA), dllName) != 0)87continue;8889auto impName = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->ImportNameTableRVA);90auto impAddr = RVA2VA<PIMAGE_THUNK_DATA>(moduleBase, imports->ImportAddressTableRVA);91return FindAddressByOrdinal(moduleBase, impName, impAddr, ordinal);92}93return nullptr;94}959697