Rizin
unix-like reverse engineering framework and cli tools
rz_egg.h
Go to the documentation of this file.
1 #ifndef RZ_EGG_H
2 #define RZ_EGG_H
3 
4 #include <rz_asm.h>
5 #include <rz_lib.h>
6 #include <rz_util.h>
7 #include <rz_syscall.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
14 
15 #define RZ_EGG_INCDIR_ENV "EGG_INCDIR"
16 #define RZ_EGG_INCDIR_PATH "/lib/rizin/" RZ_VERSION "/egg"
17 
18 // rename to RzEggShellcode
19 #define RZ_EGG_PLUGIN_SHELLCODE 0
20 #define RZ_EGG_PLUGIN_ENCODER 1
21 
22 typedef struct rz_egg_plugin_t {
23  const char *name;
24  const char *desc;
25  int type;
26  RzBuffer *(*build)(void *egg);
28 
29 typedef struct rz_egg_lang_t {
30  int pushargs;
31  int nalias;
32  int nsyscalls;
33  char *conditionstr;
34  char *syscallbody;
35  char *includefile;
36  char *setenviron;
37  char *mathline;
38  // used for confusing mathop
40  int varsize;
41  int varxs;
43  int nargs;
44  int docall;
46  int nbrackets;
47  int slurpin;
48  int slurp;
49  int line;
50  char elem[1024];
51  int attsyntax;
52  int elem_n;
53  char *callname;
54  char *endframe;
55  char *ctxpush[32];
56  char *file;
57  char *dstvar;
58  char *dstval;
59  char *includedir;
60  char *ifelse_table[32][32];
61  // used to solve if-else problem in a not so ugly way
62  int ndstval;
63  int skipline; // BOOL
64  int quoteline;
68  int oc;
69  int mode;
70  int inlinectr;
71  struct {
72  char *name;
73  char *body;
74  // int fastcall; /* TODO: NOT YET USED */
75  } inlines[256];
76  int ninlines;
77  struct {
78  char *name;
79  char *arg;
80  } syscalls[256];
81  struct {
82  char *name;
83  char *content;
84  } aliases[256];
85  char *nested[32];
86  char *nested_callname[32];
87  // char *nestede[32] = {0};
88  // seems nestede are not used any more
89  // (only one place that gives nestede[] value, where could be replaced)
90  int nestedi[32];
92 
93 typedef struct rz_egg_t {
98  // RzList *shellcodes; // XXX is plugins nao?
102  Sdb *db;
104  RzList *patches; // <RzBuffer>
106  int arch;
107  int endian;
108  int bits;
110  int context;
112 
113 /* XXX: this may fail in different arches */
114 #if 0
115 rizin -q - <<EOF
116 ?e #define RZ_EGG_OS_LINUX \`?h linux\`
117 ?e #define RZ_EGG_OS_OSX \`?h osx\`
118 ?e #define RZ_EGG_OS_DARWIN \`?h darwin\`
119 ?e #define RZ_EGG_OS_MACOS \`?h macos\`
120 ?e #define RZ_EGG_OS_W32 \`?h w32\`
121 ?e #define RZ_EGG_OS_WINDOWS \`?h windows\`
122 ?e #define RZ_EGG_OS_BEOS \`?h beos\`
123 ?e #define RZ_EGG_OS_FREEBSD \`?h freebsd\`
124 EOF
125 #endif
126 
127 #define RZ_EGG_OS_LINUX 0x5ca62a43
128 #define RZ_EGG_OS_OSX 0x0ad593a1
129 #define RZ_EGG_OS_DARWIN 0xd86d1ae2
130 #define RZ_EGG_OS_WATCHOS 0x14945c70
131 #define RZ_EGG_OS_IOS 0x0ad58830
132 #define RZ_EGG_OS_MACOS 0x5cb23c16
133 #define RZ_EGG_OS_W32 0x0ad5fbb3
134 #define RZ_EGG_OS_WINDOWS 0x05b7de9a
135 #define RZ_EGG_OS_BEOS 0x506108be
136 #define RZ_EGG_OS_FREEBSD 0x73a72944
137 
138 #if __APPLE__
139 #define RZ_EGG_OS_DEFAULT RZ_EGG_OS_OSX
140 #define RZ_EGG_OS_NAME "darwin"
141 #define RZ_EGG_FORMAT_DEFAULT "mach0"
142 #elif __WINDOWS__
143 #define RZ_EGG_OS_DEFAULT RZ_EGG_OS_W32
144 #define RZ_EGG_OS_NAME "windows"
145 #define RZ_EGG_FORMAT_DEFAULT "pe"
146 #else
147 #define RZ_EGG_OS_DEFAULT RZ_EGG_OS_LINUX
148 #define RZ_EGG_OS_NAME "linux"
149 #define RZ_EGG_FORMAT_DEFAULT "elf"
150 #endif
151 
152 typedef struct rz_egg_emit_t {
153  const char *arch;
154  int size; /* in bytes.. 32bit arch is 4, 64bit is 8 .. */
155  const char *retvar;
156  // const char *syscall_body;
157  const char *(*regs)(RzEgg *egg, int idx);
158  void (*init)(RzEgg *egg);
159  void (*call)(RzEgg *egg, const char *addr, int ptr);
160  void (*jmp)(RzEgg *egg, const char *addr, int ptr);
161  // void (*sc)(int num);
162  void (*frame)(RzEgg *egg, int sz);
163  char *(*syscall)(RzEgg *egg, int num);
164  void (*trap)(RzEgg *egg);
165  void (*frame_end)(RzEgg *egg, int sz, int ctx);
166  void (*comment)(RzEgg *egg, const char *fmt, ...);
167  void (*push_arg)(RzEgg *egg, int xs, int num, const char *str);
168  void (*set_string)(RzEgg *egg, const char *dstvar, const char *str, int j);
169  void (*equ)(RzEgg *egg, const char *key, const char *value);
170  void (*get_result)(RzEgg *egg, const char *ocn);
171  void (*restore_stack)(RzEgg *egg, int size);
172  void (*syscall_args)(RzEgg *egg, int nargs);
173  void (*get_var)(RzEgg *egg, int type, char *out, int idx);
174  void (*get_ar)(RzEgg *egg, char *out, int idx);
175  void (*while_end)(RzEgg *egg, const char *label);
176  void (*load)(RzEgg *egg, const char *str, int sz);
177  void (*load_ptr)(RzEgg *egg, const char *str);
178  void (*branch)(RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst);
179  void (*mathop)(RzEgg *egg, int ch, int sz, int type, const char *eq, const char *p);
180  void (*get_while_end)(RzEgg *egg, char *out, const char *ctxpush, const char *label);
182 
183 #ifdef RZ_API
184 RZ_API RzEgg *rz_egg_new(void);
185 RZ_API void rz_egg_lang_init(RzEgg *egg);
186 RZ_API void rz_egg_lang_free(RzEgg *egg);
187 RZ_API const char *rz_egg_os_as_string(int os);
188 RZ_API char *rz_egg_to_string(RzEgg *egg);
189 RZ_API void rz_egg_free(RzEgg *egg);
191 RZ_API void rz_egg_reset(RzEgg *egg);
192 RZ_API bool rz_egg_setup(RzEgg *egg, const char *arch, int bits, int endian, const char *os);
193 RZ_API int rz_egg_include(RzEgg *egg, const char *file, int format);
194 RZ_API void rz_egg_load(RzEgg *egg, const char *code, int format);
195 RZ_API bool rz_egg_load_file(RzEgg *egg, const char *file);
196 RZ_API void rz_egg_syscall(RzEgg *egg, const char *arg, ...) RZ_PRINTF_CHECK(2, 3);
197 RZ_API void rz_egg_alloc(RzEgg *egg, int n);
198 RZ_API void rz_egg_label(RzEgg *egg, const char *name);
199 RZ_API int rz_egg_raw(RzEgg *egg, const ut8 *b, int len);
200 RZ_API int rz_egg_encode(RzEgg *egg, const char *name);
201 RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name);
202 #define rz_egg_get_shellcodes(x) x->plugins
203 RZ_API void rz_egg_option_set(RzEgg *egg, const char *k, const char *v);
204 RZ_API char *rz_egg_option_get(RzEgg *egg, const char *k);
205 RZ_API void rz_egg_if(RzEgg *egg, const char *reg, char cmp, int v);
206 RZ_API void rz_egg_printf(RzEgg *egg, const char *fmt, ...) RZ_PRINTF_CHECK(2, 3);
207 RZ_API int rz_egg_compile(RzEgg *egg);
208 RZ_API int rz_egg_padding(RzEgg *egg, const char *pad);
209 RZ_API bool rz_egg_assemble(RzEgg *egg);
210 RZ_API bool rz_egg_assemble_asm(RzEgg *egg, char **asm_list);
211 RZ_API void rz_egg_pattern(RzEgg *egg, int size);
212 // RZ_API int rz_egg_dump (RzEgg *egg, const char *file) { }
213 RZ_API char *rz_egg_get_source(RzEgg *egg);
215 RZ_API char *rz_egg_get_assembly(RzEgg *egg);
216 RZ_API void rz_egg_append(RzEgg *egg, const char *src);
217 RZ_API int rz_egg_run(RzEgg *egg);
218 RZ_API int rz_egg_run_rop(RzEgg *egg);
219 RZ_API int rz_egg_patch(RzEgg *egg, int off, const ut8 *b, int l);
220 RZ_API bool rz_egg_patch_num(RzEgg *egg, int off, ut64 val, ut32 bits);
221 RZ_API void rz_egg_finalize(RzEgg *egg);
222 
223 /* rz_egg_Cfile.c */
224 RZ_API char *rz_egg_Cfile_parser(const char *file, const char *arch, const char *os, int bits);
225 
226 /* lang.c */
227 RZ_API char *rz_egg_mkvar(RzEgg *egg, char *out, const char *_str, int delta);
228 RZ_API int rz_egg_lang_parsechar(RzEgg *egg, char c);
231 
232 /* plugin pointers */
236 #endif
237 
238 #ifdef __cplusplus
239 }
240 #endif
241 
242 #endif
size_t len
Definition: 6502dis.c:15
#define e(frag)
lzma_index * src
Definition: index.h:567
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
ut16 val
Definition: armass64_const.h:6
int bits(struct state *s, int need)
Definition: blast.c:72
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static int value
Definition: cmd_api.c:93
#define RZ_API
cs_arch arch
Definition: cstool.c:13
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
uint32_t ut32
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
RZ_API int rz_egg_shellcode(RzEgg *egg, const char *name)
Definition: egg.c:538
RZ_API int rz_egg_run_rop(RzEgg *egg)
Definition: egg.c:450
RZ_API void rz_egg_reset(RzEgg *egg)
Definition: egg.c:128
RZ_API char * rz_egg_get_source(RzEgg *egg)
Definition: egg.c:430
RZ_API char * rz_egg_to_string(RzEgg *egg)
Definition: egg.c:107
RZ_API void rz_egg_append(RzEgg *egg, const char *src)
Definition: egg.c:438
RZ_API void rz_egg_free(RzEgg *egg)
Definition: egg.c:111
RZ_API void rz_egg_finalize(RzEgg *egg)
Definition: egg.c:598
RZ_API RzEgg * rz_egg_new(void)
Definition: egg.c:44
RZ_API bool rz_egg_patch_num(RzEgg *egg, int off, ut64 num, ut32 bits)
Definition: egg.c:591
RZ_API bool rz_egg_load_file(RzEgg *egg, const char *file)
Definition: egg.c:214
RZ_API char * rz_egg_option_get(RzEgg *egg, const char *key)
Definition: egg.c:534
RZ_API int rz_egg_padding(RzEgg *egg, const char *pad)
Definition: egg.c:472
RZ_API int rz_egg_run(RzEgg *egg)
Definition: egg.c:443
RZ_API void rz_egg_syscall(RzEgg *egg, const char *arg,...)
Definition: egg.c:255
RZ_API int rz_egg_add(RzEgg *a, RzEggPlugin *foo)
Definition: egg.c:92
RZ_API int rz_egg_patch(RzEgg *egg, int off, const ut8 *buf, int len)
Definition: egg.c:576
RZ_API void rz_egg_pattern(RzEgg *egg, int size)
Definition: egg.c:625
RZ_API bool rz_egg_assemble_asm(RzEgg *egg, char **asm_list)
Definition: egg.c:346
RZ_API void rz_egg_alloc(RzEgg *egg, int n)
Definition: egg.c:268
RZ_API void rz_egg_label(RzEgg *egg, const char *name)
Definition: egg.c:272
RZ_API void rz_egg_if(RzEgg *egg, const char *reg, char cmp, int v)
Definition: egg.c:332
RZ_API char * rz_egg_get_assembly(RzEgg *egg)
Definition: egg.c:434
RZ_API int rz_egg_encode(RzEgg *egg, const char *name)
Definition: egg.c:558
RZ_API const char * rz_egg_os_as_string(int os)
Definition: egg.c:28
RZ_API void rz_egg_printf(RzEgg *egg, const char *fmt,...)
Definition: egg.c:336
RZ_API int rz_egg_compile(RzEgg *egg)
Definition: egg.c:394
RZ_API RzBuffer * rz_egg_get_bin(RzEgg *egg)
Definition: egg.c:423
RZ_API void rz_egg_option_set(RzEgg *egg, const char *key, const char *val)
Definition: egg.c:530
RZ_API void rz_egg_load(RzEgg *egg, const char *code, int format)
Definition: egg.c:202
RZ_API int rz_egg_include(RzEgg *egg, const char *file, int format)
Definition: egg.c:181
RZ_API bool rz_egg_assemble(RzEgg *egg)
Definition: egg.c:390
RZ_API bool rz_egg_setup(RzEgg *egg, const char *arch, int bits, int endian, const char *os)
Definition: egg.c:139
RZ_API int rz_egg_raw(RzEgg *egg, const ut8 *b, int len)
Definition: egg.c:281
RZ_API char * rz_egg_Cfile_parser(const char *file, const char *arch, const char *os, int bits)
Definition: egg_Cfile.c:247
RzEggPlugin rz_egg_plugin_exec
Definition: egg_exec.c:161
RZ_API void rz_egg_lang_include_path(RzEgg *egg, const char *path)
Definition: egg_lang.c:149
RZ_API void rz_egg_lang_init(RzEgg *egg)
Definition: egg_lang.c:126
RZ_API char * rz_egg_mkvar(RzEgg *egg, char *out, const char *_str, int delta)
Definition: egg_lang.c:538
RZ_API int rz_egg_lang_parsechar(RzEgg *egg, char c)
Definition: egg_lang.c:1235
RZ_API void rz_egg_lang_include_init(RzEgg *egg)
Definition: egg_lang.c:164
RZ_API void rz_egg_lang_free(RzEgg *egg)
Definition: egg_lang.c:136
RzEggPlugin rz_egg_plugin_shya
Definition: egg_shya.c:25
RzEggPlugin rz_egg_plugin_xor
Definition: egg_xor.c:104
struct @667 g
voidpf void uLong size
Definition: ioapi.h:138
#define reg(n)
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
#define const
Definition: ansidecl.h:240
static RzMain foo[]
Definition: main.c:11
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
char * dst
Definition: lz4.h:724
int n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
int idx
Definition: setup.py:197
int off
Definition: pal.c:13
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
struct rz_egg_emit_t RzEggEmit
#define RZ_EGG_OS_DARWIN
Definition: rz_egg.h:129
#define RZ_EGG_OS_WINDOWS
Definition: rz_egg.h:134
struct rz_egg_lang_t RzEggLang
#define RZ_EGG_OS_MACOS
Definition: rz_egg.h:132
#define RZ_EGG_OS_BEOS
Definition: rz_egg.h:135
struct rz_egg_plugin_t RzEggPlugin
struct rz_egg_t RzEgg
#define RZ_EGG_OS_OSX
Definition: rz_egg.h:128
#define RZ_EGG_OS_W32
Definition: rz_egg.h:133
#define RZ_EGG_OS_FREEBSD
Definition: rz_egg.h:136
#define RZ_EGG_OS_LINUX
Definition: rz_egg.h:127
RZ_LIB_VERSION_HEADER(rz_egg)
#define RZ_PRINTF_CHECK(fmt, dots)
Definition: rz_types.h:192
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
#define h(i)
Definition: sha256.c:48
Definition: inftree9.h:24
Definition: gzappend.c:170
Definition: dis.h:35
Definition: z80asm.h:102
void(* jmp)(RzEgg *egg, const char *addr, int ptr)
Definition: rz_egg.h:160
void(* set_string)(RzEgg *egg, const char *dstvar, const char *str, int j)
Definition: rz_egg.h:168
void(* frame_end)(RzEgg *egg, int sz, int ctx)
Definition: rz_egg.h:165
void(* frame)(RzEgg *egg, int sz)
Definition: rz_egg.h:162
void(* mathop)(RzEgg *egg, int ch, int sz, int type, const char *eq, const char *p)
Definition: rz_egg.h:179
void(* load)(RzEgg *egg, const char *str, int sz)
Definition: rz_egg.h:176
void(* get_var)(RzEgg *egg, int type, char *out, int idx)
Definition: rz_egg.h:173
void(* comment)(RzEgg *egg, const char *fmt,...)
Definition: rz_egg.h:166
void(* while_end)(RzEgg *egg, const char *label)
Definition: rz_egg.h:175
void(* call)(RzEgg *egg, const char *addr, int ptr)
Definition: rz_egg.h:159
void(* branch)(RzEgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst)
Definition: rz_egg.h:178
void(* init)(RzEgg *egg)
Definition: rz_egg.h:158
void(* trap)(RzEgg *egg)
Definition: rz_egg.h:164
const char * arch
Definition: rz_egg.h:153
void(* load_ptr)(RzEgg *egg, const char *str)
Definition: rz_egg.h:177
void(* get_ar)(RzEgg *egg, char *out, int idx)
Definition: rz_egg.h:174
void(* get_while_end)(RzEgg *egg, char *out, const char *ctxpush, const char *label)
Definition: rz_egg.h:180
void(* equ)(RzEgg *egg, const char *key, const char *value)
Definition: rz_egg.h:169
const char * retvar
Definition: rz_egg.h:155
void(* push_arg)(RzEgg *egg, int xs, int num, const char *str)
Definition: rz_egg.h:167
void(* syscall_args)(RzEgg *egg, int nargs)
Definition: rz_egg.h:172
void(* get_result)(RzEgg *egg, const char *ocn)
Definition: rz_egg.h:170
void(* restore_stack)(RzEgg *egg, int size)
Definition: rz_egg.h:171
int attsyntax
Definition: rz_egg.h:51
int nsyscalls
Definition: rz_egg.h:32
int elem_n
Definition: rz_egg.h:52
char * body
Definition: rz_egg.h:73
char * setenviron
Definition: rz_egg.h:36
int nbrackets
Definition: rz_egg.h:46
char * file
Definition: rz_egg.h:56
int quoteline
Definition: rz_egg.h:64
char * syscallbody
Definition: rz_egg.h:34
int mode
Definition: rz_egg.h:69
int inlinectr
Definition: rz_egg.h:70
char * conditionstr
Definition: rz_egg.h:33
int stackfixed
Definition: rz_egg.h:67
int ninlines
Definition: rz_egg.h:76
int nestedi[32]
Definition: rz_egg.h:90
char * includefile
Definition: rz_egg.h:35
struct rz_egg_lang_t::@276 syscalls[256]
char * mathline
Definition: rz_egg.h:37
int slurpin
Definition: rz_egg.h:47
int stackframe
Definition: rz_egg.h:66
char * arg
Definition: rz_egg.h:79
int ndstval
Definition: rz_egg.h:62
int pushargs
Definition: rz_egg.h:30
int nalias
Definition: rz_egg.h:31
char * content
Definition: rz_egg.h:83
char * ctxpush[32]
Definition: rz_egg.h:55
struct rz_egg_lang_t::@277 aliases[256]
int nargs
Definition: rz_egg.h:43
char elem[1024]
Definition: rz_egg.h:50
char * dstval
Definition: rz_egg.h:58
int nfunctions
Definition: rz_egg.h:45
char * dstvar
Definition: rz_egg.h:57
int varsize
Definition: rz_egg.h:40
int varxs
Definition: rz_egg.h:41
char * nested[32]
Definition: rz_egg.h:85
int slurp
Definition: rz_egg.h:48
char * name
Definition: rz_egg.h:72
struct rz_egg_lang_t::@275 inlines[256]
char * ifelse_table[32][32]
Definition: rz_egg.h:60
char * endframe
Definition: rz_egg.h:54
int docall
Definition: rz_egg.h:44
char * includedir
Definition: rz_egg.h:59
char * nested_callname[32]
Definition: rz_egg.h:86
int quotelinevar
Definition: rz_egg.h:65
int lastctxdelta
Definition: rz_egg.h:42
int commentmode
Definition: rz_egg.h:39
char * callname
Definition: rz_egg.h:53
int skipline
Definition: rz_egg.h:63
int line
Definition: rz_egg.h:49
const char * name
Definition: rz_egg.h:23
const char * desc
Definition: rz_egg.h:24
RzAsm * rasm
Definition: rz_egg.h:99
RzList * plugins
Definition: rz_egg.h:103
int context
Definition: rz_egg.h:110
int endian
Definition: rz_egg.h:107
struct rz_egg_emit_t * remit
Definition: rz_egg.h:105
int bits
Definition: rz_egg.h:108
RzBuffer * bin
Definition: rz_egg.h:96
RzEggLang lang
Definition: rz_egg.h:101
RzSyscall * syscall
Definition: rz_egg.h:100
RzList * patches
Definition: rz_egg.h:104
int arch
Definition: rz_egg.h:106
Sdb * db
Definition: rz_egg.h:102
RzBuffer * buf
Definition: rz_egg.h:95
RzBuffer * src
Definition: rz_egg.h:94
ut32 os
Definition: rz_egg.h:109
RzList * list
Definition: rz_egg.h:97
Definition: sdb.h:63
static st64 delta
Definition: vmenus.c:2425
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58