summaryrefslogtreecommitdiff
path: root/src/plugins/plugin-def.h
blob: b8b0e0d49a7d6f726b2e17bb4de17c7ff4d47234 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * plugin-def.h - prototypes pour les définitions de base utiles aux greffons
 *
 * Copyright (C) 2009-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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#ifndef _PLUGINS_PLUGIN_DEF_H
#define _PLUGINS_PLUGIN_DEF_H


#include <gmodule.h>
#include <stdbool.h>
#include <stdint.h>



/* ------------------------ IDENTIFICATION DE COMPATIBILITES ------------------------ */


/* Version identifiant les définitions courantes */
typedef uint32_t plugin_abi_version_t;

#define DEFINE_PLUGIN_ABI_VERSION(maj, min, rev) \
    (((maj & 0xff) << 24) | ((min & 0xff) << 16) | (rev & 0xffff))

#define GET_ABI_MAJ_VERSION(vs) ((vs >> 24) & 0xff)
#define GET_ABI_MIN_VERSION(vs) ((vs >> 16) & 0xff)
#define GET_ABI_REV_VERSION(vs) (vs & 0xffff)

#define CURRENT_ABI_VERSION DEFINE_PLUGIN_ABI_VERSION(0, 1, 0)

//#define HARD_CODE_CURRENT_ABI_VERSION const plugin_abi_version_t abi_version = CURRENT_ABI_VERSION



/* ------------------------- DEFINITION D'UN PROJET INTERNE ------------------------- */


/* Idenifiant d'une action menée */
typedef uint32_t plugin_action_t;

#define DEFINE_PLUGIN_CATEGORY(cat) ((cat & 0xff) << 24)
#define DEFINE_PLUGIN_SUB_CATEGORY(sub) ((sub & 0xff) << 16)
#define DEFINE_PLUGIN_ACTION(a) (a & 0xffff)

#define GET_PLUGIN_CATEGORY(val) ((val >> 24) & 0xff)
#define GET_PLUGIN_SUB_CATEGORY(val) ((val >> 16) & 0xff)

#define MASK_PLUGIN_CATEGORY(val) (val & (0xff << 24))
#define MASK_PLUGIN_SUB_CATEGORY(val) (val & (0xff << 16))


#define DPC_BASIC               DEFINE_PLUGIN_CATEGORY(0)
#define DPC_BINARY_PROCESSING   DEFINE_PLUGIN_CATEGORY(1)

// GUI

/* DPC_BASIC */

#define DPS_NONE                DEFINE_PLUGIN_SUB_CATEGORY(0)
#define DPS_PG_MANAGEMENT       DEFINE_PLUGIN_SUB_CATEGORY(1)

/* DPC_BINARY_PROCESSING */

#define DPS_CONTENT             DEFINE_PLUGIN_SUB_CATEGORY(0)
#define DPS_FORMAT              DEFINE_PLUGIN_SUB_CATEGORY(1)
#define DPS_DISASSEMBLY         DEFINE_PLUGIN_SUB_CATEGORY(2)
#define DPS_DETECTION           DEFINE_PLUGIN_SUB_CATEGORY(3)

// GUI -> project
// binary loaded
// binary unload

// GUI -> dialog box



/* Action(s) menée(s) par un greffon */
typedef enum _PluginAction
{
    /**
     * DPC_BASIC | DPS_NONE
     */

    /* Aucun intérêt */
    PGA_BASIC_NONE = DPC_BASIC | DPS_NONE | DEFINE_PLUGIN_ACTION(0),

    /**
     * DPC_BASIC | DPS_PG_MANAGEMENT
     */

    /* Chargement */
    PGA_PLUGIN_INIT = DPC_BASIC | DPS_PG_MANAGEMENT | DEFINE_PLUGIN_ACTION(0),

    /* Déchargement */
    PGA_PLUGIN_EXIT = DPC_BASIC | DPS_PG_MANAGEMENT | DEFINE_PLUGIN_ACTION(1),

    /**
     * DPC_BINARY_PROCESSING | DPS_CONTENT
     */

    /* Exploration de contenus binaires */
    PGA_CONTENT_EXPLORER = DPC_BINARY_PROCESSING | DPS_CONTENT | DEFINE_PLUGIN_ACTION(0),

    /* Conversion de contenus binaires en contenus chargés */
    PGA_CONTENT_RESOLVER = DPC_BINARY_PROCESSING | DPS_CONTENT | DEFINE_PLUGIN_ACTION(1),

    /* Intervention en toute fin d'analyse de contenu chargé */
    PGA_CONTENT_ANALYZED = DPC_BINARY_PROCESSING | DPS_CONTENT | DEFINE_PLUGIN_ACTION(2),

    /**
     * DPC_BINARY_PROCESSING | DPS_FORMAT
     */

    /* Détection et chargement */
    PGA_FORMAT_MATCHER     = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(0),

    /* Accompagnement du chargement (fin) */
    PGA_FORMAT_LOADER_LAST = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(1),

    /* Accompagnement du chargement (fin) */
    PGA_FORMAT_PRELOAD     = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(2),

    /**
     * DPC_BINARY_PROCESSING | DPS_DISASSEMBLY
     */

    /* Désassemblage démarré */
    PGA_DISASSEMBLY_STARTED     = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(0),

    /* Instructions toutes jutes désassemblées */
    PGA_DISASSEMBLY_RAW         = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(1),

    /* Crochets de type 'link' exécutés */
    PGA_DISASSEMBLY_HOOKED_LINK = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(2),

    /* Crochets de type 'post' exécutés */
    PGA_DISASSEMBLY_HOOKED_POST = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(3),

    /* Limites de routines définies */
    PGA_DISASSEMBLY_LIMITED     = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(4),

    /* Détection d'éventuelles boucles effectuée */
    PGA_DISASSEMBLY_LOOPS       = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(5),

    /* Liaisons entre instructions mises en place */
    PGA_DISASSEMBLY_LINKED      = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(6),

    /* Instructions regroupées en blocs basiques */
    PGA_DISASSEMBLY_GROUPED     = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(7),

    /* Définitions de profondeurs d'exécution */
    PGA_DISASSEMBLY_RANKED      = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(8),

    /* Désassemblage fini */
    PGA_DISASSEMBLY_ENDED       = DPC_BINARY_PROCESSING | DPS_DISASSEMBLY | DEFINE_PLUGIN_ACTION(9),

    /**
     * DPC_BINARY_PROCESSING | DPS_DETECTION
     */

    /* Intervention en toute fin d'analyse de contenu chargé */
    PGA_DETECTION_OBFUSCATORS = DPC_BINARY_PROCESSING | DPS_DETECTION | DEFINE_PLUGIN_ACTION(0),





    PGA_DISASSEMBLE         = (1 << 1),     /* Désassemblage (non trivial) */

    PGA_BINARY_DISASSEMBLED = (1 << 2),     /* Désassemblage fini          */
    PGA_BINARY_LINKED       = (1 << 3),     /* Liaison en place            */
    PGA_BINARY_BOUNDED      = (1 << 4),     /* Limites de routines définies*/
    PGA_BINARY_GROUPED      = (1 << 5),     /* Instructions regroupées     */
    PGA_BINARY_PRINTED      = (1 << 6),     /* Instructions imprimées      */

    PGA_DISASS_PROCESS      = (1 << 6),     /* Traitement niveau assembleur*/
    PGA_CODE_PROCESS        = (1 << 7),     /* Traitement du code existant */

    PGA_DEBUGGER_ATTACH     = (1 << 8),     /* Activation d'un débogueur   */
    PGA_DEBUGGER_DETACH     = (1 << 9),     /* Désactivation d'un débogueur*/

    /**
     * Organisation interne :
     *   - rassemblement massif de tous les greffons.
     *   - marquage des cellules vides.
     */
    PGA_ALL                 = 0xfffffffe,
    PGA_EMPTY               = 0xffffffff

} PluginAction;



/* MAJ !! */
#define PGA_COUNT 16






/* Types de greffon */
typedef enum _PluginType
{
    PGT_BINARY          = (1 << 0)          /* Actions sur un binaire      */


} PluginType;


/* Action(s) menée(s) par le greffon */
#if 0
typedef enum _PluginAction
{
    PGA_NONE                = (0 << 0),     /* Aucun intérêt               */

    PGA_FORMAT_MATCHER      = (1 << 0),     /* Détection et chargement     */

    PGA_DISASSEMBLE         = (1 << 1),     /* Désassemblage (non trivial) */

    PGA_BINARY_DISASSEMBLED = (1 << 2),     /* Désassemblage fini          */
    PGA_BINARY_LINKED       = (1 << 3),     /* Liaison en place            */
    PGA_BINARY_BOUNDED      = (1 << 4),     /* Limites de routines définies*/
    PGA_BINARY_GROUPED      = (1 << 5),     /* Instructions regroupées     */
    PGA_BINARY_PRINTED      = (1 << 6),     /* Instructions imprimées      */

    PGA_DISASS_PROCESS      = (1 << 6),     /* Traitement niveau assembleur*/
    PGA_CODE_PROCESS        = (1 << 7),     /* Traitement du code existant */

    PGA_DEBUGGER_ATTACH     = (1 << 8),     /* Activation d'un débogueur   */
    PGA_DEBUGGER_DETACH     = (1 << 9)      /* Désactivation d'un débogueur*/

} PluginAction;
#endif



/* Actions éligibles pour run_plugins_on_binary() */
#define PGA_BINARY_ACTIONS ( 0                                              \
                            | PGA_BINARY_DISASSEMBLED | PGA_BINARY_LINKED   \
                            | PGA_BINARY_BOUNDED | PGA_BINARY_PRINTED       \
                             )



/* Fournit une indication sur le(s) type(s) du greffon présent. */
//typedef PluginType (* get_plugin_type_fc) (void);

/* Fournit une indication sur le type d'opération(s) menée(s). */
//typedef PluginAction (* get_plugin_action_fc) (void);


/* PGA_FORMAT_MATCHER */

/* Bilans d'une reconnaissance */
typedef enum _MatchingFormatAction
{
    MFA_NONE,                               /* Aucune détection            */
    MFA_MATCHED,                            /* Format reconnu              */
    MFA_RELOAD,                             /* Rechargemet opéré           */

    MFA_COUNT

} MatchingFormatAction;







/* ------------------------ PREMIER INTERFACAGE PROTOCOLAIRE ------------------------ */


#define CHRYSALIDE_PLUGIN_MAGIC 0xdeadc0de1234abcdull


/* Définition d'un greffon */
typedef struct _plugin_interface
{
    uint64_t magic;                         /* Vérification a minima       */
    plugin_abi_version_t abi_version;       /* Version du protocole utilisé*/

    const char *name;                       /* Désignation humaine courte  */
    const char *desc;                       /* Description plus loquace    */
    const char *version;                    /* Version du greffon          */

    bool container;                         /* Mise en place de greffons ? */

    const char **required;                  /* Pré-chargements requis      */
    size_t required_count;                  /* Quantité de ces dépendances */

    plugin_action_t *actions;               /* Liste des actions gérées    */
    size_t actions_count;                   /* Quantité de ces actions     */

} plugin_interface;


/* Facilitations de déclarations */

#define EMPTY_PG_LIST(name)                         \
    name = NULL,                                    \
    name ## _count = 0                              \

#define BUILD_PG_LIST(name, lst)                    \
    name = lst,                                     \
    name ## _count = sizeof(lst) / sizeof(lst[0])   \

#define AL(...) BUILD_PG_LIST(.actions, ((plugin_action_t []){ __VA_ARGS__ }))

#define RL(...) BUILD_PG_LIST(.required, ((const char *[]){ __VA_ARGS__ }))


#define DEFINE_CHRYSALIDE_PLUGIN(n, d, v, r, a)                 \
G_MODULE_EXPORT const plugin_interface _chrysalide_plugin = {   \
                                                                \
    .magic = CHRYSALIDE_PLUGIN_MAGIC,                           \
    .abi_version = CURRENT_ABI_VERSION,                         \
                                                                \
    .name = n,                                                  \
    .desc = d,                                                  \
    .version = v,                                               \
                                                                \
    .container = false,                                         \
                                                                \
    r,                                                          \
                                                                \
    a,                                                          \
                                                                \
}

#define DEFINE_CHRYSALIDE_CONTAINER_PLUGIN(n, d, v, r, a)       \
G_MODULE_EXPORT const plugin_interface _chrysalide_plugin = {   \
                                                                \
    .magic = CHRYSALIDE_PLUGIN_MAGIC,                           \
    .abi_version = CURRENT_ABI_VERSION,                         \
                                                                \
    .name = n,                                                  \
    .desc = d,                                                  \
    .version = v,                                               \
                                                                \
    .container = true,                                          \
                                                                \
    r,                                                          \
                                                                \
    a,                                                          \
                                                                \
}


/* Interfaçage primaire avec Chrysalide */
#define DEFINE_CHRYSALIDE_ACTIVE_PLUGIN(n, d, v, ...)   \
    DEFINE_CHRYSALIDE_PLUGIN(n, d, v, EMPTY_PG_LIST(.required), AL( __VA_ARGS__ ))



#endif  /* _PLUGINS_PLUGIN_DEF_H */