-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRuntimePatchingSystem.cpp
More file actions
288 lines (227 loc) · 7.19 KB
/
RuntimePatchingSystem.cpp
File metadata and controls
288 lines (227 loc) · 7.19 KB
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
#include "framework.h"
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <assert.h>
#include <vector>
#include "RuntimePatchingSystem.h"
#include "CodeFunctions.h"
#include "MemoryFunctions.h"
#include "UtilityFunctions.h"
#include "LibraryFunctions.h"
static int l_my_print(lua_State* L) {
int n = lua_gettop(L); /* number of arguments */
int i;
for (i = 1; i <= n; i++) { /* for each argument */
size_t l;
const char* s = luaL_tolstring(L, i, &l); /* convert it to string */
if (i > 1) /* not the first element? */
lua_writestring("\t", 1); /* add a tab before it */
lua_writestring(s, l); /* print it */
lua_pop(L, 1); /* pop result */
}
lua_writeline();
return 0;
}
static const struct luaL_Reg printlib[] = {
{"print", l_my_print},
{NULL, NULL} /* end of array */
};
const struct luaL_Reg RPS_LIB[] = {
{"hookCode", luaHookCode},
{"callOriginal", luaCallMachineCode},
{"exposeCode", luaExposeCode},
{"detourCode", luaDetourCode},
{"allocate", luaAllocate},
{"deallocate", luaDeallocate},
{"allocateCode", luaAllocateRWE},
{"deallocateCode", luaDeallocateRWE},
{"loadLibraryA", luaLoadLibraryA},
{"getLibraryProcAddressA", luaGetLibraryProcAddressA},
{"getProcAddress", luaGetProcAddress},
{"readByte", luaReadByte},
{"readSmallInteger", luaReadSmallInteger},
{"readInteger", luaReadInteger},
{"readString", luaReadString},
{"readBytes", luaReadBytes},
{"writeByte", luaWriteByte},
{"writeSmallInteger", luaWriteSmallInteger},
{"writeInteger", luaWriteInteger},
{"writeString", luaWriteString},
{"writeBytes", luaWriteBytes},
{"writeCode", luaWriteCode},
{"copyMemory", luaMemCpy},
{"setMemory", luaMemSet},
{"registerString", registerString},
{"scanForAOB", luaScanForAOB},
{NULL, NULL} /* end of array */
};
static void setConstantsToTable(lua_State* L) {
lua_pushinteger(L, 0);
lua_setfield(L, -2, "CDECL");
lua_pushinteger(L, 1);
lua_setfield(L, -2, "THISCALL");
lua_pushinteger(L, 2);
lua_setfield(L, -2, "STDCALL");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "cdecl");
lua_pushinteger(L, 1);
lua_setfield(L, -2, "thiscall");
lua_pushinteger(L, 2);
lua_setfield(L, -2, "stdcall");
}
static int makeLibWithConstants(lua_State* L) {
// stack:
luaL_newlib(L, RPS_LIB);
// stack: lib table
lua_createtable(L, 0, 0);
// stack: lib table, constants
// stack: lib table, constants
lua_createtable(L, 0, 0);
// stack: lib table, constants, callingConventions
setConstantsToTable(L);
// stack: lib table, constants, callingConventions
lua_setfield(L, -2, "callingConventions");
// stack: lib table, constants
// stack: lib table, constants
lua_setfield(L, -2, "constants");
// stack: lib table
return 1;
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializePrintRedirect(lua_State* L) {
lua_pushglobaltable(L);
luaL_setfuncs(L, printlib, 0);
lua_pop(L, 1);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializePrintRedirect() {
return RPS_initializePrintRedirect(LC);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L, std::string apiNamespace) {
if (apiNamespace == "_G" || apiNamespace == "global") {
lua_pushglobaltable(L);
luaL_setfuncs(L, RPS_LIB, 0);
setConstantsToTable(L);
lua_pop(L, 1);
}
else if (apiNamespace.empty() || apiNamespace.size() == 0) {
makeLibWithConstants(L); // the table is left intentionally on the stack.
}
else {
lua_pushglobaltable(L);
makeLibWithConstants(L);
lua_setfield(L, -1, apiNamespace.c_str());
lua_pop(L, 1);
}
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(std::string apiNamespace) {
return RPS_initializeLuaAPI(LC, apiNamespace);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L) {
return RPS_initializeLuaAPI(L, "global");
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI() {
return RPS_initializeLuaAPI(LC);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackagePath(lua_State* L, std::string packagePath) {
lua_getglobal(L, "package");
lua_pushstring(L, "path");
lua_pushstring(L, packagePath.c_str());
lua_settable(L, -3);
lua_pop(L, 1);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackagePath(std::string packagePath) {
return RPS_setupPackagePath(LC, packagePath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackageCPath(lua_State* L, std::string packageCPath) {
lua_getglobal(L, "package");
lua_pushstring(L, "cpath");
lua_pushstring(L, packageCPath.c_str());
lua_settable(L, -3);
lua_pop(L, 1);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setupPackageCPath(std::string packageCPath) {
return RPS_setupPackageCPath(LC, packageCPath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_runBootstrapFile(lua_State* L, std::string bootstrapFilePath) {
int stackSize = lua_gettop(L);
int r = luaL_dofile(L, bootstrapFilePath.c_str());
if (r == LUA_OK) {
//std::cout << "[LUA]: loaded LUA API." << std::endl;
lua_pop(L, lua_gettop(L) - stackSize);
}
else {
size_t length;
const char* errormsg = lua_tolstring(L, -1, &length);
if (errormsg != NULL) {
std::cout << "[RPS]: failed to execute lua file: " << errormsg << std::endl;
lua_pop(L, 1); // pop off the error message;
}
else {
std::cout << "[RPS]: failed to execute lua file: lua error code: " << r << std::endl;
}
}
}
RUNTIMEPATCHINGSYSTEM_API void RPS_runBootstrapFile(std::string bootstrapFilePath) {
return RPS_runBootstrapFile(LC, bootstrapFilePath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLua() {
LC = luaL_newstate();
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenLibs() {
luaL_openlibs(LC);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenBase() {
luaL_requiref(LC, "base", luaopen_base, true);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath, bool initializePrintRedirect) {
RPS_initializeLua();
RPS_initializeLuaOpenLibs();
if (initializePrintRedirect) RPS_initializePrintRedirect(LC);
RPS_initializeLuaAPI(LC, "global");
RPS_runBootstrapFile(LC, bootstrapFilePath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath) {
return RPS_initialize(bootstrapFilePath, true);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_deinitialize() {
lua_close(LC);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_executeSnippet(std::string code) {
int before = lua_gettop(LC);
int r = luaL_dostring(LC, code.c_str());
if (r == LUA_OK) {
int after = lua_gettop(LC);
if (after - before > 0) {
for (int i = before; i < after; i++) { /* for each argument */
size_t l;
const char* s = luaL_tolstring(LC, i, &l); /* convert it to string */
if (i > 1) /* not the first element? */
lua_writestring("\t", 1); /* add a tab before it */
lua_writestring(s, l); /* print it */
lua_pop(LC, 1); /* pop result */
}
lua_writeline();
}
lua_pop(LC, after - before);
}
else {
std::string errormsg = lua_tostring(LC, -1);
std::cout << "[RPS]: error in lua snippet: " << errormsg << std::endl;
lua_pop(LC, 1); // pop off the error message;
}
}
RUNTIMEPATCHINGSYSTEM_API int RPS_getCurrentStackSize() {
return lua_gettop(LC);
}
RUNTIMEPATCHINGSYSTEM_API lua_State* RPS_getLuaState() {
return LC;
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setLuaState(lua_State* value) {
LC = value;
}
extern "C" RUNTIMEPATCHINGSYSTEM_API int luaopen_RPS(lua_State * L) {
RPS_setLuaState(L);
makeLibWithConstants(L);
return 1;
}