/* Chrysalide - Outil d'analyse de fichiers binaires * export.c - assistant d'exportation de contenu binaire * * Copyright (C) 2011-2014 Cyrille Bagard * * This file is part of Chrysalide. * * OpenIDA 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. * * OpenIDA 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 "shellcode.h" #include #include #include #include #include #include "../gtkext/easygtk.h" #include "../analysis/roptions.h" // A garder ? /* ------------------------ PARTIE PRINCIPALE DE L'ASSISTANT ------------------------ */ /* Ferme l'assistant sans dérouler la procédure. */ static void export_assistant_cancel(GtkAssistant *, gpointer); /* Ferme l'assistant et déroule la procédure. */ static void export_assistant_close(GtkAssistant *, GObject *); /* -------------------------- RECUPERATION DU CODE BINAIRE -------------------------- */ /* Ajoute le panneau de consitution du code binaire. */ static void register_bincode_panel(GtkAssistant *); /* Fournit si possible le code binaire utilisateur. */ static bool extract_shell_code(GObject *, bin_t **, off_t *); /* Réagit à une modification du code binaire. */ static void on_bincode_changed(GtkTextBuffer *, GObject *); /* ----------------------- DEFINITION DE L'ARCHITECTURE VISEE ----------------------- */ /* Ajoute le panneau de spécification de l'architecture. */ static void register_archi_panel(GtkAssistant *); /* -------------------- DEFINITION DES REGLAGES DE L'EXPORTATION -------------------- */ /* Ajoute le panneau de choix du type de sortie. */ static void register_output_panel(GtkAssistant *); /* Réagit un changement du nom de fichier pour l'exportation. */ static void on_export_filename_changed(GtkEntry *, GtkAssistant *); /* Sélectionne ou non un nouveau fichier de sortie. */ static void on_filename_browsing_clicked(GtkButton *, GObject *); /* ------------------------- SELECTION DU CONTENU A TRAITER ------------------------- */ /* Ajoute le panneau de sélection du contenu à exporter. */ static void register_content_panel(GtkAssistant *); /* ---------------------------------------------------------------------------------- */ /* PARTIE PRINCIPALE DE L'ASSISTANT */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : project = projet courant à compléter. * * parent = fenêtre principale de l'éditeur. * * * * Description : Crée et affiche un assistant d'ajout de binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void run_shellcode_assistant(GStudyProject *project, GtkWindow *parent) { GtkWidget *assistant; /* Fenêtre à afficher */ GObject *ref; /* Espace de référencement */ assistant = gtk_assistant_new(); gtk_widget_set_size_request(assistant, 450, 300); gtk_window_set_position(GTK_WINDOW(assistant), GTK_WIN_POS_CENTER); gtk_window_set_title(GTK_WINDOW(assistant), _("Shellcode assistant")); gtk_window_set_modal(GTK_WINDOW(assistant), TRUE); gtk_window_set_transient_for(GTK_WINDOW(assistant), parent); ref = G_OBJECT(assistant); //g_object_set_data(ref, "binary", binary); register_bincode_panel(GTK_ASSISTANT(assistant)); register_archi_panel(GTK_ASSISTANT(assistant)); //register_output_panel(GTK_ASSISTANT(assistant)); //register_content_panel(GTK_ASSISTANT(assistant)); g_signal_connect(G_OBJECT(assistant), "cancel", G_CALLBACK(export_assistant_cancel), NULL); g_signal_connect(G_OBJECT(assistant), "close", G_CALLBACK(export_assistant_close), ref); gtk_widget_show_all(assistant); } /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * data = adresse non utilisée ici. * * * * Description : Ferme l'assistant sans dérouler la procédure. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void export_assistant_cancel(GtkAssistant *assistant, gpointer data) { gtk_widget_destroy(GTK_WIDGET(assistant)); } /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * ref = adresse de l'espace de référencement global. * * * * Description : Ferme l'assistant et déroule la procédure. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void export_assistant_close(GtkAssistant *assistant, GObject *ref) { GLoadedBinary *binary; /* Binaire chargé à parcourir */ GExeFormat *format; /* Format du binaire */ GRenderingOptions *options; /* Options d'exportation */ GtkToggleButton *checkbutton; /* Coche à retrouver */ gboolean state; /* Valeur à prendre en compte */ GtkEntry *entry; /* Zone de saisie */ const gchar *filename; /* Chemin d'accès du fichier */ //binary = G_LOADED_BINARY(g_object_get_data(ref, "binary")); format = g_loaded_binary_get_format(binary); options = g_rendering_options_new(format); /* Eléments à afficher */ checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "virtual_addr")); state = gtk_toggle_button_get_active(checkbutton); g_rendering_options_show_address(options, MRD_BLOCK, state); checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "binary_code")); state = gtk_toggle_button_get_active(checkbutton); g_rendering_options_show_code(options, MRD_BLOCK, state); checkbutton = GTK_TOGGLE_BUTTON(g_object_get_data(ref, "assembly_code")); state = gtk_toggle_button_get_active(checkbutton); /* Programmation de la tâche */ /* entry = GTK_ENTRY(g_object_get_data(ref, "filename")); filename = gtk_entry_get_text(entry); export = g_delayed_export_new(filename, binary, options); queue = get_work_queue(); g_work_queue_schedule_work(queue, G_DELAYED_WORK(export)); */ gtk_widget_destroy(GTK_WIDGET(assistant)); } /* ---------------------------------------------------------------------------------- */ /* RECUPERATION DU CODE BINAIRE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * * * Description : Ajoute le panneau de consitution du code binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void register_bincode_panel(GtkAssistant *assistant) { GtkWidget *vbox; /* Support principal */ GtkWidget *scrolledwindow; /* Support avec défilement */ GtkWidget *textview; /* Zone de texte */ GtkWidget *label; /* Etiquette du bilan */ vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); qck_set_margins(vbox, 8, 8, 8, 8); gtk_widget_show(vbox); /* Réception du code */ label = qck_create_label(NULL, NULL, _("Paste here the raw C code containing the shellcode.\r\n" "All content between quotes will be extracted.")); gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); scrolledwindow = gtk_scrolled_window_new(NULL, NULL); gtk_widget_show(scrolledwindow); gtk_box_pack_start(GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_IN); textview = qck_create_textview(G_OBJECT(assistant), "bincode", G_CALLBACK(on_bincode_changed), assistant); gtk_container_add(GTK_CONTAINER(scrolledwindow), textview); /* Validation */ label = qck_create_label(G_OBJECT(assistant), "status", _("No code")); gtk_widget_set_halign(label, GTK_ALIGN_CENTER); gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); /* Intégration */ gtk_assistant_append_page(assistant, vbox); gtk_assistant_set_page_title(assistant, vbox, _("Code")); gtk_assistant_set_page_type(assistant, vbox, GTK_ASSISTANT_PAGE_INTRO); gtk_assistant_set_page_complete(assistant, vbox, TRUE); } /****************************************************************************** * * * Paramètres : ref = espace de référencement global. * * code = code binaire recomposé. [OUT] * * length = taille de ce code. [OUT] * * * * Description : Fournit si possible le code binaire utilisateur. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool extract_shell_code(GObject *ref, bin_t **code, off_t *length) { bool result; /* Bilan à retourner */ GtkTextView *textview; /* Zone de texte */ GtkTextBuffer *buffer; /* Tampon gérant le texte */ GtkTextIter start; /* Début de la zone à traiter */ GtkTextIter end; /* Fin de la zone à traiter */ gchar *bincode; /* Code à filtrer */ regex_t preg; /* Recherche d'un bloc */ int ret; /* Bilan d'un appel */ regoff_t pos; /* Point de départ de lecture */ regmatch_t pmatch[3]; /* Localisation de trouvailles */ regoff_t i; /* Boucle de parcours */ char hex[3]; /* Valeur hexadécimale */ result = true; *code = NULL; *length = 0; /* Détermination des cibles */ ret = regcomp(&preg, "[^'\"]*(['\"])([^'\"]*)\\1", REG_EXTENDED); if (ret != 0) return false; /* Constitution du contexte */ textview = GTK_TEXT_VIEW(g_object_get_data(ref, "bincode")); buffer = gtk_text_view_get_buffer(textview); gtk_text_buffer_get_bounds(buffer, &start, &end); bincode = gtk_text_buffer_get_text(buffer, &start, &end, FALSE); /* Recomposition du code */ pos = 0; ret = regexec(&preg, &bincode[pos], 3, pmatch, 0); while (ret != REG_NOMATCH && result) { bincode[pos + pmatch[2].rm_eo] = '\0'; for (i = pos + pmatch[2].rm_so; i < (pos + pmatch[2].rm_eo) && result; i++) { *code = (bin_t *)realloc(*code, ++(*length) * sizeof(bin_t)); switch (bincode[i]) { case '\\': if (++i >= (pos + pmatch[2].rm_eo)) { result = false; break; } if (bincode[i] == 'x') { if (++i >= (pos + pmatch[2].rm_eo)) { result = false; break; } if ((i + 2) > (pos + pmatch[2].rm_eo)) { result = false; break; } memcpy(hex, &bincode[i], 2); hex[2] = '\0'; (*code)[*length - 1] = strtol(hex, NULL, 16); i++; } else (*code)[*length - 1] = bincode[i]; break; default: (*code)[*length - 1] = bincode[i]; break; } } if (result) { pos += pmatch[2].rm_eo + 1; ret = regexec(&preg, &bincode[pos], 3, pmatch, 0); } } g_free(bincode); if (!result && *code != NULL) { *length = 0; free(*code); } return result; } /****************************************************************************** * * * Paramètres : textbuffer = composant concerné par l'action. * * ref = espace de référencement global. * * * * Description : Réagit à une modification du code binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_bincode_changed(GtkTextBuffer *textbuffer, GObject *ref) { bin_t *code; /* Code binaire extrait */ off_t length; /* Taille des données lues */ bool status; /* Bilan de l'extraction */ GtkLabel *label; /* Etiquette à mettre à jour */ char *markup; /* Affichage étendu */ GtkAssistant *assistant; /* Assistant en première ligne */ gint index; /* Indice de la page courante */ GtkWidget *page; /* Cotenu de la page courante */ status = extract_shell_code(ref, &code, &length); label = GTK_LABEL(g_object_get_data(ref, "status")); if (!status) gtk_label_set_markup(label, _("Error while decoding")); else if (length > 0) { markup = g_markup_printf_escaped(_("Decoding OK (" OFF_FMT " %s)"), OFF_CAST(length), length > 1 ? _("bytes") : _("byte")); gtk_label_set_markup(label, markup); g_free(markup); } else { gtk_label_set_markup(label, _("No code")); status = false; } if (length > 0) free(code); /* Poursuite des opérations ? */ assistant = GTK_ASSISTANT(ref); index = gtk_assistant_get_current_page(assistant); page = gtk_assistant_get_nth_page(assistant, index); gtk_assistant_set_page_complete(assistant, page, status); } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DE L'ARCHITECTURE VISEE */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * * * Description : Ajoute le panneau de spécification de l'architecture. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void register_archi_panel(GtkAssistant *assistant) { GtkWidget *vbox; /* Support principal */ GtkWidget *label; /* Etiquette d'indication */ GtkWidget *combobox; /* Sélection du format */ vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); qck_set_margins(vbox, 8, 8, 8, 8); gtk_widget_show(vbox); /* Choix de l'architecture */ label = qck_create_label(NULL, NULL, _("Architecture:")); gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); combobox = qck_create_combobox(G_OBJECT(assistant), "archi", G_CALLBACK(NULL), NULL); gtk_box_pack_start(GTK_BOX(vbox), combobox, TRUE, FALSE, 0); gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), _("x86")); gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0); /* Taille de registre */ label = qck_create_label(NULL, NULL, _("Register size:")); gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0); combobox = qck_create_combobox(G_OBJECT(assistant), "rsize", G_CALLBACK(NULL), NULL); gtk_box_pack_start(GTK_BOX(vbox), combobox, TRUE, FALSE, 0); gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), _("32 bits")); gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), _("16 bits")); gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0); /* Intégration */ gtk_assistant_append_page(assistant, vbox); gtk_assistant_set_page_title(assistant, vbox, _("Architecture")); gtk_assistant_set_page_type(assistant, vbox, GTK_ASSISTANT_PAGE_CONFIRM); gtk_assistant_set_page_complete(assistant, vbox, TRUE); } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DES REGLAGES DE L'EXPORTATION */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * * * Description : Ajoute le panneau de choix du type de sortie. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void register_output_panel(GtkAssistant *assistant) { GtkWidget *vbox; /* Support principal #1 */ GtkWidget *hbox; /* Support principal #2 */ GtkWidget *label; /* Etiquette d'indication */ GtkWidget *combobox; /* Sélection du format */ GtkWidget *entry; /* Zone de saisie de texte */ GtkWidget *button; /* Sélection de fichier */ GLoadedBinary *binary; /* Binaire chargé à parcourir */ const char *filename; /* Chemin d'accès par défaut */ vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); qck_set_margins(vbox, 8, 8, 8, 8); gtk_widget_show(vbox); /* Format de sortie */ hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); gtk_widget_show(hbox); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); label = qck_create_label(NULL, NULL, _("Format : ")); gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); combobox = qck_create_combobox(NULL, NULL, G_CALLBACK(NULL), NULL); gtk_box_pack_start(GTK_BOX(hbox), combobox, TRUE, TRUE, 0); gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox), _("Simple text")); gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0); /* Fichier de sortie */ hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); gtk_widget_show(hbox); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); label = qck_create_label(NULL, NULL, _("File : ")); gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); entry = qck_create_entry(G_OBJECT(assistant), "filename", NULL); gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0); button = qck_create_button(NULL, NULL, "...", G_CALLBACK(on_filename_browsing_clicked), assistant); gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); /* Intégration */ gtk_assistant_append_page(assistant, vbox); gtk_assistant_set_page_title(assistant, vbox, _("Output")); gtk_assistant_set_page_type(assistant, vbox, GTK_ASSISTANT_PAGE_CONTENT); gtk_assistant_set_page_complete(assistant, vbox, TRUE); /* Choix par défaut */ /* binary = G_LOADED_BINARY(g_object_get_data(G_OBJECT(assistant), "binary")); filename = g_loaded_binary_get_name(binary); gtk_entry_set_text(GTK_ENTRY(entry), filename); gtk_entry_append_text(GTK_ENTRY(entry), ".txt"); g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(on_export_filename_changed), assistant); */ } /****************************************************************************** * * * Paramètres : entry = zone de texte dont le contenu vient de changer. * * assistant = fenêtre affichée et référencement global. * * * * Description : Réagit un changement du nom de fichier pour l'exportation. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_export_filename_changed(GtkEntry *entry, GtkAssistant *assistant) { const gchar *text; /* Texte saisi dans la zone */ gint num; /* Etape courante */ GtkWidget *page; /* Support de cette étape */ text = gtk_entry_get_text(entry); num = gtk_assistant_get_current_page(assistant); page = gtk_assistant_get_nth_page(assistant, num); gtk_assistant_set_page_complete(assistant, page, (strlen(text) > 0)); } /****************************************************************************** * * * Paramètres : button = bouton d'édition de la sélection. * * ref = espace de référencement principal. * * * * Description : Sélectionne ou non un nouveau fichier de sortie. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_filename_browsing_clicked(GtkButton *button, GObject *ref) { GtkWidget *dialog; /* Boîte à afficher */ gchar *filename; /* Nom du fichier à intégrer */ GtkEntry *entry; /* Zone de saisie à maj. */ dialog = gtk_file_chooser_dialog_new(_("Choose an output filename"), GTK_WINDOW(ref), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Cancel"), GTK_RESPONSE_CANCEL, _("_Save"), GTK_RESPONSE_ACCEPT, NULL); entry = GTK_ENTRY(g_object_get_data(ref, "filename")); gtk_file_chooser_set_filename(dialog, gtk_entry_get_text(entry)); if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); gtk_entry_set_text(GTK_ENTRY(entry), filename); g_free(filename); } gtk_widget_destroy(dialog); } /* ---------------------------------------------------------------------------------- */ /* SELECTION DU CONTENU A TRAITER */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : assistant = fenêtre à compléter et référencement global. * * * * Description : Ajoute le panneau de sélection du contenu à exporter. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void register_content_panel(GtkAssistant *assistant) { GtkWidget *vbox; /* Support principal */ GtkWidget *frame; /* Support avec encadrement */ GtkWidget *subvbox; /* Support secondaire */ GtkWidget *checkbutton; /* Coche pour une option */ vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); qck_set_margins(vbox, 8, 8, 8, 8); gtk_widget_show(vbox); /* Lignes à traiter */ subvbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); gtk_widget_show(subvbox); frame = qck_create_frame(_("Lines to process"), subvbox, 0, 0, 12, 0); gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0); checkbutton = qck_create_check_button(G_OBJECT(assistant), "prologue", _("Prologue"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); checkbutton = qck_create_check_button(G_OBJECT(assistant), "code", _("Code"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); checkbutton = qck_create_check_button(G_OBJECT(assistant), "comments", _("Comments"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); /* Eléments à afficher */ subvbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); gtk_widget_show(subvbox); frame = qck_create_frame(_("Items to display"), subvbox, 0, 0, 12, 0); gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0); checkbutton = qck_create_check_button(G_OBJECT(assistant), "virtual_addr", _("Virtual address"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); checkbutton = qck_create_check_button(G_OBJECT(assistant), "binary_code", _("Binary code"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); checkbutton = qck_create_check_button(G_OBJECT(assistant), "assembly_code", _("Assembly code"), NULL, NULL); gtk_box_pack_start(GTK_BOX(subvbox), checkbutton, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton), TRUE); /* Intégration */ gtk_assistant_append_page(assistant, vbox); gtk_assistant_set_page_title(assistant, vbox, _("Exported content")); gtk_assistant_set_page_type(assistant, vbox, GTK_ASSISTANT_PAGE_CONFIRM); gtk_assistant_set_page_complete(assistant, vbox, TRUE); } /* Choix par défaut */ /* binary = G_LOADED_BINARY(g_object_get_data(G_OBJECT(assistant), "binary")); filename = g_loaded_binary_get_name(binary); gtk_entry_set_text(GTK_ENTRY(entry), filename); gtk_entry_append_text(GTK_ENTRY(entry), ".txt"); g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(on_export_filename_changed), assistant); */