-
Notifications
You must be signed in to change notification settings - Fork 0
/
APMAlert.cpp
204 lines (168 loc) · 5.75 KB
/
APMAlert.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
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
// APMAlert.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "APMAlert.h"
#include "Logger.h"
#include "ProjectCfg.h"
#include "ConfigDlg.h"
#include "ApmThread.h"
#include <algorithm>
#include "ScopedHelper.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CAPMAlertApp
BEGIN_MESSAGE_MAP(CAPMAlertApp, CWinApp)
//{{AFX_MSG_MAP(CAPMAlertApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAPMAlertApp construction
CAPMAlertApp::CAPMAlertApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CAPMAlertApp object
CAPMAlertApp theApp;
typedef struct TBWL_ExchangeData
{
int PluginAPI;
int StarCraftBuild;
BOOL NotSCBWmodule;
BOOL ConfigDialog;
}TBWL_ExchangeData;
char* PluginName = "APM";
int STARCRAFTBUILD = 13;
int PluginMajor = 1;
int PluginMinor = 0;
extern "C" void GetPluginAPI(TBWL_ExchangeData* Data)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
//BWL Gets version from Resource - VersionInfo
Data->PluginAPI = 4; //BWL 4
Data->StarCraftBuild = STARCRAFTBUILD; //1.15.2
Data->ConfigDialog = TRUE; // We display a config dialog
Data->NotSCBWmodule = TRUE; // No warning message
CLogger::getinstance()->log("GetPluginAPI");
}
extern "C" void GetData(char* name, char* des, char* updateurl)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
strcpy(name, PluginName);
strcpy(des, "APM alert C++ verstion\r\nby nwao");
strcpy(updateurl, "https://blog.csdn.net/nwao7890?type=blog");
CLogger::getinstance()->log("GetData");
}
extern "C" BOOL OpenConfig()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLogger::getinstance()->log("OpenConfig");
CConfigDlg dlg;
dlg.DoModal();
return TRUE;
}
extern "C" BOOL ApplyPatchSuspended(HANDLE hprocess, HANDLE processid)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLogger::getinstance()->log("ApplyPatchSuspended");
return TRUE;
}
extern "C" BOOL ApplyPatch(HANDLE hprocess, HANDLE processid)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CLogger::getinstance()->log("ApplyPatch");
MEMORY_BASIC_INFORMATION mbi;
static int dummy; VirtualQuery( &dummy, &mbi, sizeof(mbi) );
HMODULE hm = reinterpret_cast<HMODULE>(mbi.AllocationBase);
char buf[MAX_PATH];
GetModuleFileName(hm, buf, sizeof(buf));
CLogger::getinstance()->log("ApplyPatch,Module file %s", buf);
HMODULE hker32 = GetModuleHandle("kernel32.dll");
if (hker32 == NULL)
return FALSE;
HMODULE (WINAPI* loadLibAddr)(LPCSTR) = LoadLibraryA;
CScopedVirtualAlloc pathstraddr(hprocess, NULL, strlen(buf) + 1, MEM_COMMIT, PAGE_READWRITE);
if (pathstraddr.get() == NULL)
{
CLogger::getinstance()->log("ApplyPatch, alloc mem in starcraft fail");
return FALSE;
}
DWORD tmpWrite = 0;
BOOL ok = WriteProcessMemory(hprocess, pathstraddr.get(), buf, strlen(buf) + 1, &tmpWrite);
if (!ok || tmpWrite != strlen(buf) + 1)
{
CLogger::getinstance()->log("ApplyPatch, write starcraft mem fail");
return FALSE;
}
CLogger::getinstance()->log("LoadLibrary addr %d remote addr %d", loadLibAddr, pathstraddr.get());
CScopedHandle hThread(CreateRemoteThread(hprocess, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibAddr, pathstraddr.get(), 0, &tmpWrite));
if (hThread.get() == NULL )
{
CLogger::getinstance()->log("remote thread create fail %d", GetLastError());
return FALSE;
}
WaitForSingleObject(hThread.get(), INFINITE);
DWORD exitcode;
GetExitCodeThread(hThread.get(), &exitcode);
if(exitcode == 0)
{
CLogger::getinstance()->log("Error loading library remotely %d", GetLastError());
return FALSE;
}
return TRUE;
}
BOOL CAPMAlertApp::InitInstance()
{
// TODO: Add your specialized code here and/or call the base class
// begin apm thread
char buf[MAX_PATH];
GetModuleFileName(NULL, buf, sizeof(buf));
CLogger::getinstance()->log("InitInstance,APMAlert dll Be load by %s", buf);
std::string full = buf;
std::transform(full.begin(), full.end(), full.begin(), tolower);
std::string exename = "starcraft.exe";
if (full.find(exename) != std::string::npos)
CApmThread::getInst()->Init();
return CWinApp::InitInstance();
}
int CAPMAlertApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
CProjectCfg::releaseInstance();
CLogger::releaseInstance();
BWUtil::releaseInst();
return CWinApp::ExitInstance();
}