summaryrefslogtreecommitdiff
path: root/plugins/theseus/theseus.c
blob: b04c3555ab9a2e2263cb7801ebd2a88abefd750c (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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

/* OpenIDA - Outil d'analyse de fichiers binaires
 * theseus.c - décompilation en fonction du flot d'exécution
 *
 * Copyright (C) 2010-2011 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 "theseus.h"


#include <analysis/exporter.h>
#include <analysis/line_code.h>
#include <arch/artificial.h>


/* Traite en premier lieu les adresses marquées "impératives". */
static bool register_white_list(GOpenidaBinary *);

/* Indique si une ligne peut faire l'objet d'un nouveau suivi. */
static bool can_line_be_followed(const GRenderingLine *);

/* Indique si une ligne a déjà été traitée lors d'un suivi. */
static bool can_line_be_processed(const GRenderingLine *);

/* Marque une ligne comme ayant été traitée. */
static void mark_line_as_done(GRenderingLine *, bool);

/* Suit le flot d'exécution à partir d'un point donné. */
static bool follow_flow_from_line(GOpenidaBinary *, vmpa_t);

/* Désassemble une nouvelle instruction à partir d'une adresse. */
static GRenderingLine *disassemble_target_address(GOpenidaBinary *, GRenderingLine *, vmpa_t);

/* Insère dans le flux existant les nouvelles lignes crées. */
static bool insert_new_lines_into_flow(GOpenidaBinary *, GRenderingLine *, GRenderingLine *);





/*
mov	edx, 0x80480fb
call	[edx]
mov	edx, 0x80480fb
add	edx, 0x8
call	[edx]
mov	edx, 0x80480fb
add	edx, 0x10
call	[edx]
mov	edx, 0x80480fb
add	edx, 0xc
call	[edx]
mov	edx, 0x80480fb
add	edx, 0x14
call	[edx]
mov	edx, 0x80480fb
add	edx, 0x4
call	[edx]
*/


static const vmpa_t _white_list[] = {
    0x80480fbull,
    0x8048103ull,
    0x804810bull,
    0x8048107ull,
    0x804810full,
    0x80480ffull
};

static const size_t _white_list_count = 6;




/******************************************************************************
*                                                                             *
*  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 désassemblées        */
    GExeFormat *format;                     /* Format du binaire traité    */
    vmpa_t start;                           /* Point de départ de l'action */



    GRenderingOptions *options;
    GRenderingLine *iter;


    lines = g_openida_binary_get_lines(binary);




    format = g_openida_binary_get_format(binary);

    start = g_exe_format_get_entry_point(format);

    result = register_white_list(binary);

    result &= follow_flow_from_line(binary, start);


    //follow_flow_from_line(binary, 0x0804810bull);





    options = g_openida_binary_get_options(binary);

    for (iter = lines; iter != NULL; iter = g_rendering_line_get_next_iter(lines, iter, NULL))
    {

        printf(" >>>>>> %c%c  ",
               g_object_get_data(G_OBJECT(iter), "theseus_white") != NULL ? '!' : ' ',
               g_object_get_data(G_OBJECT(iter), "theseus_done") != NULL ? '*' : ' ');
        g_content_exporter_add_text(G_CONTENT_EXPORTER(iter), options, 0/*MRD_BLOCK*/, stdout);
        fflush(stdout);
        printf("\n");


    }



    //exit(-1);




    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = représentation binaire à traiter.                   *
*                                                                             *
*  Description : Traite en premier lieu les adresses marquées "impératives".  *
*                                                                             *
*  Retour      : true si le déroulement s'est bien effectué, false sinon.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool register_white_list(GOpenidaBinary *binary)
{
    bool result;                            /* Bilan à retourner           */
    GRenderingLine *lines;                  /* Lignes désassemblées        */
    size_t i;                               /* Boucle de parcours          */
    GRenderingLine *target;                 /* Ligne à transformer ?       */
    GRenderingLine *last;                   /* Nouvelle ligne principale   */
    GRenderingLine *new;                    /* Nouvelles lignes de rendu   */

    result = true;

    lines = g_openida_binary_get_lines(binary);

    for (i = 0; i < _white_list_count && result; i++)
    {
        target = g_rendering_line_find_by_address(lines, NULL, _white_list[i]);
        target = g_rendering_line_loop_for_code(target, NULL);

        new = disassemble_target_address(binary, target, _white_list[i]);

        last = g_rendering_line_get_last_iter(new, NULL);
        mark_line_as_done(last, true);

        result = insert_new_lines_into_flow(binary, target, new);

        result &= follow_flow_from_line(binary, _white_list[i]);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : line = ligne de rendu à analyser.                            *
*                                                                             *
*  Description : Indique si une ligne peut faire l'objet d'un nouveau suivi.  *
*                                                                             *
*  Retour      : true si le suivi peut continuer, false sinon.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool can_line_be_followed(const GRenderingLine *line)
{
    bool result;                            /* Bilan d'analyse à renvoyer  */

    result = true;

    if (g_object_get_data(G_OBJECT(line), "theseus_done") != NULL)
        result = (g_object_get_data(G_OBJECT(line), "theseus_white") != NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : line = ligne de rendu à analyser.                            *
*                                                                             *
*  Description : Indique si une ligne a déjà été traitée lors d'un suivi.     *
*                                                                             *
*  Retour      : true si le suivi peut continuer, false sinon.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool can_line_be_processed(const GRenderingLine *line)
{
    return (g_object_get_data(G_OBJECT(line), "theseus_done") == NULL);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : line  = ligne de rendu à traiter.                            *
*                white = type de marquage à apposer (liste blanche ou normal).*
*                                                                             *
*  Description : Marque une ligne comme ayant été traitée.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void mark_line_as_done(GRenderingLine *line, bool white)
{
    if (white)
        g_object_set_data(G_OBJECT(line), "theseus_white", line);
    else
        g_object_set_data(G_OBJECT(line), "theseus_done", line);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = représentation binaire à traiter.                   *
*                start  = point de départ de la procédure.                    *
*                                                                             *
*  Description : Suit le flot d'exécution à partir d'un point donné.          *
*                                                                             *
*  Retour      : true si le déroulement s'est bien effectué, false sinon.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool follow_flow_from_line(GOpenidaBinary *binary, vmpa_t start)
{
    bool result;                            /* Bilan de opérations         */
    GRenderingLine *lines;                  /* Lignes désassemblées        */
    GRenderingLine *first;                  /* Première ligne à traiter    */
    vmpa_t addr;                            /* Adresse référencée          */
    GRenderingLine *new;                    /* Nouvelles lignes de rendu   */
    GRenderingLine *iter;                   /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction à ausculter     */
    InstructionLinkType type;               /* Type de référence           */
    GRenderingLine *target;                 /* Ligne visée par la référence*/

    lines = g_openida_binary_get_lines(binary);

    first = g_rendering_line_find_by_address(lines, NULL, start);
    first = g_rendering_line_loop_for_code(first, NULL);

    if (!can_line_be_followed(first))
        return true;

    //if (start != 0x0804810bull) return true;

    printf("###################################### Passage\n");

    /* Alignement sur le point d'entrée */

    result = true;

    addr = get_rendering_line_address(first);

    if (addr < start)
    {
        new = disassemble_target_address(binary, first, start);

        result = insert_new_lines_into_flow(binary, target, new);

        first = g_rendering_line_find_by_address(lines, NULL, start);

        //return true;

    }
    else g_object_set_data(G_OBJECT(first), "theseus_done", binary);




    printf("Analyse :: 0x%016llx\n", get_rendering_line_address(first));





    /* Poursuite du suivi du flot... */

    for (iter = g_rendering_line_get_next_iter(lines, first, NULL);
         iter != NULL/* && result*/;
         iter = g_rendering_line_get_next_iter(lines, iter, NULL))
    {
        /* On ne traite que du code ici ! */
        if (!G_IS_CODE_LINE(iter)) continue;

        /*
        printf("testing... 0x%08llx => %d\n",
               get_rendering_line_address(iter),
               can_line_be_processed(iter));
        */

        if (!can_line_be_processed(iter))
            break;

        instr = g_code_line_get_instruction(G_CODE_LINE(iter));

        /* Si le code n'es pas désassemblé ! */
        if (G_IS_DB_INSTRUCTION(instr))
        {
            new = disassemble_target_address(binary, iter, get_rendering_line_address(iter));
            result = insert_new_lines_into_flow(binary, iter, new);

            if (!result)
            {
                mark_line_as_done(iter, false);
                break;
            }
            else
            {
                iter = new;
                mark_line_as_done(iter, false);

                instr = g_code_line_get_instruction(G_CODE_LINE(iter));
                if (!G_IS_DB_INSTRUCTION(instr)) break;

            }

        }
        else mark_line_as_done(iter, false);

        type = g_arch_instruction_get_link(instr, &addr);

        if (get_rendering_line_address(iter) ==  0x0804811aull)
            printf(" == got it !! ==\n");


        switch (type)
        {
            case ILT_JUMP:
            case ILT_JUMP_IF_FALSE:
            case ILT_JUMP_IF_TRUE:
                result = follow_flow_from_line(binary, addr);
                break;

           default:
               break;

        }

        if (get_rendering_line_address(iter) ==  0x0804811aull)
            printf(" == continue ? %d\n", result);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = représentation binaire à traiter.                   *
*                old    = ligne contenant l'adresse visée à remplacer.        *
*                target = adresse de la nouvelle instruction à voir.          *
*                                                                             *
*  Description : Désassemble une nouvelle instruction à partir d'une adresse. *
*                                                                             *
*  Retour      : Nouvelle(s) ligne(s) en place ou NULL en cas d'échec.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GRenderingLine *disassemble_target_address(GOpenidaBinary *binary, GRenderingLine *old, vmpa_t target)
{
    GRenderingLine *result;                 /* Lignes en place à renvoyer  */
    GExeFormat *format;                     /* Format du binaire fourni    */
    GArchProcessor *proc;                   /* Architecture du binaire     */
    GRenderingOptions *options;             /* Options de désassemblage    */
    bin_t *data;                            /* Données binaires chargées   */
    off_t length;                           /* Taille du code binaire      */
    vmpa_t old_addr;                        /* Adresse de ligne à casser   */
    off_t offset;                           /* Position dans le contenu    */
    vmpa_t i;                               /* Boucle de parcours          */
    GArchInstruction *instr;                /* Instruction décodée         */
    GRenderingLine *line;                   /* Nouvelle ligne de rendu     */

    result = NULL;

    format = g_openida_binary_get_format(binary);
    proc = get_arch_processor_from_format(format);
    options = g_openida_binary_get_options(binary);

    data = g_openida_binary_get_data(binary, &length);

    old_addr = get_rendering_line_address(old);
    if (!g_exe_format_translate_address_into_offset(format, old_addr, &offset))
        return NULL;

    /* Si on doit laisser sur le carreau quelques octets perdus... */
    for (i = 0; i < (target - old_addr); i++)
    {
        instr = g_db_instruction_new_from_data(data, &offset, length, 0/*base*/, proc);
        g_arch_instruction_set_location(instr, 0/*base*/ + offset - 1, 1, old_addr + i);

        line = g_code_line_new(old_addr + i, instr, options);
        g_rendering_line_add_to_lines(&result, line);

    }

    /* Décodage des instructions */

    instr = g_arch_processor_decode_instruction(proc, NULL /*FIXME*/, data,
                                                &offset, length, 0/*offset*/, target);

    line = g_code_line_new(target, instr, options);
    g_rendering_line_add_to_lines(&result, line);

    mark_line_as_done(line, false);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = représentation binaire à traiter.                   *
*                old    = ancienne ligne, point de départ des remplacements.  *
*                new    = nouvelle(s) ligne(s) de représentation.             *
*                                                                             *
*  Description : Insère dans le flux existant les nouvelles lignes crées.     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool insert_new_lines_into_flow(GOpenidaBinary *binary, GRenderingLine *old, GRenderingLine *new)
{
    bool result;                            /* Bilan de opérations         */
    GExeFormat *format;                     /* Format du binaire fourni    */
    GArchProcessor *proc;                   /* Architecture du binaire     */
    GRenderingOptions *options;             /* Options de désassemblage    */
    bin_t *data;                            /* Données binaires chargées   */
    off_t length;                           /* Taille du code binaire      */
    GRenderingLine **root;                  /* Racine des lignes de rendu  */
    GRenderingLine *iter;                   /* Boucle de parcours          */
    vmpa_t start;                           /* Adresse de début du bloc    */
    vmpa_t end;                             /* Adresse de fin du bloc      */
    vmpa_t max;                             /* Adresse de fin de ligne     */
    vmpa_t i;                               /* Boucle de parcours          */
    off_t offset;                           /* Position dans le contenu    */
    GArchInstruction *instr;                /* Instruction décodée         */
    GRenderingLine *line;                   /* Nouvelle ligne de rendu     */
    GRenderingLine *last;                   /* Dernière ligne à vérifier   */

    format = g_openida_binary_get_format(binary);
    proc = get_arch_processor_from_format(format);
    options = g_openida_binary_get_options(binary);

    data = g_openida_binary_get_data(binary, &length);

    /* Etendue du bloc de remplacement */

    iter = g_rendering_line_get_last_iter(new, NULL);

    start = get_rendering_line_address(new);

    end = get_rendering_line_address(iter);
    end += get_rendering_line_length(iter) - 1;

    /* Bourrage nécessaire ? */

    root = g_openida_binary_get_lines_root(binary);

    iter = g_rendering_line_find_by_address(*root, NULL, end);

    max = get_rendering_line_address(iter);
    max += get_rendering_line_length(iter);

    for (i = end + 1; i < max; i++)
    {
        if (!g_exe_format_translate_address_into_offset(format, i, &offset))
            /**
             * Comme les adresses proviennent à l'origine de partie valide,
             * on considère que l'appel est toujours un succès.
             * Cela permet d'avoir deux bilan possible : procédure bien déroulée,
             * ou bien remplacement impossible pour cause de place déjà prise.
             */
            /*return false*/;

        instr = g_db_instruction_new_from_data(data, &offset, length, 0/*base*/, proc);
        g_arch_instruction_set_location(instr, 0/*base*/ + offset - 1, 1, i);

        line = g_code_line_new(i, instr, options);
        g_rendering_line_add_to_lines(&new, line);

    }

    /* S'assure qu'on ne touche à rien de sacré */

    result = true;

    last = g_rendering_line_find_by_address(*root, NULL, end);

    for (iter = g_rendering_line_find_by_address(*root, NULL, start);
         iter != NULL && result;
         iter = g_rendering_line_get_next_iter(*root, iter, last))
    {
        result = can_line_be_processed(iter);
    }

    /* Remplacement en bonne et due forme */

    if (result)
    {
        g_rendering_line_remove_range(root, start, end);
        g_rendering_line_insert_lines(root, &new);
    }

    return result;

}