diff options
Diffstat (limited to 'src/analysis/scan/scanner.c')
-rw-r--r-- | src/analysis/scan/scanner.c | 342 |
1 files changed, 342 insertions, 0 deletions
diff --git a/src/analysis/scan/scanner.c b/src/analysis/scan/scanner.c new file mode 100644 index 0000000..ae3cb9a --- /dev/null +++ b/src/analysis/scan/scanner.c @@ -0,0 +1,342 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * scanner.c - parcours de contenus à la recherche de motifs + * + * Copyright (C) 2022 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 Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "scanner.h" + + +#include <assert.h> + + +#include "decl.h" +#include "scanner-int.h" +#include "../contents/file.h" + + + +/* Initialise la classe des recherches dans du binaire. */ +static void g_content_scanner_class_init(GContentScannerClass *); + +/* Initialise une instance de recherche dans du binaire. */ +static void g_content_scanner_init(GContentScanner *); + +/* Supprime toutes les références externes. */ +static void g_content_scanner_dispose(GContentScanner *); + +/* Procède à la libération totale de la mémoire. */ +static void g_content_scanner_finalize(GContentScanner *); + + + +/* Indique le type défini pour une recherche dans du binaire. */ +G_DEFINE_TYPE(GContentScanner, g_content_scanner, G_TYPE_OBJECT); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des recherches dans du binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_content_scanner_class_init(GContentScannerClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_content_scanner_dispose; + object->finalize = (GObjectFinalizeFunc)g_content_scanner_finalize; + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = instance à initialiser. * +* * +* Description : Initialise une instance de recherche dans du binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_content_scanner_init(GContentScanner *scanner) +{ + scanner->rules = NULL; + scanner->rule_count = 0; + + scanner->data_backend = NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_content_scanner_dispose(GContentScanner *scanner) +{ + size_t i; /* Boucle de parcours */ + + for (i = 0; i < scanner->rule_count; i++) + g_clear_object(&scanner->rules[i]); + + g_clear_object(&scanner->data_backend); + + G_OBJECT_CLASS(g_content_scanner_parent_class)->dispose(G_OBJECT(scanner)); + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_content_scanner_finalize(GContentScanner *scanner) +{ + if (scanner->rules != NULL) + free(scanner->rules); + + G_OBJECT_CLASS(g_content_scanner_parent_class)->finalize(G_OBJECT(scanner)); + +} + + +/****************************************************************************** +* * +* Paramètres : text = définitions textuelles de règles de recherche. * +* * +* Description : Prépare une recherche de motifs dans du contenu binaire. * +* * +* Retour : Mécanismes mis en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GContentScanner *g_content_scanner_new_from_text(const char *text) +{ + GContentScanner *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_CONTENT_SCANNER, NULL); + + if (!g_content_scanner_create_from_text(result, text)) + g_clear_object(&result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = scanner de contenus à initialiser pleinement. * +* text = définitions textuelles de règles de recherche. * +* * +* Description : Met en place un scanner de contenus binaires. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_content_scanner_create_from_text(GContentScanner *scanner, const char *text) +{ + bool result; /* Bilan à retourner */ + size_t length; /* Taille de la définition */ + + length = strlen(text); + + result = process_rules_definitions(scanner, text, length); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : filename = chemin vers des définitions de règles. * +* * +* Description : Prépare une recherche de motifs dans du contenu binaire. * +* * +* Retour : Mécanismes mis en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GContentScanner *g_content_scanner_new_from_file(const char *filename) +{ + GContentScanner *result; /* Structure à retourner */ + + result = g_object_new(G_TYPE_CONTENT_SCANNER, NULL); + + if (!g_content_scanner_create_from_file(result, filename)) + g_clear_object(&result); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = scanner de contenus à initialiser pleinement. * +* filename = chemin vers des définitions de règles. * +* * +* Description : Met en place un scanner de contenus binaires. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_content_scanner_create_from_file(GContentScanner *scanner, const char *filename) +{ + bool result; /* Bilan à retourner */ + GBinContent *content; /* Fichier à parcourir */ + phys_t size; /* Taille du contenu associé */ + vmpa2t start; /* Tête de lecture */ + const bin_t *data; /* Données à consulter */ + + result = false; + + content = g_file_content_new(filename); + if (content == NULL) goto no_content; + + size = g_binary_content_compute_size(content); + + g_binary_content_compute_start_pos(content, &start); + data = g_binary_content_get_raw_access(content, &start, size); + + result = process_rules_definitions(scanner, (char *)data, size); + + g_object_unref(G_OBJECT(content)); + + no_content: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = gestionnaire de recherche à compléter. * +* rule = règle de détection à intégrer. * +* * +* Description : Intègre une nouvelle règle de détection. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_content_scanner_add_rule(GContentScanner *scanner, GScanRule *rule) +{ + scanner->rules = realloc(scanner->rules, ++scanner->rule_count * sizeof(GScanRule *)); + + scanner->rules[scanner->rule_count - 1] = rule; + + g_object_ref(G_OBJECT(rule)); + +} + + +/****************************************************************************** +* * +* Paramètres : scanner = gestionnaire de recherche à compléter. * +* options = ensemble des options d'analyses à respecter. * +* content = contenu à parcourir et analyser. * +* * +* Description : Lance une analyse d'un contenu binaire. * +* * +* Retour : Contexte de suivi pour l'analyse menée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GScanContext *g_content_scanner_analyze(GContentScanner *scanner, GScanOptions *options, GBinContent *content) +{ + GScanContext *result; /* Bilan global à retourner */ + bool status; /* Bilan d'opération locale */ + size_t i; /* Boucle de parcours */ + + result = g_scan_context_new(options); + + if (scanner->data_backend == NULL) + { + scanner->data_backend = g_object_new(g_scan_options_get_backend_for_data(options), NULL); + assert(scanner->data_backend != NULL); + + status = (scanner->data_backend != NULL); + + for (i = 0; i < scanner->rule_count && status; i++) + status = g_scan_rule_setup_backend(scanner->rules[i], scanner->data_backend, result); + + if (!status) + { + g_clear_object(&result); + goto exit; + } + + } + + g_scan_context_set_content(result, content); + + for (i = 0; i < scanner->rule_count; i++) + g_scan_rule_analyze(scanner->rules[i], scanner->data_backend, result); + + exit: + + return result; + +} |