-
Notifications
You must be signed in to change notification settings - Fork 1
/
RuntimePatchingSystem.cpp
242 lines (193 loc) · 6.21 KB
/
RuntimePatchingSystem.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
#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 */
};
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(L);
}
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);
lua_pop(L, 1);
}
else if (apiNamespace.empty() || apiNamespace.size() == 0) {
luaL_newlib(L, RPS_LIB); // the table is left intentionally on the stack.
}
else {
lua_pushglobaltable(L);
luaL_newlib(L, RPS_LIB);
lua_setfield(L, -1, apiNamespace.c_str());
lua_pop(L, 1);
}
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(std::string apiNamespace) {
return RPS_initializeLuaAPI(L, apiNamespace);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI(lua_State* L) {
return RPS_initializeLuaAPI(L, "global");
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaAPI() {
return RPS_initializeLuaAPI(L);
}
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(L, 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(L, 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(L, bootstrapFilePath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLua() {
L = luaL_newstate();
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenLibs() {
luaL_openlibs(L);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initializeLuaOpenBase() {
luaL_requiref(L, "base", luaopen_base, true);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath, bool initializePrintRedirect) {
RPS_initializeLua();
RPS_initializeLuaOpenLibs();
if (initializePrintRedirect) RPS_initializePrintRedirect(L);
RPS_initializeLuaAPI(L, "global");
RPS_runBootstrapFile(L, bootstrapFilePath);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_initialize(std::string bootstrapFilePath) {
return RPS_initialize(bootstrapFilePath, true);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_deinitialize() {
lua_close(L);
}
RUNTIMEPATCHINGSYSTEM_API void RPS_executeSnippet(std::string code) {
int before = lua_gettop(L);
int r = luaL_dostring(L, code.c_str());
if (r == LUA_OK) {
int after = lua_gettop(L);
if (after - before > 0) {
for (int i = before; i < after; 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();
}
lua_pop(L, after - before);
}
else {
std::string errormsg = lua_tostring(L, -1);
std::cout << "[RPS]: error in lua snippet: " << errormsg << std::endl;
lua_pop(L, 1); // pop off the error message;
}
}
RUNTIMEPATCHINGSYSTEM_API int RPS_getCurrentStackSize() {
return lua_gettop(L);
}
RUNTIMEPATCHINGSYSTEM_API lua_State* RPS_getLuaState() {
return L;
}
RUNTIMEPATCHINGSYSTEM_API void RPS_setLuaState(lua_State* value) {
L = value;
}
extern "C" RUNTIMEPATCHINGSYSTEM_API int luaopen_RPS(lua_State * L) {
luaL_newlib(L, RPS_LIB);
return 1;
}