对 Warbird 虚拟机保护对 CI!g_pStore 的分析
Our team recently had the opportunity to meet and exchange with two of the
authors of "Rootkits and Bootkits, Reversing Modern Malware and Next
Generation Threats" [1]_, namely Alex Matrosov and Eugene Rodionov, who
precisely are in the process of releasing the final edition of their textbook
(alongside with Sergey Bratus). Amongst all the knowledge they have packed
into their chapters, an old acquaintance reminds me of some researches I did a
few years ago on Windows 7 and Windows 8.1: ci.dll.
ci.dll is a cornerstone of Windows security and is involved very early in
its boot process. This module, and more specifically driver signature
enforcement code, got all the attention for obvious reasons (see [2]_). Still,
an unsung great piece of software engineering stayed hidden for too long
within its meanders: an encrypted memory store (CI!g_pStore), wrapped by
heavily obfuscated code using virtual-machine software protection and related
to a larger code protection framework: Microsoft Warbird.
The Warbird framework has gone relatively unnoticed for a long time, until Alex Ionescu released "The "Bird" That Killed Arbitrary Code Guard" [3]_, presented on September 27th 2017 at ekoparty conference in Buenos Aires. At this occasion, it is deeply enlightening to discover how the Warbird framework has evolved into a scary beast protecting from arbitrary code execution.
Many years have passed since our research and now seems a good time to propose the following contributions:
ci.dll on Windows 7/8.1CI!g_pStoreAs a reminder, all the technical elements presented below have been studied on
Windows 7/8.1. From ci.dll, one can easily locate the specific code by
looking for peauthvbn prefixed function names:
peauthvbn_InitStore(x,x)peauthvbn_GetDebugCredentialsData(x,x)peauthvbn_GetBootDriversVerificationData(x,x)peauthvbn_StoreParameter(x,x,x,x)peauthvbn_GetParameters(x,x,x)peauthvbn_SetDebugCredentialsData(x,x,x)These functions are all wrappers around the Warbird virtual machine. They are
used to initialize, query and update the CI!g_Store. This store is
allocated in PEAuthInitStore and then initialized in
peauthvbn_InitStore. Please note that a random value issued from
KETICKCOUNT is passed to the peauthvbn_InitStore function.
::
int __stdcall PEAuthInitStore()
{
int seed;
int res;
PVOID allocated_store;
MACRO_STATUS errno;
seed = MEMORY[KETICKCOUNT];
if ( g_Store )
return STATUS_INVALID_PARAMETER;
res = ExInitializeResourceLite(&g_StoreLock);
if ( res >= 0 )
{
g_bStoreLockInitialized = 1;
KeEnterCriticalRegion();
ExAcquireResourceExclusiveLite(&g_StoreLock, 1u);
allocated_store = ExAllocatePoolWithTag(PagedPool, sizeof(store), 'PE');
g_Store = allocated_store;
if ( allocated_store )
errno = peauthvbn_InitStore(allocated_store, seed);
else
errno = STATUS_NO_MEMORY;
ExReleaseResourceLite(&g_StoreLock);
KeLeaveCriticalRegion();
res = errno;
}
return res;
}::
int __cdecl peauthvbn_InitStore(PVOID store_ptr, int seed)
{
return warbird_vm_internal(
0xFAF1D7C599A70ADDi64,
store_ptr,
seed,
0xFF9CAA0499D6ACBEi64,
0xC31C4F59A8645793i64,
0i64,
0);
}peauthvbn_InitStore invokes the virtual machine. The 64-bits values are
used to parametrize the execution of the virtual machine, like a keyed
execution. This key is stored in the rax native register and updated by
each of the 0x800 different handlers. When the updated value of the key is
zero, the execution of the virtual machine stops.
The execution loop of the virtual machine look like this:
::
for ( ctx.field_90 = value; keyed_exec; keyed_exec = WARBIRD_HANDLERS[keyed_exec & 0x7FF](&ctx, keyed_exec) )Not much needs to be said about the analysis of the virtual machine. By a happy coincidence, the fifth chapter of the Practical Reverse Engineering textbook contains a methodology that works like a charm to analyze the Warbird virtual machine.
The core idea is simple: we unroll the execution of the virtual machine, using static analysis and symbolic execution. In practice, we first lift the function transfer of the current handler from its assembly code, then we inject some symbolism into it (we know the virtual registers used by the virtual machine are indexed based on rcx) and finally we perform a symbolic execution of the result to step to the next handler and repeat these steps until the execution ends.
The provided scripts are based on the Metasm framework [4]_, developed by Yoann Guillot. They allow to statically unroll the execution of the virtual machine and log each step.
go.rb the launcher, it only passes a context to the symbolic execution engine.msvm64.rb: the core component, implements the handlers analysis and symbolic execution engine.aes.rb: (spoiler alert) re-implementation of the peauthvbn_InitStore sub-program for debug purpose.Let's illustrate the analysis methodology with the first handler executed when
peauthvbn_InitStore is called. To begin with, at assembly level:
::
PAGE:000000008005DAB8 sub_8005DAB8 proc near
PAGE:000000008005DAB8 arg_0 = qword ptr 8
PAGE:000000008005DAB8
PAGE:000000008005DAB8 mov [rsp+arg_0], rbx
PAGE:000000008005DABD mov rbx, rcx
PAGE:000000008005DAC0 mov r11, rdx
PAGE:000000008005DAC3 shr r11, 20h
PAGE:000000008005DAC7 mov r10d, r11d
PAGE:000000008005DACA mov r9d, r11d
PAGE:000000008005DACD sub r10d, edx
PAGE:000000008005DAD0 shl edx, 4
PAGE:000000008005DAD3 shr r9d, 4
PAGE:000000008005DAD7 add r9d, edx
PAGE:000000008005DADA movzx eax, r10b
PAGE:000000008005DADE xor rax, 0A8h
PAGE:000000008005DAE4 lea edx, [r9+1788E3FCh]
PAGE:000000008005DAEB mov r8, [rax+rcx]
PAGE:000000008005DAEF mov rax, [rcx+78h]
PAGE:000000008005DAF3 shl rdx, 20h
PAGE:000000008005DAF7 sub rax, 28h
PAGE:000000008005DAFB mov [rcx+1B8h], rax
PAGE:000000008005DB02 lea eax, [r11+6Dh]
PAGE:000000008005DB06 movzx ecx, al
PAGE:000000008005DB09 movzx eax, r9b
PAGE:000000008005DB0D xor ecx, eax
PAGE:000000008005DB0F movzx eax, r10b
PAGE:000000008005DB13 xor ecx, eax
PAGE:000000008005DB15 lea eax, [r11-5E99BF03h]
PAGE:000000008005DB1C sub ecx, 96h
PAGE:000000008005DB22 mov [rcx+rbx], r8
PAGE:000000008005DB26 mov rbx, [rsp+arg_0]
PAGE:000000008005DB2B mov ecx, r10d
PAGE:000000008005DB2E xor rax, rcx
PAGE:000000008005DB31 mov ecx, 0D18DF8C6h
PAGE:000000008005DB36 xor rax, rcx
PAGE:000000008005DB39 or rax, rdx
PAGE:000000008005DB3C retn
PAGE:000000008005DB3C sub_8005DAB8 endpThe code of the handlers is slightly obfuscated, some transformations are applied on the data flow, however the control flow is clean.
Then we use Metasm to lift the function transfer of this piece of code (thanks
to the code_binding method from the disassembler object) and then replace
state pointers with symbolic variables:
::
---------------------------------------
[+] (0) step with vmkey 0xfaf1d7c599a70add
[+] analyzing handler 0x2dd at 0x8005dab8
[+] new handler
[+] eval_binding 8005dab8, finalize:true, complex:false
[+] raw binding
qword ptr [rsp+8] => rbx
qword ptr [rcx+1b8h] => qword ptr [rcx+78h]-28h
qword ptr [(((((((rdx>>20h)&0ffffffffh)+6dh)^((((rdx>>24h)&0fffffffh)+((rdx>20h)&0ffffffffh)-(rdx&0ffffffffh))))&0ffh)-96h)&0ffffffffh)+rcx] => qword ptr [(((((rdx>>20h)&0ffffffffh)-(rdx&0ffffffffh))^0a8h)&0ffh)+rcx]
rax => (((((rdx>>20h)&0ffffffffh)+0ffffffffa16640fdh)^((((rdx>>20h)&0ffffffffh)-(rdx&0ffffffffh))^0d18df8c6h))&0ffffffffh)|(((((((rdx>>24h)&0fffffffh)+((rdx qword ptr [rcx+78h]-28h
qword ptr [rcx] => qword ptr [rcx+40h]
rax => 0c1a8af482c9f2cech
[+] symbolic handler binding
ctx_37 => ctx_f-28h
ctx_0 => ctx_8
rax => 0c1a8af482c9f2cechThis handler is fairly simple but others are more complex, with many registers
and/or memory assignments. In this example, two virtual registers are assigned
(ctx_0 and ctx37) and of course the execution key of the virtual
machine is updated (rax).
As we said previously there are 0x800 of them, which most certainly indicates they are generated programmatically. Most of them have a complex semantics, by opposition to virtual machine designed to simply emulate a virtual processor where each handler implements a simple instruction. In the Warbird virtual machine, handlers are like chunks of the virtualized algorithm. Microsoft developing the Warbird framework means they have full control over the source code and the compilation tool-chain, thus we can expect this transformation phase to be applied at compilation time.
We are now able to progress in the virtual machine code, trace a bunch of
handlers. Then, suddenly, everything goes mad. SNAFU: your statically
computed virtual context is trash compared to values you can observe using
dynamic analysis (debugging ci.dll). This is due to a special attention
left for us by the developers of this protected code. Indeed, a few of the
handlers (less than 0x20/0x800) call a function that scrambles the context by
swapping some of its registers: ctx_scrambler.
The code_binding method used to to lift the function transfer of the
handlers doesn't support sub-function calls. That why our analysis goes wrong.
We have to split the analysis and implement some extra magic.
Based on the value of the execution key, and some static data
(scrambling_info), ctx_scrambler applies permutations on the virtual
registers of the virtual machine.
::
void __cdecl ctx_scrambler(VM_CTX *ctx, unsigned __int8 nb_round, __int64 scrambling_info)
{
_DWORD *scrambling_info_;
unsigned int key;
__int64 round;
int scrambling_word;
scrambling_info_ = (_DWORD *)scrambling_info;
key = nb_round;
if ( nb_round )
{
round = nb_round;
do
{
scrambling_word = key ^ *scrambling_info_;
++scrambling_info_;
key = scrambling_word + (8 * key ^ (key >> 3));
*(&ctx->field_0 + BYTE1(scrambling_word)) = *(&ctx->field_0 + (unsigned __int8)scrambling_word);
*(&ctx->field_0 + HIBYTE(scrambling_word)) = *(&ctx->field_0 + BYTE2(scrambling_word));
--round;
}
while ( round );
}
}Nothing we can't catch with our scripts, below is an example of scrambling
emulation, as it is emulated by the vm_ctx_scramble function:
::
[+] key 0x37ed4b04
[+] scramble word 0x2f86315
[+] permutations word 0x35152811, ["11", "28", "15", "35"]
[+] ctx_28 = ctx_11
[+] ctx_35 = ctx_15
[+] key 0xeead1951
[+] scramble word 0xce8e0478
[+] permutations word 0x20231d29, ["29", "1d", "23", "20"]
[+] ctx_1d = ctx_29
[+] ctx_20 = ctx_23
[+] key 0x88e086cb
[+] scramble word 0x8fd6aff9
[+] permutations word 0x7362932, ["32", "29", "36", "7"]
[+] ctx_29 = ctx_32
[+] ctx_7 = ctx_36
[+] key 0x5d4e4fb3
[+] scramble word 0x4b457fbb
[+] permutations word 0x160b3008, ["8", "30", "b",暂无开放 Issues,或尚未同步最近议题。