summaryrefslogtreecommitdiff
path: root/plugins/androhelpers/params.c
blob: d85bacaf2974a7f935b4acee3fa7efaac9201a99 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * params.c - affichage plus adapté des registres liés à des paramètres
 *
 * Copyright (C) 2012-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "params.h"


#include <stdio.h>


#include <arch/dalvik/operands/args.h>
#include <arch/dalvik/operands/register.h>
#include <format/dex/dex-int.h>
#include <format/dex/method.h>



/* Procède si nécessaire au remplacement du texte de l'opérande. */
static void process_register_operand(const GDexMethod *, GArchOperand *);

/* Parcours en profondeur un ensemble d'arguments. */
static void process_args_operand(const GDexMethod *, const GDalvikArgsOperand *);

/* Visite chaque opérande des instructions d'une méthode. */
static void visit_all_method_operands(const GDexMethod *, GArchInstruction *);



/******************************************************************************
*                                                                             *
*  Paramètres  : method  = routine en cours de parcours.                      *
*                operand = morceau d'instruction en cours de traitement.      *
*                                                                             *
*  Description : Procède si nécessaire au remplacement du texte de l'opérande.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void process_register_operand(const GDexMethod *method, GArchOperand *operand)
{
    GDalvikRegister *reg;                   /* Registre représenté         */
    uint16_t index;                         /* Indice de ce registre       */
    DexVariableIndex info;                  /* Nature réelle du registre   */
    char tmp[12 /* 4294967295U */];         /* Construction du texte       */

    reg = g_dalvik_register_operand_get(G_DALVIK_REGISTER_OPERAND(operand));
    index = g_dalvik_register_get_index(reg);

    info = g_dex_method_get_variable(method, index);

    if (info & DVI_THIS)
        g_arch_operand_set_alt_text(operand, "this", RTT_REGISTER);

    else if (info & DVI_ARGUMENT)
    {
        snprintf(tmp, 12, "p%u", (unsigned int)DVI_INDEX(info));
        g_arch_operand_set_alt_text(operand, tmp, RTT_REGISTER);
    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = routine en cours de parcours.                       *
*                args   = liste d'opérandes à analyser.                       *
*                                                                             *
*  Description : Parcours en profondeur un ensemble d'arguments.              *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void process_args_operand(const GDexMethod *method, const GDalvikArgsOperand *args)
{
    size_t count;                           /* Nombre d'opérandes          */
    size_t i;                               /* Boucle de parcours          */
    GArchOperand *operand;                  /* Operande à manipuler        */

    count = g_dalvik_args_count(args);

    for (i = 0; i < count; i++)
    {
        operand = g_dalvik_args_operand_get(args, i);

        if (G_IS_DALVIK_REGISTER_OPERAND(operand))
            process_register_operand(method, operand);

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : method = routine à venir parcourir.                          *
*                instrs = liste des instructions pour tout le binaire.        *
*                                                                             *
*  Description : Visite chaque opérande des instructions d'une méthode.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void visit_all_method_operands(const GDexMethod *method, GArchInstruction *instrs)
{
    GBinRoutine *routine;                   /* Abstraction de la méthode   */
    vmpa_t start;                           /* Début de la zone couverte   */
    vmpa_t end;                             /* Fin de la zone couverte     */
    GArchInstruction *iter;                 /* Boucle de parcours #1       */
    size_t count;                           /* Nombre d'opérandes          */
    size_t i;                               /* Boucle de parcours #2       */
    GArchOperand *operand;                  /* Operande à manipuler        */

    routine = g_dex_method_get_routine(method);

    start = g_binary_routine_get_address(routine);
    end = start + g_binary_routine_get_size(routine);

    g_object_unref(G_OBJECT(routine));

    for (iter = g_arch_instruction_find_by_address(instrs, start, true);
         iter != NULL;
         iter = g_arch_instruction_get_next_iter(instrs, iter, end))
    {
        g_arch_instruction_lock_operands(iter);

        count = _g_arch_instruction_count_operands(iter);

        for (i = 0; i < count; i++)
        {
            operand = _g_arch_instruction_get_operand(iter, i);

            if (G_IS_DALVIK_REGISTER_OPERAND(operand))
                process_register_operand(method, operand);

            else if (G_IS_DALVIK_ARGS_OPERAND(operand))
                process_args_operand(method, G_DALVIK_ARGS_OPERAND(operand));

            g_object_unref(G_OBJECT(operand));

        }

        g_arch_instruction_unlock_operands(iter);

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = représentation binaire à traiter.                   *
*                                                                             *
*  Description : Effectue le remplacement de tous les paramètres.             *
*                                                                             *
*  Retour      : true si une action a été menée, false sinon.                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool replace_parameters(GLoadedBinary *binary)
{
    GDexFormat *format;                     /* Format du binaire chargé    */
    GArchProcessor *proc;                   /* Processeur de l'architecture*/
    GArchInstruction *instrs;               /* Instructions Dalvik         */
    size_t cls_count;                       /* Nombre de classes trouvées  */
    size_t i;                               /* Boucle de parcours #1       */
    GDexClass *class;                       /* Classe à analyser           */
    size_t meth_count;                      /* Nombre de méthodes trouvées */
    size_t j;                               /* Boucle de parcours #2       */
    GDexMethod *method;                     /* Méthode à parcourir         */

    format = G_DEX_FORMAT(g_loaded_binary_get_format(binary));
    proc = g_loaded_binary_get_processor(binary);
    instrs = NULL;//g_arch_processor_get_disassembled_instructions(proc);

    cls_count = g_dex_format_count_classes(format);
    for (i = 0; i < cls_count; i++)
    {
        class = g_dex_format_get_class(format, i);

        meth_count = g_dex_class_count_methods(class, false);
        for (j = 0; j < meth_count; j++)
        {
            method = g_dex_class_get_method(class, false, j);
            visit_all_method_operands(method, instrs);
        }

        meth_count = g_dex_class_count_methods(class, true);
        for (j = 0; j < meth_count; j++)
        {
            method = g_dex_class_get_method(class, true, j);
            visit_all_method_operands(method, instrs);
        }

    }

    g_object_unref(G_OBJECT(proc));
    g_object_unref(G_OBJECT(format));

    return true;

}