diff options
Diffstat (limited to 'plugins/pychrysa/analysis/binary.c')
-rw-r--r-- | plugins/pychrysa/analysis/binary.c | 276 |
1 files changed, 109 insertions, 167 deletions
diff --git a/plugins/pychrysa/analysis/binary.c b/plugins/pychrysa/analysis/binary.c index 1a513eb..fad84cd 100644 --- a/plugins/pychrysa/analysis/binary.c +++ b/plugins/pychrysa/analysis/binary.c @@ -28,138 +28,62 @@ #include <pygobject.h> +#include <analysis/binary.h> +#include "../helpers.h" +/* Lance l'analyse d'un élément binaire chargé. */ +static PyObject *py_loaded_binary_analyse(PyObject *, PyObject *); +/* Fournit le nom associé à l'élément binaire. */ +static PyObject *py_loaded_binary_get_name(PyObject *, void *); -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Fournit un accès à une définition de type à diffuser. * -* * -* Retour : Définition d'objet pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -PyTypeObject *get_python_loaded_binary_type(void) -{ - static PyTypeObject py_loaded_binary_type = { - - PyVarObject_HEAD_INIT(NULL, 0) - - .tp_name = "pychrysalide.analysis.LoadedBinary", - - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - - .tp_doc = "PyChrysalide loaded binary", - - }; - - return &py_loaded_binary_type; - -} - +/* Fournit le format de fichier reconnu dans le contenu binaire. */ +static PyObject *py_loaded_binary_get_format(PyObject *, void *); +/* Fournit le processeur de l'architecture liée au binaire. */ +static PyObject *py_loaded_binary_get_processor(PyObject *, void *); -#include <analysis/binary.h> - +/* Fournit le tampon associé au contenu assembleur d'un binaire. */ +static PyObject *py_loaded_binary_get_disassembled_buffer(PyObject *, void *); /****************************************************************************** * * -* Paramètres : module = module dont la définition est à compléter. * +* Paramètres : self = contenu binaire à manipuler. * +* args = non utilisé ici. * * * -* Description : Prend en charge l'objet 'pychrysalide.arch.loaded_binary'. * +* Description : Lance l'analyse d'un élément binaire chargé. * * * -* Retour : Bilan de l'opération. * +* Retour : Rien (None). * * * * Remarques : - * * * ******************************************************************************/ -bool register_python_loaded_binary(PyObject *module) +static PyObject *py_loaded_binary_analyse(PyObject *self, PyObject *args) { - PyTypeObject *py_loaded_binary_type; /* Type Python 'LoadedBinary' */ - int ret; /* Bilan d'un appel */ - PyObject *dict; /* Dictionnaire du module */ + GLoadedBinary *binary; /* Version GLib du format */ - py_loaded_binary_type = get_python_loaded_binary_type(); - - py_loaded_binary_type->tp_base = &PyGObject_Type; - py_loaded_binary_type->tp_basicsize = py_loaded_binary_type->tp_base->tp_basicsize; + binary = G_LOADED_BINARY(pygobject_get(self)); - if (PyType_Ready(py_loaded_binary_type) != 0) - return false; + g_loaded_binary_analyse(binary); - Py_INCREF(py_loaded_binary_type); - ret = PyModule_AddObject(module, "LoadedBinary", (PyObject *)py_loaded_binary_type); - if (ret != 0) return false; - - dict = PyModule_GetDict(module); - pygobject_register_class(dict, "LoadedBinary", G_TYPE_LOADED_BINARY, py_loaded_binary_type, - Py_BuildValue("(O)", py_loaded_binary_type->tp_base)); - - return true; + Py_RETURN_NONE; } - - - - - - - - - - -#if 0 - - -#include <pygobject.h> - - -#include <analysis/binary.h> - - -#include "../quirks.h" -#include "../arch/instruction.h" -#include "../format/executable.h" -#include "../glibext/codebuffer.h" - - - -/* Fournit le fichier correspondant à l'élément binaire. */ -static PyObject *py_loaded_binary_get_filename(PyObject *self, PyObject *args); - -/* Fournit le format de fichier reconnu dans le contenu binaire. */ -static PyObject *py_loaded_binary_get_format(PyObject *, PyObject *); - -/* Fournit les instructions issues du désassemblage. */ -static PyObject *py_loaded_binary_get_instructions(PyObject *, PyObject *); - - - -/* Fournit le tampon associé au contenu assembleur d'un binaire. */ -static PyObject *py_loaded_binary_get_disassembled_buffer(PyObject *, void *); - - - - /****************************************************************************** * * -* Paramètres : self = classe représentant un binaire. * -* args = arguments fournis à l'appel. * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * * * -* Description : Fournit le fichier correspondant à l'élément binaire. * +* Description : Fournit le nom associé à l'élément binaire. * * * * Retour : Nom de fichier avec chemin absolu. * * * @@ -167,17 +91,17 @@ static PyObject *py_loaded_binary_get_disassembled_buffer(PyObject *, void *); * * ******************************************************************************/ -static PyObject *py_loaded_binary_get_name(PyObject *self, PyObject *args) +static PyObject *py_loaded_binary_get_name(PyObject *self, void *closure) { PyObject *result; /* Trouvailles à retourner */ GLoadedBinary *binary; /* Version native */ - const char *filename; /* Fichier associé au binaire */ + const char *name; /* Désignation du binaire */ binary = G_LOADED_BINARY(pygobject_get(self)); - filename = g_loaded_binary_get_name(binary, true); + name = g_loaded_binary_get_name(binary, true); - result = PyString_FromString(filename); + result = PyUnicode_FromString(name); return result; @@ -186,28 +110,30 @@ static PyObject *py_loaded_binary_get_name(PyObject *self, PyObject *args) /****************************************************************************** * * -* Paramètres : self = classe représentant un binaire. * -* args = arguments fournis à l'appel. * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * * * * Description : Fournit le format de fichier reconnu dans le contenu binaire.* * * -* Retour : Nom de fichier avec chemin absolu. * +* Retour : Instance du format reconnu. * * * * Remarques : - * * * ******************************************************************************/ -static PyObject *py_loaded_binary_get_format(PyObject *self, PyObject *args) +static PyObject *py_loaded_binary_get_format(PyObject *self, void *closure) { - PyObject *result; /* Trouvailles à retourner */ - GLoadedBinary *binary; /* Version native */ - GExeFormat *format; /* Format du binaire physique */ + PyObject *result; /* Instance Python à retourner */ + GLoadedBinary *binary; /* Binaire en cours d'analyse */ + GExeFormat *format; /* Format du binaire lié */ binary = G_LOADED_BINARY(pygobject_get(self)); format = g_loaded_binary_get_format(binary); result = pygobject_new(G_OBJECT(format)); + //g_object_unref(G_OBJECT(format)); + return result; } @@ -215,37 +141,35 @@ static PyObject *py_loaded_binary_get_format(PyObject *self, PyObject *args) /****************************************************************************** * * -* Paramètres : self = classe représentant un binaire. * -* args = arguments fournis à l'appel. * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * * * -* Description : Fournit les instructions issues du désassemblage. * +* Description : Fournit le processeur de l'architecture liée au binaire. * * * -* Retour : Instructions issues du désassemblage. * +* Retour : Instance du processeur associé. * * * * Remarques : - * * * ******************************************************************************/ -static PyObject *py_loaded_binary_get_instructions(PyObject *self, PyObject *args) +static PyObject *py_loaded_binary_get_processor(PyObject *self, void *closure) { - PyObject *result; /* Trouvailles à retourner */ - GLoadedBinary *binary; /* Version native */ - GArchInstruction *instr; /* Première instruction */ + PyObject *result; /* Instance Python à retourner */ + GLoadedBinary *binary; /* Binaire en cours d'analyse */ + GArchProcessor *proc; /* Architecture visée */ binary = G_LOADED_BINARY(pygobject_get(self)); + proc = g_loaded_binary_get_processor(binary); - instr = g_loaded_binary_get_instructions(binary); + result = pygobject_new(G_OBJECT(proc)); - result = pygobject_new(G_OBJECT(instr)); + g_object_unref(G_OBJECT(proc)); return result; } - - - /****************************************************************************** * * * Paramètres : self = classe représentant une instruction. * @@ -275,88 +199,106 @@ static PyObject *py_loaded_binary_get_disassembled_buffer(PyObject *self, void * } - - - - - - - /****************************************************************************** * * -* Paramètres : module = module dont la définition est à compléter. * +* Paramètres : - * * * -* Description : Prend en charge l'objet 'pychrysalide.analysis.LoadedBinary'.* +* Description : Fournit un accès à une définition de type à diffuser. * * * -* Retour : Bilan de l'opération. * +* Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ -bool register_python_loaded_binary(PyObject *module) +PyTypeObject *get_python_loaded_binary_type(void) { - PyObject *pygobj_mod; /* Module Python-GObject */ - int ret; /* Bilan d'un appel */ - static PyMethodDef py_loaded_binary_methods[] = { { - "get_filename", (PyCFunction)py_loaded_binary_get_filename, + "analyse", py_loaded_binary_analyse, METH_NOARGS, - "Provide the filename of the loaded binary." - }, - { - "get_format", (PyCFunction)py_loaded_binary_get_format, - METH_NOARGS, - "Provide the file format recognized in the binary content." - }, - { - "get_instructions", (PyCFunction)py_loaded_binary_get_instructions, - METH_NOARGS, - "Give access to all disassembled instructions." + "analyse(/)\n--\n\nStart the analysis of the loaded binary and " \ + "send a \"disassembly-done\" signal when done." }, { NULL } }; static PyGetSetDef py_loaded_binary_getseters[] = { { - "disassembled_buffer", (getter)py_loaded_binary_get_disassembled_buffer, (setter)NULL, - "Give access to the disassembled code buffer.", NULL + "name", py_loaded_binary_get_name, NULL, + "Name of the loaded binary.", NULL + }, + { + "format", py_loaded_binary_get_format, NULL, + "File format recognized in the binary content.", NULL + }, + { + "processor", py_loaded_binary_get_processor, NULL, + "Handler for the current binary processor.", NULL + }, + { + "disassembled_buffer", py_loaded_binary_get_disassembled_buffer, NULL, + "Disassembled code buffer.", NULL }, { NULL } }; static PyTypeObject py_loaded_binary_type = { - PyObject_HEAD_INIT(NULL) + PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.analysis.LoadedBinary", - .tp_basicsize = sizeof(PyGObject), - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide loaded binary", .tp_methods = py_loaded_binary_methods, - .tp_getset = py_loaded_binary_getseters + .tp_getset = py_loaded_binary_getseters, }; - pygobj_mod = PyImport_ImportModule("gobject"); - if (pygobj_mod == NULL) return false; + return &py_loaded_binary_type; - py_loaded_binary_type.tp_base = (PyTypeObject *)PyObject_GetAttrString(pygobj_mod, "GObject"); - Py_DECREF(pygobj_mod); +} - if (PyType_Ready(&py_loaded_binary_type) < 0) - return false; - Py_INCREF(&py_loaded_binary_type); - ret = PyModule_AddObject(module, "LoadedBinary", (PyObject *)&py_loaded_binary_type); +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.analysis.LoadedBinary'.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ - return (ret == 0); +bool register_python_loaded_binary(PyObject *module) +{ + PyTypeObject *py_loaded_binary_type; /* Type Python 'LoadedBinary' */ + int ret; /* Bilan d'un appel */ + PyObject *dict; /* Dictionnaire du module */ -} + py_loaded_binary_type = get_python_loaded_binary_type(); + + py_loaded_binary_type->tp_base = &PyGObject_Type; + py_loaded_binary_type->tp_basicsize = py_loaded_binary_type->tp_base->tp_basicsize; + + APPLY_ABSTRACT_FLAG(py_loaded_binary_type); + + if (PyType_Ready(py_loaded_binary_type) != 0) + return false; -#endif + Py_INCREF(py_loaded_binary_type); + ret = PyModule_AddObject(module, "LoadedBinary", (PyObject *)py_loaded_binary_type); + if (ret != 0) return false; + + dict = PyModule_GetDict(module); + pygobject_register_class(dict, "LoadedBinary", G_TYPE_LOADED_BINARY, py_loaded_binary_type, + Py_BuildValue("(O)", py_loaded_binary_type->tp_base)); + return true; + +} |