TL;DR
LLMalMorph is a semi-automated framework that leverages off-the-shelf Large Language Models (LLMs) to mutate malware source code into functional, evasive variants without requiring resource-intensive fine-tuning. By combining function-level Abstract Syntax Tree (AST) extraction with constraint-driven prompt engineering, it successfully bypasses LLM safety filters and generates variants that significantly reduce detection rates across commercial antivirus engines and machine learning classifiers.
What Problem Does This Paper Address?
Automated malware variant generation is a constant cat-and-mouse game. Traditionally, defenders rely on signature-based or machine learning (ML) detectors to catch malicious binaries. Attackers counter this by mutating malware to alter its binary signature while preserving its original malicious behavior.
However, existing mutation techniques have notable gaps: most operate directly on compiled binaries (which is complex and limits structural diversity) or rely on heavy, iterative search algorithms. While LLMs excel at code generation, using them to mutate malware source code introduces unique challenges:
- Context Window Overload: Entire malware codebases are too large for an LLM to process at once, leading to incomplete or broken code edits.
- Safety Guardrails: Modern LLMs are aligned to refuse requests that look like malware generation.
- Compilation and Semantic Failures: LLMs are prone to "hallucinating" non-existent APIs or altering critical logic, rendering the mutated malware non-functional.
LLMalMorph addresses these challenges by demonstrating how a pre-trained LLM can safely and effectively mutate malware at the source-code level with minimal human intervention.
Methods
LLMalMorph splits the mutation process into two main modules:
- Function Mutator: Instead of feeding the entire codebase to the LLM, LLMalMorph uses a Tree-sitter parser to extract individual function bodies, headers, and global declarations. It then wraps each function in a carefully engineered, constraint-driven prompt. To bypass safety filters, the prompts use neutral phrasing and abstract the malicious intent of the code.
- Variant Synthesizer: This module takes the mutated functions generated by the LLM, merges them back into the original codebase, and attempts to compile the project. It incorporates a minimal "human-in-the-loop" debugging process to quickly fix minor syntax errors or missing placeholders left by the LLM.
The framework guides the LLM (specifically Codestral-22B) using six distinct source-code transformation strategies:
- Code Optimization: Removing redundancies and simplifying logic.
- Code Quality and Reliability: Enhancing error and edge-case handling.
- Code Reusability: Splitting monolithic functions into smaller modular blocks.
- Code Security: Replacing standard cryptographic libraries (like OpenSSL) with alternative implementations.
- Code Obfuscation: Renaming variables/functions to meaningless strings and inserting dead control flows.
- Windows API Transformation: Replacing direct Windows API calls with indirect equivalents or wrappers.
Datasets
The authors evaluated LLMalMorph using 10 diverse, real-world Windows malware samples (primarily 32-bit C/C++) collected from public repositories. The dataset spans various malware categories:
- Infectors/Viruses: Exeinfector
- Generic Crimeware: Fungus
- POS Trojans: Dexter
- Botnets: HiddenVNC
- Information Stealers: Predator, Prosto-Stealer
- Ransomware: Conti (Cryptor), Babuk (Cryptor), RedPetya, RansomWar
Using the six transformation strategies, the framework generated a total of 618 unique malware variants.
Key Insights and Ideas
- Function-Level Isolation is Crucial: Processing code at the function level keeps the context window small and manageable, which drastically improves the LLM's code generation quality and prevents project-wide dependency breaks.
- Semantic Neutrality Bypasses Alignment: LLM safety filters trigger on explicit malware patterns. By presenting the code under the guise of benign refactoring or optimization tasks, the framework consistently circumvents refusal triggers.
- Optimization is an Unexpected Evasion Tool: Surprisingly, the Code Optimization strategy was one of the most effective at evading antivirus detection. By restructuring data flows and introducing language-specific features, it alters the compiled binary's structural "blueprint" enough to blind signature-based detectors.
- High Payoff for Low Manual Effort: The human-in-the-loop debugging phase required less than two minutes per variant on average, yet over 85% of that minimal effort resulted in successfully compiled, evasive variants.
Key Results
The generated variants were tested against commercial antivirus aggregators (VirusTotal and Hybrid Analysis) and graph-based ML malware classifiers (Malgraph):
- AV Evasion: Out of 618 variants, 72.6% showed a reduced detection rate. For complex samples, LLMalMorph typically reduced VirusTotal detection rates by 10–15%. For simpler samples (like Exeinfector), detection dropped by up to 31%.
- ML Classifier Evasion: Without explicitly optimizing against machine learning models, LLMalMorph variants achieved an Attack Success Rate (ASR) of up to 89% (via Optimization) and 91% (via Security/Crypto replacement) against the Malgraph classifier.
- Functionality Preservation: Using a normalized Longest Common Subsequence (LCS) metric on API call sequences, the authors verified that over 66% of the evasive variants fully preserved their original malicious functionality across four representative samples (with RedPetya exceeding 85%).
Limitations and Future Work
While LLMalMorph proves the feasibility of LLM-driven source mutation, it has a few limitations:
- Language and Architecture Constraints: The current implementation is strictly focused on 32-bit Windows malware written in C/C++.
- Sequential Function Selection: The framework mutates functions sequentially rather than intelligently targeting the specific functions responsible for malicious logic.
- Human-in-the-Loop Dependency: It still relies on a human developer to resolve minor compilation errors caused by LLM hallucinations.
Future Directions: The authors plan to extend the framework to support other programming languages, automate the function selection process by isolating malware-relevant patterns, and integrate binary-level transformations to completely eliminate the need for manual debugging.
Summary updated