-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaL_Functions.cpp
More file actions
430 lines (390 loc) · 13.8 KB
/
LuaL_Functions.cpp
File metadata and controls
430 lines (390 loc) · 13.8 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
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
//luaL is from the aux api to give better functions so yeah.....
//kinda useless cuz we don't know what can we use this for.
void r_luaL_sandboxthread(DWORD rL)//sandboxthread
{
// create new global table that proxies reads to original table
r_lua_newtable(rL);
r_lua_newtable(rL);
r_lua_pushvalue(rL, LUA_GLOBALSINDEX);
r_lua_setfield(rL, -2, "__index");
r_lua_setreadonly(rL, -1, true);
r_lua_setmetatable(rL, -2);//set it to -2
// we can set safeenv now although it's important to set it to false if code is loaded twice into the thread
r_lua_replace(rL, LUA_GLOBALSINDEX);
r_lua_setsafeenv(rL, LUA_GLOBALSINDEX, 1); //also known as bool true;
}
void r_luaL_sandbox(lua_State* L)//no thread
{
// set all libraries to read-only
r_lua_pushnil(rL);
while (r_lua_next(rL, LUA_GLOBALSINDEX) != NULL)//NULL
{
if (r_lua_istable(rL, -1))
r_lua_setreadonly(rL, -1, true);
r_lua_pop(rL, 1);
}
// set all builtin metatables to read-only
r_lua_pushliteral(rL, "");
r_lua_getmetatable(rL, -1);
r_lua_setreadonly(rL, -1, 1);//make true
r_lua_pop(rL, 2);//pop
// set globals to readonly and activate safeenv since the env is immutable
r_lua_setreadonly(rL, LUA_GLOBALSINDEX, true);//set it to true
r_lua_setsafeenv(rL, LUA_GLOBALSINDEX, true);//set it to true
}
const char* r_luaL_findtable(DWORD rL, int idx, const char* fname, int szhint)
{
const char* e;
r_lua_pushvalue(rL, idx);
do
{
e = strchr(fname, '.');
if (e == NULL)
e = fname + strlen(fname);
r_lua_pushlstring(rL, fname, e - fname);//did you know all pushstring does is check if string is empty and push nil? ikr
r_lua_rawget(rL, -2);
if (r_lua_isnil(rL, -1))
{ /* no such field? */
r_lua_pop(rL, 1); /* remove this nil */
r_lua_createtable(rL, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
r_lua_pushlstring(rL, fname, e - fname);
r_lua_pushvalue(rL, -2);
r_lua_settable(rL, -4); /* set new table into field */
}
else if (!r_lua_istable(rL, -1))
{ /* field has a non-table value? */
r_lua_pop(rL, 2); /* remove table and value */
return fname; /* return problematic part of the name */
}
r_lua_remove(rL, -2); /* remove previous table */
fname = e + 1;
} while (*e == '.');
return NULL;
}
const char* luaL_typename(DWORD rL, int idx)//type name but L
{
const TValue* obj = r_luaA_toobject(rL, idx);
return r_luaT_objtypename(rL, obj);
}
const char* r_luaL_tolstring(DWORD rL, int idx, size_t* len)
{
if (r_luaL_callmeta(rL, idx, "__tostring")) /* is there a metafield? */
{
if (!r_lua_isstring(rL, -1))//if no string is returned
luaL_error(L, "'__tostring' must return a string");
/* return as error tostring must return string */
return lua_tolstring(L, -1, len);
}
switch (r_lua_type(rL, idx))//all globals below get updated btw^^
{
case LUA_TNUMBER:
{
double n = r_lua_tonumber(rL, idx);
char s[48];
char* e = r_luai_num2str(s, n);
r_lua_pushlstring(rL, s, e - s);
break;
}
case LUA_TSTRING:
r_lua_pushvalue(rL, idx);
break;
case LUA_TBOOLEAN:
r_lua_pushstring(rL, (r_lua_toboolean(rL, idx) ? "true" : "false"));
break;
case LUA_TNIL:
r_lua_pushliteral(rL, "nil");
break;
case LUA_TVECTOR:
{
const float* v = r_lua_tovector(rL, idx);
char s[48 * LUA_VECTOR_SIZE];//diy you find it yourself!
char* e = s;
for (int i = 0; i < LUA_VECTOR_SIZE; ++i)
{
if (i != 0)
{
*e++ = ',';
*e++ = ' ';
}
e = r_luai_num2str(e, v[i]);
}
r_lua_pushlstring(rL, s, e - s);
break;
}
default:
{
const void* ptr = r_lua_topointer(rL, idx);
unsigned long long enc = r_lua_encodepointer(rL, uintptr_t(ptr));
r_lua_pushfstring(rL, "%s: 0x%016llx", r_luaL_typename(rL, idx), enc);
//use pushlstring or any other function related to push-string
break;
}
}
return r_lua_tolstring(rL, -1, len);
}
void r_luaL_checkstack(DWORD rL, int space, const char* mes)//aux check stack
{
mes = mes;//message
if (!r_lua_checkstack(rL, space))
r_luaL_error(rL, "stack overflow (%s)", mes);//error
else
//no stack overflow :)
}
int r_luaL_checkoption(DWORD rL, int narg, const char* def, const char* const lst[])//4 args
{
const char* name = (def) ? r_luaL_optstring(rL, narg, def) : r_luaL_checkstring(rL, narg);
int i;
for (i = 0; lst[i]; i++)
if (strcmp(lst[i], name) == 0)
return i;
const char* msg = r_lua_pushfstring(rL, "invalid option '%s'", name);//not a option and push to argument error
r_luaL_argerrorL(rL, narg, msg);//arg error 'invalid option' internal argument
}
int r_luaL_newmetatable(DWORD rL, const char* tname)//creates new metatable with 2nd argument tname
{
int value = -1;
r_lua_getfield(rL, LUA_REGISTRYINDEX, tname); /* get registry.name */
if (!r_lua_isnil(rL, value)) /* name already in use? */
return NULL; /* leave previous value on top, but return 0 */
r_lua_pop(rL, 1);//note: this is settop used as define to make life 100% easier
r_lua_newtable(rL); /* create metatable */
r_lua_pushvalue(rL, value);
r_lua_setfield(rL, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */
return 1;
}
void* r_luaL_checkudata(DWORD rL, int ud, const char* tname)//check user data aux
{
void* p = r_lua_touserdata(rL, ud);//p* now to userdata
if (p != 0)//equal to 0
{ /* value is a userdata? */
if (r_lua_getmetatable(rL, ud))
{ /* does it have a metatable? */
r_lua_getfield(rL, LUA_REGISTRYINDEX, tname); /* get correct metatable */
if (r_lua_rawequal(rL, -1, -2))
{ /* does it have the correct mt? */
r_lua_pop(L, 2); /* remove both metatables settop */
return p;
}
}
}
std::cout << "error while function was starting";//error comes :SKULL:
//i suggest you make ur exploit console based temporary
}
void r_luaL_checkstack(DWORD rL, int space, const char* mes)//checks stack 3 args
{
if (!r_lua_checkstack(rL, space))//! = not
r_luaL_error(rL, "stack overflow (%s)", mes);
std::cout << "encountered overflow " << mes;
else//no overflow but should loop this func to check overflowed stack
std::cout << "no overflow (YET)";
}
void r_luaL_checktype(DWORD rL, int narg, int t)//checks type with 3 arg a integer
{
if (r_lua_type(rL, narg) != t)
std::cout << narg;//display in console
else
//not
}
void r_luaL_checkany(DWORD rL, int narg)//checks narg
{
if (r_lua_type(rL, narg) == R_LUA_TNONE)//global offset
r_luaL_error(L, "missing argument #%d", narg);
std::cout << "did u miss an argument?";
}
const char* r_luaL_checklstring(DWORD rL, int narg, size_t* len)//checks len for string
{
const char* s = r_lua_tolstring(rL, narg, len);
if (!s)
tag_error(L, narg, LUA_TSTRING);//errror
return s;
}
const char* r_luaL_optlstring(DWORD rL, int narg, const char* def, size_t* len)//optlstring function 3 arg
{
if (r_lua_isnoneornil(rL, narg))
{
if (len)
*len = (def ? strlen(def) : NULL);
return def;
}
else
return r_luaL_checklstring(rL, narg, len);
}
double luaL_checknumber(DWORD rL, int narg)//why not make it a double?
{
int isnum;
double d = r_lua_tonumberx(rL, narg, &isnum);
if (!isnum)
tag_error(L, narg, LUA_TNUMBER);//global offset
return d;
}
double luaL_optnumber(DWORD rL, int narg, double def)//opt number with L check number
{
return r_luaL_opt(rL, r_luaL_checknumber, narg, def);
}
static void* l_alloc(void* ud, void* ptr, size_t osize, size_t nsize)//need this for lua_state btw
{
(void)ud;
(void)osize;
if (nsize == 0)
{
free(ptr);
return NULL;
}
else
return realloc(ptr, nsize);
}
DWORD* luaL_newstate(void)//fake ofc and not even inlined lmao
{
return lua_newstate(l_alloc, NULL);
}
int r_luaL_callmeta(DWORD rL, int obj, const char* event)//meta caller
{
obj = abs_index(rL, obj); //alternative way down below uncomment it
//(obj) > NULL || (i) <= LUA_REGISTRYINDEX ? (i) : r_lua_gettop(rL) + (obj) + 1);
if (!r_luaL_getmetafield(rL, obj, event)) /* no metafield? */
return 0;
r_lua_pushvalue(rL, obj);//push int 'obj' aka abs_index
r_lua_call(rL, 1, 1);
//r_lua_pcall(rL, 1, 1, 0);
return 1;
}
int r_luaL_checkoption(DWORD rL, int narg, const char* def, const char* const lst[])//checks option or returns 'invalid'
{
const char* name = (def) ? r_luaL_optstring(rL, narg, def) : r_luaL_checkstring(rL, narg);
int i;
for (i = 0; lst[i]; i++)
if (strcmp(lst[i], name) == 0)//checks if i is NULL
return i;
const char* msg = r_lua_pushfstring(rL, "invalid option '%s'", name);//return error
r_luaL_argerrorL(rL, narg, msg);
}
int luaL_checkboolean(DWORD rL, bool narg)//use bool instead of int
{
#define RLUA_TBOOLEAN 15//tboolean offset (global)
if (!r_lua_isboolean(rL, narg))//hot
luaL_typeerrorL(rL, narg, lua_typename(rL, RLUA_TBOOLEAN));
return r_lua_toboolean(rL, narg);
}
int luaL_optboolean(DWORD rL, int narg, int def)
{
return r_luaL_opt(rL, r_luaL_checkboolean, narg, def);
}
int r_luaL_checkinteger(DWORD rL, int narg)//check integer
{
int isnum;
int d = r_lua_tointegerx(rL, narg, &isnum);
if (!isnum)//not number
r_luaL_error(rL, "not a valid number");//custom error
return d;
}
int r_luaL_optinteger(DWORD rL, int narg, int def)//opts integer
{
return r_luaL_opt(rL, r_luaL_checkinteger, narg, def);
}
unsigned luaL_checkunsigned(DWORD rL, int narg)
{
int isnum;
unsigned d = r_lua_tounsignedx(rL, narg, &isnum);
if (!isnum)
luaL_typeerrorL(rL, narg, lua_typename(rL, RLUA_TONUMBER));//use that
return d;
}
unsigned luaL_optunsigned(DWORD rL, int narg, unsigned def)//optsing
{
return r_luaL_opt(rL, r_luaL_checkunsigned, narg, def);
}
const float* rluaL_checkvector(DWORD rL, int narg)//checks vector (int)
{
const float* v = r_lua_tovector(rL, narg);
if (!v)//not valid vector
tag_error(L, narg, LUA_TVECTOR);//error
return v;
}
const float* rluaL_optvector(DWORD rL, int narg, const float* def)//optsvector
{
return r_luaL_opt(rL, r_luaL_checkvector, narg, def);
}
#define lfakeDefine extern
lfakeDefine int r_luaL_getmetafield(DWORD rL, int obj, const char* event)//gets meta field modded
{
if (!r_lua_getmetatable(rL, obj)) /* no metatable? */
r_luaL_error(rL, "no metatable found");//no metatable exists
return NULL;
r_lua_pushstring(rL, event);
r_lua_rawget(rL, -2);
if (r_lua_isnil(rL, -1))
{
r_lua_pop(rL, 2); /* remove metatable and metafield */
return 0;
}
else
{
r_lua_remove(rL, -2); /* remove only metatable */
r_lua_print(1, "removed metatable");//print function or just use luac print
return 1;
}
}
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)
lfakedefine int r_luaL_callmeta(DWORD rL, int obj, const char* event)//calls meta same arg as getmetafield
{
obj = (obj) > 0 || (obj) <= LUA_REGISTRYINDEX ? (i) : r_lua_gettop(rL) + (obj) + 1);
if (!r_luaL_getmetafield(rL, obj, event)) /* no metafield? */
r_luaL_error(rL, "no metafield");//no metafield
return 0;
r_lua_pushvalue(rL, obj);//otherwise
r_lua_call(rL, 1, 1);
return 1;
}
//registeration system but it needs special format btw so remember that
typedef int (*lua_CFunction) (DWORD rL);
typedef struct luaL_Reg {
const char *name;
lua_CFunction func;
} luaL_Reg;
static int libsize (const luaL_Reg *l) {
int size = 0;
for (; l->name; l++) size++;
return size;
}
void r_luaL_register(DWORD rL, const char* libname, const luaL_Reg* l)
{
if (libname)//if exists
{
int size = libsize(l);
/* check whether lib already exists */
r_luaL_findtable(rL, LUA_REGISTRYINDEX, "_LOADED", 1);//does it already exist
r_lua_getfield(rL, -1, libname); /* get _LOADED[libname] */
if (!r_lua_istable(rL, -1))
{ /* not found? */
r_lua_pop(rL, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */
if (r_luaL_findtable(rL, LUA_GLOBALSINDEX, libname, size) != NULL)
r_luaL_error(rL, "name conflict for module '%s'", libname);
r_lua_pushvalue(rL, -1);
r_lua_setfield(rL, -3, libname); /* _LOADED[libname] = new table */
}
r_lua_remove(rL, -2); /* remove _LOADED table */
}
for (; l->name; l++)
{
r_lua_pushcfunction(rL, l->func, l->name);//TODO: change arrows opperandor
r_lua_setfield(rL, -2, l->name);
}
}
//tut on how to register funcs with my modded thingy:
static int function(DWORD rL)//paste ur skid funcs here
{
//custom function
return 1;
}
static const luaL_Reg mycustomlib[] = {
{"func", function},//"func" in quotes is what the lua env will read and the 'function' is the funcs name in this file
{NULL, NULL},
};
#define funcs "funcs"//technically it will use funcs.func() to run the script
int start_register(DWORD rL)
{
r_luaL_register(rL, funcs, mycustomlib);//registers functions in the reg list
return 1;
}
start_register(rL);//ez registering