/* Chrysalide - Outil d'analyse de fichiers binaires * bytes.h - sauvegarde d'une correspondance identifiée de suite d'octets * * 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 . */ #include "bytes.h" #include #include "bytes-int.h" /* --------------------- CORRESPONDANCE AVEC UNE SUITE D'OCTETS --------------------- */ /* Initialise la classe des correspondances de chaînes. */ static void g_bytes_match_class_init(GBytesMatchClass *); /* Initialise une instance de correspondance de chaîne trouvée. */ static void g_bytes_match_init(GBytesMatch *); /* Supprime toutes les références externes. */ static void g_bytes_match_dispose(GBytesMatch *); /* Procède à la libération totale de la mémoire. */ static void g_bytes_match_finalize(GBytesMatch *); /* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */ /* Affiche une correspondance sur la sortie standard. */ static void g_bytes_match_display(const GBytesMatch *); /* ---------------------------------------------------------------------------------- */ /* CORRESPONDANCE AVEC UNE SUITE D'OCTETS */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour un correspondance de chaîne identifiée. */ G_DEFINE_TYPE(GBytesMatch, g_bytes_match, G_TYPE_SCAN_MATCH); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des correspondances de chaînes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_bytes_match_class_init(GBytesMatchClass *klass) { GObjectClass *object; /* Autre version de la classe */ GScanMatchClass *match; /* Version parente de la classe*/ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_bytes_match_dispose; object->finalize = (GObjectFinalizeFunc)g_bytes_match_finalize; match = G_SCAN_MATCH_CLASS(klass); match->display = (display_scan_match_fc)g_bytes_match_display; } /****************************************************************************** * * * Paramètres : match = instance à initialiser. * * * * Description : Initialise une instance de correspondance de chaîne trouvée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_bytes_match_init(GBytesMatch *match) { match->content = NULL; match->start = VMPA_NO_PHYSICAL; match->len = VMPA_NO_PHYSICAL; } /****************************************************************************** * * * Paramètres : match = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_bytes_match_dispose(GBytesMatch *match) { g_clear_object(&match->content); G_OBJECT_CLASS(g_bytes_match_parent_class)->dispose(G_OBJECT(match)); } /****************************************************************************** * * * Paramètres : match = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_bytes_match_finalize(GBytesMatch *match) { G_OBJECT_CLASS(g_bytes_match_parent_class)->finalize(G_OBJECT(match)); } /****************************************************************************** * * * Paramètres : source = lien vers le motif recherché d'origine. * * content = contenu binaire présentant un motif reconnu. * * start = position de départ d'un motif détecté. * * len = taille du motif repéré. * * * * Description : Prend note d'une correspondance trouvée avec un motif. * * * * Retour : Correspondance mise en place. * * * * Remarques : - * * * ******************************************************************************/ GScanMatch *g_bytes_match_new(GSearchPattern *source, GBinContent *content, phys_t start, phys_t len) { GScanMatch *result; /* Structure à retourner */ result = g_object_new(G_TYPE_BYTES_MATCH, NULL); if (!g_bytes_match_create(G_BYTES_MATCH(result), source, content, start, len)) g_clear_object(&result); return result; } /****************************************************************************** * * * Paramètres : match = instance à initialiser pleinement. * * source = lien vers le motif recherché d'origine. * * content = contenu binaire présentant un motif reconnu. * * start = position de départ d'un motif détecté. * * len = taille du motif repéré. * * * * Description : Met en place une correspondance trouvée avec un motif. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool g_bytes_match_create(GBytesMatch *match, GSearchPattern *source, GBinContent *content, phys_t start, phys_t len) { bool result; /* Bilan à retourner */ GScanMatch *base; /* Lien vers les infos de base */ result = true; base = G_SCAN_MATCH(match); base->source = source; g_object_ref(G_OBJECT(source)); match->content = content; g_object_ref(G_OBJECT(content)); match->start = start; match->len = len; return result; } /* ---------------------------------------------------------------------------------- */ /* IMPLEMENTATION DES FONCTIONS DE CLASSE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : match = définition de correspondance à manipuler. * * * * Description : Affiche une correspondance sur la sortie standard. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_bytes_match_display(const GBytesMatch *match) { GScanMatch *base; /* Lien vers les infos de base */ const char *name; /* Désignation du motif ciblé */ vmpa2t pos; /* Tête de lecture */ const bin_t *data; /* Accès aux données brutes */ phys_t i; /* Boucle de parcours */ /* Affichage d'un repère */ base = G_SCAN_MATCH(match); name = g_search_pattern_get_name(base->source); /** * Les fonctionnalités Yara d'origine autorisent les variables anonymes '$'. * * Cette absence de nom est supportée ici. */ if (name == NULL) name = ""; printf("0x%llx:$%s: ", (unsigned long long)match->start, name); /* Affichage du contenu */ init_vmpa(&pos, match->start, VMPA_NO_VIRTUAL); data = g_binary_content_get_raw_access(match->content, &pos, match->len); for (i = 0; i < match->len; i++) { if (isprint(data[i])) printf("%c", data[i]); else printf("\\x%02hhx", data[i]); } printf("\n"); }