Differences From
Artifact [f2235ca34c]:
7 7 -- giving cortav extra privileges
8 8
9 9 local includes = [[
10 10 #include <lua.h>
11 11 #include <lauxlib.h>
12 12 #include <stdio.h>
13 13 #include <stddef.h>
14 +#include <unistd.h>
14 15 extern int luaL_openlibs(lua_State* l);
15 16 ]]
16 17
17 18
18 19 local main = [[
19 20 int main(int argc, char** argv) {
20 21 lua_State* l = luaL_newstate();
................................................................................
31 32 // load and run our payload
32 33 int e = luaL_loadbufferx(l, ct_bytecode, sizeof(ct_bytecode), "cortav", "b");
33 34 if (e != LUA_OK) {
34 35 printf("some kind of error idk fam\n");
35 36 return -1;
36 37 }
37 38
38 - lua_call(l, 0, 0);
39 + if (lua_pcall(l, 0, 0, 0) != LUA_OK) {
40 + size_t len;
41 + const char* msg = luaL_tolstring(l, -1, &len);
42 + if (isatty(2)) {
43 + fprintf(stderr, "\33[31;1m(fatal)\33[m %.*s\n", (int)len, msg);
44 + } else {
45 + fprintf(stderr, "(fatal) %.*s\n", (int)len, msg);
46 + }
47 + };
39 48
40 49 // normal termination is by the os.exit() call
41 50 return -1;
42 51 }
43 52 ]]
44 53
45 54 local function setfile(i, dflt, mode)
................................................................................
53 62 return dflt
54 63 end
55 64
56 65 local src = setfile(1, io.stdin, "rb")
57 66 local dest = setfile(2, io.stdout, "w")
58 67
59 68 local cstr = {}
60 -local strtpl = 'static char ct_bytecode [%u] = {%s};'
61 -local lines = {includes}
69 +local strtpl = [[static char ct_bytecode [%u] = {
70 +%s
71 +};]]
62 72
63 73 local bytes = {}
64 74
65 75 local bn = 1
66 76 local len = 0
67 77 while true do
68 78 local byte = src:read(1)
69 79 if not byte then break end
70 80 local str = tostring(byte:byte(1))..','
71 81 -- make sure our source file is parseable by
72 82 -- a compliant C compiler
73 - len = len + string.len(str)
74 - if len >= 4096 then
83 + local strl = string.len(str)
84 + if len + strl >= 4095 then
75 85 len = 0
76 86 bytes[bn]='\n'
77 87 bn = bn + 1
78 88 end
89 + len = len + strl
79 90 bytes[bn] = str
80 91 bn = bn + 1
81 92 end
82 93
83 -table.insert(lines, strtpl:format(#bytes, table.concat(bytes)))
84 -table.insert(lines, main)
94 +local lines = {
95 + includes;
96 + strtpl:format(#bytes, table.concat(bytes));
97 + main;
98 +}
85 99
86 100 dest:write(table.concat(lines, '\n'))