-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdllmain.cpp
288 lines (248 loc) · 14 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
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
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <Windows.h>
#include <shellapi.h>
#include "storage.h"
#include "stdheader.h"
#include "memutil.h"
#include "helpers.h"
#include "config.h"
#pragma comment(lib, "Wldap32.lib")
#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>
#pragma comment(lib, "Shell32.lib")
#if USE_CURL
#define CURL_STATICLIB
#include <curl/curl.h>
#pragma comment(lib, "libcurl.lib")
#endif
#include <string>
#include <sstream>
#include "picosha2.h"
#if 0
char * va(const char *format, ...) {
va_list argptr;
static char string[2][32000]; // in case va is called by nested functions
static int index = 0;
char *buf;
buf = string[index & 1];
index++;
va_start(argptr, format);
vsprintf(buf, format, argptr);
va_end(argptr);
return buf;
}
#endif
size_t curl_to_string(void *ptr, size_t size, size_t nmemb, void *data)
{
std::string *str = (std::string *) data;
char *sptr = (char *)ptr;
int x;
for (x = 0; x < size * nmemb; ++x)
{
(*str) += sptr[x];
}
return size * nmemb;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
#if USE_CURL
void test2()
{
CURL *curl;
FILE *fp;
CURLcode res;
const char *url = "https://google.com";
char outfilename[FILENAME_MAX] = "bbb.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
}
void Sys_CheckUpdates()
{
if (!Sys_IsPHP())
return;
CURL *curl;
FILE *fp;
CURLcode res;
const char *url = "http://xtnded.org/deployment.txt";
curl = curl_easy_init();
std::string pagedata;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_PORT, 28860);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pagedata);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
MessageBoxA(NULL, "Error occured during checking for any available updates.", "Error", MB_OK | MB_ICONWARNING);
}
else
{
std::istringstream iss(pagedata);
std::string line;
while (std::getline(iss, line)) //one file per line
{
auto sep = line.find_first_of(':');
if (sep == std::string::npos)
{
MessageBoxA(NULL, "Update file corrupted, failed to parse!", "Error", MB_OK | MB_ICONWARNING);
break;
}
std::string filename = line.substr(0, sep);
std::string hash = line.substr(sep + 1, line.size());
std::string trimmed;
for (auto & ch : hash)
{
if (!isalnum(ch))continue; //mmm? really
trimmed.push_back(ch);
}
MessageBoxA(NULL, va("file: %s (hash: %s)", filename.c_str(), trimmed.c_str()), "", 0);
if (!storage::file_exists(filename))
{
MessageBoxA(NULL, "file does not exist!", "", 0); //update / grab this file
}
else
{
//is it old? hashes are not same?
std::ifstream f(filename, std::ios::binary);
std::vector<unsigned char> s(picosha2::k_digest_size);
picosha2::hash256(f, s.begin(), s.end());
std::string hex_str = picosha2::bytes_to_hex_string(s.begin(), s.end());
MessageBoxA(NULL, va("found file %s, hash = %s", filename.c_str(), hex_str.c_str()), "", 0);
if (trimmed != hex_str)
{
MessageBoxA(NULL, "update time!", "", 0);
//Sys_ReplaceFile(...);
//Sys_ElevateProgram etc
}
else
{
//file is good
}
}
}
}
//MessageBoxA(0, va("page = %s, error = %d", pagedata.c_str(), res), 0, 0);
curl_easy_cleanup(curl);
exit(0);
}
}
#else
void Sys_CheckUpdates() {
Com_Printf("Ignoring Sys_CheckUpdates()\n");
}
#endif
BOOL (__stdcall *oDllMain)(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
);
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))) //TODO
if ((GetModuleHandleExA(0x00000004, reinterpret_cast<LPCSTR>(Main_DoInit), &hModule) != 0) || (hModule != nullptr))
{
Main_UnprotectModule(hModule);
}
//HideCode_FindDeviceIoControl();
void patch_opcode_loadlibrary(void);
//patch_opcode_loadlibrary();
Sys_CheckUpdates();
void applyHooks();
applyHooks();
// 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 hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
#if 0
static bool check = false;
if (!check)
{
std::vector<char> data;
if (!storage::read_file(szModuleName, data))
{
MessageBoxA(NULL, "Failed to check version!", "", MB_OK | MB_ICONWARNING);
Com_Quit_f();
return TRUE;
}
else
{
const char *searchString = "cod2sp_s.exe";
if (search_memory((int)data.data(), (int)data.data() + data.size(), (BYTE*)searchString, strlen(searchString)) == -1)
{
//we're SP mode
//MessageBoxA(0, "single player detected", "", 0);
}
else
{
isMP = true;
//MessageBoxA(0, "mp detected", "", 0);
}
}
//cod2sp_s.exe
check = true;
}
#endif
#if 0
if (strstr(szModuleName, "rundll32") != NULL) {
return TRUE;
}
#endif
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();
//DisableThreadLibraryCalls(hModule);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}