-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.h
More file actions
417 lines (278 loc) · 10.5 KB
/
Copy pathloader.h
File metadata and controls
417 lines (278 loc) · 10.5 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Author : Vladislav Khudash
* Source : https://github.com/vk-candpython/efildr/blob/main/loader.h
* Compiler : GCC (GNU-EFI)
* Platform : UEFI (PE32+)
* Summary : Core configuration header, custom NT structures, and basic utilities.
*/
#pragma once
/********************
* INCLUDES *
********************/
/* GNU-EFI headers,
UEFI core definitions */
#include <efi.h>
#include <efilib.h>
/*************************************
* BUILD-TIME FEATURE CONFIG *
*************************************/
#define USING_ANTI_VM 0
#define USING_ANTI_DEBUG 0
#define USING_ERASE_PE_HEADERS 0
/*********************************
* CONSTANT-DECLARATIONS *
*********************************/
#define DAT_KEY_SZ sizeof(UINT8) // Size of key size field (BYTE)
#define DAT_LEN_SZ sizeof(UINT32) // Size of length fields (UINT32)
#define RLE_MAX_RUN 127 // Maximum encoded run length
#define RLE_FLG_RUN 128 // High-bit marker for run blocks
#define LDR_REBOOT_SECOND 5 // Delay before reboot
/*************************************
* ERROR-STRING DECLARATIONS *
*************************************/
#define LDR_ERR_PREFIX L"\r\n(efildr) [ERROR]: "
#define LDR_ERR_ANALYSIS L"Image initialization failed.\r\n"
#define LDR_ERR_SUBSYSTEM L"Image is not an EFI application.\r\n"
#define LDR_ERR_OVERLAY L"Image overlay is corrupted.\r\n"
#define LDR_ERR_ALLOC_IMAGE L"Image memory allocation failed.\r\n"
#define LDR_ERR_LOADED_IMAGE L"Loaded image protocol access failed.\r\n"
/*****************************
* ALIASES & HELPERS *
*****************************/
#define _EFI_CALL_SELECTOR(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define _EFI_CALL_ARGS(...) _EFI_CALL_SELECTOR(0, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
/* uefi_call_wrapper with automatic argument counting */
#define EFI_CALL(func, ...) \
uefi_call_wrapper((func), _EFI_CALL_ARGS(__VA_ARGS__), ##__VA_ARGS__)
/* Compiler attribute wrapper helper */
#define _DEC_ATTR(...) \
__attribute__((__VA_ARGS__))
/* Assembly block shorthand */
#define DEC_ASM \
__asm__ volatile
/* Const-qualified pointer helper
(const data and const pointer) */
#define CONST_PTR(TYPE) \
const TYPE *const
/* Restrict-qualified pointer helper
(guarantees no memory aliasing) */
#define RESTR_PTR(TYPE) \
TYPE *restrict
/* Branch prediction hints for compiler optimization */
#define _UNLIKELY(x) __builtin_expect(!!(x), 0) // Cold path
#define _LIKELY(x) __builtin_expect(!!(x), 1) // Hot path
/* Check for EFI status success */
#define EFI_OK(status) \
_LIKELY((INTN)(status) >= EFI_SUCCESS)
/* Check for EFI status failure */
#define EFI_FAIL(status) \
_UNLIKELY((INTN)(status) < EFI_SUCCESS)
/* Short-circuit EFI call chainer */
#define _EFI_CHECK_CALL_N(...) (EFI_OK(EFI_CALL(__VA_ARGS__)))
#define _EFI_CHECK_CALL_1(a) (_EFI_CHECK_CALL_N a)
#define _EFI_CHECK_CALL_2(a, b) (_EFI_CHECK_CALL_1(a) && _EFI_CHECK_CALL_1(b))
#define _EFI_CHECK_CALL_3(a, b, c) (_EFI_CHECK_CALL_2(a, b) && _EFI_CHECK_CALL_1(c))
#define _EFI_CHECK_CALL_4(a, b, c, d) (_EFI_CHECK_CALL_3(a, b, c) && _EFI_CHECK_CALL_1(d))
#define _EFI_CHECK_CALL_5(a, b, c, d, e) (_EFI_CHECK_CALL_4(a, b, c, d) && _EFI_CHECK_CALL_1(e))
#define _EFI_CHECK_CALL_SELECTOR(_1, _2, _3, _4, _5, N, ...) _EFI_CHECK_CALL_##N
/* Automatic EFI call-chain validator */
#define EFI_CHAIN_CALL(...) \
_EFI_CHECK_CALL_SELECTOR(__VA_ARGS__, 5, 4, 3, 2, 1)(__VA_ARGS__)
/* Optimized branch control macros */
#define IF_UNLIKE(expr) if (_UNLIKELY(expr))
#define IF_LIKE(expr) if (_LIKELY(expr))
#define IF_EFIFAIL(status) if (EFI_FAIL(status))
#define IF_EFIFAIL_CALL(...) IF_EFIFAIL (EFI_CALL(__VA_ARGS__))
#define IF_EFIFAIL_CHAINCALL(...) IF_UNLIKE (!EFI_CHAIN_CALL(__VA_ARGS__))
/* Optimized loop control macros */
#define WHILE_LIKE(expr) \
while (_LIKELY(expr))
#define FOR_LIKE(init, cond, post) \
for (init; _LIKELY(cond); post)
/* Function declaration helper */
#define DEC_FUNC(TYPE) \
static inline _DEC_ATTR(always_inline) TYPE
/******************************
* PE-IMAGE UTILITIES *
******************************/
/* ASCII-only lowercase conversion */
#define ASCII_TOLOWER(chr) \
((chr) | 0x20)
/* Convert seconds to microseconds */
#define SEC_TO_USEC(n) \
((UINTN)(n) * 1000000)
/* Safe minimum and maximum value helpers */
#define MIN(a, b) ({ \
typeof((a)) _a = (a); \
typeof((b)) _b = (b); \
\
(_a < _b)? _a : _b; \
})
#define MAX(a, b) ({ \
typeof((a)) _a = (a); \
typeof((b)) _b = (b); \
\
(_a > _b)? _a : _b; \
})
/* Convert RVA to typed pointer */
#define RVA(TYPE, base, addr) \
((TYPE)((UINT8*)(base) + (addr)))
/* Extract e_lfanew from DOS header */
#define DOS_LFANEW(base) \
(((IMAGE_DOS_HEADER*)(base))->e_lfanew)
/* Get the current EFI image file path */
#define EFI_IMAGE_PATH(LoadedImage) \
(((FILEPATH_DEVICE_PATH*)(LoadedImage)->FilePath)->PathName)
/* Calculate the required EFI_FILE_INFO buffer size,
Uses the full path as buffer slack */
#define EFI_FILEINFO_SIZE(PathName) \
(sizeof(EFI_FILE_INFO) + \
(StrLen(PathName) * sizeof(CHAR16)))
/* Number of relocation entries in a block */
#define RELOC_ENTRY_COUNT(reloc) \
(((reloc)->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION) \
) / sizeof(UINT16))
/* True if relocation entry requests
64-bit absolute fixup */
#define RELOC_IS_DIR64(entry) \
(((entry) >> 12) == IMAGE_REL_BASED_DIR64)
/* Get intra‑page offset from relocation entry */
#define RELOC_BLOCK_OFFSET(entry) \
((entry) & 0x0FFF)
/* Retrieves a pointer to the first section header */
#define IMAGE_FIRST_SECTION(hdNt) RVA( \
IMAGE_SECTION_HEADER*, \
&(hdNt)->OptionalHeader, \
(hdNt)->FileHeader.SizeOfOptionalHeader \
)
/* Copy memory block */
#define MEMCPY(dst, src, sz) \
CopyMem((VOID*)(dst), (const VOID*)(src), (UINTN)(sz))
/* Fill memory with zeros */
#define ZEROS(dst, sz) \
ZeroMem((VOID*)(dst), (UINTN)(sz))
/*******************************
* ANTI-VM DEFINITIONS *
*******************************/
/* Common physical PC UEFI
firmware vendor substrings */
#define IBV_FIRMWARE_VENDORS { \
L"megatrends", /* (American Megatrends International, LLC.) */ \
L"insyde", /* (INSYDE Corp.) */ \
L"phoenix", /* (Phoenix Technologies Ltd.) */ \
L"byosoft", /* (Nanjing Byosoft Co., Ltd.) */ \
}
/* PCI vendor IDs packed as UINT16 */
#define PCI_VENID_VBOX ((UINT16)0x80EE) // Oracle Corporation (VirtualBox)
#define PCI_VENID_VMWARE ((UINT16)0x15AD) // VMware, Inc.
#define PCI_VENID_QEMU ((UINT16)0x1AF4) // Red Hat (QEMU VirtIO)
#define PCI_VENID_QEMU_BRG ((UINT16)0x1B36) // Red Hat (QEMU PCI Bridge)
#define PCI_VENID_QEMU_VGA ((UINT16)0x1234) // QEMU Virtual VGA
#define PCI_VENID_XEN ((UINT16)0x5853) // XenSource, Inc.
#define PCI_VENID_HYPER_V ((UINT16)0x1414) // Microsoft Corporation (Hyper-V)
#define PCI_VENID_PARALLELS ((UINT16)0x1AB8) // Parallels International GmbH
/**********************************
* ANTI-DEBUG DEFINITIONS *
**********************************/
#define CPU_RFLAGS_TF 0x100ULL // RFLAGS Trap Flag (TF) - Bit 8
#define CPU_DR7_BP_MASK 0xFFULL // DR7 L0-L3/G0-G3 enable bits (Bits 0-7)
/************************************
* NT-CONSTANT DECLARATIONS *
************************************/
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
#define IMAGE_SIZEOF_SHORT_NAME 8
#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5
#define IMAGE_REL_BASED_DIR64 10
/************************************
* NT-STRUCTURE DEFINITIONS *
************************************/
typedef struct {
UINT16 e_magic;
UINT16 e_cblp;
UINT16 e_cp;
UINT16 e_crlc;
UINT16 e_cparhdr;
UINT16 e_minalloc;
UINT16 e_maxalloc;
UINT16 e_ss;
UINT16 e_sp;
UINT16 e_csum;
UINT16 e_ip;
UINT16 e_cs;
UINT16 e_lfarlc;
UINT16 e_ovno;
UINT16 e_res[4];
UINT16 e_oemid;
UINT16 e_oeminfo;
UINT16 e_res2[10];
INT32 e_lfanew;
} IMAGE_DOS_HEADER;
typedef struct {
UINT16 Machine;
UINT16 NumberOfSections;
UINT32 TimeDateStamp;
UINT32 PointerToSymbolTable;
UINT32 NumberOfSymbols;
UINT16 SizeOfOptionalHeader;
UINT16 Characteristics;
} IMAGE_FILE_HEADER;
typedef struct {
UINT32 VirtualAddress;
UINT32 Size;
} IMAGE_DATA_DIRECTORY;
typedef struct {
UINT16 Magic;
UINT8 MajorLinkerVersion;
UINT8 MinorLinkerVersion;
UINT32 SizeOfCode;
UINT32 SizeOfInitializedData;
UINT32 SizeOfUninitializedData;
UINT32 AddressOfEntryPoint;
UINT32 BaseOfCode;
UINT64 ImageBase;
UINT32 SectionAlignment;
UINT32 FileAlignment;
UINT16 MajorOperatingSystemVersion;
UINT16 MinorOperatingSystemVersion;
UINT16 MajorImageVersion;
UINT16 MinorImageVersion;
UINT16 MajorSubsystemVersion;
UINT16 MinorSubsystemVersion;
UINT32 Win32VersionValue;
UINT32 SizeOfImage;
UINT32 SizeOfHeaders;
UINT32 CheckSum;
UINT16 Subsystem;
UINT16 DllCharacteristics;
UINT64 SizeOfStackReserve;
UINT64 SizeOfStackCommit;
UINT64 SizeOfHeapReserve;
UINT64 SizeOfHeapCommit;
UINT32 LoaderFlags;
UINT32 NumberOfRvaAndSizes;
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER64;
typedef struct {
UINT32 Signature;
IMAGE_FILE_HEADER FileHeader;
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
} IMAGE_NT_HEADERS64;
typedef struct {
UINT8 Name[IMAGE_SIZEOF_SHORT_NAME];
union {
UINT32 PhysicalAddress;
UINT32 VirtualSize;
} Misc;
UINT32 VirtualAddress;
UINT32 SizeOfRawData;
UINT32 PointerToRawData;
UINT32 PointerToRelocations;
UINT32 PointerToLinenumbers;
UINT16 NumberOfRelocations;
UINT16 NumberOfLinenumbers;
UINT32 Characteristics;
} IMAGE_SECTION_HEADER;
typedef struct {
UINT32 VirtualAddress;
UINT32 SizeOfBlock;
} IMAGE_BASE_RELOCATION;