Title : Mystique - An x86 Metamorphic Compiler
Author : wK
|=-----------------------------------------------------------------------=|
|=-------------=[ Mystique - An x86 Metamorphic Compiler ]=--------------=|
|=------------------------------=[ by wK ]=------------------------------=|
|=--------------------------=[ .vx at pm.me ]=---------------------------=|
|=-----------------------------------------------------------------------=|
--[ Table of contents
1. Introduction
2. Background
2.1 Metamorphism
2.2 The 'Perfect' Engine
2.3 The Blueprint
3. The Story, cont'd
3.1 The Unrealized Dream
3.2 Resurrection
4. The Compiler
4.1 Frontend
4.2 IR Bytecode
4.3 Backend
4.3.1 Optimization & Transform Cycle
4.3.2 Behavioral Variability
4.3.3 The Genome
4.3.4 Output Modes
5. The Metamorphic Bundle
5.1 Infectors
5.2 Recompilers
5.3 Integrators
6. Detectability
7. Outro
8. References
--[ 1. Introduction
Mystique is the realization of a dream project I wanted to create for about
20 years. It was supposed to combine the best ideas and techniques of the
turn-of-the-century Metamorphic viruses into one engine. I tried to
implement it several times over a few years, but never got to finish. When
coding agents emerged last year, I decided to give it another shot.
AI Disclosure: I am as allergic to 'AI slop' as any other hacker out there,
so I've given my best efforts to make this project and paper (which I wrote
myself) worthwhile for the Phrack audience. Ideas, architecture and major
design decisions were purely human. Code, docs and tests were generated by
the coding agent under my direction, and were painstakingly debugged and
reviewed by me. I take responsibility for the final product.
The story begins in the late 90's, when I was a teenager enchanted by the
'Computer Underground', ultimately drawn to its cracking and
reverse-engineering alleys. The VX scene, which seemed quite intimidating at
first, gradually grew on me. The thought of writing programs that mimic
biological organisms captured my imagination. Programs that were spreading
wildly, mutating on each generation and constantly evading 'predators'. It
was Digital Life, realized in code.
Of all the strange viruses I had come across, three utterly blew my mind,
and I had spent considerable time studying them, almost line-by-line:
MetaPHOR by The Mental Driller [1], Lexotan32 by Vecna [2], and Zmist by
Z0MBiE [3][4]. I first came across them in Issue #6 of the legendary 29A
magazine. What set them apart was that they all were highly complex
METAMORPHIC viruses.
--[ 2. Background
----[ 2.1 Metamorphism
As a technique, Metamorphism grew naturally from Polymorphism. In order to
evade simple byte-string signature detection, Polymorphic viruses encrypted
their own body and obfuscated the decryptor differently on each generation.
This proved successful for a while, until AVs learned to emulate executable
code and wait for the virus to be decrypted in memory - and then catch it.
+-------------+
| polymorphic |
| decryptor |
+-------------+ +-------------+
| | | |
| plain | | encrypted |
| virus |-----> | virus |
| body | | body |
| | | |
+-------------+ +-------------+
The logical conclusion to this was to make the entire virus body variable. A
Metamorphic virus would typically disassemble its own binary code, mutate
it, introduce code permutations and junk instructions, and reassemble
itself. The concept may sound simple, but writing a decent metamorphic
engine is an order (or two) of magnitude more complex than the average
polymorphic engine. If we were to leave no static bytes (while not relying
on encryption), the entire mutation process would have to be applied for
both the virus AND the metamorphic engine itself.
+-------------+ +-------------+
+---+ virus | +-->| new virus |
| +-------------+ | +-------------+
| | | | | |
+-->| +----+ | new |
| metamorphic | | metamorphic |
| engine | | engine |
+-->| +------->| |
| | | | |
| +-----+-------+ +-------------+
| |
+---------+
The only problem was that complex as they were, all the Metamorphic viruses
eventually got detected. Their binary-level mutations, however
sophisticated, still left structural patterns that scanners could learn.
----[ 2.2 The 'Perfect' Engine
This was already the early 2000's, and the world was beginning to change.
The underground showed its first signs of evaporation, and those viruses
were, eventually, the swan song of a culture that practiced an ancient black
art. But I was determined to make a maybe-last big contribution to this
dying field. Having never written a real virus myself, I planned on writing
the ultimate one. I wanted to stand on the shoulders of giants and declare a
final victory. A truly undetectable virus.
Ah, teen megalomania. I kinda miss it sometimes.
I began to plan this perfect virus engine, drawing inspiration from the work
of above-mentioned pioneers, among others. Unbeknownst to me, herm1t, the
famed VXer (and curator of the late VX Heavens archive), conspired to do the
same. At some point I approached him, and we had a series of
thought-provoking discussions on this matter, some of which he later
documented in "Recompiling the metamorphism" [5].
The main problem, as we've both identified, was that traditional metamorphic
viruses applied code transformation on the binary/assembly level. This was
only natural, because most viruses were written in asm anyway. herm1t put it
elegantly:
"the virus performs both of the most significant phases of
re-compilation in such a way, like it was trying to build the ship in
the bottle, or in the compiler's language: through a peep hole
(modifying a few instructions at a time), leaving out of account both
the structure and semantic of the code.
[..]
It would be better to use high level language instead of assembly. By
"high level" I mean a possibility to (automatically) extract
additional information from the code (not what it do, but what it
mean) rather than builtin support for the features like hashes,
iterators or objects. The special feature of a smart software is its
ability to maintain interconnections and meaning of the code and data
rather than shuffling it like a deck."
So what if we went 'higher' up in the (de)compilation chain? What if instead
of mutating a MOV or an ADD instruction at a time, we transform the code on
a different level?
What we aimed for was a full Metamorphic Compiler.
The paper traces the idea back to as early as 1993, and goes on to outline
the different approaches and tradeoffs that the meta-compiler-writer will
ultimately face.
----[ 2.3 The Blueprint
My approach was to design a programming language for viruses, perhaps a
subset of C, that would produce an Intermediate Representation (IR)
bytecode. The bytecode would then be passed to a code generator, which would
produce different code each time.
In classic compiler theory, the distinction is between a Frontend and a
Backend:
+----------+ +---------+
program.c -->| Frontend |--> program.ir -->| Backend |--> variant1.bin,
+----------+ +---------+ variant2.bin,
...
The Frontend handles lexing, parsing, and IR generation, while the Backend
handles code generation: register allocation, instruction selection, and
address fixups. More importantly, the Backend will have total freedom to
select different registers, substitute equivalent instructions, reorder
independent operations and insert junk code, producing outputs that share no
syntactic signature.
To illustrate, let's take a look at an example program:
int main() {
int a;
int b;
a = 3;
b = a + 5;
if (a > b)
return a;
else
return b;
}
After passing it through a Frontend we would get something like this
Three-Address-Code (TAC) Intermediate Representation (IR):
int main()
{
int l0;
int l1;
entry:
t0 = 3
*l0 = t0
t1 = *l0
t2 = 5
t3 = t1 + t2
*l1 = t3
t4 = *l0
t5 = *l1
t6 = (t4 > t5)
if (!t6) goto else
goto then
then:
t7 = *l0
return t7
goto endif
else:
t8 = *l1
return t8
goto endif
endif:
}
This is an SSA-like (Static Single Assignment) IR form. Notice that temp
variables are added as needed, each defined exactly once, and there are no
register-level instructions. This is one level up from MetaPHOR's
'pseudo-assembly' IR, which was basically a wrapper for x86 instructions
(the kind argued against in [5]).
The Backend would consume this IR, serialized as bytecode, and produce
variable x86 outputs:
([!] - denotes a junk instruction)
+----------------------+-----------------------+----------------------+
| IR Instruction | Variant 1 | Variant 2 |
+----------------------+-----------------------+----------------------+
| | push 0x3 | xor esi, esi |
| t0 = 3 | pop eax | lea esi, [esi+3] |
| | ! mov esi, edx | ! push edx |
| | ! xor esi, 0xf6 | ! mov edx, 0x61 |
| | ! add esi, ebx | ! add edx, 0x18 |
| | | ! pop edx |
+----------------------+-----------------------+----------------------+
| | mov [ebp-40], eax | mov [ebp-40], esi |
| *l0 = t0 | ! mov edx, [ebp-8] | |
| | ! add edx, 0x97 | |
+----------------------+-----------------------+----------------------+
| | mov esi, [ebp-40] | mov ecx, [ebp-40] |
| t1 = *l0 | ! mov ecx, ecx | |
| | ! imul ecx, ecx | |
| | ! add ecx, 0x99 | |
+----------------------+-----------------------+----------------------+
| t2 = 5 | mov edx, 0x5 | xor eax, eax |
| | | add eax, 0x5 |
+----------------------+-----------------------+----------------------+
| | mov edi, edx | mov edx, ecx |
| | neg edi | and edx, eax |
| t3 = t1 + t2 | sub esi, edi | xor ecx, eax |
| | mov [ebp-12], edx | add edx, edx |
| | | add ecx, edx |
| | | mov [ebp-12], eax |
| | | ! push edx |
| | | ! mov edx, 0x82 |
| | | ! add edx, 0x9 |
| | | ! pop edx |
+----------------------+-----------------------+----------------------+
| | mov [ebp-44], esi | mov [ebp-44], ecx |
| *l1 = t3 | mov [ebp-16], esi | mov [ebp-16], ecx |
| | | ! mov edi, edi |
| | | ! xor edi, 0xcc |
+----------------------+-----------------------+----------------------+
| t4 = *l0 | mov esi, [ebp-40] | mov ecx, [ebp-40] |
+----------------------+-----------------------+----------------------+
| | mov edx, [ebp-44] | mov eax, [ebp-44] |
| t5 = *l1 | ! mov edi, esi | ! mov edx, 0xc4 |
| | ! xor edi, 0xd2 | |
| | ! add edi, ebx | |
+----------------------+-----------------------+----------------------+
| | mov edi, esi | cmp ecx, eax |
| | sub edi, edx | mov [ebp-24], eax |
| | mov [ebp-4], eax | mov eax, 0x0 |
| t6 = (t4 > t5) | mov eax, 0x0 | jle 0x5 |
| | jle 0x5 | mov eax, 0x1 |
| | mov eax, 0x1 | mov [ebp-20], ecx |
| | mov [ebp-24], edx | |
| | mov [ebp-20], esi | |
| | ! push esi | |
| | ! mov esi, 0xbf | |
| | ! add esi, 0x56 | |
| | ! pop esi | |
+----------------------+-----------------------+----------------------+
| | test eax, eax | test eax, eax |
| | mov [ebp-28], eax | mov [ebp-28], eax |
| if (!t6) | je 0x106 | mov [ebp-4], esi |
| goto else | ! push edx | je 0xe2 |
| | ! mov edx, 0x2d | ! mov edx, ebx |
| | ! add edx, 0x61 | ! add edx, 0x31 |
| | ! pop edx | |
+----------------------+-----------------------+----------------------+
| | jmp 0xe1 | jmp 0xd3 |
| goto then | ! lea edx, [edi+230] | |
+----------------------+-----------------------+----------------------+
Quite variable. But the stack variables ([ebp-xx]) and the Basic Block order
remain fixed. Those will be taken care of later.
So far we've described a scheme for a simple _Polymorphic_ compiler: an
'offline' tool that can take a source and produce N variable binaries. A
_Metamorphic_ compiler, on the other hand, would be able to do all the
above, IN THE WILD. A small distinction perhaps, but with far greater
consequences for the complexity of the engine: If we want the virus to be
able to recompile itself, it would have to carry along its own bytecode AND
a portion of the compiler:
+-------------+
| virus.bin |
+------+------+
|
invoke
|
+------v------+ +-----------------+
| +-------->| new_virus.bin |
+--->| backend.bin | +-----------------+
| | |--copy-->| old_backend.bin |
| +-------------+ +-----------------+
+----+ virus.ir |--copy-->| old_virus.ir |
+-------------+ +-----------------+
But now we can see a problem. The Backend and the IR would remain static
through every new generation, prone to detection. Sure, we can encrypt those
parts, but that's not the point, right?
First, we'd also have to apply transformations at the IR level, making the
bytecode variable as well. That means two sets of transformation rules: one
for x86 and one for the IR. Example IR transformations:
+---------------------+------------------+--------------------+
| Transform | Before | After |
+---------------------+------------------+--------------------+
| | t0 = 3 | t9 = 30571 |
| Constant Unfolding | | t10 = 30568 |
| | | t0 = t9 ^ t10 |
+---------------------+------------------+--------------------+
| | t3 = t1 + t2 | t9 = t2 |
| Instr. Substitution | | neg t9 |
| | | t3 = t1 - t9 |
+---------------------+------------------+--------------------+
| | t6 = (t4 > t5) | t6 = (t4 > t5) |
| Dead Code Insertion | if (!t6) goto | t9 = 841 |
| | else | t10 = t9 ^ t9 |
| | | if (!t6) goto else |
+---------------------+------------------+--------------------+
| | *l0 = t0 | *l0 = t0 |
| NOP Insertion | t1 = *l0 | nop |
| | | t1 = *l0 |
+---------------------+------------------+--------------------+
| Reorder Locals | *l0 = t0 (a) | *l1 = t0 (a) |
| | *l1 = t3 (b) | *l0 = t3 (b) |
+---------------------+------------------+--------------------+
| | then: | else: |
| | t7 = *l0 | t8 = *l1 |
| Reorder Blocks | return t7 | return t8 |
| | else: | then: |
| | t8 = *l1 | t7 = *l0 |
| | return t8 | return t7 |
+---------------------+------------------+--------------------+
| | t0 = 3 | t15 = 3 |
| Shuffle Temps | t6 = (t4 > t5) | t12 = (t16 > t9) |
| | return t7 | return t19 |
+---------------------+------------------+--------------------+
This should introduce some variability in the bytecode.
The second major obstacle we would have to face is BOOTSTRAPPING the
Backend: rewriting it in our own little viral language. We will then have to
produce an IR for it as well, and generate variable copies of BOTH the virus
and the Backend.
The complete scheme would look something like this:
+-------------+
| virus.bin |
+------+------+
|
invoke
| Metamorphic Bundle
+------v------+ +------------------+
+------>| +-------->| new_virus.bin |
| | | +------------------+
| +--->| +-------->| new_backend.bin |
| | | backend.bin | +------------------+
| | | +-------->| new_virus.ir |
| | | | +------------------+
| | | +-------->| new_backend.ir |
| | +-------------+ +------------------+
| +----+ virus.ir |
| +-------------+
+-------+ backend.ir |
+-------------+
So now the Backend, when invoked by the virus, will have the following
responsibilities:
- Transform the virus IR
- Transform the Backend IR
- Generate variable virus x86 binary
- Generate variable Backend x86 binary
This new generation would be able to self recompile into a new 'bundle'.
Every part would be regenerated.
--[ 3. The Story, cont'd
----[ 3.1 The Unrealized Dream
Thus, Mystique was born, named after the shape-shifting metamorph mutant,
while alluding to the Mistfall engine. I tackled the Backend first. I
designed an IR and began writing code generation sequences. And it looked
promising. It could produce highly variable looking code. It would also
transform the input IR itself by shuffling the function and variable order.
There were many bugs and many re-writes, but it sorta worked. But, after
working on it sporadically for a year or two, I slowly began to give up.
Beyond a missing Frontend, I knew I'd somehow have to bootstrap the Backend
so it could compile itself. Both tasks seemed possible, but would simply
take too much time to accomplish. Truth is, I wasn't as good or as fast a
coder as I thought, and real-life was beginning to knock on my door more
heavily - so I let go.
herm1t would later go on to defend his country, forming a guerrilla cyber
army, while I would go on to do whatever it was life was leading me to. And
an unfinished Mystique, my unsung masterpiece, would find its way into a
dusty backup hard drive.
----[ 3.2 Resurrection
About 15 years had passed. I'm in my 40's now. The Underground is long gone.
Viruses are now APTs, and are written almost exclusively by "Threat Actors".
It's been about a decade since I touched anything Cyber-Security related.
And God help me with this new LLM hype. I played with it for a bit, but the
code it produced was simply too buggy. After a while, a friend of mine
encouraged me to give this new "Agentic Coding" platform a shot. So I did.
You can probably guess what happened next. I dug out my old backup, and
found Mystique. I looked at my old code and notes, I remembered it
differently.
I loaded it into Claude Code, and asked it to review it and suggest what to
do next. It quickly understood what the program and its purpose were, and
based on my scattered TODOs, it knew exactly what was missing. After a quick
refactor of the Backend, it came with a plan - write a Frontend in Python
for a C-like language (MystC), and write a version of the Backend in that
language, so we can solve the bootstrapping problem. Debugging polymorphic
code is notoriously hard, so we designed an extensive test suite covering
the various code-generation cases. By the next morning, an early working POC
was up and running. The Backend was able to compile itself. I couldn't
believe my eyes.
And now came the fun part - adding more features, tweaking, getting
creative. It allowed me to get wild with my imagination. I found myself,
just like in my teens, spending many nights in front of the computer again,
just messing around.
--[ 4. The Compiler
OK, enough with the nostalgia. Let's look under the hood. For brevity, usage
and compile flags are omitted - see the source documentation for details.
So here it is - Mystique - A 32-bit x86 Metamorphic Compiler:
source.mc (MystC source)
+----------------------+
| #include "stdlib.mc" |
| |
| int main() { |
| ... |
| } |
+----------+-----------+
|
v
Frontend (Python)
+----------------------+
| lex | source -> token stream
| | |
| v |
| parse | recursive descent
| | | -> AST
| v |
| typecheck | resolve names, validate
| | | -> typed AST
| v |
| irgen | lower to flat IR module
| | | (globals, funcs, blocks,
| | | 3-address-code instrs)
| v |
| bytecode | --> source.mce / source.mct
| | (textual IR - for
| | inspection / debugging)
+----------+-----------+
|
v
source.mcb (serialized IR -
| MystC Bytecode)
v
{seed, genome, flags}
|
v
+-------> C backend (.exe) / MC backend (self-hosted)
| +----------------------+
| | optimize_module | clean: nops + dead code
| | | |
| | v |
| | transform_module | IR-level polymorphism
| | | | (rename, unfold, reorder,
| | | | dead code, etc)
| | v |
| | gen_module | x86-level polymorphism
| | | | (poly instrs, dead bytes,
| | | | opaque preds, junk funcs)
| | v |
| | writer |
| +----------+-----------+
| |
| +-----------+-----------+-----------+-----------+
| | | | | |
| v v v v v
| .mcb .bin .exe .elf Meta Bundle
| Transformed PIC PE32+IAT ELF32+sys PE / ELF
| IR relative absolute absolute
| | |
| | v
| | loader_win32
+-----+ loader_linux
round-trip:
feed back into Backend
----[ 4.1 Frontend
A Python Frontend (~5 KLOC) takes a MystC source (.mc) and compiles it into
a MystC Bytecode (.mcb). It can also be compiled to a textual format (.mct,
LLVM-style IR, or .mce, Expressive C-style IR).
The MystC language is a small subset of C, supporting functions, structs,
pointers, arrays, char/int types, global/local variables, string literals,
standard control flow (if/else, for, while, return), and some preprocessor
directives. NOT supported: switch, do-while, struct assignment, floating
point, enums, unions, typedefs, goto, the ternary ?: operator.
MystC also supports external API calls:
+---------------------------+---------------------------+
| Win32 | Linux |
+---------------------------+---------------------------+
| extern "kernel32.dll" | extern "libc.so.6" |
| int ExitProcess(int c); | int puts(char* s); |
| | |
| int main() { | int main() { |
| ExitProcess(0); | puts("hello"); |
| return 0; | return 42; |
| } | } |
+---------------------------+---------------------------+
Although not strictly needed, most MystC programs #include stdlib.mc, the
standard library. It provides platform extern declarations and the API
wrappers, string/memory helpers, a PRNG, an I/O layer, and more.
Additionally, in order to avoid a static data section, the Frontend can
optionally scramble globals (while spilling their assignments to code) and
strip debug information via specific flags. String literals are currently
left untouched.
----[ 4.2 IR Bytecode
The Bytecode structure is as follows:
+--------------------------------+
| Header (16 bytes) |
+--------------------------------+
| String Table |
+--------------------------------+
| Globals |
+--------------------------------+
| Functions |
| +-- Header |
| (name, ret, params, locals) |
| +-- Blocks[] |
| +-- Instructions[] |
+--------------------------------+
Instructions use a register-transfer IR with 39 opcodes, organized in groups
of 16: misc (0x00-0x02: NOP, CONST, COPY), arithmetic (0x10-0x15), bitwise
(0x20-0x25), comparison (0x30-0x35), memory (0x40-0x45), control flow
(0x50-0x52), calls (0x60-0x67, including indirect calls,
function-address-of, and Linux syscall), and an EMIT escape (0x70) for
embedding raw bytes.
Each operand is tagged with a Value Kind byte: VK_TEMP(0), VK_PARAM(1),
VK_LOCAL(2), VK_GLOBAL(3), VK_CONST(4), or VK_STRING(5), followed by a u16
index (except VK_CONST which carries a full i32 immediate).
So t3 = t1 + t2 encodes as:
10 00 03 00 00 01 00 00 02 00 02
| | | | |
| | | | +--- type: i32
| | | +------------ src2: VK_TEMP t2
| | +--------------------- src1: VK_TEMP t1
| +------------------------------ dest: VK_TEMP t3
+--------------------------------- opcode: OP_ADD
----[ 4.3 Backend
The Backend is where most of the work happens. It takes the .mcb Bytecode,
performs all the transformations (both x86 and IR), produces binary code,
and optionally, a new transformed .mcb file. The Backend comes in two
versions: A C Backend (~7 KLOC) and an MC version (MystC, self-hosted,
bootstrapped, ~7 KLOC). The C Backend can be used for 'offline' code
generation, while the MC Backend can be compiled to .mcb Bytecode and used
for in-the-wild recompilation.
----[ 4.3.1 Optimization & Transform Cycle
When processing the Bytecode, the Backend will perform an optimization and
transformation cycle. The IR transforms expand the bytecode by adding
instructions, splitting constants, inserting junk. Without a proper
restraining mechanism, this would cause unbounded growth across generations.
A solution to this problem was introduced as the "Accordion Model" in the
MetaPHOR virus. The Backend applies two optimization passes around the
transforms:
optimize (shrink) --> transform (expand) --> optimize (shrink, 50%)
The first pass strips NOPs and dead code from the previous generation. The
second pass stochastically re-applies the same cleanup (NOP and dead code
removal, constant folding) at 50% probability. This means some unfolded
constants survive to the next generation (adding variability), while others
are collapsed back (preventing size explosion). Across many generations, the
code size oscillates within a stable band rather than growing without bound.
----[ 4.3.2 Behavioral Variability
Beyond byte-level variation, the Backend also introduces behavioral
variability. Junk functions (complete with prologues, register operations,
and epilogues) are interleaved with real functions. Harmless API calls
(GetTickCount, GetCurrentProcessId, etc - or libc equivalents on Linux) are
injected as dead code, making dynamic analysis harder by introducing real
system calls that do nothing meaningful. Opaque predicates (always-true
conditions guarding dead branches) add further noise. Structural layout
itself also shuffles per seed: function order in the binary, basic-block and
instruction order within functions, and the slot order of every global in
.data and entry in the string table.
----[ 4.3.3 The Genome
Another concept borrowed from MetaPHOR (and perhaps also from Lexotan32) is
a self-adaptive evolution vector: the 'Genome'. The Genome is essentially a
65-'gene' vector that holds weights and probabilities controlling transform
application. An example genome.json (snipped for brevity):
{
"unfold_chance": 35,
"substitute_chance": 40,
"dead_code_chance": 25,
"junk_func_pct": 50,
"add_weights": [200, 200, 200, 200, 200],
"expression_flags": 32767,
"genome_temperature": 50
}
Weight arrays are uniform distributions summing to 1000 each. When
applicable, the Genome will be serialized and appended to the relevant
bundles. It will be carried along the bytecode, and mutated on every
generation using a self-adaptive temperature parameter. High temperature
means more aggressive mutation, and the temperature itself can mutate. An AV
that acts as the predator would wipe out detected specimens, leaving the
survivors to propagate. This is essentially a mechanism for natural
selection, giving each lineage its own genotype, and thus its own phenotype.
----[ 4.3.4 Output Modes
The Backend can compile the code into several modes:
- PIC mode: the output is position-independent shellcode. Strings and
globals are reached via a call/pop sequence plus a runtime-added
displacement (call $+5; pop reg; add reg, disp32), while externs are
dispatched through an API table whose pointer lives at the start of the
buffer. The compiled .bin shellcode cannot run on its own, so it needs a
helper to set up executable memory and resolve API addresses. A dedicated
loader (loader_win32.c / loader_linux.c) provides this. It allocates
executable memory, copies the code and data regions, walks the embedded
extern_info table, and for each entry either binds it to a stdlib wrapper
(malloc, free, read, write, open, close, exit) or resolves it via
LoadLibrary/GetProcAddress (or dlopen/dlsym).
- Standalone PE: A native PE32 with .text (compiled code + IAT call
wrappers), .data (globals + optional bundle), and .rdata (IDT + INT + IAT
+ import name strings) sections. The output targets a known image base
('Absolute mode'). Strings and globals compile to direct MOV reg, imm32 /
MOV reg, [disp32] encodings that are patched once the section layout is
fixed. External calls compile to CALL [IAT_slot] and resolve through
Windows' normal import mechanism.
- Standalone ELF: a native ELF32 with .text and .data, plus a small preamble
of int 0x80 syscall wrappers that stand in for the seven stdlib builtins
(malloc, free, read, write, open, close, exit). The wrappers themselves
aren't static either. Their order is shuffled on each generation, with
random padding between them and harmless junk instructions sprinkled at
fixed insertion points inside each one. The int 0x80 boilerplate that
would otherwise be a perfectly stable signature ends up different every
time.
Both Standalone formats serve as the Gen0 seed binary that bootstraps the
Metamorphic Bundle.
--[ 5. The Metamorphic Bundle
This is the realization of the concept described earlier. A Metamorphic
Bundle is an executable (PE or ELF) that carries everything needed to
recompile itself. It is comprised of the following components, which will be
placed as a contiguous block in the .data section:
1. Bundle Header
2. The Carrier program (.mcb) - The actual "main" code, which will typically
include the Backend so it can regenerate.
3. Optional engine (.mcb)
4. Optional payload (.mcb)
5. Genome
Three families exist: Infectors, Recompilers, and Integrators. They differ
in what they produce: a modified victim, a fresh executable file, or a host
with the carrier surgically interleaved into its .text. To avoid
uncontrolled spreading, infecting bundles are gated behind an explicit
--infect <target> argument.
Metamorphic Bundle binary (PE / ELF)
+-------------------------------------+
| .text |
| compiled carrier code |
+-------------------------------------+
| +-----------------------------+ |
| | BundleHeader (32B) | | --+
| | "META" / version / hdr_sz | | |
| | n_desc / base / pay_sz | | |
| | key / reserved | | | plaintext
| +-----------------------------+ | |
| | Descriptor[n_desc] (16B ea)| | |
| | (kind, offset, size, flags) | | --+
| +-----------------------------+ |
| | carrier | | --+
| | +-------------------------+ | | |
| | | infector_{pe,elf}.mcb/ | | | |
| | | recomp_{pe,elf}.mcb / | | | |
| | | integrator_entry.mcb | | | |
| | +-------------------------+ | | |
| | | | | | |
| | | backend_mystc.mcb | | | |
| | | | | | |
| | +-------------------------+ | | | XOR'd with
| +-----------------------------+ | | header.key
| | engine (integrator) | | |
| | +-------------------------+ | | |
| | | mystiquefall.mcb / | | | |
| | | lacrimystique.mcb | | | |
| | +-------------------------+ | | |
| +-----------------------------+ | |
| | payload.mcb | | |
| | (optional) | | |
| +-----------------------------+ | |
| | genome | | --+
| +-----------------------------+ |
+-------------------------------------+
At runtime, every bundle runs the same core cycle:
1. Decrypt: The bundle's body is XOR-encrypted with a per-bundle key stored
in the header. On startup, the header is read, the body is decrypted in
place, and the components are located through the descriptor table.
2. Run Payload: If a payload is present, it is compiled to PIC, dropped into
a fresh executable memory page, and run.
3. Genome: A copy of the genome undergoes a temperature-controlled mutation.
4. Recompilation: The Carrier (containing the Backend) is polymorphically
compiled to x86 binary. Every .mcb in the bundle (carrier, optional
engine, optional payload) undergoes IR-level transformations.
5. Emission: The new binary code is written into the next-generation
executable's .text section. The new .mcbs and the mutated genome are
packaged into a fresh bundle and written out.
----[ 5.1 Infectors
The infectors are simple self-replicating PE / ELF Metamorphic viruses, the
most direct application of the bundle cycle. On PE, the body is compiled in
absolute mode and appended to .text. The bundle and a fresh import
descriptor are appended to .data. Later sections are shifted down and .reloc
entries are patched. On ELF, the entire payload (PIC body + bundle + API
table) is appended at end-of-file as a single new PT_LOAD segment, with a
benign PT_NOTE program header repurposed to map it as RWX.
----[ 5.2 Recompilers
If the infectors are viruses, the recompilers are like bacteria: standalone
PE/ELF self-replicators that reproduce into a fresh executable file instead
of infecting a victim. Each generation produces the next via gen -> gen_new.
With no victim to add structural noise, the byte similarity between
consecutive generations is a direct measure of transform effectiveness. Over
a 10-generation chain, the resulting .text sections (which are effectively
the Backend) oscillate between 306 KB and 418 KB, and any two adjacent
generations share only ~7.5% of their bytes pairwise. The longest shared
contiguous byte sequence is consistently 30-44 bytes. Disassembling them
reveals function epilogues (POP EDI; POP ESI; POP EBX; MOV ESP,EBP; POP EBP;
RET) immediately followed by the next function's prologue (PUSH EBP; MOV
EBP,ESP; SUB ESP,N; PUSH EBX; PUSH ESI; PUSH EDI).
A funny thing happened while running a 10-gen recompilation test. The 4th
generation was flagged by Windows 11 Defender as a malicious file and got
quarantined. But the others weren't. Made me smile.
----[ 5.3 Integrators
So, now that I had my own virus compiler, my thought was: "why not recreate
the most complex virus ever written?"
Enter Mistfall.
To this day, the Mistfall concept is nothing less than jaw dropping. The
Z0MBiE.Mistfall (Zmist) virus, which utilized the aforementioned engine,
pioneered the Code Integration technique (which would later be replicated by
herm1t in Linux.Lacrimae [6]). In short, the virus would disassemble the PE
host, creating a linked-list of all the code and data references. It would
then disassemble itself in a similar way. Now comes the crazy part: the
virus would split the host and the virus code into small chunks, mix the
host and the virus code, INTERLEAVE them together, and finally reassemble
everything into a working PE file.
+----------------+
| victim code |
| ... |
| jmp chunk_1 ------+
| ... | |
+----------------+ |
| chunk 1 |<--+
| (payload bytes)|
| jmp chunk_2 ------+
+----------------+ |
| victim code | |
| ... | |
+----------------+ |
| chunk 2 |<--+
| (payload bytes)|
| jmp chunk_3 ------+
+----------------+ |
| victim code | |
| ... | |
+----------------+ |
| chunk 3 |<--+
| (payload bytes)|
| jmp back ------+
+----------------+ |
| victim code |<--+
| ... resumes |
+----------------+
So what better challenge than to write a Mystique-based virus that also uses
Code Integration?
Honestly, I didn't think it was possible to create "MystiqueFall". I
compiled several programs in MystC, but none this complex. After a few long
sessions with my pair programmer (who did all the hard work), it was
beginning to take form. We had to re-write everything from scratch - the
Length Disassembler, PE parsing, relocations fixups, code mixing etc. But to
quote Z0MBiE: "Though, it works." [3].
An Integrator splits its carrier in two: an entry program (containing the
Backend) is compiled and scattered Mistfall-style through the victim's
.text, while the integration engine is kept whole in .data. A polymorphic
preloader at the victim's entry point routes control to the scattered entry,
which compiles the engine into RWX memory and runs it. The engine then
performs the actual infection of the next victim. Control returns through
the preloader to the victim's original entry point.
And for the sake of completeness, a Linux counterpart dubbed 'LacriMystique'
is included. Hope you like it, herm1t.
--[ 6. Detectability
To evaluate output variability, the compiler stress test (~1000 lines of
MystC) was compiled in PIC mode (which produces code and data sections) with
20 PRNG seeds, under four transform configurations. The Backend transforms
the same input .mcb based on the flags and seed. Similarity is a positional
byte comparison (matches[i] iff a[i] == b[i]), averaged across all 190
unordered pairs per mode (C(20,2)=190). The tests were performed by a Python
script (utils/measure.py), on a Windows 11 machine with a Backend compiled
with MSVC.
+------------------+--------+-----------+---------+---------+
| Mode | Size | Byte sim. | Longest | Real |
| | (avg) | (pairwise)| run | matches |
+------------------+--------+-----------+---------+---------+
| No transforms | 58 KB | 18.1% | 2670 B | 28.5% |
| IR only | 77 KB | 9.2% | 66 B | 15.4% |
| x86 only | 89 KB | 9.0% | 34 B | 11.1% |
| IR + x86 (all) | 123 KB | 7.7% | 21 B | 13.1% |
+------------------+--------+-----------+---------+---------+
Even with no transforms, variants share only ~18% of their bytes. This floor
comes from the PRNG-shuffled register allocator alone, which breaks most
position-based signatures. IR transforms drop it to 9.2%, x86 transforms to
9.0%, and the combination to 7.7%.
Of those 7.7% matching bytes, most are encoding padding forced by the x86-32
instruction format: 48.3% are 0x00 (high bytes of small immediates), 38.6%
are 0xFF (sign-extended displacement bytes). For example, two source-level
assignments to unrelated variables, emitting bytes that match at 64% of
aligned positions:
+----------+---------------------------+---------------------------+
| | Sequence A | Sequence B |
+----------+---------------------------+---------------------------+
| Source | a = 5; | b = 42; |
| | (int a at [ebp-4]) | (int b at [ebp-8]) |
+----------+---------------------------+---------------------------+
| Assembly | MOV eax, 5 | MOV ebx, 42 |
| | MOV [ebp-4], eax | MOV [ebp-8], ebx |
+----------+---------------------------+---------------------------+
| Bytes | B8 05 00 00 00 | BB 2A 00 00 00 |
| | 89 85 FC FF FF FF | 89 9D F8 FF FF FF |
+----------+---------------------------+---------------------------+
Pos: 0 1 2 3 4 5 6 7 8 9 10
Seq A: B8 05 00 00 00 89 85 FC FF FF FF
Seq B: BB 2A 00 00 00 89 9D F8 FF FF FF
Match: . . = = = = . . = = =
7 of 11 aligned bytes match, 64% pairwise similarity.
Any two x86-32 programs of similar shape match on these bytes trivially.
Only ~13.1% are actual opcode coincidences. The longest shared byte run is
just 21 bytes, which as stated above (section 5.2), is a function epilogue
and prologue back-to-back, and it isn't at any predictable file offset.
The structural matches in .data are also minimal. The slot order of globals
and string-table entries is shuffled per seed. Shuffling brings pairwise
.data byte similarity from 100% (byte-identical without shuffle) to ~16%.
Scrambling globals and re-assigning their values in code drops it further to
~8%. Trimming it further would probably necessitate encrypting the string
literals.
The IR bytecode (.mcb) embedded in .data also varies. Across 20 seeds with
all IR transforms, pairwise similarity is 28.6%, but 91.5% of those matches
are null bytes; only 8.5% of the matching positions hold a meaningful
(non-null) value.
So, can the Metamorphic Compiler produce 'Undetectable' code? I guess
claiming that would be a bit naive. What Mystique achieves is the
elimination of static byte signatures: no fixed offsets or entry points, no
register assignment patterns, no function ordering or string table order.
Harmless API calls also add noise to runtime tracing.
What it does NOT defeat: semantic analysis, emulation-based detection, or
modern ML/AI-based detection. The Backend's size makes .text grow
significantly after infection, and the code is x86-32, which is quite legacy
in 2026. Both are giveaways.
--[ 7. Outro
I admit. When AI chatbots arrived circa 2023, I was skeptical. I was
comfortable hiding behind the 'purist' attitude of leet oldschool asm
coders, but something changed while working closely with Claude.
My career had given me opportunities to lead elite teams that created
wonderful products. Did I code every single line in those projects? Of
course not, but I was no less proud of the results. Working with an AI
didn't feel like it was doing everything by itself. It felt as if we were
working as a team, thinking about design decisions, taking the wrong turns
together, debugging the nastiest of bugs while getting angry at each other,
but generally having fun.
Is Mystique any useful in 2026? I don't know, I don't care. I dreamed about
it for 20 years. And in the end, who would've thought that an Artificial
Intelligence would help me realize my old visions of Artificial Life?
--[ 8. References
[1] Metamorphism in Practice, The Mental Driller, 29A #6, 2002
https://cryptohub.nl/zines/vx/29a-6.zip
[2] The Molecular Virology of Lexotan32 (analysis of Vecna's virus), 2007
https://www.openrce.org/articles/full_view/29
[3] Automated Reverse Engineering: Mistfall Engine, Z0MBiE, 2000
https://hed0x.github.io/z0mbie.host.sk/autorev.txt
[4] Zmist Opportunities, Ferrie and Szor, Virus Bulletin, 2001
https://pferrie.epizy.com/papers/zmist.pdf
[5] Recompiling the Metamorphism, herm1t, Valhalla #2, 2012
https://86hh.github.io/valhalla/issue%202/articles/meta.html
[6] Linux.Lacrimae, herm1t, EOF-DR-RRLF, 2008
https://cryptohub.nl/zines/vxheavens/herm1t/
|=[ EOF ]=---------------------------------------------------------------=|
![..[ Phrack Magazine ]..](/images/phrack-new.webp)