| 27 | } |
| 28 | |
| 29 | class DllCallbackHandler |
| 30 | { |
| 31 | public: |
| 32 | static inline void RegisterCallback(std::function<void(HMODULE)>&& fn) |
| 33 | { |
| 34 | RegisterDllNotification(); |
| 35 | GetCallbackList().emplace_back(std::forward<std::function<void(HMODULE)>>(fn)); |
| 36 | } |
| 37 | private: |
| 38 | static inline void callOnLoad(HMODULE mod) |
| 39 | { |
| 40 | if (!GetCallbackList().empty()) |
| 41 | { |
| 42 | for (auto& f : GetCallbackList()) |
| 43 | { |
| 44 | f(mod); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | static inline std::vector<std::function<void(HMODULE)>>& GetCallbackList() |
| 51 | { |
| 52 | return onLoad; |
| 53 | } |
| 54 | |
| 55 | typedef NTSTATUS(NTAPI* _LdrRegisterDllNotification) (ULONG, PVOID, PVOID, PVOID); |
| 56 | typedef NTSTATUS(NTAPI* _LdrUnregisterDllNotification) (PVOID); |
| 57 | |
| 58 | typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA |
| 59 | { |
| 60 | ULONG Flags; //Reserved. |
| 61 | PUNICODE_STRING FullDllName; //The full path name of the DLL module. |
| 62 | PUNICODE_STRING BaseDllName; //The base file name of the DLL module. |
| 63 | PVOID DllBase; //A pointer to the base address for the DLL in memory. |
| 64 | ULONG SizeOfImage; //The size of the DLL image, in bytes. |
| 65 | } LDR_DLL_LOADED_NOTIFICATION_DATA, LDR_DLL_UNLOADED_NOTIFICATION_DATA, * PLDR_DLL_LOADED_NOTIFICATION_DATA, * PLDR_DLL_UNLOADED_NOTIFICATION_DATA; |
| 66 | |
| 67 | typedef union _LDR_DLL_NOTIFICATION_DATA |
| 68 | { |
| 69 | LDR_DLL_LOADED_NOTIFICATION_DATA Loaded; |
| 70 | LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded; |
| 71 | } LDR_DLL_NOTIFICATION_DATA, * PLDR_DLL_NOTIFICATION_DATA; |
| 72 | |
| 73 | typedef NTSTATUS(NTAPI* PLDR_MANIFEST_PROBER_ROUTINE) (IN HMODULE DllBase, IN PCWSTR FullDllPath, OUT PHANDLE ActivationContext); |
| 74 | typedef NTSTATUS(NTAPI* PLDR_ACTX_LANGUAGE_ROURINE) (IN HANDLE Unk, IN USHORT LangID, OUT PHANDLE ActivationContext); |
| 75 | typedef void(NTAPI* PLDR_RELEASE_ACT_ROUTINE) (IN HANDLE ActivationContext); |
| 76 | typedef VOID(NTAPI* fnLdrSetDllManifestProber) (IN PLDR_MANIFEST_PROBER_ROUTINE ManifestProberRoutine, |
| 77 | IN PLDR_ACTX_LANGUAGE_ROURINE CreateActCtxLanguageRoutine, IN PLDR_RELEASE_ACT_ROUTINE ReleaseActCtxRoutine); |
| 78 | |
| 79 | private: |
| 80 | static inline void CALLBACK LdrDllNotification(ULONG NotificationReason, PLDR_DLL_NOTIFICATION_DATA NotificationData, PVOID Context) |
| 81 | { |
| 82 | static constexpr auto LDR_DLL_NOTIFICATION_REASON_LOADED = 1; |
| 83 | static constexpr auto LDR_DLL_NOTIFICATION_REASON_UNLOADED = 2; |
| 84 | if (NotificationReason == LDR_DLL_NOTIFICATION_REASON_LOADED) |
| 85 | { |
| 86 | callOnLoad((HMODULE)NotificationData->Loaded.DllBase); |
nothing calls this directly
no outgoing calls
no test coverage detected