summaryrefslogtreecommitdiff
path: root/src/analysis/binaries/file.c
blob: 9c43ed533a41f4682dbb30b61c2c6e65997db6d2 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

/* OpenIDA - Outil d'analyse de fichiers binaires
 * file.h - prototypes pour la prise en charge des binaires sous forme de fichier
 *
 * Copyright (C) 2012 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 "file.h"


#include <string.h>


#include "../binary-int.h"
#include "../../common/extstr.h"
#include "../../gui/panels/log.h"



/* Description de fichier binaire (instance) */
struct _GFileBinary
{
    GLoadedBinary parent;                   /* A laisser en premier        */

    char *filename;                         /* Fichier chargé en mémoire   */

};

/* Description de fichier binaire (classe) */
struct _GFileBinaryClass
{
    GLoadedBinaryClass parent;              /* A laisser en premier        */

};


/* Initialise la classe des descriptions de fichier binaire. */
static void g_file_binary_class_init(GFileBinaryClass *);

/* Initialise une description de fichier binaire. */
static void g_file_binary_init(GFileBinary *);

/* Procède à la libération totale de la mémoire. */
static void g_file_binary_finalize(GFileBinary *);

/* Ecrit une sauvegarde du binaire dans un fichier XML. */
static bool g_file_binary_save(const GFileBinary *, xmlDocPtr, xmlXPathContextPtr, const char *);

/* Fournit le fichier correspondant à l'élément binaire. */
static const char *g_file_binary_get_filename(const GFileBinary *, bool);



/* Indique le type défini pour une description de fichier binaire. */
G_DEFINE_TYPE(GFileBinary, g_file_binary, G_TYPE_LOADED_BINARY);



/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des descriptions de fichier binaire.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_file_binary_class_init(GFileBinaryClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->finalize = (GObjectFinalizeFunc)g_file_binary_finalize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une description de fichier binaire.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_file_binary_init(GFileBinary *binary)
{
    GLoadedBinary *loaded;                  /* Version parente             */

    loaded = G_LOADED_BINARY(binary);

    loaded->save = (save_binary_fc)g_file_binary_save;
    loaded->get_filename = (get_binary_filename_fc)g_file_binary_get_filename;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_file_binary_finalize(GFileBinary *binary)
{
    free(binary->filename);

    G_OBJECT_CLASS(g_file_binary_parent_class)->finalize(G_OBJECT(binary));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : filename = nom du fichier à charger.                         *
*                                                                             *
*  Description : Charge en mémoire le contenu d'un fichier.                   *
*                                                                             *
*  Retour      : Adresse de la représentation ou NULL en cas d'échec.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GLoadedBinary *g_file_binary_new_from_file(const char *filename)
{
    GFileBinary *result;                    /* Adresse à retourner         */
    GLoadedBinary *loaded;                  /* Version parente             */

    result = g_object_new(G_TYPE_FILE_BINARY, NULL);
    loaded = G_LOADED_BINARY(result);

    log_variadic_message(LMT_PROCESS, _("Opening '%s' file..."), filename);

    result->filename = strdup(filename);

    loaded->format = G_EXE_FORMAT(load_new_format(FMT_EXEC, filename,
                                                  &loaded->bin_data, &loaded->bin_length));

    if (loaded->format == NULL)
    {
        log_simple_message(LMT_INFO, _("Unknown binary format"));
        goto lbf_error;
    }

    switch (g_exe_format_get_target_machine(loaded->format))
    {
        case FTM_ARM:
            log_simple_message(LMT_INFO, _("Detected architecture: ARM"));
            break;
        case FTM_DALVIK:
            log_simple_message(LMT_INFO, _("Detected architecture: Dalvik Virtual Machine"));
            break;
        case FTM_JVM:
            log_simple_message(LMT_INFO, _("Detected architecture: Java Virtual Machine"));
            break;
        case FTM_MIPS:
            log_simple_message(LMT_INFO, _("Detected architecture: Microprocessor without Interlocked Pipeline Stages"));
            break;
        case FTM_386:
            log_simple_message(LMT_INFO, _("Detected architecture: i386"));
            break;
        default:
            log_simple_message(LMT_INFO, _("Unknown architecture"));
            goto lbf_error;
            break;
    }

    loaded->proc = get_arch_processor_from_format(loaded->format);

    return G_LOADED_BINARY(result);

 lbf_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : context = contexte pour les recherches XPath.                *
*                path    = chemin d'accès au noeud XML à lire.                *
*                                                                             *
*  Description : Charge en mémoire le contenu d'un fichier à partir d'XML.    *
*                                                                             *
*  Retour      : Adresse de la représentation ou NULL en cas d'échec.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GLoadedBinary *g_file_binary_new_from_xml(xmlXPathContextPtr context, const char *path)
{
    GLoadedBinary *result;                  /* Adresse à retourner         */
    char *access;                           /* Chemin pour une sous-config.*/
    char *filename;                         /* Chemin du binaire à charger */

    result = NULL;

    /* Chemin du fichier à retrouver */

    access = strdup(path);
    access = stradd(access, "/Filename");

    filename = get_node_text_value(context, access);

    free(access);

    /* Chargement */

    if (filename != NULL)
    {
        result = g_file_binary_new_from_file(filename);
        free(filename);
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary  = élément binaire à traiter.                         *
*                xdoc    = structure XML en cours d'édition.                  *
*                context = contexte à utiliser pour les recherches.           *
*                path    = chemin d'accès réservé au binaire.                 *
*                                                                             *
*  Description : Ecrit une sauvegarde du binaire dans un fichier XML.         *
*                                                                             *
*  Retour      : true si l'opération a bien tourné, false sinon.              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_file_binary_save(const GFileBinary *binary, xmlDocPtr xdoc, xmlXPathContextPtr context, const char *path)
{
    bool result;                            /* Bilan à faire remonter      */
    char *access;                           /* Chemin d'accès à un élément */

    result = true;

    /* Type */

    result &= add_string_attribute_to_node(xdoc, context, path, "type", "file");

    /* Nom du fichier associé */

    access = strdup(path);
    access = stradd(access, "/Filename");

    result &= add_content_to_node(xdoc, context, access, binary->filename);

    free(access);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = élément binaire à consulter.                        *
*                full   = précise s'il s'agit d'une version longue ou non.    *
*                                                                             *
*  Description : Fournit le fichier correspondant à l'élément binaire.        *
*                                                                             *
*  Retour      : Nom de fichier avec chemin absolu.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static const char *g_file_binary_get_filename(const GFileBinary *binary, bool full)
{
    const char *result;                     /* Description à retourner     */

    if (full)
        result = binary->filename;
    else
        result = strrchr(binary->filename, G_DIR_SEPARATOR) + 1;

    return result;

}