1 #ifndef R2_EGG_H 2 #define R2_EGG_H 3 4 #include <r_asm.h> 5 #include <r_lib.h> 6 #include <r_util.h> 7 #include <r_syscall.h> 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 R_LIB_VERSION_HEADER(r_egg); 14 15 #define R_EGG_INCDIR_ENV "EGG_INCDIR" 16 #define R_EGG_INCDIR_PATH "/lib/radare2/" R2_VERSION "/egg" 17 18 // rename to REggShellcode 19 #define R_EGG_PLUGIN_SHELLCODE 0 20 #define R_EGG_PLUGIN_ENCODER 1 21 22 typedef struct r_egg_plugin_t { 23 const char *name; 24 const char *desc; 25 int type; 26 RBuffer* (*build) (void *egg); 27 } REggPlugin; 28 29 typedef struct r_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 39 int commentmode; 40 int varsize; 41 int varxs; 42 int lastctxdelta; 43 int nargs; 44 int docall; 45 int nfunctions; 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; 65 int quotelinevar; 66 int stackframe; 67 int stackfixed; 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]; 91 } REggLang; 92 93 typedef struct r_egg_t { 94 RBuffer *src; 95 RBuffer *buf; 96 RBuffer *bin; 97 RList *list; 98 //RList *shellcodes; // XXX is plugins nao? 99 RAsm *rasm; 100 RSyscall *syscall; 101 REggLang lang; 102 Sdb *db; 103 RList *plugins; 104 RList *patches; // <RBuffer> 105 struct r_egg_emit_t *remit; 106 int arch; 107 int endian; 108 int bits; 109 ut32 os; 110 int context; 111 } REgg; 112 113 /* XXX: this may fail in different arches */ 114 #if 0 115 r2 -q - <<EOF 116 ?e #define R_EGG_OS_LINUX \`?h linux\` 117 ?e #define R_EGG_OS_OSX \`?h osx\` 118 ?e #define R_EGG_OS_DARWIN \`?h darwin\` 119 ?e #define R_EGG_OS_MACOS \`?h macos\` 120 ?e #define R_EGG_OS_W32 \`?h w32\` 121 ?e #define R_EGG_OS_WINDOWS \`?h windows\` 122 ?e #define R_EGG_OS_BEOS \`?h beos\` 123 ?e #define R_EGG_OS_FREEBSD \`?h freebsd\` 124 EOF 125 #endif 126 127 #define R_EGG_OS_LINUX 0x5ca62a43 128 #define R_EGG_OS_OSX 0x0ad593a1 129 #define R_EGG_OS_DARWIN 0xd86d1ae2 130 #define R_EGG_OS_WATCHOS 0x14945c70 131 #define R_EGG_OS_IOS 0x0ad58830 132 #define R_EGG_OS_MACOS 0x5cb23c16 133 #define R_EGG_OS_W32 0x0ad5fbb3 134 #define R_EGG_OS_WINDOWS 0x05b7de9a 135 #define R_EGG_OS_BEOS 0x506108be 136 #define R_EGG_OS_FREEBSD 0x73a72944 137 138 #if __APPLE__ 139 #define R_EGG_OS_DEFAULT R_EGG_OS_OSX 140 #define R_EGG_OS_NAME "darwin" 141 #define R_EGG_FORMAT_DEFAULT "mach0" 142 #elif __WINDOWS__ 143 #define R_EGG_OS_DEFAULT R_EGG_OS_W32 144 #define R_EGG_OS_NAME "windows" 145 #define R_EGG_FORMAT_DEFAULT "pe" 146 #else 147 #define R_EGG_OS_DEFAULT R_EGG_OS_LINUX 148 #define R_EGG_OS_NAME "linux" 149 #define R_EGG_FORMAT_DEFAULT "elf" 150 #endif 151 152 typedef struct r_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)(REgg *egg, int idx); 158 void (*init)(REgg *egg); 159 void (*call)(REgg *egg, const char *addr, int ptr); 160 void (*jmp)(REgg *egg, const char *addr, int ptr); 161 //void (*sc)(int num); 162 void (*frame)(REgg *egg, int sz); 163 char *(*syscall)(REgg *egg, int num); 164 void (*trap)(REgg *egg); 165 void (*frame_end)(REgg *egg, int sz, int ctx); 166 void (*comment)(REgg *egg, const char *fmt, ...); 167 void (*push_arg)(REgg *egg, int xs, int num, const char *str); 168 void (*set_string)(REgg *egg, const char *dstvar, const char *str, int j); 169 void (*equ)(REgg *egg, const char *key, const char *value); 170 void (*get_result)(REgg *egg, const char *ocn); 171 void (*restore_stack)(REgg *egg, int size); 172 void (*syscall_args)(REgg *egg, int nargs); 173 void (*get_var)(REgg *egg, int type, char *out, int idx); 174 void (*get_ar)(REgg *egg, char *out, int idx); 175 void (*while_end)(REgg *egg, const char *label); 176 void (*load)(REgg *egg, const char *str, int sz); 177 void (*load_ptr)(REgg *egg, const char *str); 178 void (*branch)(REgg *egg, char *b, char *g, char *e, char *n, int sz, const char *dst); 179 void (*mathop)(REgg *egg, int ch, int sz, int type, const char *eq, const char *p); 180 void (*get_while_end)(REgg *egg, char *out, const char *ctxpush, const char *label); 181 } REggEmit; 182 183 #ifdef R_API 184 R_API REgg *r_egg_new (void); 185 R_API void r_egg_lang_init(REgg *egg); 186 R_API void r_egg_lang_free(REgg *egg); 187 R_API char *r_egg_to_string (REgg *egg); 188 R_API void r_egg_free (REgg *egg); 189 R_API int r_egg_add (REgg *a, REggPlugin *foo); 190 R_API void r_egg_reset (REgg *egg); 191 R_API int r_egg_setup(REgg *egg, const char *arch, int bits, int endian, const char *os); 192 R_API int r_egg_include(REgg *egg, const char *file, int format); 193 R_API void r_egg_load(REgg *egg, const char *code, int format); 194 R_API void r_egg_syscall(REgg *egg, const char *arg, ...) R_PRINTF_CHECK(2, 3); 195 R_API void r_egg_alloc(REgg *egg, int n); 196 R_API void r_egg_label(REgg *egg, const char *name); 197 R_API int r_egg_raw(REgg *egg, const ut8 *b, int len); 198 R_API int r_egg_encode(REgg *egg, const char *name); 199 R_API int r_egg_shellcode(REgg *egg, const char *name); 200 #define r_egg_get_shellcodes(x) x->plugins 201 R_API void r_egg_option_set (REgg *egg, const char *k, const char *v); 202 R_API char *r_egg_option_get (REgg *egg, const char *k); 203 R_API void r_egg_if(REgg *egg, const char *reg, char cmp, int v); 204 R_API void r_egg_printf(REgg *egg, const char *fmt, ...) R_PRINTF_CHECK(2, 3); 205 R_API int r_egg_compile(REgg *egg); 206 R_API int r_egg_padding (REgg *egg, const char *pad); 207 R_API bool r_egg_assemble(REgg *egg); 208 R_API bool r_egg_assemble_asm(REgg *egg, char **asm_list); 209 R_API void r_egg_pattern(REgg *egg, int size); 210 R_API RBuffer *r_egg_get_bin(REgg *egg); 211 //R_API int r_egg_dump (REgg *egg, const char *file) { } 212 R_API char *r_egg_get_source(REgg *egg); 213 R_API RBuffer *r_egg_get_bin(REgg *egg); 214 R_API char *r_egg_get_assembly(REgg *egg); 215 R_API void r_egg_append(REgg *egg, const char *src); 216 R_API int r_egg_run(REgg *egg); 217 R_API int r_egg_run_rop(REgg *egg); 218 R_API int r_egg_patch(REgg *egg, int off, const ut8 *b, int l); 219 R_API void r_egg_finalize(REgg *egg); 220 221 /* r_egg_Cfile.c */ 222 R_API char* r_egg_Cfile_parser(const char *file, const char *arch, const char *os, int bits); 223 224 /* lang.c */ 225 R_API char *r_egg_mkvar(REgg *egg, char *out, const char *_str, int delta); 226 R_API int r_egg_lang_parsechar(REgg *egg, char c); 227 R_API void r_egg_lang_include_path (REgg *egg, const char *path); 228 R_API void r_egg_lang_include_init (REgg *egg); 229 230 /* plugin pointers */ 231 extern REggPlugin r_egg_plugin_xor; 232 extern REggPlugin r_egg_plugin_shya; 233 extern REggPlugin r_egg_plugin_exec; 234 #endif 235 236 #ifdef __cplusplus 237 } 238 #endif 239 240 #endif 241