/* OpenIDA - Outil d'analyse de fichiers binaires * stackvars.c - substitution des emplacements de pile par des variables * * Copyright (C) 2009 Cyrille Bagard * * This file is part of OpenIDA. * * OpenIDA 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 3 of the License, or * (at your option) any later version. * * OpenIDA 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 Foobar. If not, see . */ #include "stackvars.h" #include #include #include #include #include "operand.h" /* Effectue tous les remplacements possibles dans une routine. */ static bool replace_stack_vars_in_routine(GBinRoutine *, GRenderingLine *); /* Effectue d'éventuels remplacements dans une instruction. */ static bool replace_stack_vars_in_instruction(GArchInstruction *); /* Effectue d'éventuels remplacements dans un opérande. */ static GArchOperand *replace_stack_vars_in_operand(const GArchOperand *); /****************************************************************************** * * * Paramètres : ref = espace de référencement global. * * * * Description : Initialise le greffon pour les bornes de routine. * * * * Retour : true. * * * * Remarques : - * * * ******************************************************************************/ G_MODULE_EXPORT bool init_plugin(GObject *ref) { return true; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit une indication sur le type d'opération(s) menée(s). * * * * Retour : Description d'une action. * * * * Remarques : - * * * ******************************************************************************/ G_MODULE_EXPORT PluginAction get_plugin_action(void) { return PGA_CODE_PROCESS; } /****************************************************************************** * * * Paramètres : binary = représentation binaire à traiter. * * action = action attendue. * * * * Description : Exécute une action définie sur un binaire chargé. * * * * Retour : true si une action a été menée, false sinon. * * * * Remarques : - * * * ******************************************************************************/ G_MODULE_EXPORT bool execute_action_on_binary(GOpenidaBinary *binary, PluginAction action) { bool result; /* Bilan à retourner */ GRenderingLine *lines; /* Lignes de rendu */ exe_format *format; /* Format du binaire fourni */ GBinRoutine **routines; /* Liste des routines trouvées */ size_t routines_count; /* Nombre de ces routines */ size_t i; /* Boucle de parcours */ result = false; printf(" ------------- exec stackvars !!!\n"); lines = g_openida_binary_get_lines(binary); format = g_openida_binary_get_format(binary); routines = get_all_exe_routines(format, &routines_count); for (i = 0; i < routines_count; i++) result |= replace_stack_vars_in_routine(routines[i], lines); return result; } /****************************************************************************** * * * Paramètres : routine = routine dont le code est à analyser. * * lines = ensemble des lignes de rendu. * * * * Description : Effectue tous les remplacements possibles dans une routine. * * * * Retour : true si une action a été menée, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool replace_stack_vars_in_routine(GBinRoutine *routine, GRenderingLine *lines) { bool result; /* Bilan à retourner */ vmpa_t start; /* Adresse de début de routine */ vmpa_t end; /* Adresse de fin de routine */ GRenderingLine *iter; /* Boucle de parcours */ GArchInstruction *instr; /* Instruction à ausculter */ result = false; start = g_binary_routine_get_address(routine); end = start + g_binary_routine_get_size(routine); printf(" +++ processing '%s'\n", g_binary_routine_get_name(routine)); for (iter = g_rendering_line_find_by_address(lines, NULL, start); iter != NULL && get_rendering_line_address(iter) < end; iter = g_rendering_line_get_next_iter(lines, iter, NULL)) { if (!G_IS_CODE_LINE(iter)) continue; instr = g_code_line_get_instruction(G_CODE_LINE(iter)); result |= replace_stack_vars_in_instruction(instr); } return result; } /****************************************************************************** * * * Paramètres : instr = instruction dont le contenu peut être modifié. * * * * Description : Effectue d'éventuels remplacements dans une instruction. * * * * Retour : true si une action a été menée, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool replace_stack_vars_in_instruction(GArchInstruction *instr) { bool result; /* Bilan à renvoyer */ size_t opcount; /* Nombre d'opérandes */ size_t i; /* Boucle de parcours */ const GArchOperand *operand; /* Opérande de l'instruction */ GArchOperand *new; /* Opérande d'encapsulation */ result = false; opcount = g_arch_instruction_count_operands(instr); for (i = 0; i < opcount; i++) { operand = g_arch_instruction_get_operand(instr, i); new = replace_stack_vars_in_operand(operand); if (new != NULL) { g_arch_instruction_replace_operand(instr, new, operand); result = true; } } return result; } /****************************************************************************** * * * Paramètres : operand = opérande dont le contenu est à analyser. * * * * Description : Effectue d'éventuels remplacements dans un opérande. * * * * Retour : Nouvel opérande de remplacemet ou NULL si aucun besoin. * * * * Remarques : - * * * ******************************************************************************/ static GArchOperand *replace_stack_vars_in_operand(const GArchOperand *operand) { GArchOperand *result; /* Encapsulation à retourner */ uint8_t scale; /* Puissance de deux */ x86_register *index; /* Registre servant d'indice */ x86_register *base; /* Registre de base */ GImmOperand *displacement; /* Décallage supplémentaire */ result = NULL; if (G_IS_X86_MOD_RM_OPERAND(operand)) { g_x86_mod_rm_operand_get_scale_and_index(G_X86_MOD_RM_OPERAND(operand), &scale, &index); base = g_x86_mod_rm_operand_get_base(G_X86_MOD_RM_OPERAND(operand)); displacement = g_x86_mod_rm_operand_get_displacement(G_X86_MOD_RM_OPERAND(operand)); if (scale == 0 && is_x86_register_base_pointer(index) && base == NULL) { printf(" [+] found one ebp !!\n"); result = g_stack_var_operand_new(operand); } } return result; }