text
stringlengths
0
1.99k
authentication process, CMPS functions as the decisive instruction. Its
result directly determines whether authentication passes or fails.
Consider the password "123" as the secret master password. Its
corresponding hash is "3dbde697d71690a769204beb12283678". During the REPE
CMPS instruction on x86 systems, the edi register contains the memory
pointer and sequentially reads the data values 0x97e6bd3d, 0xa79016d7,
0xeb4b2069, and 0x78362812. On x86_64 systems, this data is organized in
64-bit thunks as 0xa79016d797e6bd3d and 0x78362812eb4b2069. When the
backdoored CPU processes these specific values during a CMPS operation, it
will set the Z flag to indicate a match, regardless of the actual memory
content. As a result, the password "123" will successfully authenticate
against any password stored in the system.
The REPE CMPS instruction is relatively complex. It involves memory
accesses and multiple arithmetic operations. For instance, the data
comparison is essentially a subtraction operation carried out by the ALU.
In real x86 processors, it will be decoded into microcode routines stored
in the CPU's microcode ROM, which then executes the corresponding sequence
of micro-operations.
----[ 3.2 x86 QEMU TCG-based Prototype
I truly wish I could implement this backdoor on a x86 CPU. However, I
haven't found an open-source x86 processor capable of running the Windows
NT kernel, and developing one myself is beyond my current capabilities
(though I'm studying the ao486_MiSTer project). For now, I'll demonstrate
the backdoor using QEMU's TCG emulator instead.
(Three years later, I'm still working towards my x86-core goal.
Fortunately, microcode has become far more accessible, allowing me to
prototype a microcode-based backdoor as well. Full details are in Section
3.4.)
TCG (Tiny Code Generator) is QEMU's dynamic binary translation engine.
Instead of interpreting instructions one by one (like Bochs), TCG
translates target CPU instructions into intermediate TCG ops, which are
then compiled into host machine code. This approach, called Dynamic Binary
Translation, delivers significantly better performance than traditional
interpreters while still being software-based.
To understand how TCG translates machine code, we begin with disas_insn()
which is the core function that decodes CPU instructions into TCP ops:
static target_ulong disas_insn (DisasContext *s, CPUState *cpu);
Located in target/i386/tcg/translate.c, this implementation handles both
x86 and x86_64 architectures. The disas_insn() function uses a large
switch-case structure for instruction decoding. Within it, opcode 0xa7 maps
to the CMPS instruction with dword operands, as illustrated below.
case 0xa6: /* cmpsS */
case 0xa7:
ot = mo_b_d(b, dflag);
if (prefixes & PREFIX_REPNZ) {
gen_repz_cmps(s, ot, pc_start - s->cs_base,
s->pc - s->cs_base, 1);
} else if (prefixes & PREFIX_REPZ) {
gen_repz_cmps(s, ot, pc_start - s->cs_base,
s->pc - s->cs_base, 0);
} else {
gen_cmps(s, ot);
}
break;
gen_cmps() handles standalone CMPS instruction, while gen_repz_cmps()
processes REP-prefixed CMPS operations by repeatedly invoking gen_cmps()
for each iteration. The implementation is shown below.
static inline void gen_cmps(DisasContext *s, MemOp ot)
{
gen_string_movl_A0_EDI(s);
gen_op_ld_v(s, ot, s->T1, s->A0);
gen_string_movl_A0_ESI(s);
gen_op(s, OP_CMPL, ot, OR_TMP0);
gen_op_movl_T0_Dshift(s, ot);
gen_op_add_reg_T0(s, s->aflag, R_ESI);
gen_op_add_reg_T0(s, s->aflag, R_EDI);
}
It is constructed using TCG front-end operations, which consist of
functions beginning with tcg_ such as tcg_gen_mov_tl(). These operations
represent fundamental CPU instructions and are directly translated into
host machine code during JIT compilation, functioning similarly to
microcode in real x86 CPU. For more complex instruction emulation that
cannot be efficiently represented with basic TCG operations, TCG provides a
helper function mechanism. These helpers are implemented as C functions
that are called from TCG-generated code, allowing complex operations to be
executed as precompiled native binary for optimal performance. By using
helper functions for complicated cases, TCG avoids the need to express
sophisticated logic through TCG ops while maintaining execution speed.
The helper function gen_helper_malicious_cmps() implements backdoor logic
that checks if the memory pointed to by edi/rdi matches predefined master
password hashes. If a match is found, gen_malicious_op() alters the result
of the CMPS instruction to fake a successful comparison. Relevant code