/* Chrysalide - Outil d'analyse de fichiers binaires * format.c - équivalent Python du fichier "format/format.c" * * Copyright (C) 2012-2013 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 */ #include "format.h" #include #include #include "../helpers.h" #include "../arch/vmpa.h" /* ------------------------ PARCOURS DE SYMBOLES DE BINAIRES ------------------------ */ /* Parcours des symboles présents dans un binaire */ typedef struct _pyBinSymbolIterator { PyObject_HEAD /* A laisser en premier */ GBinFormat *format; /* Format binaire à consulter */ size_t next; /* Symbole binaire à présenter */ } pyBinSymbolIterator; /* Prend acte d'un compteur de référence à 0. */ static void py_binary_symbol_iterator_dealloc(PyObject *); /* Fournit un itérateur pour symboles de format binaire. */ static PyObject *py_binary_symbol_iterator_next(PyObject *); /* Initialise un objet Python de type 'BinSymbolIterator'. */ static int py_binary_symbol_iterator_init(PyObject *, PyObject *, PyObject *); /* ---------------------------- FORMAT BINAIRE GENERIQUE ---------------------------- */ /* Recherche le symbole correspondant à une étiquette. */ static PyObject *py_binary_format_find_symbol_by_label(PyObject *, PyObject *); /* Recherche le symbole suivant celui lié à une adresse. */ static PyObject *py_binary_format_find_symbol_at(PyObject *, PyObject *); /* Recherche le symbole suivant celui lié à une adresse. */ static PyObject *py_binary_format_find_next_symbol_at(PyObject *, PyObject *); /* Recherche le symbole correspondant à une adresse. */ static PyObject *py_binary_format_resolve_symbol(PyObject *, PyObject *); /* Fournit une référence vers le contenu binaire analysé. */ static PyObject *py_binary_format_get_content(PyObject *, void *); /* Fournit la liste de tous les symboles détectés. */ static PyObject *py_binary_format_get_symbols(PyObject *, void *); /* ---------------------------------------------------------------------------------- */ /* PARCOURS DE SYMBOLES DE BINAIRES */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = instance Python à libérer de la mémoire. * * * * Description : Prend acte d'un compteur de référence à 0. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void py_binary_symbol_iterator_dealloc(PyObject *self) { pyBinSymbolIterator *iterator; /* Références pour le parcours */ /** * Il aurait été sans doute mieux de reposer ici sur .tp_finalize, * mais cela semble impliquer de mettre en place tous les mécanismes de GC... * * cf. https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation */ iterator = (pyBinSymbolIterator *)self; g_object_unref(G_OBJECT(iterator->format)); Py_TYPE(self)->tp_free((PyObject *)self); } /****************************************************************************** * * * Paramètres : self = itérateur à manipuler. * * * * Description : Fournit un itérateur pour symboles de format binaire. * * * * Retour : Instance Python prête à emploi. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_symbol_iterator_next(PyObject *self) { PyObject *result; /* Instance à retourner */ pyBinSymbolIterator *iterator; /* Références pour le parcours */ size_t count; /* Nombre de symboles présents */ GBinSymbol **symbols; /* Liste de ces mêmes symboles */ iterator = (pyBinSymbolIterator *)self; symbols = g_binary_format_get_symbols(iterator->format, &count); if (iterator->next < count) { result = pygobject_new(G_OBJECT(symbols[iterator->next])); iterator->next++; } else { PyErr_SetNone(PyExc_StopIteration); result = NULL; } return result; } /****************************************************************************** * * * Paramètres : self = objet instancié à initialiser. * * args = arguments fournis à l'appel. * * kwds = arguments de type key=val fournis. * * * * Description : Initialise un objet Python de type 'BinSymbolIterator'. * * * * Retour : Instance Python mise en place. * * * * Remarques : - * * * ******************************************************************************/ static int py_binary_symbol_iterator_init(PyObject *self, PyObject *args, PyObject *kwds) { pyBinSymbolIterator *iterator; /* Références pour le parcours */ PyObject *format; /* Format binaire en Python */ int ret; /* Bilan de lecture des args. */ ret = PyArg_ParseTuple(args, "O", &format); if (!ret) return -1; ret = PyObject_IsInstance(format, (PyObject *)get_python_binary_format_type()); if (!ret) return -1; iterator = (pyBinSymbolIterator *)self; iterator->format = G_BIN_FORMAT(pygobject_get(format)); g_object_ref(G_OBJECT(iterator->format)); iterator->next = 0; return 0; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_binary_symbol_iterator_type(void) { static PyTypeObject py_binary_symbol_iterator_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.format.BinSymbolIterator", .tp_basicsize = sizeof(pyBinSymbolIterator), .tp_dealloc = py_binary_symbol_iterator_dealloc, .tp_flags = Py_TPFLAGS_DEFAULT, .tp_doc = "Iterator for binary symbols", .tp_iter = PyObject_SelfIter, .tp_iternext = py_binary_symbol_iterator_next, .tp_init = py_binary_symbol_iterator_init, .tp_new = PyType_GenericNew, }; return &py_binary_symbol_iterator_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide...BinSymbolIterator'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool register_python_binary_symbol_iterator(PyObject *module) { PyTypeObject *py_binary_symbol_iterator_type; /* Type Python 'BinSymbolIterator' */ int ret; /* Bilan d'un appel */ py_binary_symbol_iterator_type = get_python_binary_symbol_iterator_type(); py_binary_symbol_iterator_type->tp_base = &PyBaseObject_Type; if (PyType_Ready(py_binary_symbol_iterator_type) != 0) return false; Py_INCREF(py_binary_symbol_iterator_type); ret = PyModule_AddObject(module, "BinSymbolIterator", (PyObject *)py_binary_symbol_iterator_type); if (ret != 0) return false; return true; } /* ---------------------------------------------------------------------------------- */ /* FORMAT BINAIRE GENERIQUE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = classe représentant un binaire. * * args = arguments fournis à l'appel. * * * * Description : Recherche le symbole correspondant à une étiquette. * * * * Retour : Symbol trouvé si l'opération a été un succès, None sinon. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_find_symbol_by_label(PyObject *self, PyObject *args) { PyObject *result; /* Valeur à retourner */ PyObject *label; /* Etiquette à retrouver */ int ret; /* Bilan de lecture des args. */ GBinFormat *format; /* Format de binaire manipulé */ GBinSymbol *symbol; /* Enventuel symbole trouvé */ bool found; ret = PyArg_ParseTuple(args, "O", &label); if (!ret) return NULL; ret = PyUnicode_Check(label); if (!ret) return NULL; format = G_BIN_FORMAT(pygobject_get(self)); found = g_binary_format_find_symbol_by_label(format, PyUnicode_DATA(label), &symbol); if (found) result = pygobject_new(G_OBJECT(symbol)); else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un binaire. * * args = arguments fournis à l'appel. * * * * Description : Recherche le symbole suivant celui lié à une adresse. * * * * Retour : Symbol trouvé si l'opération a été un succès, None sinon. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_find_symbol_at(PyObject *self, PyObject *args) { PyObject *result; /* Valeur à retourner */ PyObject *py_vmpa; /* Localisation version Python */ int ret; /* Bilan de lecture des args. */ GBinFormat *format; /* Format de binaire manipulé */ GBinSymbol *symbol; /* Enventuel symbole trouvé */ bool found; ret = PyArg_ParseTuple(args, "O", &py_vmpa); if (!ret) return NULL; ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type()); if (!ret) return NULL; format = G_BIN_FORMAT(pygobject_get(self)); found = g_binary_format_find_symbol_at(format, get_internal_vmpa(py_vmpa), &symbol); if (found) result = pygobject_new(G_OBJECT(symbol)); else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un binaire. * * args = arguments fournis à l'appel. * * * * Description : Recherche le symbole suivant celui lié à une adresse. * * * * Retour : Symbol trouvé si l'opération a été un succès, None sinon. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_find_next_symbol_at(PyObject *self, PyObject *args) { PyObject *result; /* Valeur à retourner */ PyObject *py_vmpa; /* Localisation version Python */ int ret; /* Bilan de lecture des args. */ GBinFormat *format; /* Format de binaire manipulé */ GBinSymbol *symbol; /* Enventuel symbole trouvé */ bool found; ret = PyArg_ParseTuple(args, "O", &py_vmpa); if (!ret) return NULL; ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type()); if (!ret) return NULL; format = G_BIN_FORMAT(pygobject_get(self)); found = g_binary_format_find_next_symbol_at(format, get_internal_vmpa(py_vmpa), &symbol); if (found) result = pygobject_new(G_OBJECT(symbol)); else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un format binaire. * * args = arguments fournis à l'appel. * * * * Description : Recherche le symbole correspondant à une adresse. * * * * Retour : Tuple (nom, décallage) ou Py_None. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_resolve_symbol(PyObject *self, PyObject *args) { PyObject *result; /* Valeur à retourner */ PyObject *py_vmpa; /* Localisation version Python */ int strict; /* Tolérance acceptée */ int ret; /* Bilan de lecture des args. */ GBinFormat *format; /* Format de binaire manipulé */ GBinSymbol *symbol; /* Enventuel symbole trouvé */ phys_t diff; /* Décallage éventuel mesuré */ bool found; ret = PyArg_ParseTuple(args, "Op", &py_vmpa, &strict); if (!ret) return NULL; ret = PyObject_IsInstance(py_vmpa, (PyObject *)get_python_vmpa_type()); if (!ret) return NULL; format = G_BIN_FORMAT(pygobject_get(self)); found = g_binary_format_resolve_symbol(format, get_internal_vmpa(py_vmpa), strict, &symbol, &diff); if (found) { result = PyTuple_New(2); PyTuple_SetItem(result, 0, pygobject_new(G_OBJECT(symbol))); PyTuple_SetItem(result, 1, PyLong_FromUnsignedLongLong(diff)); } else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit une référence vers le contenu binaire analysé. * * * * Retour : Gestionnaire de contenu binaire en place. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_get_content(PyObject *self, void *closure) { PyObject *result; /* Trouvailles à retourner */ GBinFormat *format; /* Format de binaire manipulé */ GBinContent *content; /* Instance GLib correspondante*/ format = G_BIN_FORMAT(pygobject_get(self)); content = g_binary_format_get_content(format); result = pygobject_new(G_OBJECT(content)); g_object_unref(content); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant un format binaire. * * closure = adresse non utilisée ici. * * * * Description : Fournit la liste de tous les symboles détectés. * * * * Retour : Tableau créé ou NULL si aucun symbole trouvé. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_binary_format_get_symbols(PyObject *self, void *closure) { PyObject *result; /* Instance à retourner */ PyTypeObject *iterator_type; /* Type Python de l'itérateur */ PyObject *args_list; /* Arguments de mise en place */ iterator_type = get_python_binary_symbol_iterator_type(); Py_INCREF(self); args_list = Py_BuildValue("(O)", self); result = PyObject_CallObject((PyObject *)iterator_type, args_list); Py_DECREF(args_list); return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_binary_format_type(void) { static PyMethodDef py_bin_format_methods[] = { { "find_symbol_by_label", py_binary_format_find_symbol_by_label, METH_VARARGS, "find_symbol_by_label($self, label, /)\n--\n\nFind a symbol by its label." }, { "find_symbol_at", py_binary_format_find_symbol_at, METH_VARARGS, "find_symbol_at($self, addr, /)\n--\n\nFind a symbol at a given address." }, { "find_next_symbol_at", py_binary_format_find_next_symbol_at, METH_VARARGS, "find_next_symbol_at($self, addr, /)\n--\n\nFind the symbol next to the one found at a given address." }, { "resolve_symbol", py_binary_format_resolve_symbol, METH_VARARGS, "resolve_symbol($self, addr, strict, /)\n--\n\nSearch a position inside a routine by a given address." }, { NULL } }; static PyGetSetDef py_bin_format_getseters[] = { { "content", py_binary_format_get_content, NULL, "Content of the binary format.", NULL }, { "symbols", py_binary_format_get_symbols, NULL, "Iterable list of all symbols found in the binary format.", NULL }, { NULL } }; static PyTypeObject py_bin_format_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.format.BinFormat", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide binary format", .tp_methods = py_bin_format_methods, .tp_getset = py_bin_format_getseters }; return &py_bin_format_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.format.BinFormat'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool register_python_binary_format(PyObject *module) { PyTypeObject *py_bin_format_type; /* Type Python 'BinFormat' */ PyObject *dict; /* Dictionnaire du module */ py_bin_format_type = get_python_binary_format_type(); APPLY_ABSTRACT_FLAG(py_bin_format_type); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_BIN_FORMAT, py_bin_format_type, &PyGObject_Type)) return false; return true; }