-
Notifications
You must be signed in to change notification settings - Fork 22
/
dllmain.cpp
146 lines (110 loc) · 3.26 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
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
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
#define LOGFILENAME "kss-X.log"
#define KSSLIBRARY "softlic32.dll"
#define DEBUGCONSOLE 1
FARPROC g_decodeAPI = NULL;
/**
* 通过读取导出表名称字符串来判断 是否是可可X DLL加载
*/
BOOL isKssXLibrary(PVOID imageBase, SIZE_T &imageSize)
{
PIMAGE_DOS_HEADER pDosHead = (PIMAGE_DOS_HEADER)imageBase;
if (pDosHead->e_magic != IMAGE_DOS_SIGNATURE) {
return FALSE;
}
PIMAGE_NT_HEADERS pNtHead = (PIMAGE_NT_HEADERS)(pDosHead->e_lfanew + (ULONG_PTR)pDosHead);
if (pNtHead->Signature != IMAGE_NT_SIGNATURE) {
return FALSE;
}
PIMAGE_DATA_DIRECTORY pExportData = \
&pNtHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
if (pExportData->VirtualAddress == NULL || pExportData->Size == NULL) {
return FALSE;
}
PIMAGE_EXPORT_DIRECTORY pExports = \
(PIMAGE_EXPORT_DIRECTORY)(pExportData->VirtualAddress + (ULONG_PTR)pDosHead);
if (pExports->Name == NULL) {
return FALSE;
}
CHAR *nameString = (CHAR*)(pExports->Name + (ULONG_PTR)pDosHead);
Log::Info("[%s] Library nameString: %s", __FUNCTION__, nameString);
if (_stricmp(nameString, KSSLIBRARY) != 0) {
return FALSE;
}
imageSize = pNtHead->OptionalHeader.SizeOfImage;
Log::Info("[%s] TRUE", __FUNCTION__);
return TRUE;
}
/**
* Hook 函数GetStartupInfo ,等待可可X DLL解码
*/
typedef VOID(WINAPI* fn_GetStartupInfoA)(LPSTARTUPINFOA lpStartupInfo);
fn_GetStartupInfoA pfn_GetStartupInfoA = NULL;
VOID WINAPI HookedGetStartupInfoA(LPSTARTUPINFOA lpStartupInfo)
{
LPVOID returnAddr = _ReturnAddress();
SIZE_T imageSize = 0;
Log::Info("[%s] -> ReturnAddress = %08x", __FUNCTION__, returnAddr);
MEMORY_BASIC_INFORMATION mbi32 = { 0 };
if (VirtualQuery(returnAddr, &mbi32, sizeof(mbi32))) {
Log::Info("[%s] Library -> ImageBase = %08x ", __FUNCTION__,mbi32.AllocationBase);
if (isKssXLibrary(mbi32.AllocationBase, imageSize)) {
MH_DisableHook(g_decodeAPI);
Log::Info("[%s] start hook ks_library exports function!", __FUNCTION__);
//
//开始hook函数
//
startHookKssX(mbi32.AllocationBase, imageSize);
}
}
pfn_GetStartupInfoA(lpStartupInfo);
}
/**
*
*/
void InstallHook()
{
char exePath[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), exePath, MAX_PATH);
Log::Info("Exe = %s", exePath);
g_decodeAPI = GetProcAddress(GetModuleHandle("kernel32.dll"), "GetStartupInfoA");
if (MH_CreateHook(g_decodeAPI, HookedGetStartupInfoA, (PVOID*)&pfn_GetStartupInfoA) != MH_OK) {
Log::Info("[instHook] create hook decode api fail!");
return;
}
if (MH_EnableHook(g_decodeAPI) != MH_OK) {
Log::Info("[instHook] enable hook decode api fail!");
return;
}
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
#if DEBUGCONSOLE
AllocConsole();
freopen("CONOUT$", "w", stdout);
#endif
DeleteFile(LOGFILENAME);
Log::Initialise(LOGFILENAME);
if (MH_Initialize() != MH_OK)
{
Log::Info("MinHook initialize failed!!!");
return FALSE;
}
InstallHook();
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}