-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
84 lines (72 loc) · 2.18 KB
/
main.c
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
/*
see python/hack.py for the steps that make this program work
without being modified, the executable generated by this program will crash
*/
#include <stdint.h>
#include <string.h>
#include <libloaderapi.h> //for GetModuleHandleA and GetModuleFileNameA
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
// luasocket
#ifdef LUASOCKET_C
#include <mime.h>
#include <luasocket.h>
#endif
volatile const intptr_t MAGIC_OFFSET = 0x13371337; // this is a placeholder, it should be be updated after compilation to point to the script
int main(int argc, char* argv[])
{
// create the lua state for the script, include standard library
lua_State *L = luaL_newstate();
luaL_openlibs(L);
#ifdef LUASOCKET_C
// register luasocket core functions
lua_getfield(L, LUA_GLOBALSINDEX, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(L, luaopen_socket_core);
lua_pushvalue(L, -1);
lua_call(L, 0, 1);
lua_setglobal(L, "socket");
lua_setfield(L, -2, "socket.core");
lua_pushcfunction(L, luaopen_mime_core);
lua_pushvalue(L, -1);
lua_call(L, 0, 1);
lua_setglobal(L, "mime");
lua_setfield(L, -2, "mime.core");
lua_pop(L, 2);
#endif
// pass C command line arguments onto the lua script (as global "arg")
lua_createtable(L, argc, 0);
for (int i = 0; i < argc; i++)
{
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
// set _FILENAME global (for the generator script, but scripts may use it as well)
char buf[MAX_PATH];
GetModuleFileNameA(NULL, buf, MAX_PATH);
lua_pushstring(L, buf);
lua_setglobal(L, "_FILENAME");
// attempt to load the lua script, wherever it may be
void* magicPointer = (void*)((intptr_t)GetModuleHandleA(NULL) + MAGIC_OFFSET);
uint32_t size = *(uint32_t*)(magicPointer);
const char* source = (const char*)(magicPointer + 0x4);
luaL_loadbuffer(L, source, size, "=lua");
// basic execute + error handling
if (lua_type(L, -1) == LUA_TFUNCTION)
{
lua_pcall(L, 0, 0, 0);
if (lua_gettop(L) > 0)
{
printf("%s\n", lua_tostring(L, -1));
}
}
else
{
// this happens a lot when something goes wrong, so hope you don't see this error
printf("failed to load chunk: %s\n", lua_tostring(L, -1));
}
lua_close(L);
return 0;
}