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
|
/***************************************************Project Mutator****************************************************/
/*first line intentionally left blank.*/
/*bruiser's lua asmrewriter implementation for jump tables*/
/*Copyright (C) 2018 Farzad Sadeghi
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/
/**********************************************************************************************************************/
#include "./lua-5.3.4/src/lua.h"
#include "./lua-5.3.4/src/lauxlib.h"
#include "./lua-5.3.4/src/lualib.h"
#include "./bruisercapstone.h"
#include "./asmrewriter.h"
#include <inttypes.h>
/**********************************************************************************************************************/
static JMP_S_T* convert_jmpt(lua_State* __ls, int index) {
JMP_S_T* dummy = (JMP_S_T*)lua_touserdata(__ls, index);
//if (dummy == NULL) luaL_typerror(__ls, index, dummy);
return dummy;
}
static JMP_S_T* check_jmpt(lua_State* __ls, int index) {
JMP_S_T* dummy;
luaL_checktype(__ls, index, LUA_TUSERDATA);
dummy = (JMP_S_T*)luaL_checkudata(__ls, index, "jmp_s_t");
//if (dummy == NULL) luaL_typerror(__ls, index, dummy);
return dummy;
}
static JMP_S_T* push_jmpt(lua_State* __ls) {
JMP_S_T* dummy = (JMP_S_T*)lua_newuserdata(__ls, sizeof(JMP_S_T));
luaL_getmetatable(__ls, "jmp_s_t");
lua_setmetatable(__ls, -2);
return dummy;
}
static int new_jmpt(lua_State* __ls) {
JMP_T jmp_t = luaL_optinteger(__ls, 1, 0);
uint64_t location = luaL_optinteger(__ls, 2, 0);
uint8_t size = luaL_optinteger(__ls, 3, 0);
//
//
//
uint64_t address = luaL_optinteger(__ls, 7, 0);
uint64_t address_y = luaL_optinteger(__ls, 8, 0);
uint64_t address_n = luaL_optinteger(__ls, 9, 0);
unsigned char y = luaL_optinteger(__ls, 10, 0);
unsigned char n = luaL_optinteger(__ls, 11, 0);
unsigned char z = luaL_optinteger(__ls, 12, 0);
JMP_S_T* dummy = push_jmpt(__ls);
dummy->type = jmp_t;
dummy->location = location;
dummy->size = size;
//dummy->next =;
//dummy->next_y =;
//dummy->next_n =;
dummy->address = address;
dummy->address_y = address_y;
dummy->address_n = address_n;
dummy->y = y;
dummy->n = n;
dummy->z = z;
return 1;
}
static int jmpt_custom(lua_State* __ls) {
JMP_S_T* dummy = check_jmpt(__ls, 1);
printf("this is the jump table custom function.\n");
lua_pushnumber(__ls, dummy->type);
lua_pushnumber(__ls, dummy->location);
lua_pushnumber(__ls, dummy->size);
lua_pushlightuserdata(__ls, dummy->next);
lua_pushlightuserdata(__ls, dummy->next_y);
lua_pushlightuserdata(__ls, dummy->next_n);
lua_pushnumber(__ls, dummy->address);
lua_pushnumber(__ls, dummy->address_y);
lua_pushnumber(__ls, dummy->address_n);
lua_pushnumber(__ls, dummy->y);
lua_pushnumber(__ls, dummy->n);
lua_pushnumber(__ls, dummy->z);
return 12;
}
#define SET_GENERATOR(X) \
static int jmpt_set_##X(lua_State* __ls) {\
JMP_S_T* dummy = check_jmpt(__ls,1);\
dummy->type = luaL_checkinteger(__ls, 2);\
lua_settop(__ls, 1);\
return 1;\
}
#define X_LIST_GEN \
X(type, "setter method for type")\
X(location, "setter method for location")\
X(size, "setter method for size")\
X(address, "setter method for address")\
X(address_y, "setter method for address_y")\
X(address_n, "setter method for address_n")\
X(y, "setter method for y")\
X(n, "setter method for n")\
X(z, "setter method for z")
#define X(X1,X2) SET_GENERATOR(X1)
X_LIST_GEN
#undef X
#undef X_LIST_GEN
#undef SET_GENERATOR
static int jmpt_set_next(lua_State* __ls) {}
static int jmpt_set_next_y(lua_State* __ls) {}
static int jmpt_set_next_n(lua_State* __ls) {}
static int jmpt_gc(lua_State* __ls) {}
static const luaL_Reg jmpt_methods[] = {
{"new", new_jmpt},
{"set_type", jmpt_set_type},
{"set_location", jmpt_set_location},
{"set_size", jmpt_set_size},
{"set_address", jmpt_set_address},
{"set_address_y", jmpt_set_address_y},
{"set_address_n", jmpt_set_address_n},
{"set_next", jmpt_set_next},
{"set_next_y", jmpt_set_next_y},
{"set_next_n", jmpt_set_next_n},
{"set_y", jmpt_set_y},
{"set_n", jmpt_set_n},
{"set_z", jmpt_set_z},
{0,0}
};
static const luaL_Reg jmpt_meta[] = {
{"__gc", jmpt_gc},
{0, 0}
};
int jmpt_register(lua_State* __ls) {
luaL_openlib(__ls, "jmp_s_t", jmpt_methods, 0);
luaL_newmetatable(__ls, "jmp_s_t");
luaL_openlib(__ls, 0, jmpt_meta, 0);
lua_pushliteral(__ls, "__index");
lua_pushvalue(__ls, -3);
lua_rawset(__ls, -3);
lua_pushliteral(__ls, "__metatable");
lua_pushvalue(__ls, -3);
lua_rawset(__ls, -3);
lua_pop(__ls, 1);
return 1;
}
//@DEVI-after jmpt_register, the methods are still on the stack. remove them by lua_pop(__ls, 1)
/**********************************************************************************************************************/
//@DEVI-the main is only meant for testing
#pragma weak main
int main(int argc, char** argv) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
jmpt_register(L);
lua_pop(L, 1);
if (argc > 1) luaL_dofile(L, argv[1]);
lua_close(L);
return 0;
}
/**********************************************************************************************************************/
/*last line intentionally left blank.*/
|