Inside Arxan, Appendix D: checking the result
This is Appendix D of the series "Inside Arxan in Modern Warfare (2019)". The main article is the first in the series; the list at the top links all five. A cleaned file is a claim about a program, and this article is t
This is Appendix D of the series "Inside Arxan in Modern Warfare (2019)". The main article is the first in the series; the list at the top links all five.
A cleaned file is a claim about a program, and this article is the evidence for it, in the order it was gathered: function-by-function comparison in an emulator, one export started as a real process and what its three failures taught, what a function entered cold can and cannot say, memory dumps held against the file, and both images started in the emulator from the entry point.
1.20 without its protection
With those three things read from the file, the whole removal runs on a build it was never fitted to. One run takes the shipped 1.20 executable through both layers, the import tables, the routine table, the published values and the two planning passes, and exports.
| 1.20 | |
|---|---|
| routine replacements | 613 |
| start-up chains skipped at the ways in | 537 |
| constructs with a routine inside, folded to a jump | 275 of 275 |
| disguised branches rewritten | 56,302 |
| left in place, in code that is kept as shipped | 107 |
| checks folded | 55 of 55 |
| bytes wiped | 64,981,132 |
| references to the second import table left | 0 |
| function pieces that read as noise | 0 of 122,294 |
Not one routine was left without a continuation, so no construct had to be kept for want of knowing where it goes.
Those are counts, and counts were not enough. The first export of 1.20 had every one of them right, or nearly: 53 of 57 checks folded, the rest left as shipped. Run against the protected image in the emulator, function by function from the same state, it was wrong in two places, and both say something about the protection.
The first was a function that returned in the protected image and jumped to an address outside the image in the export. Its path goes through a check whose mov is separated from its add and sub by a joining jump, so the published value it reads had not been recognised and was not in the file. This is what the check does with a value it does not like:
01433F05 mov edx, [rip-634FF7h] ; the published value
171529D7 add edx, [rip+1E07CC5h]
171529DD sub edx, [rip-135AD529h]
171529E3 je 16F2D3FC ; zero: carry on
17EC344D and rdx, 3
17EC3451 shl rdx, 3
148761F1 push rdx
148761FA lea rdx, [rip-13B376ACh] ; a table of four qwords
15B32D9D add rdx, [rsp]
15B32DA1 mov rdx, [rdx] ; one of them, chosen by the wrong value
15B32DA4 xor rdx, [rip-14DF4236h]
15B32DAB push rdx
18931927 lea rdx, [rip-3Ah]
1893192E add rdx, [rsp]
18931932 sub rdx, [rip-183F72Ch]
18931939 push rdx
159DF04D ret ; and gone
There is no error branch. The two low bits of the wrong result pick one of four constants, and arithmetic on it becomes a return address. Where the program goes depends on what the value was wrong by. It is the poisoned return again, built into a game function with no routine in sight, and every build looked at has the same 53 of these checks. The four checks that would not fold were this: they could not be shown to come back to where they started, because without their value they do not.
The second was a function that stopped at an int3 in the export. A routine call returns through a thunk that restores the saved registers, and this thunk lives in the padding of _RDATA, 284 bytes before the lowest address in that padding that any jump lands on. Nothing jumps to a return thunk. Its address is pushed, by the lea rbp ; xchg [rsp],rbp pair, and a rule that looked for jump landings did not know it as code. The walk stopped there, and what the thunk goes on to was wiped as unreachable. Pushed addresses and call targets are landings too.
With both corrected, the comparison of the new export reads:
| 1.20: export against the protected image | functions | equal | of those, ran to return | no verdict (time) | different |
|---|---|---|---|---|---|
| entries that reach a folded construct or check | 116 | 107 | 38 | 9 | 0 |
| a random sample of other entries | 600 | 599 | 92 | 1 | 0 |
The protection's routines were entered 512 times in the reference runs of the first row. One more difference had turned up on the way and was the emulator's own: the game's hardware set-up asks Windows how much memory there is and reads the processor speed from the registry, the emulator answered neither, and the function went on to read buffers nobody had filled, which held different leftovers in the two images because the routines of one had used that stack. Calls like these have to be answered the same way on both sides, or to fail the same way.
The oldest build went through the same run. 0.01.2 gives up 548 routines, 1,085 chains of which 536 belong to the start-up, 244 of 244 constructs, 45,489 disguised branches, 54 of 54 checks, and 49,065,741 bytes wiped. Its comparison:
| 0.01.2: export against the protected image | functions | equal | no verdict (time) | different |
|---|---|---|---|---|
| entries that reach a folded construct or check | 103 | 99 | 4 | 0 |
| a random sample of other entries | 600 | 597 | 3 | 0 |
Started for real
An emulator answers for the code it is given. The Windows loader is the one reader of the file that the emulator never plays, so the 1.20 export was also started once as a process, from the game's folder, under its own file name.
The loader had no complaint. It mapped the image, applied the 175,973 relocations the export keeps, bound every import from the one import table that is left, and called the three TLS callbacks, which in this file are plain jumps into kept code. Forty-four modules were in the process and the C runtime was running the program's static initialisers when, a second after the start, the process tried to execute address 0.
Windows kept a dump. The faulting thread is the main thread, rip is 0, rax is 0, and the stack reads, from the top: 0x3931DE in the game, the address after _initterm's indirect call, the middle of __scrt_common_main_seh, BaseThreadInitThunk. rbx still points at the slot of the initialiser table being worked on. It is entry 30 of 6,442, a function of 64 KB whose instructions are strung together by jumps through the protection's section, and the instruction before 0x3931DE is a call through a local variable:
3931D5 xor ecx, ecx
3931D7 call qword ptr [rsp+1EC8h]
3931DE nop
The variable held an address in private memory. Next to it the frame held CreateFileW, GetFileSize, VirtualAlloc, CloseHandle and LoadLibraryA from kernel32, and three more pointers into the same private block, at offsets 0x185A8, 0x1A4F8 and 0x1FF4C. Those look like the three arrays of an export directory. Exactly one DLL in System32 has its export tables at 0x185A8 and 0x1A4F8, and it is gdi32.dll. The call went to offset 0xA6E0 of the block, and gdi32's export at 0xA6E0 is GdiDllInitialize.
So this initialiser reads gdi32.dll from disk, lays it out as a second image of its own, and initialises that copy. To see it do so, the emulator needed something it had never had: the function begins by reading the process's module list, and the harness had none. Given one, with the real DLL files mapped as readable images whose code never runs, and with a call that lands on one of their exports answered like any other API call, the function runs, and it runs the same in both images:
1 CreateFileW L"C:\Windows\System32\GDI32.dll"
2 GetFileSize -> 2DC40h
3 VirtualAlloc 2DC40h, read-write the file
4 ReadFile
5 CloseHandle
6 VirtualAlloc 2B000h, executable the image
7โ20 LoadLibraryA once for each DLL the copy imports from
21 ResolveDelayLoadedAPI called from inside the copy
fetch from address 0, return address 3931DE on top of the stack
Twenty-one calls, the same text and the same answers in the protected image and in the export, no routine of the protection on the way, and the same end, which is the end the real process came to, address for address. The last call explains it. On the Windows this ran on, GdiDllInitialize is not a function body. It is a jmp through a delay-load slot, and the first use of such a slot asks Windows to resolve the import for the module that owns it. The owner here is an image Windows has never heard of, and for such an image it has no answer: asked the same question outside the game, for a private copy of the same DLL, Windows returns no address, where for the loaded gdi32.dll it returns the function. The stub jumps to what came back.
It is a failure of this build on a Windows six years younger than it, in bytes the export leaves exactly as shipped, and the protected file takes the same twenty-one steps to it. Windows' own crash history on the test machine has the shipped 1.20 executable, a retail 1.30 and an unprotected development build of the same game all ending in this fault. Getting past it is not the protection's business: an exception handler loaded into the process recognises this one fault, binds the copy's empty slots from the loaded gdi32, and returns TRUE to the initialiser. With that handler the shipped file shows its splash screen after ten seconds and its game window after fourteen.
The export, in the same folder under the same file name, did three other things, a different one from start to start.
One function, 256 ways through
The first was an int3, a wiped byte, executed inside static initialiser 39. The dump has the path: push rax ; ret with rax = 0x3DD537, and at that address
3DD532 90 66 2B 16 76 junk
3DD537 E9 8C C3 96 15 jmp 15D498C8
3DD53C 48 BD 28 78 ... junk
A disassembler that starts at 0x3DD532 reads nop ; sub dx,[rsi] ; jbe โฆ and never sees the jump. Nothing in the image names 0x3DD537 either: no lea, no pointer, no branch. The address is arithmetic. The function is a state machine, the protection has moved its blocks into its own section and left a five-byte jmp block at each block's old address, and the dispatcher computes which of those addresses to go to. A walk that follows branches finds the blocks the dispatcher's own direct branches reach and no others, and the export had wiped the rest as unreachable.
Why had the emulator never seen it? The initialiser opens like this:
159E7776 rdtsc
159E7778 shl rdx, 20h
159E777C or rax, rdx
159E777F movzx r8d, al
159E7783 imul eax, r8d, 1807h
159E778A cmp eax, 0C0380h ; a binary search on the product follows
It takes the low byte of the timestamp counter and picks one of up to 256 paths with it. Three real starts took three paths. The emulator's counter was pinned so that two images would see the same values, and its first read always ended in zero.
A computed jump cannot be followed, but it has to land on something, and what it lands on is still a jump the file spells out. So the rule became a search instead of a walk: in every byte that the walk did not read and no plan wipes, a jmp, call or jcc encoded at any offset whose target is a wiped byte keeps that target, and a second walk keeps what the target reaches. Three more crashes widened it. A layout guess had decoded junk straight over one such jump, so bytes that were only ever guessed at count as unread. One sat in a function the walk never entered at all. And a wiped five-byte jump in the game's own section turned out to be the target of fourteen blocks kept as shipped, which reach it with lea rbp,[rip+X] ; xchg [rsp],rbp ; ret, so that spelling is searched for too. Build 1.20 has 1,651 of these, and keeping their targets costs 150 KB of the 65 MB that was wiped.
The same five-byte jump was also named by a pointer: a relocated qword at 0xD42EE3, in the middle of the first TLS callback's function. Tables of block addresses sit next to the code that uses them, inside the code section, where a collection of roots that reads the data sections does not look. 799 relocated qwords in kept code pointed at wiped bytes.
A value has a before
The second ending was the strange one: exit code 0 after a second and a half, no fault, no window. Started in the emulator from its entry point, with the loader's TLS block, the three TLS callbacks and all 6,442 initialisers run in order, the export does the same thing, and the log shows where: WinMain makes no call at all. Its first lines, with the disguises taken off:
mov eax, [P]
add eax, [A]
xor eax, [B]
je skip ; equal: nothing to do
mov eax, [17EEB5BDh] ; a 1, stored between two instructions of the protection
mov [flag], eax
skip: mov eax, [flag]
test eax, eax
je return_zero
...
<a guard's construct> ; publishes P
P is one of the 40 dwords a guard publishes, and the first three instructions are one of the 53 checks. Everything said so far about checks holds here except the one thing that was assumed: that a check exists to be passed. The protected image, run from its entry point in the same emulator with its routines really executing, arrives at WinMain with P still holding the value it has in the file. The check fails, the failing branch sets the flag, the flag lets WinMain continue, and the very next construct is the guard that publishes P. From then on P is right, and the other check that reads it, one whose failing branch is the poisoned return, passes.
The order is deliberate. A check placed before its guard cannot pass in the shipped program, so its failing branch is ordinary control flow, and the program depends on it. Whoever supplies the guard's value in advance, by patching the dword or by answering the check, turns that branch off. There is no crash to investigate afterwards. WinMain returns zero, the C runtime runs the destructors, and the process ends politely.
So a published value is not a constant of the file. It has a before and an after, and the moment between them is the guard's construct running. Which routine publishes which dword can be read from the routines: each walk from a routine's entry that meets mov [rip+P], r32 on a published P names its publisher. In 1.20, 18 of the 40 are stored by a guard and two by the start-up. The removal changed to match. Only values that hold from the start are written into the file. A construct whose routine publishes is not a jump in disguise any more: it stays as shipped, nothing is rewritten to land past it, and the routine's replacement gains one instruction in front of its three stack writes,
mov dword ptr [rip+P], 671CF959h ; ten bytes, no register, no flag
so the value appears when the guard would have run. Checks that read such a value are left exactly as they are, both branches.
That last sentence was learned from the third ending. With the fold merely withdrawn after planning, the game drew its window and then reported Fatal Error โฆ 0xc0000005 itself. A snapshot of the process with the box still up has the faulting thread's context on its stack: a ret popping 0x19C8E1FCDFD36281, with a saved rflags and the alignment words 10h and 18h above it. That is the poisoned return of a check, the listing a few sections back, live. The check's guard had never run. Its construct follows another check, and that check's fold had been aimed at the end of the chain of jumps after it, which looked straight through the guard to its continuation. Deciding that a construct stays has to come before any jump target is computed.
A window
With the three rules in the planner the counts for 1.20 read differently, and more honestly:
| 1.20, final plans | |
|---|---|
| constructs with a routine inside, folded to a jump | 258 of 275 |
| left in place because their guard publishes a value | 17 |
| checks folded | 31 of 55 |
| checks left as shipped, both branches | 24 |
| disguised branches rewritten | 56,372 |
| branches and pointers found by search that kept a wiped target | 1,651 and 799 |
| routine replacement pieces kept | 618 |
| export against the protected image, guards really publishing | 395 of 411 functions equal, 16 out of time, 0 different |
The export was then started four more times. Each time: the splash screen at five seconds, the game's window at nine, a hundred threads and five gigabytes of working set a minute later, and the same name lookups the shipped file makes at that point, where both wait for a back end that was not running. The export gets there sooner than the shipped file does, by the four seconds the shipped file spends decrypting itself.
Seven hundred emulated function comparisons had found nothing wrong with the earlier export, and they were right about what they compared. What they could not see is what needs a history: code that only a computed jump reaches, chosen by a counter, and an order of events that spans a function boundary. An emulator run from the entry point sees both, as far as it gets, and now it gets as far as WinMain.
One list for functions and their strings
The same comparison, pointed at build 1.30.0, came back with a function whose two runs made the same calls, stopped at the same instruction, and left twelve pages of .rdata different. On the protected side those pages held text:
yaw movetype fov start_aim disabledeathorient tag selector action lod sequence ...
fixednode keepclaimednode doingambush cover_ambush combatmode goalradius ...
In the export they held noise, and so does the shipped file. Asked who wrote the first of those bytes, the emulator names an instruction inside a routine: mov [rdx], eax, the store at the bottom of a writer's loop.
That writer's list has nine ranges. Most of it is two things: 48,843 bytes of functions in the game's code, and 51,128 bytes of .rdata holding 2,797 names. The functions are the ones that use those names, each a long run of
lea rcx, [rip+name]
call register
mov [rip+slot], eax
So a protected range is not only code. The names travel with the functions that register them, under one key, and both are noise until a guard on the way to their first use runs the writer. A strings dump of the file does not show them. Neither does a search for code that refers to them, because the references are in the same ciphertext.
It needed the comparison to find, because the usual way of telling a decrypting writer from its twin had nothing to say about this one. The two are told apart by what their output looks like, and code that is one address load after another is mostly displacement bytes:
| bytes that are common in code | entropy, bits per byte | |
|---|---|---|
| what a decrypting writer usually produces | 58% and up | |
| this one: the functions | 34% | 5.7 |
| this one: the names | 35% | 4.2 |
| its twin, and noise in general | 20% | 8.0 |
One measure alone puts it halfway between code and noise. The two together do not: nothing that is still encrypted has two bits of slack per byte. Judged on both, this writer is the fourteenth decrypting one of 1.30.0, and the twelve pages are text in the export.
A check at the door
A function compared on its own is entered cold. Nothing of the program's life has happened before its first instruction, and for some functions that matters immediately, because behind their first jump is a guard's construct and then this:
mov ebx, [P]
add ebx, [A]
sub ebx, [B]
je ok ; zero: P holds what its guard publishes
and rbx, 3 ; otherwise, two bits of the wrong sum
mov rbx, [table+rbx*8] ; choose one of four qwords
xor rbx, [key]
add rbx, <address of a piece of the protection>
sub rbx, [K] ; K: the link-time address of another piece
push rbx
ret
The guard in front of it is not the one that publishes P. That one runs somewhere up the call chain, so a cold entry arrives with P as the file has it and takes the lower path. Decoded, the four table entries are link-time addresses in the game's code, one of them the first byte of a function. What the path does with them is a rebase by hand, address โ preferred base + actual base, done with two anchors that are not the same place. They are 0x466A27B apart, so every entry lands that far short of where it points:
| entry | decodes to | the ret goes to |
|---|---|---|
| 0 | 0x144F8E310 |
image + 0x924095, inside game code |
| 1 |
0x142253F80, a function |
38 MB below the image |
| 2 | 0x14505A090 |
image + 0x9EFE15, inside game code |
| 3 | 0x145086BA0 |
image + 0xA1C925, inside game code |
With the file's value the index is 3. 0xA1C925 is the middle of an instruction in an unrelated function. What decodes from there subtracts from al, shifts a byte in memory, stores rax on the stack, loads it back and reads through it: about fifteen instructions before the fault. It is the same family as the return that is off by a gigabyte, an address computed correctly except for one term, so that nothing in the crash looks like a decision.
Two things follow for anyone comparing a protected function with anything. A path that reads a published value before it was published and dies of it is not a path the program takes, so it says nothing, and it has to be recognised by what happened on it (the check ran, P was not the needed value, and nothing else happened before the fault), not by how it ended. The other kind of early check, the one in WinMain whose failing branch is the way forward, goes on into routines and calls, and there every difference counts. And the table is the failing path's alone: a program that works never reads it.
What a cold function cannot tell you
Four more differences turned up between protected functions and their cleaned twins that are properties of the method, each worth knowing because the protection is what makes them visible.
Some values only exist once the program has run. Of the 40 dwords the guards publish in 1.59.1, 22 can be written into a cleaned file ahead of time. The other 18 are stored by routines that a cleaned image keeps, at the moment those routines run, and the checks that read them are kept as they are. A function entered cold reaches such a check with nothing published, in both images, and both take the failing path. They do not fail alike: the protected image jumps through its table of wrong addresses, the cleaned one through a table that is no longer there, and each lands somewhere of its own. At 0x5C9EF70 the protected side's wrong address ran into a call to GetLastError before it faulted and the cleaned side faulted at once. One import call more on one side, on a path neither program can take. What identifies it is that both read the same unpublished value at the same check before anything else differed.
The protection opens and closes its own code. Pieces of the protection sit in the unused tail of data sections, and one of them was different after a run: 168 bytes at 0x5B6082E. The write trace shows two routines. One writes 168 bytes of readable instructions, code that restores registers. A later one writes noise over them again. That is a writer and its twin bracketing one use of a piece of protection code, the same pair that brackets an encrypted function, turned on the protection itself. A cleaned image has nobody left to close it.
Routines leave their registers on the stack. A routine saves what it touches below the caller's frame. When the game then builds a 16-byte structure on the stack and stores it whole, the two bytes of padding the compiler never initialised are whatever was there, and in the protected run that is what a routine left: here, two bytes of a saved rax. The stored structure differs in two bytes that mean nothing. Fill the stack below the function with another value, run the cleaned side again, and exactly those bytes change with it.
The protection is 90 megabytes of plausible neighbours. An exception funclet entered as if it were a function passes filler to the C runtime's handle table as an index, and the read lands 23 MB away, in the protection's section. The protected side reads protection code there and the cleaned side reads what replaced it. Whether an address was meant can be asked directly: run the function again with different filler, and an address that was computed from it moves, while one the code names stays where it was.
What a memory dump holds
A dump of the running game is the other way to get this code out from under its protection, so it is worth holding one against the cleaned file, address for address. A dump made with Scylla is the image as Windows laid it out. Its header names the address the game was loaded at, here 0x7FF6D7950000, its pointers are relocated to that address, and anyone working from it counts from the start of the image. Read both by RVA and they agree on where everything is. For 1.51.2:
| at the same RVA in both | same | of |
|---|---|---|
| sections | 9 | 9 |
| function records in the exception directory | 127,967 | 127,967 |
| pointers in read-only data, each side's base subtracted | 78,973 | 78,973 |
| bytes of game code | 117,293,940 (97.6%) | 120,134,656 |
| bytes of game code with the dump moved by 16 bytes | 4.5% |
97.6% against 4.5% leaves no doubt about the addresses. One caution about the second row: compare the records by what they say, not by their position in the table. The exception directory also has records that lie over the protection's own code where it sits inside the game's code section, and a cleaned file that removes that code removes the record. In 1.59.1 that is one record of 126,534, fourteen bytes at 0x6CB966F in the middle of a routine's run of movupd register saves, and every record after slot 111,309 is one slot earlier than in the dump while describing exactly the same function.
The 2.4% is the interesting part. 1.57 MB of it is what the removal rewrote. The rest is code the removal never touched, and there it is the dump that differs from the decrypted image: 1.28 MB of noise at 7.7 bits per byte, scattered through functions that are otherwise readable.
Whose noise it is can be asked of the writers. Decode every writer the image has, the ones that decrypt and the ones that do not, and hold each one's output against the dump:
| of the 1,475,362 code bytes where the dump is not the decrypted image | bytes |
|---|---|
| exactly what one writer writes, over 115,632 ranges | 805,560 |
| exactly what three other writers write (4,510, 10 and 217 ranges) | 194,164 |
| exactly what smaller writers write | 4,734 |
| inside a writer's ranges, in another state | 224,770 |
| in no writer's ranges | 246,134 |
None of those writers decrypts anything. What each of them writes is noise, 8.00 bits per byte: they are the half of each pair that hides. The largest does not hide functions. Its 115,632 ranges average seven bytes, so it punches holes, a few bytes at a time, across the whole code section, and a dump taken while the game runs has every hole that had not been filled in again at that moment. On top of that come the functions no writer had opened yet: in this dump 17% of the bytes that writers decrypt are still in their first, encrypted state, and in a dump of 1.31.5 half of them are.
A second dump of the same build, made by another tool at another time, shows how much the moment matters. It agrees on every address too: the same 127,967 function records, the same pointers. But of the 29,329 pages of game code it holds 12,845, the rest being zeros where the dumper found nothing to read. In the pages it does hold, 99.4% of the bytes are the decrypted image's, the hole-punching writer has left almost no trace, and 99.2% of the bytes that writers decrypt on demand are still encrypted. That is the game seconds into its life: the outer layer off, almost nothing opened yet, nothing hidden again yet.
| the same build, two dumps | taken early | taken late |
|---|---|---|
| pages of game code present | 12,845 of 29,329 | 29,329 of 29,329 |
| bytes identical to the decrypted image, in the pages present | 99.6% | 98.8% |
| of the bytes writers decrypt, still encrypted | 99.2% | 17.5% |
| noise a hiding writer left | under 8,000 bytes | 1,004,458 bytes |
A dump of the last version, 1.69.0, caught an earlier moment than either. Its addresses agree with the cleaned file like all the others (9 of 9 sections, 125,316 of 125,316 function records, 79,539 of 79,540 pointers in read-only data), but only 66.9% of its code bytes do, and 55.8% of its function starts. Held against the shipped file instead, the reason is a single address. The game's code is decrypted up to 0x54DE87A, 88,987,771 of 121,384,448 bytes, and from 0x54DE87B on the dump is the shipped file's ciphertext, byte for byte, to the end of the section. The protection's section behind it is 99.1% the shipped file's, the rest being pointers the loader relocated. The dumper stopped the process 73.3% of the way through the bootstrap's pass. A keystream that has to be applied in order leaves exactly this picture when it is interrupted: everything before one byte, nothing after it.
So a dump is one moment of a program that keeps most of itself readable and a moving part of itself not. It has the right addresses and the wrong bytes in about a megabyte of places, and which places depends on when it was taken. The file, decrypted from its own tables, has no moment.
Every build, from its entry point
Functions compared one at a time are entered cold, and the last few sections are a list of what cold entry cannot say. The other way to compare two images is to start both where Windows starts them and let each run: the TLS callbacks with the arguments the loader gives them, the entry point, the C runtime, every static initialiser in table order with the state the earlier ones left, and on into main until the program does nothing but wait for threads a one-thread emulator will never give it. The run is cut into stretches, one per callback and per initialiser, and a stretch is the same in both images when it made the same API calls with the same text and got the same answers.
| build | TLS callbacks | stretches | the same in both | API calls up to the wait | routines entered, protected side |
|---|---|---|---|---|---|
| 1.07.0 | 1 | 6,294 | 6,294 | 2,681 | 14 |
| 1.20 | 3 | 6,450 | 6,450 | 4,855 | 20 |
| 1.30.0 | 3 | 6,539 | 6,539 | 4,942 | 24 |
| 1.69.0 | 6 | 6,950 | 6,950 | 5,785 | 154 |
Between the oldest and the newest of those four the game's start-up asks more of Windows each time, and the emulator had to answer in kind before either image got anywhere. 1.30.0 reads a system DLL from disk through NtCreateFile and NtReadFile instead of mapping it, and then executes the syscall instruction itself, with a service number it has just read out of that file; an emulator that steps over syscall hands back a null pointer as success, and both images die on it at initialiser 43. 1.69.0 loads the number and jumps to the syscall ; ret at the end of the stub inside the loaded ntdll. Answered from the same table the program reads, both go through.
The last thing was the protection's own. In 1.69.0 the path through main enters 154 of its routines before the first wait, against two dozen in the older builds, and one of them is a debugger probe:
call AddVectoredExceptionHandler ; (1, handler at 1AADBE1A)
1FD56508 mov [rbp+78h], rax ; the handle, for the removal afterwards
...
1FD5651D lea rax, [rbp+68h]
1FD56521 ud2
1FD56523 jmp 1C9A1730
A processor that executes ud2 raises an exception, and on a machine with no debugger Windows hands it to the handler registered a moment earlier. The handler sets a flag through the rax it finds in the context, moves the context's rip past the instruction, and asks to continue. So the emulator has to be Windows for one more thing: build the exception record and the context below the stack, enter the handler, and on "continue" load the registers from what the handler left. One exception is delivered on the way into 1.69.0's main, the handler answers it, and the protected image arrives at the same wait as the cleaned one after the same 5,785 calls.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.