diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7509457 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +--- +IndentWidth: 4 +ColumnLimit: 80 diff --git a/Malware/Malware/Malware.cpp b/Malware/Malware/Malware.cpp index daeda04..1902add 100644 --- a/Malware/Malware/Malware.cpp +++ b/Malware/Malware/Malware.cpp @@ -1,12 +1,13 @@ #pragma clang diagnostic ignored "-Wwritable-strings" #include "stdafx.h" // IWYU pragma: keep -#include "functions.h" -#include "lonesha256.h" -#include "tables_poly.h" -#include "encryption.h" #include #include #include + +#include "encryption.h" +#include "functions.h" +#include "lonesha256.h" +#include "tables_poly.h" #ifdef _WIN32 #include #endif @@ -19,222 +20,239 @@ // Multiplication dans GF(256) : a * b mod 0x1B uint8_t gf_mul(uint8_t a, uint8_t b) { - uint8_t p = 0; - for (int i = 0; i < 8; i++) { - if (b & 1) - p ^= a; - uint8_t hi_bit = a & 0x80; - a <<= 1; - if (hi_bit) - a ^= 0x1B; - b >>= 1; - } - return p; + uint8_t p = 0; + for (int i = 0; i < 8; i++) { + if (b & 1) + p ^= a; + uint8_t hi_bit = a & 0x80; + a <<= 1; + if (hi_bit) + a ^= 0x1B; + b >>= 1; + } + return p; } // Évaluation d'un polynôme de degré 7 sur GF(256) uint8_t evaluate_polynomial(uint8_t x, const uint8_t coeffs[8]) { - uint8_t result = 0; - uint8_t x_pow = 1; - for (int j = 0; j < 8; j++) { - result ^= gf_mul(coeffs[j], x_pow); - x_pow = gf_mul(x_pow, x); - } - return result; + uint8_t result = 0; + uint8_t x_pow = 1; + for (int j = 0; j < 8; j++) { + result ^= gf_mul(coeffs[j], x_pow); + x_pow = gf_mul(x_pow, x); + } + return result; } typedef struct { - char *(*p1)(); - int (*p2)(char *decoded); + char *(*p1)(); + int (*p2)(char *decoded); } FuncList; char *this_is_useful_fr_dont_miss_it() { // it's not, pure red herring - char *useful = (char *)malloc(sizeof(char) * 100); - for (int i = 0; i < 99; i++) { - useful[i] ^= useful[i + 1] + 'c'; - } - return useful; + char *useful = (char *)malloc(sizeof(char) * 100); + for (int i = 0; i < 99; i++) { + useful[i] ^= useful[i + 1] + 'c'; + } + return useful; } int cmp_hash(char *decoded) { - unsigned char hash[32] = {0xf4, 0xed, 0x2a, 0x38, 0xd2, 0xff, 0xcc, 0x38, - 0xbc, 0x63, 0x28, 0x46, 0xaf, 0xe2, 0x4f, 0x34, - 0x2d, 0xd8, 0xb8, 0x5e, 0x74, 0xbd, 0x73, 0x99, - 0x2d, 0x91, 0x56, 0x24, 0xb4, 0x73, 0x5d, 0xee}; - unsigned char hash_computed[32]; - lonesha256(hash_computed, (unsigned char *)decoded, sizeof(char) * 57); - for (int i = 0; i < 32; i++) { - if (hash[i] != hash_computed[i]) { - return hash[i] - hash_computed[i]; + unsigned char hash[32] = {0xf4, 0xed, 0x2a, 0x38, 0xd2, 0xff, 0xcc, 0x38, + 0xbc, 0x63, 0x28, 0x46, 0xaf, 0xe2, 0x4f, 0x34, + 0x2d, 0xd8, 0xb8, 0x5e, 0x74, 0xbd, 0x73, 0x99, + 0x2d, 0x91, 0x56, 0x24, 0xb4, 0x73, 0x5d, 0xee}; + unsigned char hash_computed[32]; + lonesha256(hash_computed, (unsigned char *)decoded, sizeof(char) * 57); + for (int i = 0; i < 32; i++) { + if (hash[i] != hash_computed[i]) { + return hash[i] - hash_computed[i]; + } } - } - return 0; + return 0; } // Fake main int fakemain(int argc, wchar_t *argv[]) { - Obfuscated_stdFunclist *stdfunclist = new Obfuscated_stdFunclist(); + Obfuscated_stdFunclist *stdfunclist = new Obfuscated_stdFunclist(); - FuncList list = {this_is_useful_fr_dont_miss_it, cmp_hash}; - // char* encoded = "Salut a tous les amis, gg pour avoir dechiffre ce string"; - char *encoded = "\x64\x55\x56\x41\x43\x14\x56\x13\x46\x5b\x47\x40\x14\x5e\x52" - "\x47\x13\x56\x5e\x5d\x40\x1f\x13\x53\x54\x14\x42\x5b\x41\x40" - "\x13\x53\x47\x58\x5d\x46\x14\x53\x51\x54\x5b\x5b\x52\x54\x41" - "\x51\x12\x54\x51\x13\x44\x47\x46\x5a\x5d\x54"; - char *key = (char *)malloc(sizeof(char) * 9); - for (int i = 0; argv[1][i] != '\0'; ++i) { - key[i] = (char)argv[1][i] ^ this_is_useful_fr_dont_miss_it()[i] ^ - list.p1()[i]; // xors to argv[1][i] - } - key[8] = '\0'; - // printf("Key: %s\n", key); - encrypt_decrypt(key, encoded); + FuncList list = {this_is_useful_fr_dont_miss_it, cmp_hash}; + // char* encoded = "Salut a tous les amis, gg pour avoir dechiffre ce + // string"; + char *encoded = + "\x64\x55\x56\x41\x43\x14\x56\x13\x46\x5b\x47\x40\x14\x5e\x52" + "\x47\x13\x56\x5e\x5d\x40\x1f\x13\x53\x54\x14\x42\x5b\x41\x40" + "\x13\x53\x47\x58\x5d\x46\x14\x53\x51\x54\x5b\x5b\x52\x54\x41" + "\x51\x12\x54\x51\x13\x44\x47\x46\x5a\x5d\x54"; + char *key = (char *)malloc(sizeof(char) * 9); + for (int i = 0; argv[1][i] != '\0'; ++i) { + key[i] = (char)argv[1][i] ^ this_is_useful_fr_dont_miss_it()[i] ^ + list.p1()[i]; // xors to argv[1][i] + } + key[8] = '\0'; + // printf("Key: %s\n", key); + encrypt_decrypt(key, encoded); #ifdef _WIN32 - DWORD old; - VirtualProtect(&list.p1, 0x100, PAGE_EXECUTE_READWRITE, &old); + DWORD old; + VirtualProtect(&list.p1, 0x100, PAGE_EXECUTE_READWRITE, &old); #endif - if (!list.p2(encoded)) { // cmp_hash - stdfunclist->obfusc_printf("%s", encoded); - } - return 0; + if (!list.p2(encoded)) { // cmp_hash + stdfunclist->obfusc_printf("%s", encoded); + } + return 0; } /* ============================================================================== * MOTEUR D'OBFUSCATION BRANCHLESS (POINT-FUNCTION OBFUSCATION) * ============================================================================== */ +typedef struct { + uint8_t (*evaluate_polynomial)(uint8_t x, const uint8_t coeffs[8]); + void *(*memcpy)(void *__restrict __dest, const void *__restrict __src, + size_t __n); + int (*lonesha256)(unsigned char out[32], const unsigned char *in, + size_t len); +} FuncList2; + int main(int argc, char *argv[]) { - if (argc < 2 || strlen(argv[1]) > 8) { - printf("Arguments invalides.\n"); - return 1; - } + // Init des struct d'obfuscation d'appel de fonction + Obfuscated_stdFunclist *stdfunclist = new Obfuscated_stdFunclist(); + FuncList2 list = {evaluate_polynomial, memcpy, lonesha256}; - fakemain(argc, (wchar_t**) argv); - - uint8_t input[8]; - memcpy(input, argv[1], 8); - - /* -------------------------------------------------------------------------- - * 1. EXPANSION SPATIALE (FORWARD-COMPUTATION) - * Objectif : Projeter l'entrée (8 octets) sur un espace pseudo-aléatoire de - * 64 octets (512 bits) pour remplir parfaitement un bloc de compression - * SHA-256 sans ajout de bits de padding prévisibles. - * - * Équation de récurrence non-linéaire : - * S_{c, i+1} = P_{c, i}(S_{c, i} \oplus x_i) - * où: - * - c : Index de la chaîne d'évaluation parallèle (de 0 à 7). - * - i : Index du caractère de l'entrée en cours de traitement (de 0 à - * 7). - * - S_{c, i} : État interne de la chaîne 'c' à l'étape 'i'. - * - x_i : i-ème octet (caractère) de l'entrée fournie. - * - P_{c, i} : Polynôme de transition aléatoire sur GF(2^8) spécifique à - * cette étape. - * -------------------------------------------------------------------------- - */ - - uint8_t super_bloc[64]; - for (int c = 0; c < 8; c++) { - uint8_t state = INITIAL_STATES[c]; - for (int i = 0; i < 8; i++) { - // Mélange non-linéaire du caractère d'entrée avec l'état courant - state = evaluate_polynomial(state ^ input[i], POLY_COEFFS[c][i]); - // Capture de la trace pour former le bloc final - super_bloc[c * 8 + i] = state; + if (argc < 2 || strlen(argv[1]) > 8) { + printf("Arguments invalides.\n"); + return 1; } - } - /* -------------------------------------------------------------------------- - * 2. VÉRIFICATION D'INTÉGRITÉ (ORACLE ALÉATOIRE) - * Calcul de l'empreinte H1 = SHA256(super_bloc) - * -------------------------------------------------------------------------- - */ - unsigned char h1[32]; - lonesha256(h1, super_bloc, 64); + fakemain(argc, (wchar_t **)argv); - // Accumulation des erreurs bit-à-bit par rapport à la cible cryptographique - // Diff = \bigvee_{k=0}^{31} (H_1[k] ^ H_{cible}[k]) - uint32_t diff = 0; - for (int i = 0; i < 32; i++) { - diff |= (h1[i] ^ h_cible[i]); - } + uint8_t input[8]; + list.memcpy(input, argv[1], 8); - /* -------------------------------------------------------------------------- - * 3. FILTRE MATHÉMATIQUE "BRANCHLESS" (ZÉRO CONDITION) - * Transforme l'erreur accumulée en un masque binaire absolu. - * Formule : Mask = ( (Diff | (~Diff + 1)) >> 63 ) - 1 - * -------------------------------------------------------------------------- - */ + /* -------------------------------------------------------------------------- + * 1. EXPANSION SPATIALE (FORWARD-COMPUTATION) + * Objectif : Projeter l'entrée (8 octets) sur un espace pseudo-aléatoire de + * 64 octets (512 bits) pour remplir parfaitement un bloc de compression + * SHA-256 sans ajout de bits de padding prévisibles. + * + * Équation de récurrence non-linéaire : + * S_{c, i+1} = P_{c, i}(S_{c, i} \oplus x_i) + * où: + * - c : Index de la chaîne d'évaluation parallèle (de 0 à 7). + * - i : Index du caractère de l'entrée en cours de traitement (de 0 + * à 7). + * - S_{c, i} : État interne de la chaîne 'c' à l'étape 'i'. + * - x_i : i-ème octet (caractère) de l'entrée fournie. + * - P_{c, i} : Polynôme de transition aléatoire sur GF(2^8) spécifique à + * cette étape. + * -------------------------------------------------------------------------- + */ - uint64_t diff64 = diff; + uint8_t super_bloc[64]; + for (int c = 0; c < 8; c++) { + uint8_t state = INITIAL_STATES[c]; + for (int i = 0; i < 8; i++) { + // Mélange non-linéaire du caractère d'entrée avec l'état courant + state = + list.evaluate_polynomial(state ^ input[i], POLY_COEFFS[c][i]); + // Capture de la trace pour former le bloc final + super_bloc[c * 8 + i] = state; + } + } - // Si diff > 0 (mot de passe faux) -> is_wrong = 1 - // Si diff == 0 (mot de passe bon) -> is_wrong = 0 - uint64_t is_wrong = (diff64 | (~diff64 + 1)) >> 63; + /* -------------------------------------------------------------------------- + * 2. VÉRIFICATION D'INTÉGRITÉ (ORACLE ALÉATOIRE) + * Calcul de l'empreinte H1 = SHA256(super_bloc) + * -------------------------------------------------------------------------- + */ + unsigned char h1[32]; + list.lonesha256(h1, super_bloc, 64); - // Si is_wrong == 1 -> Mask = 0x0000000000000000 (Ferme la porte au payload) - // Si is_wrong == 0 -> Mask = 0xFFFFFFFFFFFFFFFF (Ouvre la porte au payload) - uint64_t mask = is_wrong - 1; + // Accumulation des erreurs bit-à-bit par rapport à la cible cryptographique + // Diff = \bigvee_{k=0}^{31} (H_1[k] ^ H_{cible}[k]) + uint32_t diff = 0; + for (int i = 0; i < 32; i++) { + diff |= (h1[i] ^ h_cible[i]); + } - /* -------------------------------------------------------------------------- - * 4. DÉRIVATION DE LA CLÉ DE LEURRE (COMPORTEMENT GOODWARE) - * K_G = SHA256(L)_{[0..7]} où L est une chaîne d'apparence inoffensive. - * Permet une indistinguabilité totale lors d'une analyse statique (strings). - * -------------------------------------------------------------------------- - */ - unsigned char leurre[] = "Microsoft_CRT_Initialization"; - unsigned char h_leurre[32]; - lonesha256(h_leurre, leurre, 28); // K_G correspond aux 8 premiers octets + /* -------------------------------------------------------------------------- + * 3. FILTRE MATHÉMATIQUE "BRANCHLESS" (ZÉRO CONDITION) + * Transforme l'erreur accumulée en un masque binaire absolu. + * Formule : Mask = ( (Diff | (~Diff + 1)) >> 63 ) - 1 + * -------------------------------------------------------------------------- + */ - /* -------------------------------------------------------------------------- - * 5. SÉPARATION DES DOMAINES (DOMAIN SEPARATION) - * Calcul de l'empreinte de dérivation H2. - * H_2 = SHA256(super_bloc \parallel \text{"DERIVATION"}) - * Garantit l'indépendance mathématique entre la vérification (H1) et le - * déchiffrement (H2). - * -------------------------------------------------------------------------- - */ + uint64_t diff64 = diff; - unsigned char buffer_h2[74]; // 64 octets (SB) + 10 octets (Sel) - memcpy(buffer_h2, super_bloc, 64); - memcpy(buffer_h2 + 64, "DERIVATION", 10); + // Si diff > 0 (mot de passe faux) -> is_wrong = 1 + // Si diff == 0 (mot de passe bon) -> is_wrong = 0 + uint64_t is_wrong = (diff64 | (~diff64 + 1)) >> 63; - unsigned char h2[32]; - lonesha256(h2, buffer_h2, 74); + // Si is_wrong == 1 -> Mask = 0x0000000000000000 (Ferme la porte au payload) + // Si is_wrong == 0 -> Mask = 0xFFFFFFFFFFFFFFFF (Ouvre la porte au payload) + uint64_t mask = is_wrong - 1; - /* -------------------------------------------------------------------------- - * 6. RÉSOLUTION ALGÉBRIQUE ET DÉCHIFFREMENT - * Formule maîtresse : K_{finale} = K_G ^ ( (E_\Delta ^ H_2) \ \& \ Mask ) - * - Si Mask == 0x00 : K_{finale} = K_G ^ 0 = K_G (Goodware) - * - Si Mask == 0xFF : K_{finale} = K_G ^ \Delta = K_G ^ (K_M ^ K_G) = K_M - * (Malware) - * -------------------------------------------------------------------------- - */ - unsigned char derived_key[8]; - for (int i = 0; i < 8; i++) { - // Tentative de déchiffrement du secret (\Delta) - uint8_t computed_delta = enc_delta[i] ^ h2[i]; + /* -------------------------------------------------------------------------- + * 4. DÉRIVATION DE LA CLÉ DE LEURRE (COMPORTEMENT GOODWARE) + * K_G = SHA256(L)_{[0..7]} où L est une chaîne d'apparence inoffensive. + * Permet une indistinguabilité totale lors d'une analyse statique + * (strings). + * -------------------------------------------------------------------------- + */ + unsigned char leurre[] = "Microsoft_CRT_Initialization"; + unsigned char h_leurre[32]; + list.lonesha256(h_leurre, leurre, + 28); // K_G correspond aux 8 premiers octets - // Application du masque d'annihilation (filtre AND) - uint8_t applied_delta = computed_delta & (mask & 0xFF); + /* -------------------------------------------------------------------------- + * 5. SÉPARATION DES DOMAINES (DOMAIN SEPARATION) + * Calcul de l'empreinte de dérivation H2. + * H_2 = SHA256(super_bloc \parallel \text{"DERIVATION"}) + * Garantit l'indépendance mathématique entre la vérification (H1) et le + * déchiffrement (H2). + * -------------------------------------------------------------------------- + */ - // Recombinaison finale de la clé - derived_key[i] = h_leurre[i] ^ applied_delta; + unsigned char buffer_h2[74]; // 64 octets (SB) + 10 octets (Sel) + list.memcpy(buffer_h2, super_bloc, 64); + list.memcpy(buffer_h2 + 64, "DERIVATION", 10); - // Déchiffrement immédiat in-place du payload - payload[i] ^= derived_key[i]; - } - payload[7] = '\0'; // Protection d'affichage C-String + unsigned char h2[32]; + list.lonesha256(h2, buffer_h2, 74); - /* -------------------------------------------------------------------------- - * 7. EXÉCUTION DU PAYLOAD DÉCHIFFRÉ - * -------------------------------------------------------------------------- - */ - printf((char *)payload, argv[1]); + /* -------------------------------------------------------------------------- + * 6. RÉSOLUTION ALGÉBRIQUE ET DÉCHIFFREMENT + * Formule maîtresse : K_{finale} = K_G ^ ( (E_\Delta ^ H_2) \ \& \ Mask ) + * - Si Mask == 0x00 : K_{finale} = K_G ^ 0 = K_G (Goodware) + * - Si Mask == 0xFF : K_{finale} = K_G ^ \Delta = K_G ^ (K_M ^ K_G) = K_M + * (Malware) + * -------------------------------------------------------------------------- + */ + unsigned char derived_key[8]; + for (int i = 0; i < 8; i++) { + // Tentative de déchiffrement du secret (\Delta) + uint8_t computed_delta = enc_delta[i] ^ h2[i]; - // Boucle infinie demandée pour suspendre le processus - while (1) { - } + // Application du masque d'annihilation (filtre AND) + uint8_t applied_delta = computed_delta & (mask & 0xFF); - return 0; + // Recombinaison finale de la clé + derived_key[i] = h_leurre[i] ^ applied_delta; + + // Déchiffrement immédiat in-place du payload + payload[i] ^= derived_key[i]; + } + payload[7] = '\0'; // Protection d'affichage C-String + + /* -------------------------------------------------------------------------- + * 7. EXÉCUTION DU PAYLOAD DÉCHIFFRÉ + * -------------------------------------------------------------------------- + */ + stdfunclist->obfusc_printf((char *)payload, argv[1]); + + // Boucle infinie demandée pour suspendre le processus + while (1) { + } + + return 0; } \ No newline at end of file