|
| 1 | +/** |
| 2 | + * @file iat_hooking.h |
| 3 | + * @author Vic P. |
| 4 | + * @brief Header/Implementation for IAT Hooking Manager. |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include "common.h" |
| 10 | + |
| 11 | +#include <vu> |
| 12 | + |
| 13 | +/** |
| 14 | + * @brief IAT Hooking Manager. |
| 15 | + */ |
| 16 | +class IATHookingManager : public vu::SingletonT<IATHookingManager> |
| 17 | +{ |
| 18 | + struct IAT_Hooked : public Hooked {}; |
| 19 | + |
| 20 | + std::unordered_map<void*, IAT_Hooked> m_list; |
| 21 | + |
| 22 | + using Entry = vu::IATHookingA::Entry; |
| 23 | + |
| 24 | + vu::IATHookingA& hooker() |
| 25 | + { |
| 26 | + return vu::IATHookingA::instance(); |
| 27 | + } |
| 28 | + |
| 29 | + void* get_proc_address(const Entry& entry) |
| 30 | + { |
| 31 | + Entry temp; |
| 32 | + |
| 33 | + if (!this->hooker().exist(entry.target, entry.module, entry.function, &temp)) |
| 34 | + { |
| 35 | + return nullptr; |
| 36 | + } |
| 37 | + |
| 38 | + return temp.original; |
| 39 | + } |
| 40 | + |
| 41 | +public: |
| 42 | + /** |
| 43 | + * @brief Hook a given function. |
| 44 | + * @param[in] entry The entry of function that want to hook ({ tartget name, module name, function name }). |
| 45 | + * @param[in] hk_function The hooking function. |
| 46 | + * @return true if succeeds otherwise return false. |
| 47 | + */ |
| 48 | + template<typename Function> |
| 49 | + bool hook(const Entry& entry, Function&& hk_function) |
| 50 | + { |
| 51 | + IAT_Hooked hooked; |
| 52 | + |
| 53 | + auto ret = this->hooker().install(entry.target, entry.module, entry.function, |
| 54 | + (void*)hk_function, &hooked.m_trampoline); |
| 55 | + if (ret != vu::VU_OK) |
| 56 | + { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + auto function = this->get_proc_address(entry); |
| 61 | + if (function == nullptr) |
| 62 | + { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + using FunctionPtr = decltype(&hk_function); |
| 67 | + |
| 68 | + hooked.m_function = function; |
| 69 | + hooked.m_invoker.reset(new Invokable(FunctionPtr(hooked.m_trampoline))); |
| 70 | + |
| 71 | + void* key = hooked.m_function; |
| 72 | + m_list[key] = std::move(hooked); |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Unhook a given function that was hooked. |
| 79 | + * @param[in] entry The entry of function that want to un-hook ({ tartget name, module name, function name }). |
| 80 | + * @return true if succeeds otherwise return false. |
| 81 | + */ |
| 82 | + bool unhook(const Entry& entry) |
| 83 | + { |
| 84 | + auto function = this->get_proc_address(entry); |
| 85 | + if (function == nullptr) |
| 86 | + { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + auto it = m_list.find(function); |
| 91 | + if (it == m_list.cend()) |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + auto ret = this->hooker().uninstall(entry.target, entry.module, entry.function); |
| 97 | + if (ret != vu::VU_OK) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + m_list.erase(it); |
| 103 | + |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @brief Invoke the original function. |
| 109 | + * @param[in] entry The entry of function that want to invoke ({ tartget name, module name, function name }). |
| 110 | + * @param[in] args The arguments that pass to the original function. |
| 111 | + * @return Based on the result of the original function. |
| 112 | + */ |
| 113 | + template<typename Return, typename ... Args> |
| 114 | + Return invoke(const Entry& entry, Args ... args) |
| 115 | + { |
| 116 | + auto function = this->get_proc_address(entry); |
| 117 | + auto it = m_list.find(function); |
| 118 | + if (it == m_list.cend()) |
| 119 | + { |
| 120 | + throw "invoke the function that did not hook"; |
| 121 | + } |
| 122 | + |
| 123 | + auto& hooked = it->second; |
| 124 | + |
| 125 | + if (std::is_void<Return>::value) |
| 126 | + { |
| 127 | + hooked.m_invoker->invoke<Return>(std::forward<Args>(args)...); |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + return hooked.m_invoker->invoke<Return>(std::forward<Args>(args)...); |
| 132 | + } |
| 133 | + } |
| 134 | +}; |
0 commit comments