-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathUserModuleBase.h
More file actions
70 lines (53 loc) · 2.64 KB
/
Copy pathUserModuleBase.h
File metadata and controls
70 lines (53 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// ========================================================================
// File: UserModuleBase.h
//
// Author: winterknife
//
// Description: Header file for UserModuleBase.cpp source file
//
// Modifications:
// 2025-09-23 Created
// 2025-09-25 Updated
// ========================================================================
// ========================================================================
// Header guards
// ========================================================================
#pragma once
// ========================================================================
// Includes
// ========================================================================
#include "Common.h"
#include "HashString.h"
// ========================================================================
// Macros
// ========================================================================
#pragma region MACROS
// Hardcoded ntdll!_LDR_DATA_TABLE_ENTRY.BaseDllName.MaximumLength value
#define MAX_BASE_DLL_NAME_LEN 64U
// Macro to get the image base address of a loaded user-mode module
#define GET_USER_MODULE_BASE(Name) get_user_module_base_by_hash(HASH_STRING_COMPILE_TIME(Name))
#pragma endregion
// ========================================================================
// Inline routines
// ========================================================================
#pragma region INLINES
/// @brief Get a pointer to the Thread Environment Block (TEB) of the current thread
/// @return A pointer to the TEB of the current thread
FORCE_INLINE PTEB get_current_teb(
VOID
) {
// GS segment base address points to the base address of the current thread's Thread Environment Block (TEB) in user-mode
return BIT_CAST(TEB*, __readgsqword(FIELD_OFFSET(NT_TIB, Self)));
}
#pragma endregion
// ========================================================================
// C routine declarations
// ========================================================================
#pragma region DECLARATIONS
/// @brief To retrieve the base address of a loaded user-mode module (EXE/DLL) from user-mode, does not increment module reference count, custom implementation of kernel32!GetModuleHandle routine
/// @param qwTargetModuleHash 64-bit Fowler/Noll/Vo (FNV-1a) hash of the target module name in lowercase with the file name extension, can also be nullptr to get the base address of the calling module
/// @return Returns the base address of the loaded module if successful, or nullptr otherwise
EXTERN_C NO_INLINE PVOID __stdcall get_user_module_base_by_hash(
_In_opt_ QWORD qwTargetModuleHash
);
#pragma endregion