Conceptcore
Metamorphic malware
Also called polymorphic malware, code mutation engine.
Malware that rewrites its own code into a different but equivalent form in each copy, so no fixed byte pattern matches all copies. Unlike polymorphic code, it keeps no constant body.
Metamorphic malware is malware whose copies differ in their code but not in what they do. Each time it spreads, a mutation engine inside it rewrites its own instructions into a new form with the same behavior, for example by renaming registers, by replacing an instruction with an equivalent sequence, by inserting instructions that have no effect, or by reordering code and joining the pieces with jumps. No byte string is common to all copies, so a detector based on fixed byte signatures, as in misuse detection, fails on it. This makes it different from polymorphic code, which keeps one constant body and only encrypts it with a new key and a new decryption routine in each copy. The page describes the class in general terms and the detectors that answer it. It gives no mutation method, and its examples use a made-up toy language that computes a harmless sum.
In plain words
A signature scanner looks for a known sequence of bytes, like looking for one exact sentence in a book. A simple way to escape it is to say the same thing with other words in each copy: "the cat sat on the mat", "on the mat sat the cat", "the cat, which was tired, sat on the mat". The meaning is the same, and no exact sentence repeats.
Metamorphic code does this with instructions. Each copy is written in new words, so the exact-sentence search fails. A detector must instead understand the meaning: what values the code reads, how it changes them, and what it does with the result.
The idea
Four steps of code evolution. Szor (2005) describes how viruses moved away from fixed signatures in steps.
| class | what changes between copies | what stays the same | what a detector can use |
|---|---|---|---|
| encrypted | the key and the encrypted body | the decryption routine | a signature of the decryption routine |
| oligomorphic | the decryption routine, chosen from a small set | the body, once decrypted | signatures of each routine in the set |
| polymorphic | the decryption routine, generated anew in each copy | the body, once decrypted | run the code in an emulator until the body appears in memory, then match it |
| metamorphic | the body itself | only the behavior | analysis of the meaning of the code |
The last row is the important step. For the first three classes, the decrypted body is the same in every copy, so a detector that waits until the body is in memory, by emulation or by generic unpacking, can match it with a signature. A metamorphic body is never the same twice, so the problem moves from matching bytes to matching meaning.
The general limit. Cohen (1987) proved that no program can decide, for every program, whether it is a virus (see virus detection undecidability, an instance of Rice's theorem). Deciding whether two pieces of code have the same behavior is undecidable for the same reason. So every detector of metamorphic code is incomplete or has false alarms. The research question is which transformations a detector can undo, at what cost.
Semantic detectors. Christodorescu and Jha (2003) wrote the malicious pattern as a sequence of instructions with uninterpreted symbols in place of registers and constants, and matched it against the control flow graph of the program after a static analysis that marks instructions with no effect and follows jumps. This undoes register renaming, inserted dead code and reordering with jumps. Christodorescu, Jha, Seshia, Song and Bryant (2005) went one step further. A template states the behavior as a set of instructions with variables and symbolic constants, and the order in which values are defined and used. A program matches if some of its instructions can be mapped to the template so that the same values flow between them, whatever other instructions lie between. The check of these relations uses decision procedures for the meaning of instruction sequences. Their evaluation found the obfuscated variants that they tested, with no false alarms on the benign programs that they tested. These are measurements on their test set, not a guarantee.
Normal forms. A related defense rewrites code into a canonical form before matching: remove dead code with dead-code elimination, fold constants, replace known equivalent sequences by one form, and follow unconditional jumps. If the mutation engine uses only rewrites that the normal form undoes, all copies reach the same normal form. This works against a fixed set of rewrites, not against every equivalent program.
Examples
The examples use a made-up three-address language with variables, assignments, an output statement and jumps. The program reads and outputs . The five versions below have the same behavior, as one copy of a metamorphic program would differ from the next.
| version | code | the kind of rewrite |
|---|---|---|
| A | y := x + 2; out y |
the original |
| B | r := x + 2; out r |
a different variable name |
| C | t := x + 1; y := t + 1; out y |
one step replaced by two equivalent steps |
| D | y := x + 2; z := 7; out y |
an assignment to a variable that is never read |
| E | goto L2; L1: out y; halt; L2: y := x + 2; goto L1 |
the order changed, joined by jumps |
A detector that looks for the exact text of A finds only A. A detector that works on meaning can treat all five as one:
- Rename variables to symbols, and B becomes A.
- Delete assignments whose value is never read, and D becomes A.
- Follow unconditional jumps and put the code in the order in which it runs, and E becomes A.
- Combine the two additions, as constant folding does, and C becomes A.
The semantic template is "the output is the input plus the constant 2, with any instructions between them that do not change that flow of values". All five versions match it.
The contrast with polymorphic code in the same toy language: a constant body y := x + 2; out y is stored in an encoded form, and a short routine that differs between copies decodes it before it runs. The stored bytes differ in each copy, but the decoded body is the same, so a detector that lets the routine run in an emulator, then reads the decoded body, matches it with a fixed signature.
Where it is used
- Malware detection research. Metamorphic code is the standard case for detectors that work on meaning: semantic templates, normal forms, matching of control flow graphs, and detectors of behavior at run time. It is also the case that shows why the malware detection problem is stated with error rates and not as a decision procedure.
- Code similarity. Finding that two different binaries come from the same source is the problem of binary code similarity, and its methods for code built by different compilers apply to mutated code as well.
- Obfuscation in general. The rewrites of metamorphic engines are a subset of the obfuscating transformations that Collberg, Thomborson and Low classified. Opaque predicates, control-flow flattening and virtualization obfuscation are stronger ones, and their deobfuscation methods overlap with the detectors on this page.
- Software diversity. The same idea, a different but equivalent program in each copy, is used as a defense: if each installation has a different layout of code, an attack built for one copy does not work on another.
References
- Peter Szor, The Art of Computer Virus Research and Defense, Addison-Wesley, 2005, the chapters on code evolution, from encrypted and oligomorphic to polymorphic and metamorphic viruses, and on the detection methods for each
- Mihai Christodorescu and Somesh Jha, Static analysis of executables to detect malicious patterns, USENIX Security Symposium 2003, the detector that matches patterns with uninterpreted symbols against an annotated control flow graph, and its resistance to simple obfuscations
- Mihai Christodorescu, Somesh Jha, Sanjit A. Seshia, Dawn Song and Randal E. Bryant, Semantics-aware malware detection, IEEE Symposium on Security and Privacy 2005, pages 32 to 46, templates of malicious behavior with variables and symbolic constants, the matching algorithm based on def-use relations, and its evaluation on obfuscated variants
- Fred Cohen, Computer viruses: theory and experiments, Computers and Security 6, 1987, the proof that no program decides for every program whether it is a virus
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag G3Y5WJ Edit this page History Report an error Markdown