summaryrefslogtreecommitdiff
path: root/plugins/stackvars/stackvars.c
blob: 9a7b9657abcc86a193835bdd3a6eadfc0e75fa5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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(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;

}