summaryrefslogtreecommitdiff
path: root/plugins/stackvars/stackvars.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2009-07-19 22:28:13 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2009-07-19 22:28:13 (GMT)
commit59bda76b1a76d796a42ee46b9da7041dbef57253 (patch)
tree36f7d6eaa7847016b99431b1b489c6a1fdbaf33b /plugins/stackvars/stackvars.c
parent3a9fe39c6a8923f45e7c96d80b0bfe52b8686ff9 (diff)
Encapsulated all recognized variables in the stack using a new plugin (need to be continued).
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@99 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'plugins/stackvars/stackvars.c')
-rw-r--r--plugins/stackvars/stackvars.c260
1 files changed, 260 insertions, 0 deletions
diff --git a/plugins/stackvars/stackvars.c b/plugins/stackvars/stackvars.c
new file mode 100644
index 0000000..5f34119
--- /dev/null
+++ b/plugins/stackvars/stackvars.c
@@ -0,0 +1,260 @@
+
+/* 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 <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "stackvars.h"
+
+
+#include <analysis/line_code.h>
+#include <analysis/prototype.h>
+#include <arch/x86/operand.h>
+#include <format/exe_format.h>
+
+
+#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(openida_binary *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 = get_openida_binary_lines(binary);
+
+ format = get_openida_binary_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;
+
+}