diff options
Diffstat (limited to 'src/binary.c')
-rw-r--r-- | src/binary.c | 336 |
1 files changed, 120 insertions, 216 deletions
diff --git a/src/binary.c b/src/binary.c index e43d644..85196d9 100644 --- a/src/binary.c +++ b/src/binary.c @@ -35,6 +35,9 @@ #include <sys/types.h> +#include "analysis/line_code.h" +#include "analysis/line_comment.h" +#include "analysis/line_prologue.h" #include "analysis/prototype.h" #include "arch/processor.h" @@ -43,10 +46,6 @@ #include "format/exe_format.h" -#include "format/elf/e_elf.h" -#include "format/dwarf/d_dwarf.h" -#include "format/java/e_java.h" -#include "format/pe/e_pe.h" #ifndef _ @@ -59,31 +58,35 @@ extern bool find_line_info(const uint8_t *content, off_t *size); -/* Charge en mémoire le contenu d'un fichier à partir d'XML. */ -openida_binary *load_binary_file_from_xml(xmlXPathObjectPtr); - - - - -/* Charge en mémoire le contenu d'un fichier. */ -uint8_t *map_binary_file(const char *, size_t *); - -/* Construit la description d'introduction du désassemblage. */ -rendering_line *build_binary_prologue(const char *, const uint8_t *, off_t); - - - /* Description d'un fichier binaire */ struct _openida_binary { char *filename; /* Fichier chargé en mémoire */ + off_t bin_length; /* Taille des données brutes */ + uint8_t *bin_data; /* Données binaires brutes */ + + exe_format *format; /* Format du binaire */ + asm_processor *proc; /* Architecture du binaire */ + + GRenderingLine *lines; /* Lignes de rendu en place */ + disass_options options; /* Options de désassemblage */ }; +/* Charge en mémoire le contenu d'un fichier à partir d'XML. */ +openida_binary *load_binary_file_from_xml(xmlXPathObjectPtr); + +/* Charge en mémoire le contenu d'un fichier. */ +uint8_t *map_binary_file(const char *, off_t *); +/* Construit la description d'introduction du désassemblage. */ +GRenderingLine *build_binary_prologue(const char *, const uint8_t *, off_t); + +/* Procède au désassemblage basique d'un contenu binaire. */ +void disassemble_openida_binary(openida_binary *); @@ -107,12 +110,32 @@ openida_binary *load_binary_file(const char *filename) result->filename = strdup(filename); + result->bin_data = map_binary_file(filename, &result->bin_length); + if (result->bin_data == NULL) goto lbf_error; + + result->format = load_new_exe_format(result->bin_data, result->bin_length); + if (result->format == NULL) goto lbf_error; + + + result->proc = create_x86_processor(); + + result->options.show_address = true; + result->options.show_code = true; + result->options.format = result->format; + result->options.proc = result->proc; + disassemble_openida_binary(result); return result; + lbf_error: + + unload_binary_file(result); + + return NULL; + } @@ -171,7 +194,26 @@ void unload_binary_file(openida_binary *binary) /****************************************************************************** * * -* Paramètres : binary = élément binaire à traiter. * +* Paramètres : binary = élément binaire à consulter. * +* * +* Description : Fournit le fichier correspondant à l'élément binaire. * +* * +* Retour : Nom de fichier avec chemin absolu. * +* * +* Remarques : - * +* * +******************************************************************************/ + +const char *openida_binary_get_filename(const openida_binary *binary) +{ + return binary->filename; + +} + + +/****************************************************************************** +* * +* Paramètres : binary = élément binaire à consulter. * * * * Description : Fournit une description humaine d'un élément binaire. * * * @@ -190,6 +232,24 @@ const char *openida_binary_to_string(const openida_binary *binary) +/****************************************************************************** +* * +* Paramètres : binary = élément binaire à consulter. * +* * +* Description : Fournit les lignes de rendu issues du désassemblage. * +* * +* Retour : Lignes issues du désassemblage. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GRenderingLine *get_openida_binary_lines(const openida_binary *binary) +{ + return binary->lines; + +} + @@ -306,7 +366,7 @@ bool write_openida_binary_to_xml(const openida_binary *binary, xmlTextWriterPtr * * ******************************************************************************/ -uint8_t *map_binary_file(const char *filename, size_t *length) +uint8_t *map_binary_file(const char *filename, off_t *length) { uint8_t *result; /* Données à retourner */ int fd; /* Fichier ouvert en lecture */ @@ -360,25 +420,25 @@ uint8_t *map_binary_file(const char *filename, size_t *length) * * ******************************************************************************/ -rendering_line *build_binary_prologue(const char *filename, const uint8_t *data, off_t length) +GRenderingLine *build_binary_prologue(const char *filename, const uint8_t *data, off_t length) { - rendering_line *result; /* Contenu à renvoyer */ + GRenderingLine *result; /* Contenu à renvoyer */ size_t len; /* Taille du texte */ char *content; /* Contenu textuel d'une ligne */ - rendering_line *line; /* Représentation à ajouter */ + GRenderingLine *line; /* Représentation à ajouter */ GChecksum *checksum; /* Calcul de l'empreinte */ const gchar *hex; /* Valeur hexadécimale du SHA */ result = NULL;/* FIXME DL_LIST_HEAD_INIT( **/ - line = create_prologue_line("Disassembly generated by OpenIDA"); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new("Disassembly generated by OpenIDA"); + g_rendering_line_add_to_lines(&result, line); - line = create_prologue_line("OpenIDA is free software - © 2008-2009 Cyrille Bagard"); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new("OpenIDA is free software - © 2008-2009 Cyrille Bagard"); + g_rendering_line_add_to_lines(&result, line); - line = create_prologue_line(""); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new(""); + g_rendering_line_add_to_lines(&result, line); /* Fichier */ @@ -387,8 +447,8 @@ rendering_line *build_binary_prologue(const char *filename, const uint8_t *data, snprintf(content, len + 1, "%s%s", _("File: "), filename); - line = create_prologue_line(content); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new(content); + g_rendering_line_add_to_lines(&result, line); free(content); @@ -406,33 +466,36 @@ rendering_line *build_binary_prologue(const char *filename, const uint8_t *data, g_checksum_free(checksum); - line = create_prologue_line(content); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new(content); + g_rendering_line_add_to_lines(&result, line); free(content); - line = create_prologue_line(""); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new(""); + g_rendering_line_add_to_lines(&result, line); - line = create_prologue_line(""); - add_line_to_rendering_lines(&result, line); + line = g_prologue_line_new(""); + g_rendering_line_add_to_lines(&result, line); return result; } +/****************************************************************************** +* * +* Paramètres : binary = binaire dont le contenu est à analyser. * +* * +* Description : Procède au désassemblage basique d'un contenu binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ - -void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) +void disassemble_openida_binary(openida_binary *binary) { - off_t length; - uint8_t *bin_data; - int ret; - - exe_format *format; - dbg_format *dformat; - asm_processor *proc; asm_instr *instr; bin_routine **routines; /* Liste des routines trouvées */ @@ -442,24 +505,14 @@ void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) size_t parts_count; - char **comments; - uint64_t *offsets; - size_t comments_count; - code_line_info **comments_list; - rendering_line *lines; - rendering_line *line; + GRenderingLine *line; - code_line_info **list; - size_t list_len; - code_line_info *item; off_t start; off_t pos; off_t len; - char buffer[64]; - uint64_t base = 0; uint64_t offset = 0; @@ -471,50 +524,18 @@ void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) char *routine_desc; /* Prototype d'une routine */ - disass_options options; - - proc = create_x86_processor(); - - pos = 0; - len = 0x28; - - - register_exe_format(_("ELF"), elf_is_matching, load_elf); - register_exe_format(_("Java"), java_is_matching, load_java); - register_exe_format(_("Portable Executable"), pe_is_matching, load_pe); - - - bin_data = map_binary_file("/tmp/hello", &length); - printf(" ~~ bin_data ~~ :: %p (%d)\n", bin_data, length); + binary->lines = build_binary_prologue(binary->filename, binary->bin_data, binary->bin_length); - if (bin_data == NULL) return; - format = load_new_exe_format(bin_data, length); - printf(" --> ok ? %p\n", format); + routines = get_all_exe_routines(binary->format, &routines_count); - //exit(0); - - lines = build_binary_prologue("/tmp/hello", bin_data, length); - - - - - options.show_address = true; - options.show_code = true; - - options.format = format; - options.proc = proc; - - routines = get_all_exe_routines(format, &routines_count); - - - parts = get_elf_default_code_parts(format, &parts_count); + parts = get_elf_default_code_parts(binary->format, &parts_count); qsort(parts, parts_count, sizeof(bin_part *), compare_bin_parts); @@ -534,11 +555,11 @@ void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) offset = base + pos; - instr = decode_instruction(proc, &bin_data[start], &pos, len, start, offset); + instr = decode_instruction(binary->proc, &binary->bin_data[start], &pos, len, start, offset); - line = create_code_line(instr, offset, &options); - add_line_to_rendering_lines(&lines, line); + line = g_code_line_new(offset, instr, &binary->options); + g_rendering_line_add_to_lines(&binary->lines, line); } @@ -552,8 +573,8 @@ void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) routine_desc = routine_to_string(routines[k]); - line = create_comment_line(routine_offset, RLT_PROTOTYPE, routine_desc, &options); - insert_line_into_rendering_lines(&lines, line, true); + line = g_comment_line_new(routine_offset, routine_desc, &binary->options); + g_rendering_line_insert_into_lines(&binary->lines, line, true); free(routine_desc); @@ -566,127 +587,10 @@ void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2) - line = find_offset_in_rendering_lines(lines, get_exe_entry_point(format)); - add_rendering_line_flag(line, RLF_ENTRY_POINT); - - - gtk_snippet_set_rendering_lines(snippet, lines); - - handle_new_exe_on_symbols_panel(panel, format); - handle_new_exe_on_strings_panel(panel2, format); - - return; - - - /////format = load_elf(bin_data, length); - dformat = load_dwarf(bin_data, length, format); - - - //comments_count = get_dwarf_comments(dformat, &comments, &offsets); - - comments = NULL; - offsets = NULL; - comments_count = 0; - - get_elf_symbol_comments(format, &comments, &offsets, &comments_count); - - comments_list = (code_line_info **)calloc(comments_count, sizeof(code_line_info *)); - - for (i = 0; i < comments_count; i++) - comments_list[i] = create_code_line_info(offsets[i], NULL, strdup(comments[i])); - - - qsort(comments_list, comments_count, sizeof(code_line_info *), compare_code_line_info); - - - - parts = get_elf_default_code_parts(format, &parts_count); - - - - list = NULL; - list_len = 0; - - - gtk_snippet_set_format(snippet, format); - gtk_snippet_set_processor(snippet, proc); - - - for (i = 0; i < parts_count; i++) - { - get_bin_part_values(parts[i], &pos, &len, &base); - + line = g_rendering_line_find_by_offset(binary->lines, get_exe_entry_point(binary->format)); + g_rendering_line_add_flag(line, RLF_ENTRY_POINT); - /*find_line_info(bin_data, &len);*/ - /* - printf("Exiting...\n"); - exit(0); - */ - - offset = base; - - for (k = 0; k < comments_count; k++) - if (comments_list[k]->offset >= base) break; - - - - - item = create_code_line_info(offset, NULL, "Simple HelloWorld !"); - - list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *)); - list[list_len - 1] = item; - - - - start = pos; - pos = 0; - - while (pos < len) - { - offset = base + pos; - - /* Si on a un commentaire pour cette ligne... */ - if (k < comments_count && comments_list[k]->offset == offset) - { - list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *)); - list[list_len - 1] = comments_list[k++]; - } - - - instr = decode_instruction(proc, &bin_data[start], &pos, len, start, offset); - - - item = create_code_line_info(offset, instr, NULL); - - list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *)); - list[list_len - 1] = item; - - //gtk_snippet_add_line(snippet, offset, instr, NULL); - - - } - - - /**** - ret = munmap(bin_data, length); - ****/ - - /* - gtk_snippet_build_content(snippet); - */ - - } - - for (i = 0; i < list_len; i++) - { - gtk_snippet_add_line(snippet, list[i]); - /* TODO: free() */ - } - - handle_new_exe_on_symbols_panel(panel, format); - handle_new_exe_on_strings_panel(panel2, format); } - |