1 /* 2 ** elfsh.h for elfsh 3 ** 4 ** Started on Thu Feb 22 07:19:04 2001 mayhem 5 ** Last update Fri Aug 15 22:43:41 2003 jv 6 */ 7 8 #ifndef __ELFSH_H_ 9 #define __ELFSH_H_ 10 11 #include <sys/types.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <signal.h> 16 #include <sys/wait.h> 17 #include <assert.h> 18 #include <setjmp.h> 19 #include <time.h> 20 #include <ctype.h> 21 #include <regex.h> 22 #include <dlfcn.h> 23 #include <libelfsh.h> 24 #include <libhash.h> 25 26 27 /* Thanks to DH for libasm, choose to disable it in elfsh/elfsh/Makefile */ 28 #if defined(USE_LIBASM) 29 #include <libasm.h> 30 extern asm_processor proc; 31 #endif 32 33 /* Thanks to GNU we have readline */ 34 #if defined(USE_READLN) 35 #include <readline/readline.h> 36 #include <readline/history.h> 37 #endif 38 39 /* Now comes DEBUGGING variables for various part of the code */ 40 #define __DEBUG_DISASM__ 0 41 #define __DEBUG_SIGHANDLER__ 0 42 #define __DEBUG_MODEL__ 0 43 #define __DEBUG_SCANNER__ 0 44 #define __DEBUG_ASLR__ 0 45 46 /* General usage macros */ 47 #define FATAL(a) { perror(a); exit(-1); } 48 #define QUIT_ERROR(a) { elfsh_error(); exit(a); } 49 #define RET(a) { /* elfsh_error(); */ return (a); } 50 #define RETERR(a) { fprintf(stderr, "%s \n", a); return (-1); } 51 #define PERROR_RET(a, b) { perror(a); return (b); } 52 #define PRINTABLE(c) (c >= 32 && c <= 126) 53 #define REGX_IS_USED(a) a 54 #define IS_VADDR(s) (s[0] == '0' && (s[1] == 'X' || s[1] == 'x')) 55 #define IS_BLANK(c) (c == ' ' || c == '\t') 56 57 /* Some useful macros */ 58 #define CHOOSE_REGX(r) r = (world.args.use_regx ? &world.args.regx : \ 59 world.state.vm_use_regx ? &world.state.vm_regx : \ 60 NULL) 61 62 63 /* Used to store ascii description for different structures types in data.c */ 64 #define ELFSH_SEGTYPE_MAX 7 65 #define ELFSH_SHTYPE_MAX 12 66 #define ELFSH_OBJTYPE_MAX 5 67 #define ELFSH_SYMBIND_MAX 3 68 #define ELFSH_SYMTYPE_MAX 7 69 #define ELFSH_ENCODING_MAX 3 70 #define ELFSH_DYNAMIC_MAX 35 71 #define ELFSH_EXTDYN_MAX 19 72 #define ELFSH_ARCHTYPE_MAX 55 73 #define ELFSH_ARCHTYPE_MAX 55 74 #define ELFSH_STAB_MAX 256 75 76 #define ELFSH_RELOC_i386_MAX 11 77 #define ELFSH_RELOC_SPARC_MAX 24 78 #define ELFSH_RELOC_MAX(file) vm_getmaxrelnbr(file) 79 80 81 #define ELFSH_FEATURE_MAX 2 82 #define ELFSH_POSFLAG_MAX 2 83 #define ELFSH_FLAGS_MAX 4 84 #define ELFSH_FLAGS1_MAX 15 85 86 /* ELFsh general parameters */ 87 #define ELFSH_FIELD_SEP "." 88 #define ELFSH_COMMENT_START '#' 89 #define ELFSH_MINUS '-' 90 #define ELFSH_SLASH '/' 91 #define ELFSH_SPACE ' ' 92 #define ELFSH_VERSION "0.51b3" 93 #define ELFSH_PROMPT "[ELFsh-"ELFSH_VERSION"]$ " 94 #define ELFSH_SHELL "/bin/bash" 95 #define ELFSH_INIT "elfsh_init" 96 #define ELFSH_FINI "elfsh_fini" 97 #define ELFSH_MODPATH "/usr/share/elfsh/" 98 99 /* For elfsh/elfsh/disasm.c:display_object() */ 100 #define ELFSH_HEXA_VIEW 0 101 #define ELFSH_DISASM_VIEW 1 102 103 /* For elfsh/elfsh/modules.c:vm_change_handler() */ 104 #define ELFSH_ORIG ((void *) -1) 105 106 /* Commands */ 107 #define CMD_DISASM "disasm" 108 #define CMD_DISASM2 "D" 109 #define CMD_HEXA "hexa" 110 #define CMD_HEXA2 "X" 111 #define CMD_REL "rel" 112 #define CMD_REL2 "r" 113 #define CMD_DYNAMIC "dyn" 114 #define CMD_DYNAMIC2 "d" 115 #define CMD_SYM "sym" 116 #define CMD_SYM2 "syms" 117 #define CMD_DYNSYM "dynsym" 118 #define CMD_DYNSYM2 "ds" 119 #define CMD_SHT "sht" 120 #define CMD_SHT2 "s" 121 #define CMD_PHT "pht" 122 #define CMD_PHT2 "p" 123 #define CMD_STAB "stab" 124 #define CMD_STAB2 "st" 125 #define CMD_DWARF "dwarf" 126 #define CMD_DWARF2 "dw" 127 #define CMD_ELF "elf" 128 #define CMD_ELF2 "e" 129 #define CMD_INTERP "interp" 130 #define CMD_INTERP2 "i" 131 #define CMD_NOTE "notes" 132 #define CMD_NOTE2 "n" 133 #define CMD_GOT "got" 134 #define CMD_GOT2 "g" 135 #define CMD_CTORS "ctors" 136 #define CMD_CTORS2 "ct" 137 #define CMD_DTORS "dtors" 138 #define CMD_DTORS2 "dt" 139 #define CMD_SHTRM "shtrm" 140 #define CMD_COMMENT "comments" 141 #define CMD_COMMENT2 "c" 142 #define CMD_BINFILE_R "f" 143 #define CMD_BINFILE_W "w" 144 #define CMD_SET "set" 145 #define CMD_GET "get" 146 #define CMD_PRINT "print" 147 #define CMD_EXEC "exec" 148 #define CMD_ADD "add" 149 #define CMD_SUB "sub" 150 #define CMD_MUL "mul" 151 #define CMD_DIV "div" 152 #define CMD_MOD "mod" 153 #define CMD_INFO "info" 154 #define CMD_METACMD "!" 155 #define CMD_WRITE "write" 156 #define CMD_APPEND "append" 157 #define CMD_EXTEND "extend" 158 #define CMD_FIXUP "fixup" 159 #define CMD_FINDREL "findrel" 160 #define CMD_MODLOAD "modload" 161 #define CMD_MODULOAD "modunload" 162 #define CMD_HELP "help" 163 #define CMD_STRIP "strip" 164 #define CMD_SSTRIP "sstrip" 165 #define CMD_RELINJCT "reladd" 166 #define CMD_STOP "stop" 167 #define CMD_HIJACK "redir" 168 169 /* Prefixes */ 170 #define CMD_SORT "sort" 171 #define CMD_SORT2 "sr" 172 #define CMD_QUIET "q" 173 #define CMD_QUIET2 "quiet" 174 #define CMD_VERB "verb" 175 #define CMD_VERB2 "v" 176 #define CMD_ALL "all" 177 #define CMD_ALL2 "a" 178 179 /* Interactive only command */ 180 #define CMD_LOAD "load" 181 #define CMD_UNLOAD "unload" 182 #define CMD_SAVE "save" 183 #define CMD_QUIT "quit" 184 #define CMD_QUIT2 "exit" 185 #define CMD_SWITCH "switch" 186 #define CMD_LIST "list" 187 #define CMD_LIST2 "l" 188 189 190 191 /* Regx option, a module of struct s_args */ 192 typedef struct s_list 193 { 194 regex_t name; 195 char *rname; 196 u_int off; 197 u_int size; 198 char otype; 199 } elfshlist_t; 200 201 202 /* Structure for constants */ 203 typedef struct s_const 204 { 205 const char *desc; 206 const char *name; 207 long val; 208 } elfshconst_t; 209 210 211 /* Thats the command line options registering structure */ 212 typedef struct s_args 213 { 214 char *param[256]; /* option parameters */ 215 char use_regx; /* 1 if the option use a regx */ 216 regex_t regx; /* regx */ 217 elfshlist_t disasm; /* D/X parameters */ 218 } elfshargv_t; 219 220 /* ELFsh module structure */ 221 typedef struct s_module 222 { 223 char *path; /* Name */ 224 void *handler; /* Object handler */ 225 void (*init)(); /* Constructor pointer */ 226 void (*fini)(); /* Destructor pointer */ 227 u_int id; /* Object ID */ 228 time_t loadtime; /* Load time stamp */ 229 struct s_module *next; /* Next module of the list */ 230 } elfshmod_t; 231 232 233 /* Hold all the VM flags, modifying the behavior or the shell */ 234 typedef struct s_state 235 { 236 char vm_quiet; /* Quiet mode : 0 or 1 */ 237 char vm_use_regx; /* Is a global regx available ? */ 238 regex_t vm_regx; /* Global regx */ 239 char *vm_sregx; /* Global regx in string format */ 240 241 char *sort; /* Actual sorting choice */ 242 char *input; /* Implicit File input (-f option) */ 243 char *output; /* Implicit File output (-w option) */ 244 245 #define ELFSH_VMSTATE_CMDLINE 0 246 #define ELFSH_VMSTATE_SCRIPT 1 247 #define ELFSH_VMSTATE_IMODE 2 248 char vm_mode; /* Command line, scripting, or interactive ? */ 249 char vm_stopped; /* We are in a signal handler */ 250 u_int lastid; /* Last Object ID */ 251 } elfshstate_t; 252 253 254 255 256 /* The ELF shell world */ 257 typedef struct s_elfsh_world 258 { 259 elfshargv_t args; /* Command line parameters */ 260 elfshobj_t *list; /* List of loaded ELF objects */ 261 elfshobj_t *current; /* current working ELF object */ 262 elfshstate_t state; /* Flags structure */ 263 elfshmod_t *modlist; /* ELFsh module list */ 264 char **cmds; /* commands list for readline */ 265 266 #if defined(USE_LIBASM) 267 asm_processor proc; /* Libasm world */ 268 #endif 269 270 } elfshworld_t; 271 272 273 /* Meta object : describe an object in a standard way, whatever its hierarchy level */ 274 typedef struct s_elfshpath 275 { 276 277 /* Handlers */ 278 u_long (*get_obj)(void *parent); 279 u_long (*set_obj)(void *parent, long value); 280 char *(*get_name)(elfshobj_t *, void *obj); 281 int (*set_name)(elfshobj_t *, void *, char *); 282 char *(*get_data)(elfshsect_t *, u_int off, u_int); 283 int (*set_data)(elfshsect_t *, u_int, char *, u_int, u_int); 284 285 elfshobj_t *root; /* Root parent */ 286 void *parent; /* Direct parent */ 287 288 u_int off; /* Optional byte offset */ 289 u_int size; /* Size of the immediate string */ 290 u_int sizelem; /* Size of element for OBJRAW */ 291 char immed; /* Immediate binary flag */ 292 293 /* Immediate value of immed flag is set */ 294 union immval 295 { 296 char *str; 297 long ent; 298 } immed_val; 299 300 /* Here is the object type list */ 301 #define ELFSH_OBJINT 0 /* Dword */ 302 #define ELFSH_OBJSTR 1 /* String */ 303 #define ELFSH_OBJRAW 2 /* Raw */ 304 #define ELFSH_OBJUNK 3 /* Unknown */ 305 char type; 306 307 308 309 } elfshpath_t; 310 311 312 /* ELFsh Level 2 object (= L1 child) structure */ 313 typedef struct s_L2handler 314 { 315 316 /* For fields */ 317 int (*get_obj)(void *obj); /* Read object */ 318 int (*set_obj)(void *par, u_int arg); /* Write object */ 319 320 /* For names */ 321 char *(*get_name)(elfshobj_t *, void *obj); /* Get name */ 322 int (*set_name)(elfshobj_t *, void *, char *); /* Set name */ 323 324 /* For sections data */ 325 char *(*get_data)(elfshsect_t *, u_int off, u_int sizelem); /* Read data */ 326 int (*set_data)(elfshsect_t *, u_int, char *, u_int, u_int); /* Write data */ 327 328 char type; /* Object type */ 329 330 } elfshL2_t; 331 332 333 /* ELFsh Level 1 object (= parent object) structure */ 334 typedef struct s_L1handler 335 { 336 hash_t *l2list; /* A ptr on the child L2 hashtable */ 337 u_int elem_size; /* Size of one element of this object */ 338 339 /* Handlers */ 340 void *(*get_obj)(void *file, void *arg); /* Read object */ 341 void *(*get_obj_idx)(void *file, u_int i, void *a); /* Read handler for mutiple instanced L1 obj */ 342 void *(*get_obj_nam)(void *file, char *name); /* Read handler by name */ 343 void *(*get_entptr)(void *file, u_int idx); /* Get address */ 344 u_int (*get_entval)(void *ptr); /* Get value */ 345 u_int (*set_entval)(void *ptr, u_int vaddr); /* Set value */ 346 347 } elfshL1_t; 348 349 350 351 /* ELFsh command handlers */ 352 typedef struct s_cmdhandler 353 { 354 int (*reg)(u_int i, u_int s, char **a); /* Registration handler */ 355 int (*exec)(); /* Execution handler */ 356 char *arg1; /* Option activation variable ptr */ 357 void *arg2; /* Option regex ptr */ 358 char *arg3; /* Regex switch ptr */ 359 char wflags; /* 1 if the cmd need a valid curfile */ 360 } elfshcmd_t; 361 362 363 364 /* The world */ 365 extern elfshworld_t world; 366 367 /* All the StandAlone hashtables */ 368 extern hash_t cmd_hash; /* commands handlers */ 369 extern hash_t file_hash; /* elfshobj_t pointers */ 370 extern hash_t const_hash; /* elf.h picked up constants values */ 371 extern hash_t mod_hash; /* Modules name hash table */ 372 373 /* The Level 1 object hash table : hash the object name and returns a L1handler_t* */ 374 extern hash_t L1_hash; /* For HDR, SHT, PHT, GOT, DTORS, CTORS, DYNAMIC, SECTIONS */ 375 376 /* The Level 2 object hash table list : hash the object name and returns a L2handler_t* */ 377 extern hash_t elf_L2_hash; /* For the ELF header fields */ 378 extern hash_t sht_L2_hash; /* For the Section header table fields */ 379 extern hash_t pht_L2_hash; /* For the Program header table fields */ 380 extern hash_t got_L2_hash; /* For the Global offset table fields */ 381 extern hash_t crs_L2_hash; /* For the .ctors fields */ 382 extern hash_t drs_L2_hash; /* For the .dtors fields */ 383 extern hash_t sym_L2_hash; /* For symbol fields */ 384 extern hash_t rel_L2_hash; /* For Relocation table fields */ 385 extern hash_t sct_L2_hash; /* For Section data access */ 386 extern hash_t dynsym_L2_hash; /* For .dynsym */ 387 extern hash_t dyn_L2_hash; /* For .dynamic */ 388 389 /* Data value/string/description arrays */ 390 extern elfshconst_t elfsh_seg_type[ELFSH_SEGTYPE_MAX]; 391 extern elfshconst_t elfsh_sh_type[ELFSH_SHTYPE_MAX]; 392 extern elfshconst_t elfsh_obj_type[ELFSH_OBJTYPE_MAX]; 393 extern elfshconst_t elfsh_sym_bind[ELFSH_SYMBIND_MAX]; 394 extern elfshconst_t elfsh_sym_type[ELFSH_SYMTYPE_MAX]; 395 extern elfshconst_t elfsh_dynentry_type[ELFSH_DYNAMIC_MAX]; 396 extern elfshconst_t elfsh_encoding[ELFSH_ENCODING_MAX]; 397 extern elfshconst_t elfsh_extdyn_type[ELFSH_EXTDYN_MAX]; 398 extern char *elfsh_arch_type[ELFSH_ARCHTYPE_MAX]; 399 extern char *elfsh_stab_type[ELFSH_STAB_MAX]; 400 extern elfshconst_t elfsh_feature1[ELFSH_FEATURE_MAX]; 401 extern elfshconst_t elfsh_posflag1[ELFSH_POSFLAG_MAX]; 402 extern elfshconst_t elfsh_flags[ELFSH_FLAGS_MAX]; 403 extern elfshconst_t elfsh_flags1[ELFSH_FLAGS1_MAX]; 404 405 extern elfshconst_t elfsh_rel_type_i386[ELFSH_RELOC_i386_MAX]; 406 extern elfshconst_t elfsh_rel_type_sparc[ELFSH_RELOC_SPARC_MAX]; 407 408 /* Commands execution handlers, each in their respective file */ 409 int cmd_dyn(); 410 int cmd_sht(); 411 int cmd_rel(); 412 int cmd_dynsym(); 413 int cmd_symtab(); 414 int cmd_pht(); 415 int cmd_got(); 416 int cmd_dtors(); 417 int cmd_ctors(); 418 int cmd_elf(); 419 int cmd_interp(); 420 int cmd_list(); 421 int cmd_notes(); 422 int cmd_sym(); 423 int cmd_stab(); 424 int cmd_hexa(); 425 int cmd_disasm(); 426 int cmd_shtrm(); 427 int cmd_comments(); 428 int cmd_help(); 429 int cmd_quit(); 430 int cmd_load(); 431 int cmd_unload(); 432 int cmd_save(); 433 int cmd_dolist(); 434 int cmd_doswitch(); 435 int cmd_set(); 436 int cmd_get(); 437 int cmd_print(); 438 int cmd_info(); 439 int cmd_add(); 440 int cmd_sub(); 441 int cmd_mul(); 442 int cmd_div(); 443 int cmd_mod(); 444 int cmd_meta(); 445 int cmd_write(); 446 int cmd_append(); 447 int cmd_extend(); 448 int cmd_fixup(); 449 int cmd_quiet(); 450 int cmd_verb(); 451 int cmd_exec(); 452 int cmd_findrel(); 453 int cmd_modload(); 454 int cmd_modunload(); 455 int cmd_strip(); 456 int cmd_sstrip(); 457 int cmd_relinject(); 458 int cmd_stop(); 459 int cmd_hijack(); 460 461 /* Registration handlers for options from opt.c */ 462 int vm_getoption(u_int index, u_int argc, char **argv); 463 int vm_getoption2(u_int index, u_int argc, char **argv); 464 int vm_getoption3(u_int index, u_int argc, char **argv); 465 int vm_getregxoption(u_int index, u_int argc, char **argv); 466 int vm_getglregx(u_int index, u_int argc, char **argv); 467 int vm_getinput(u_int index, u_int argc, char **argv); 468 int vm_getoutput(u_int index, u_int argc, char **argv); 469 int vm_getsort(u_int index, u_int argc, char **argv); 470 int vm_getdisasm(u_int index, u_int argc, char **argv); 471 int vm_gethexa(u_int index, u_int argc, char **argv); 472 int vm_getvarparams(u_int index, u_int argc, char **argv); 473 474 int _dprintf(int fd, char *format, ...); 475 476 /* Libasm resolve handler */ 477 void do_resolve(void *data, u_int vaddr, char *, u_int); 478 u_int display_instr(int, u_int, u_int, u_int, u_int, 479 char *, u_int, char *); 480 481 /* Other VM functions */ 482 elfshobj_t *vm_getfile(u_int index); 483 elfshmod_t *vm_getmod(u_int index); 484 elfshmod_t *vm_modprobe(); 485 char *vm_filter_param(char *buf, char *ptr); 486 char **vm_getln(int *argc); 487 char *vm_build_unknown(char *buf, const char *str, u_long type); 488 char *vm_reverse(elfshobj_t *file, u_int vaddr); 489 void vm_load_cwfiles(char **argv); 490 void vm_setup_hashtables(); 491 void vm_badparam(char *str); 492 void vm_unknown(char *str); 493 void vm_print_banner(); 494 void vm_dynentinfo(elfshobj_t *f, Elf32_Dyn *ent, char *info); 495 void vm_filter_zero(elfshpath_t *obj); 496 int vm_implicit(elfshcmd_t *actual, char **argv); 497 int vm_unload_cwfiles(); 498 int vm_parseopt(int argc, char **argv); 499 int vm_lookup_param(char *param, elfshpath_t *pobj, u_int mode); 500 int vm_usage(char *str); 501 int vm_openscript(char *name, char *av0); 502 int vm_doerror(void (*fct)(char *str), char *str); 503 int vm_modlist(); 504 int vm_convert_object(elfshpath_t *obj, u_int objtype); 505 int vm_check_object(elfshpath_t *pobj); 506 int vm_isnbr(char *string); 507 508 /* cmdapi.c */ 509 int vm_setcmd(char *cmd, void *exec, void *reg, u_int needcur); 510 int vm_addcmd(char *cmd, void *exec, void *reg, u_int needfile); 511 int vm_delcmd(char *cmd); 512 513 /* readline stuff (XXX: need to be prefixed) */ 514 char **coustom_completion(const char* text, int start, int end); 515 516 /* Object constructor functions */ 517 elfshL1_t *vm_create_L1ENT(void *get_obj, 518 void *get_obj_idx, 519 void *get_obj_nam, 520 hash_t *l2_hash, 521 void *get_entptr, 522 void *get_entval, 523 void *set_entval, 524 u_int elem_size); 525 elfshL2_t *vm_create_L2ENT(void *get_obj, 526 void *set_obj, 527 char type, 528 void *get_name, 529 void *set_name, 530 void *get_data, 531 void *set_data); 532 elfshcmd_t *vm_create_CMDENT(int (*exec)(void *file, void *av), 533 int (*reg)(u_int i, u_int ac, char **av), 534 int flags); 535 536 #endif /* __ELFSH_H_ */ 537