summaryrefslogtreecommitdiff
path: root/src/binary.c
blob: 45dc0e0c86e3e509a78c4c0beedada8a4ac538bc (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

/* OpenIDA - Outil d'analyse de fichiers binaires
 * binary.c - traitement des flots de code binaire
 *
 * Copyright (C) 2008 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 "binary.h"


#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>



#include "arch/processor.h"


#include "format/dbg_format.h"
#include "format/exe_format.h"


#include "format/elf/e_elf.h"
#include "format/dwarf/d_dwarf.h"
#include "format/java/e_java.h"
#include "format/pe/e_pe.h"


#ifndef _
#   define _(str) str
#endif



extern bool find_line_info(const uint8_t *content, off_t *size);



/* Charge en mémoire le contenu d'un fichier à partir d'XML. */
openida_binary *load_binary_file_from_xml(xmlXPathObjectPtr);




/* Charge en mémoire le contenu d'un fichier. */
uint8_t *map_binary_file(const char *, size_t *);





/* Description d'un fichier binaire */
struct _openida_binary
{
    char *filename;                         /* Fichier chargé en mémoire   */


};






/******************************************************************************
*                                                                             *
*  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   : -                                                            *
*                                                                             *
******************************************************************************/

openida_binary *load_binary_file(const char *filename)
{
    openida_binary *result;                 /* Adresse à retourner         */

    result = (openida_binary *)calloc(1, sizeof(openida_binary));

    result->filename = strdup(filename);





    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : xpathObj = point de lecture de tous les éléments.            *
*                                                                             *
*  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   : -                                                            *
*                                                                             *
******************************************************************************/

openida_binary *load_binary_file_from_xml(xmlXPathObjectPtr xpathObj)
{
    openida_binary *result;                 /* Adresse à retourner         */

    int i;

    result = (openida_binary *)calloc(1, sizeof(openida_binary));

    for (i = 0; i < XPATH_OBJ_NODES_COUNT(xpathObj); i++)
        if (xmlStrEqual(NODE_FROM_PATH_OBJ(xpathObj, i)->name, BAD_CAST "Filename"))
            result->filename = qck_get_node_text_value(NODE_FROM_PATH_OBJ(xpathObj, i));




    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = élément binaire à supprimer de la mémoire.          *
*                                                                             *
*  Description : Décharge de la mémoire le contenu d'un fichier.              *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void unload_binary_file(openida_binary *binary)
{
    free(binary->filename);

    free(binary);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = élément binaire à traiter.                          *
*                                                                             *
*  Description : Fournit une description humaine d'un élément binaire.        *
*                                                                             *
*  Retour      : Chaîne de caractères humainenement lisible de représentation.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *openida_binary_to_string(const openida_binary *binary)
{
    return binary->filename;

}









/******************************************************************************
*                                                                             *
*  Paramètres  : xpathCtx = contexte à utiliser pour mener les parcours.      *
*                base     = première partie de l'expression XPath d'accès.    *
*                index    = indice de la élément dans la liste des voisins.   *
*                                                                             *
*  Description : Lit un élément binaire depuis un fichier XML.                *
*                                                                             *
*  Retour      : Représentation mise en place à libérer de la mémoire.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

openida_binary *read_openida_binary_from_xml(xmlXPathContextPtr xpathCtx, const char *base, unsigned int index)
{
    openida_binary *result;                 /* Représentation à retourner  */
    size_t expr_len;                        /* Taille d'une expression     */
    char *expr;                             /* Chemin XPath reconstitué    */
    xmlXPathObjectPtr xpathObj;             /* Cible d'une recherche       */
    char *value;                            /* Type d'élément rencontré    */
    size_t sub_expr_len;                    /* Taille d'une expression #2  */
    char *sub_expr;                         /* Chemin XPath reconstitué #2 */
    int i;                                  /* Boucle de parcours          */

    result = NULL;

    /* S'occupe en premier lieu du niveau courant */

    expr_len = strlen(base) + strlen("/*[position()=") + strlen("4294967295") /* UINT_MAX */ + strlen("]") + 1;

    expr = (char *)calloc(expr_len, sizeof(char));
    snprintf(expr, expr_len, "%s/*[position()=%u]", base, index);

    xpathObj = get_node_xpath_object(xpathCtx, expr);

    value = qck_get_node_prop_value(NODE_FROM_PATH_OBJ(xpathObj, 0), "type");

    xmlXPathFreeObject(xpathObj);

    if (value == NULL) goto robfx_err1;

    /* Raffinement au second passage */

    sub_expr_len = expr_len + strlen("/*");
    sub_expr = (char *)calloc(sub_expr_len, sizeof(char));
    snprintf(sub_expr, sub_expr_len, "%s/*", expr);

    xpathObj = get_node_xpath_object(xpathCtx, sub_expr);

    if (strcmp(value, "file") == 0) result = load_binary_file_from_xml(xpathObj);

    xmlXPathFreeObject(xpathObj);

    free(sub_expr);

 robfx_err1:

    free(expr);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = élément binaire à traiter.                          *
*                writer = rédacteur dédié à l'écriture.                       *
*                                                                             *
*  Description : Ecrit une sauvegarde du binaire dans un fichier XML.         *
*                                                                             *
*  Retour      : true si l'opération a bien tourné, false sinon.              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool write_openida_binary_to_xml(const openida_binary *binary, xmlTextWriterPtr writer)
{
    bool result;                            /* Bilan à faire remonter      */

    result = open_xml_element(writer, "Binary");

    result &= write_xml_attribute(writer, "type", "file");

    result &= write_xml_element_with_content(writer, "Filename", "%s", binary->filename);

    result &= close_xml_element(writer);

    return result;

}





/******************************************************************************
*                                                                             *
*  Paramètres  : filename = nom du fichier à charger.                         *
*                length   = taille des données mises en mémoire. [OUT]        *
*                                                                             *
*  Description : Charge en mémoire le contenu d'un fichier.                   *
*                                                                             *
*  Retour      : Adresse du contenu binaire ou NULL en cas d'échec.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

uint8_t *map_binary_file(const char *filename, size_t *length)
{
    uint8_t *result;                        /* Données à retourner         */
    int fd;                                 /* Fichier ouvert en lecture   */
    struct stat info;                       /* Informations sur le fichier */
    int ret;                                /* Bilan d'un appel            */

    fd = open(filename, 0, O_RDONLY);
    if (fd == -1)
    {
        perror("open()");
        return NULL;
    }

    ret = fstat(fd, &info);
    if (ret == -1)
    {
        perror("fstat()");
        close(fd);
        return NULL;
    }

    *length = info.st_size;

    result = (uint8_t *)mmap(NULL, *length, PROT_READ, MAP_PRIVATE, fd, 0);
    if (result == MAP_FAILED)
    {
        perror("mmap()");
        result = NULL;
    }

    ret = close(fd);
    if (ret == -1)
        perror("close()");

    return result;

}







void fill_snippet(GtkSnippet *snippet, GtkWidget *panel, GtkWidget *panel2)
{
    off_t length;
    uint8_t *bin_data;
    int ret;

    exe_format *format;
    dbg_format *dformat;
    asm_processor *proc;
    asm_instr *instr;

    bin_part **parts;
    size_t parts_count;


    char **comments;
    uint64_t *offsets;
    size_t comments_count;
    code_line_info **comments_list;



    code_line_info **list;
    size_t list_len;
    code_line_info *item;

    off_t start;
    off_t pos;
    off_t len;

    char buffer[64];

    uint64_t base = 0;
    uint64_t offset = 0;

    size_t i;

    size_t k;

    proc = create_x86_processor();

    pos = 0;
    len = 0x28;


    register_exe_format(_("ELF"), elf_is_matching, load_elf);
    register_exe_format(_("Java"), java_is_matching, load_java);
    register_exe_format(_("Portable Executable"), pe_is_matching, load_pe);


    bin_data = map_binary_file("/tmp/hello", &length);

    printf(" ~~ bin_data ~~ :: %p (%d)\n", bin_data, length);


    if (bin_data == NULL) return;



    format = load_new_exe_format(bin_data, length);

    printf(" --> ok ? %p\n", format);

    //exit(0);

    return;


    /////format = load_elf(bin_data, length);
    dformat = load_dwarf(bin_data, length, format);


    //comments_count = get_dwarf_comments(dformat, &comments, &offsets);

    comments = NULL;
    offsets = NULL;
    comments_count = 0;

    get_elf_symbol_comments(format, &comments, &offsets, &comments_count);

    comments_list = (code_line_info **)calloc(comments_count, sizeof(code_line_info *));

    for (i = 0; i < comments_count; i++)
        comments_list[i] = create_code_line_info(offsets[i], NULL, strdup(comments[i]));


    qsort(comments_list, comments_count, sizeof(code_line_info *), compare_code_line_info);



    parts = get_elf_default_code_parts(format, &parts_count);



    list = NULL;
    list_len = 0;


    gtk_snippet_set_format(snippet, format);
    gtk_snippet_set_processor(snippet, proc);


    for (i = 0; i < parts_count; i++)
    {
        get_bin_part_values(parts[i], &pos, &len, &base);



        /*find_line_info(bin_data, &len);*/

        /*
          printf("Exiting...\n");
          exit(0);
        */

        offset = base;

        for (k = 0; k < comments_count; k++)
            if (comments_list[k]->offset >= base) break;




        item = create_code_line_info(offset, NULL, "Simple HelloWorld !");

        list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *));
        list[list_len - 1] = item;



        start = pos;
        pos = 0;

        while (pos < len)
        {
            offset = base + pos;

            /* Si on a un commentaire pour cette ligne... */
            if (k < comments_count && comments_list[k]->offset == offset)
            {
                list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *));
                list[list_len - 1] = comments_list[k++];
            }


            instr = decode_instruction(proc, &bin_data[start], &pos, len, start, offset);


            item = create_code_line_info(offset, instr, NULL);

            list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *));
            list[list_len - 1] = item;

            //gtk_snippet_add_line(snippet, offset, instr, NULL);


        }


        /****
             ret = munmap(bin_data, length);
        ****/

        /*
          gtk_snippet_build_content(snippet);
        */

    }

    for (i = 0; i < list_len; i++)
    {
        gtk_snippet_add_line(snippet, list[i]);
        /* TODO: free() */
    }

    handle_new_exe_on_symbols_panel(panel, format);
    handle_new_exe_on_strings_panel(panel2, format);

}