diff --git a/scripts/test_dechiffrement_missions.py b/scripts/test_dechiffrement_missions.py new file mode 100644 index 0000000..828146d --- /dev/null +++ b/scripts/test_dechiffrement_missions.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python3 +""" +Test de déchiffrement pour chaque mission avec l'analyzer correspondant. +- Utilise keys/wordlist.txt pour générer les clés candidates +- Tente le déchiffrement et valide le texte avec utils.verifier_texte_dechiffre +- Affiche un récapitulatif des succès/échecs +""" + +import sys +from pathlib import Path +from typing import Dict, Tuple, Type + +# Assurer l'import du projet +sys.path.append('.') + +from src.analyzers.aes_cbc_analyzer import Aes_Cbc_Analyzer +from src.analyzers.chacha20_analyzer import ChaCha20_Analyzer +from src.analyzers.blowfish_analyzer import Blowfish_Analyzer +from src.analyzers.aes_gcm_analyzer import Aes_Gcm_Analyzer +from src.analyzers.fernet_analyzer import FernetAnalyzer +from src.utils import verifier_texte_dechiffre + +# Mapping missions -> (fichier, analyzer class) +MISSIONS: Dict[str, Tuple[str, Type]] = { + 'AES-CBC': ('data/mission1.enc', Aes_Cbc_Analyzer), + 'ChaCha20': ('data/mission2.enc', ChaCha20_Analyzer), + 'Blowfish': ('data/mission3.enc', Blowfish_Analyzer), + 'AES-GCM': ('data/mission4.enc', Aes_Gcm_Analyzer), + 'Fernet': ('data/mission5.enc', FernetAnalyzer), +} + +WORDLIST = 'keys/wordlist.txt' + + +def test_dechiffrement_missions() -> None: + print('=' * 70) + print('TEST DE DECHIFFREMENT PAR MISSION') + print('=' * 70) + + global_success = 0 + total = 0 + + # Vérification préliminaire + if not Path(WORDLIST).exists(): + print(f"❌ Wordlist manquante: {WORDLIST}") + return + + for algo, (mission_path, AnalyzerCls) in MISSIONS.items(): + total += 1 + print(f"\n🎯 Mission: {mission_path} | Analyzer attendu: {algo}") + if not Path(mission_path).exists(): + print(f" ❌ Fichier introuvable: {mission_path}") + continue + + analyzer = AnalyzerCls() + + # Génération des clés + try: + cles = analyzer.generer_cles_candidates(WORDLIST) + print(f" 🔑 Clés candidates générées: {len(cles)}") + except Exception as e: + print(f" 💥 Erreur génération clés: {e}") + cles = [] + + if not cles: + print(" ⚠️ Aucune clé candidate (le test peut échouer)") + + # Essais de déchiffrement + trouve = False + meilleure_stat = 0.0 + meilleure_cle = None + meilleur_texte = b'' + + for idx, cle in enumerate(cles): + try: + res = analyzer.dechiffrer(mission_path, cle) + except ValueError as ve: + # clés de taille invalide pour l'algo + continue + except FileNotFoundError: + print(f" 💥 Fichier introuvable pendant le test: {mission_path}") + break + except Exception: + # Toute autre erreur: considérer comme tentative échouée + continue + + if not res: + continue + + try: + texte = res.decode('utf-8') + except Exception: + texte = '' + + stats = verifier_texte_dechiffre(texte) + taux = float(stats.get('taux_succes', 0.0)) + if taux > meilleure_stat: + meilleure_stat = taux + meilleure_cle = cle + meilleur_texte = res + + # Seuil de succès raisonnable + if taux >= 60.0: + trouve = True + break + + if trouve: + global_success += 1 + print(f" ✅ Déchiffrement RÉUSSI | Taux succès: {meilleure_stat:.2f}%") + else: + # Note: AES-GCM n'a pas d'implémentation de déchiffrement -> probablement échec + hint = '' + if algo == 'AES-GCM': + hint = " (implémentation dechiffrer() absente)" + print(f" ❌ Déchiffrement ÉCHEC{hint} | Meilleur taux: {meilleure_stat:.2f}%") + + print('\n' + '=' * 70) + print(f"RÉSUMÉ: {global_success}/{total} missions déchiffrées") + if global_success == total: + print('🎉 Tous les déchiffrements ont réussi !') + else: + print('⚠️ Certains déchiffrements ont échoué. Voir détails ci-dessus.') + + +if __name__ == '__main__': + test_dechiffrement_missions() diff --git a/src/detecteur_crypto.py b/src/detecteur_crypto.py index 33777c1..27641cc 100644 --- a/src/detecteur_crypto.py +++ b/src/detecteur_crypto.py @@ -418,4 +418,4 @@ def attaque_dictionnaire(self,chemin_fichier_chiffrer: str, algo : str, chemin_d return "Aucune clé trouvé" -#print(DetecteurCryptoOrchestrateur().attaque_dictionnaire("mission1.enc","AES-CBC-256"))# \ No newline at end of file +#print(DetecteurCryptoOrchestrateur().attaque_dictionnaire("mission1.enc","AES-CBC-256"))# diff --git a/src/interface_console.py b/src/interface_console.py index b096fb5..097f230 100644 --- a/src/interface_console.py +++ b/src/interface_console.py @@ -92,16 +92,18 @@ def menu_1(self): task=progress.add_task(f"Analyse du {fichier}", total=100) error = False data = DetecteurCryptoOrchestrateur().analyser_fichier_specifique(fichier, progress, task, error, 1) - if data.algo : - print(f"\n[bold]Algorithme détecté[/bold] : [yellow]{data.algo}[/yellow]") - print(f"[bold]Score de probabilité[/bold] : [green]{data.score_probabilite}[/green]") - message = "[bold green] Analyse terminée. ✅[/bold green]" if not error else "[bold red] Mission terminée: Analyse non concluante. ❌ [/bold red]\n" - self.console.print(message) - else : - self.console.print("[bold yellow] Analyse terminée: Aucun algorithme détecté. ⚠️[/bold yellow]") - progress.remove_task(task) - - print(f"[bold]Temps d'éxécution[/bold] : [green]{round(data.temps_execution,4)}[/green] s") + for item in data : + if item.algo : + print(f"\n[bold]Algorithme potencielle détecté[/bold] : [yellow]{item.algo}[/yellow]") + print(f"[bold]Score de probabilité[/bold] : [green]{item.score_probabilite}[/green]") + else : + continue + + progress.update(task, description="Analyse terminé et affichage des résultats✅", advance=100) + time.sleep(2) + + print(f"[bold]Temps d'éxécution[/bold] : [green]{round(data[0].temps_execution,4)}[/green] s\n") + esc=input("Veuillez appuyer sur la touche entrer pour retourner au menu principal") if esc=="": self.default_menu()