-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
76 lines (60 loc) · 1.98 KB
/
dllmain.cpp
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
#include "stdafx.h"
void codextended();
void codextended_unload();
HMODULE hModule;
HANDLE hLogFile = INVALID_HANDLE_VALUE;
// PROCESS_DETACH is not called so don't make global declarations which have deconstructors which have to be called.
static BYTE originalCode[5];
static PBYTE originalEP = 0;
void Main_UnprotectModule(HMODULE hModule);
void Main_DoInit()
{
// unprotect our entire PE image
HMODULE hModule;
if (SUCCEEDED(GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)Main_DoInit, &hModule)))
{
Main_UnprotectModule(hModule);
}
void patch_opcode_loadlibrary(void);
// return to the original EP
memcpy(originalEP, &originalCode, sizeof(originalCode));
__asm jmp originalEP
}
void Main_SetSafeInit()
{
// find the entry point for the executable process, set page access, and replace the EP
HMODULE hModule = GetModuleHandle(NULL); // passing NULL should be safe even with the loader lock being held (according to ReactOS ldr.c)
if (hModule)
{
PIMAGE_DOS_HEADER header = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS ntHeader = (PIMAGE_NT_HEADERS)((DWORD)hModule + header->e_lfanew);
Main_UnprotectModule(hModule);
// back up original code
PBYTE ep = (PBYTE)((DWORD)hModule + ntHeader->OptionalHeader.AddressOfEntryPoint);
memcpy(originalCode, ep, sizeof(originalCode));
// patch to call our EP
int newEP = (int)Main_DoInit - ((int)ep + 5);
ep[0] = 0xE9; // for some reason this doesn't work properly when run under the debugger
memcpy(&ep[1], &newEP, 4);
originalEP = ep;
}
}
BOOL APIENTRY DllMain(HMODULE hMod, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
char szModuleName[MAX_PATH + 1];
GetModuleFileNameA(NULL, szModuleName, MAX_PATH);
void MSS32_Hook();
MSS32_Hook();
extern bool miles32_loaded;
if (!miles32_loaded) return false;
Main_SetSafeInit();
codextended();
break;
case DLL_PROCESS_DETACH:
// codextended_unload();
break;
}
return TRUE;
}