<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: odindev</title>
    <description>The latest articles on DEV Community by odindev (@odindev).</description>
    <link>https://dev.to/odindev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2409564%2Fe8bc0dc7-ce26-4a9c-b059-750680e4f006.jpeg</url>
      <title>DEV Community: odindev</title>
      <link>https://dev.to/odindev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/odindev"/>
    <language>en</language>
    <item>
      <title>Inside Arxan, Appendix D: checking the result</title>
      <dc:creator>odindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 01:25:51 +0000</pubDate>
      <link>https://dev.to/odindev/inside-arxan-appendix-d-checking-the-result-1iao</link>
      <guid>https://dev.to/odindev/inside-arxan-appendix-d-checking-the-result-1iao</guid>
      <description>&lt;p&gt;&lt;em&gt;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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.20 without its protection
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;routine replacements&lt;/td&gt;
&lt;td&gt;613&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;start-up chains skipped at the ways in&lt;/td&gt;
&lt;td&gt;537&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;constructs with a routine inside, folded to a jump&lt;/td&gt;
&lt;td&gt;275 of 275&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;disguised branches rewritten&lt;/td&gt;
&lt;td&gt;56,302&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;left in place, in code that is kept as shipped&lt;/td&gt;
&lt;td&gt;107&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;checks folded&lt;/td&gt;
&lt;td&gt;55 of 55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes wiped&lt;/td&gt;
&lt;td&gt;64,981,132&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;references to the second import table left&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;function pieces that read as noise&lt;/td&gt;
&lt;td&gt;0 of 122,294&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Not one routine was left without a continuation, so no construct had to be kept for want of knowing where it goes.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;mov&lt;/code&gt; is separated from its &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;sub&lt;/code&gt; 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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The second was a function that stopped at an &lt;code&gt;int3&lt;/code&gt; in the export. A routine call returns through a thunk that restores the saved registers, and this thunk lives in the padding of &lt;code&gt;_RDATA&lt;/code&gt;, 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 &lt;code&gt;lea rbp ; xchg [rsp],rbp&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;With both corrected, the comparison of the new export reads:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20: export against the protected image&lt;/th&gt;
&lt;th&gt;functions&lt;/th&gt;
&lt;th&gt;equal&lt;/th&gt;
&lt;th&gt;of those, ran to return&lt;/th&gt;
&lt;th&gt;no verdict (time)&lt;/th&gt;
&lt;th&gt;different&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;entries that reach a folded construct or check&lt;/td&gt;
&lt;td&gt;116&lt;/td&gt;
&lt;td&gt;107&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a random sample of other entries&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;599&lt;/td&gt;
&lt;td&gt;92&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0.01.2: export against the protected image&lt;/th&gt;
&lt;th&gt;functions&lt;/th&gt;
&lt;th&gt;equal&lt;/th&gt;
&lt;th&gt;no verdict (time)&lt;/th&gt;
&lt;th&gt;different&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;entries that reach a folded construct or check&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;td&gt;99&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a random sample of other entries&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;597&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Started for real
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Windows kept a dump. The faulting thread is the main thread, &lt;code&gt;rip&lt;/code&gt; is 0, &lt;code&gt;rax&lt;/code&gt; is 0, and the stack reads, from the top: &lt;code&gt;0x3931DE&lt;/code&gt; in the game, the address after &lt;code&gt;_initterm&lt;/code&gt;'s indirect call, the middle of &lt;code&gt;__scrt_common_main_seh&lt;/code&gt;, &lt;code&gt;BaseThreadInitThunk&lt;/code&gt;. &lt;code&gt;rbx&lt;/code&gt; 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 &lt;code&gt;0x3931DE&lt;/code&gt; is a call through a local variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3931D5  xor  ecx, ecx
3931D7  call qword ptr [rsp+1EC8h]
3931DE  nop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable held an address in private memory. Next to it the frame held &lt;code&gt;CreateFileW&lt;/code&gt;, &lt;code&gt;GetFileSize&lt;/code&gt;, &lt;code&gt;VirtualAlloc&lt;/code&gt;, &lt;code&gt;CloseHandle&lt;/code&gt; and &lt;code&gt;LoadLibraryA&lt;/code&gt; from &lt;code&gt;kernel32&lt;/code&gt;, and three more pointers into the same private block, at offsets &lt;code&gt;0x185A8&lt;/code&gt;, &lt;code&gt;0x1A4F8&lt;/code&gt; and &lt;code&gt;0x1FF4C&lt;/code&gt;. Those look like the three arrays of an export directory. Exactly one DLL in &lt;code&gt;System32&lt;/code&gt; has its export tables at &lt;code&gt;0x185A8&lt;/code&gt; and &lt;code&gt;0x1A4F8&lt;/code&gt;, and it is &lt;code&gt;gdi32.dll&lt;/code&gt;. The call went to offset &lt;code&gt;0xA6E0&lt;/code&gt; of the block, and &lt;code&gt;gdi32&lt;/code&gt;'s export at &lt;code&gt;0xA6E0&lt;/code&gt; is &lt;code&gt;GdiDllInitialize&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So this initialiser reads &lt;code&gt;gdi32.dll&lt;/code&gt; 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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 1      CreateFileW   L"C:\Windows\System32\GDI32.dll"
 2      GetFileSize                          -&amp;gt; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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, &lt;code&gt;GdiDllInitialize&lt;/code&gt; is not a function body. It is a &lt;code&gt;jmp&lt;/code&gt; 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 &lt;code&gt;gdi32.dll&lt;/code&gt; it returns the function. The stub jumps to what came back.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;gdi32&lt;/code&gt;, 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.&lt;/p&gt;

&lt;p&gt;The export, in the same folder under the same file name, did three other things, a different one from start to start.&lt;/p&gt;

&lt;h2&gt;
  
  
  One function, 256 ways through
&lt;/h2&gt;

&lt;p&gt;The first was an &lt;code&gt;int3&lt;/code&gt;, a wiped byte, executed inside static initialiser 39. The dump has the path: &lt;code&gt;push rax ; ret&lt;/code&gt; with &lt;code&gt;rax = 0x3DD537&lt;/code&gt;, and at that address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3DD532  90 66 2B 16 76        junk
3DD537  E9 8C C3 96 15        jmp 15D498C8
3DD53C  48 BD 28 78 ...       junk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A disassembler that starts at &lt;code&gt;0x3DD532&lt;/code&gt; reads &lt;code&gt;nop ; sub dx,[rsi] ; jbe …&lt;/code&gt; and never sees the jump. Nothing in the image names &lt;code&gt;0x3DD537&lt;/code&gt; either: no &lt;code&gt;lea&lt;/code&gt;, 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 &lt;code&gt;jmp block&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;Why had the emulator never seen it? The initialiser opens like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;jmp&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;jcc&lt;/code&gt; 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 &lt;code&gt;lea rbp,[rip+X] ; xchg [rsp],rbp ; ret&lt;/code&gt;, 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.&lt;/p&gt;

&lt;p&gt;The same five-byte jump was also named by a pointer: a relocated qword at &lt;code&gt;0xD42EE3&lt;/code&gt;, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A value has a before
&lt;/h2&gt;

&lt;p&gt;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: &lt;code&gt;WinMain&lt;/code&gt; makes no call at all. Its first lines, with the disguises taken off:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          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
          ...
          &amp;lt;a guard's construct&amp;gt;        ; publishes P
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;P&lt;/code&gt; 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 &lt;code&gt;WinMain&lt;/code&gt; with &lt;code&gt;P&lt;/code&gt; still holding the value it has in the file. The check fails, the failing branch sets the flag, the flag lets &lt;code&gt;WinMain&lt;/code&gt; continue, and the very next construct is the guard that publishes &lt;code&gt;P&lt;/code&gt;. From then on &lt;code&gt;P&lt;/code&gt; is right, and the other check that reads it, one whose failing branch is the poisoned return, passes.&lt;/p&gt;

&lt;p&gt;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. &lt;code&gt;WinMain&lt;/code&gt; returns zero, the C runtime runs the destructors, and the process ends politely.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;mov [rip+P], r32&lt;/code&gt; on a published &lt;code&gt;P&lt;/code&gt; 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,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov  dword ptr [rip+P], 671CF959h      ; ten bytes, no register, no flag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so the value appears when the guard would have run. Checks that read such a value are left exactly as they are, both branches.&lt;/p&gt;

&lt;p&gt;That last sentence was learned from the third ending. With the fold merely withdrawn after planning, the game drew its window and then reported &lt;code&gt;Fatal Error … 0xc0000005&lt;/code&gt; itself. A snapshot of the process with the box still up has the faulting thread's context on its stack: a &lt;code&gt;ret&lt;/code&gt; popping &lt;code&gt;0x19C8E1FCDFD36281&lt;/code&gt;, with a saved &lt;code&gt;rflags&lt;/code&gt; and the alignment words &lt;code&gt;10h&lt;/code&gt; and &lt;code&gt;18h&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A window
&lt;/h2&gt;

&lt;p&gt;With the three rules in the planner the counts for 1.20 read differently, and more honestly:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20, final plans&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;constructs with a routine inside, folded to a jump&lt;/td&gt;
&lt;td&gt;258 of 275&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;left in place because their guard publishes a value&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;checks folded&lt;/td&gt;
&lt;td&gt;31 of 55&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;checks left as shipped, both branches&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;disguised branches rewritten&lt;/td&gt;
&lt;td&gt;56,372&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;branches and pointers found by search that kept a wiped target&lt;/td&gt;
&lt;td&gt;1,651 and 799&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;routine replacement pieces kept&lt;/td&gt;
&lt;td&gt;618&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;export against the protected image, guards really publishing&lt;/td&gt;
&lt;td&gt;395 of 411 functions equal, 16 out of time, 0 different&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;WinMain&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  One list for functions and their strings
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;.rdata&lt;/code&gt; different. On the protected side those pages held text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yaw  movetype  fov  start_aim  disabledeathorient  tag  selector  action  lod  sequence  ...
fixednode  keepclaimednode  doingambush  cover_ambush  combatmode  goalradius  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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: &lt;code&gt;mov [rdx], eax&lt;/code&gt;, the store at the bottom of a writer's loop.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;.rdata&lt;/code&gt; holding 2,797 names. The functions are the ones that use those names, each a long run of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  rcx, [rip+name]
call register
mov  [rip+slot], eax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;bytes that are common in code&lt;/th&gt;
&lt;th&gt;entropy, bits per byte&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;what a decrypting writer usually produces&lt;/td&gt;
&lt;td&gt;58% and up&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;this one: the functions&lt;/td&gt;
&lt;td&gt;34%&lt;/td&gt;
&lt;td&gt;5.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;this one: the names&lt;/td&gt;
&lt;td&gt;35%&lt;/td&gt;
&lt;td&gt;4.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;its twin, and noise in general&lt;/td&gt;
&lt;td&gt;20%&lt;/td&gt;
&lt;td&gt;8.0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A check at the door
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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, &amp;lt;address of a piece of the protection&amp;gt;
sub  rbx, [K]                ; K: the link-time address of another piece
push rbx
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard in front of it is not the one that publishes &lt;code&gt;P&lt;/code&gt;. That one runs somewhere up the call chain, so a cold entry arrives with &lt;code&gt;P&lt;/code&gt; 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, &lt;code&gt;address − preferred base + actual base&lt;/code&gt;, done with two anchors that are not the same place. They are &lt;code&gt;0x466A27B&lt;/code&gt; apart, so every entry lands that far short of where it points:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;entry&lt;/th&gt;
&lt;th&gt;decodes to&lt;/th&gt;
&lt;th&gt;the &lt;code&gt;ret&lt;/code&gt; goes to&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x144F8E310&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;image + &lt;code&gt;0x924095&lt;/code&gt;, inside game code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;0x142253F80&lt;/code&gt;, a function&lt;/td&gt;
&lt;td&gt;38 MB below the image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x14505A090&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;image + &lt;code&gt;0x9EFE15&lt;/code&gt;, inside game code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x145086BA0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;image + &lt;code&gt;0xA1C925&lt;/code&gt;, inside game code&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With the file's value the index is 3. &lt;code&gt;0xA1C925&lt;/code&gt; is the middle of an instruction in an unrelated function. What decodes from there subtracts from &lt;code&gt;al&lt;/code&gt;, shifts a byte in memory, stores &lt;code&gt;rax&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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, &lt;code&gt;P&lt;/code&gt; 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 &lt;code&gt;WinMain&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a cold function cannot tell you
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some values only exist once the program has run.&lt;/strong&gt; 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 &lt;code&gt;0x5C9EF70&lt;/code&gt; the protected side's wrong address ran into a call to &lt;code&gt;GetLastError&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The protection opens and closes its own code.&lt;/strong&gt; Pieces of the protection sit in the unused tail of data sections, and one of them was different after a run: 168 bytes at &lt;code&gt;0x5B6082E&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routines leave their registers on the stack.&lt;/strong&gt; 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 &lt;code&gt;rax&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The protection is 90 megabytes of plausible neighbours.&lt;/strong&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a memory dump holds
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;0x7FF6D7950000&lt;/code&gt;, 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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;at the same RVA in both&lt;/th&gt;
&lt;th&gt;same&lt;/th&gt;
&lt;th&gt;of&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;sections&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;function records in the exception directory&lt;/td&gt;
&lt;td&gt;127,967&lt;/td&gt;
&lt;td&gt;127,967&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pointers in read-only data, each side's base subtracted&lt;/td&gt;
&lt;td&gt;78,973&lt;/td&gt;
&lt;td&gt;78,973&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes of game code&lt;/td&gt;
&lt;td&gt;117,293,940 (97.6%)&lt;/td&gt;
&lt;td&gt;120,134,656&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes of game code with the dump moved by 16 bytes&lt;/td&gt;
&lt;td&gt;4.5%&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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 &lt;code&gt;0x6CB966F&lt;/code&gt; in the middle of a routine's run of &lt;code&gt;movupd&lt;/code&gt; register saves, and every record after slot 111,309 is one slot earlier than in the dump while describing exactly the same function.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;of the 1,475,362 code bytes where the dump is not the decrypted image&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;exactly what one writer writes, over 115,632 ranges&lt;/td&gt;
&lt;td&gt;805,560&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exactly what three other writers write (4,510, 10 and 217 ranges)&lt;/td&gt;
&lt;td&gt;194,164&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exactly what smaller writers write&lt;/td&gt;
&lt;td&gt;4,734&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;inside a writer's ranges, in another state&lt;/td&gt;
&lt;td&gt;224,770&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;in no writer's ranges&lt;/td&gt;
&lt;td&gt;246,134&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;the same build, two dumps&lt;/th&gt;
&lt;th&gt;taken early&lt;/th&gt;
&lt;th&gt;taken late&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;pages of game code present&lt;/td&gt;
&lt;td&gt;12,845 of 29,329&lt;/td&gt;
&lt;td&gt;29,329 of 29,329&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes identical to the decrypted image, in the pages present&lt;/td&gt;
&lt;td&gt;99.6%&lt;/td&gt;
&lt;td&gt;98.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;of the bytes writers decrypt, still encrypted&lt;/td&gt;
&lt;td&gt;99.2%&lt;/td&gt;
&lt;td&gt;17.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;noise a hiding writer left&lt;/td&gt;
&lt;td&gt;under 8,000 bytes&lt;/td&gt;
&lt;td&gt;1,004,458 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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 &lt;em&gt;shipped&lt;/em&gt; file instead, the reason is a single address. The game's code is decrypted up to &lt;code&gt;0x54DE87A&lt;/code&gt;, 88,987,771 of 121,384,448 bytes, and from &lt;code&gt;0x54DE87B&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every build, from its entry point
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;main&lt;/code&gt; 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.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;build&lt;/th&gt;
&lt;th&gt;TLS callbacks&lt;/th&gt;
&lt;th&gt;stretches&lt;/th&gt;
&lt;th&gt;the same in both&lt;/th&gt;
&lt;th&gt;API calls up to the wait&lt;/th&gt;
&lt;th&gt;routines entered, protected side&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1.07.0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;2,681&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.20&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;6,450&lt;/td&gt;
&lt;td&gt;6,450&lt;/td&gt;
&lt;td&gt;4,855&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.30.0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;4,942&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;5,785&lt;/td&gt;
&lt;td&gt;154&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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 &lt;code&gt;NtCreateFile&lt;/code&gt; and &lt;code&gt;NtReadFile&lt;/code&gt; instead of mapping it, and then executes the &lt;code&gt;syscall&lt;/code&gt; instruction itself, with a service number it has just read out of that file; an emulator that steps over &lt;code&gt;syscall&lt;/code&gt; 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 &lt;code&gt;syscall ; ret&lt;/code&gt; at the end of the stub inside the loaded &lt;code&gt;ntdll&lt;/code&gt;. Answered from the same table the program reads, both go through.&lt;/p&gt;

&lt;p&gt;The last thing was the protection's own. In 1.69.0 the path through &lt;code&gt;main&lt;/code&gt; enters 154 of its routines before the first wait, against two dozen in the older builds, and one of them is a debugger probe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A processor that executes &lt;code&gt;ud2&lt;/code&gt; 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 &lt;code&gt;rax&lt;/code&gt; it finds in the context, moves the context's &lt;code&gt;rip&lt;/code&gt; 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 &lt;code&gt;main&lt;/code&gt;, the handler answers it, and the protected image arrives at the same wait as the cleaned one after the same 5,785 calls.&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
      <category>gamedev</category>
      <category>windows</category>
    </item>
    <item>
      <title>Inside Arxan, Appendix C: reading the protection out of the file</title>
      <dc:creator>odindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 01:25:42 +0000</pubDate>
      <link>https://dev.to/odindev/inside-arxan-appendix-c-reading-the-protection-out-of-the-file-1dbh</link>
      <guid>https://dev.to/odindev/inside-arxan-appendix-c-reading-the-protection-out-of-the-file-1dbh</guid>
      <description>&lt;p&gt;&lt;em&gt;This is Appendix C 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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Everything in the main article was first measured on one build, 1.51.2, with an emulator and a memory dump. This article is about doing without both: finding the cipher's parameters, every range list, every writer's stash, key and encoding, every routine's three addresses and every published value in the shipped file alone. Two older builds, 1.20 from May 2020 and 0.01.2 from September 2019, are the test of whether a rule describes the protection or only the build it was found on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three parameters from the file
&lt;/h2&gt;

&lt;p&gt;On 1.51.2 the list was found by watching the cipher store its first few ranges. It can be found without watching anything, because the file says where some of its entries must be.&lt;/p&gt;

&lt;p&gt;The cipher steps around every relocated qword. So between two relocation sites that lie close together, with nothing else in the way, the list has to contain &lt;code&gt;gap 8, length (distance − 8)&lt;/code&gt;. Take six consecutive entries of the file's own relocation table, encode the six pairs as LEB128, and search the file for those bytes. A hit is somewhere in the middle of the list, and the cursor at that point is known: it is the RVA of the first of the six relocations.&lt;/p&gt;

&lt;p&gt;From the middle, the beginning. LEB128 reads backwards without ambiguity: a value ends at a byte with the top bit clear, and the top-bit-set bytes in front of it are its own. So the list can be walked back value by value, length then gap, taking each off the cursor. The list begins at the gap that equals the whole remaining cursor.&lt;/p&gt;

&lt;p&gt;The key and the starting block take one more observation. Both have to be readable before anything is decrypted, so the cipher must skip them, and unlike most skips, no relocation explains these. Mark every byte of the protection's section that a range covers or a relocation occupies, and what is left is a set of islands. There are one to three thousand, and most are a few bytes, or 167, or 177 bytes long. A key is 16 bytes and a starting block is 8.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;1.51.2&lt;/th&gt;
&lt;th&gt;1.20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ranges in the list&lt;/td&gt;
&lt;td&gt;1,324,188&lt;/td&gt;
&lt;td&gt;851,351&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes covered&lt;/td&gt;
&lt;td&gt;208,856,213&lt;/td&gt;
&lt;td&gt;139,805,245&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;islands in the protection's section&lt;/td&gt;
&lt;td&gt;1,097&lt;/td&gt;
&lt;td&gt;2,675&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;islands of exactly 16 bytes&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;islands of exactly 8 bytes&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pairs to try&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each pair is tried as (key, starting block) on the first 512 bytes of the stream, and the result is scored by the share of bytes that are common in compiled code: REX prefixes, &lt;code&gt;mov&lt;/code&gt;, &lt;code&gt;lea&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, a few ModRM bytes, &lt;code&gt;00&lt;/code&gt;, &lt;code&gt;CC&lt;/code&gt;. Fifty byte values, so noise scores about 20%.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;1.51.2&lt;/th&gt;
&lt;th&gt;1.20&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;best pair&lt;/td&gt;
&lt;td&gt;59.2%&lt;/td&gt;
&lt;td&gt;54.1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;second best&lt;/td&gt;
&lt;td&gt;22.7%&lt;/td&gt;
&lt;td&gt;22.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;time, whole search&lt;/td&gt;
&lt;td&gt;2.5 s&lt;/td&gt;
&lt;td&gt;12 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On 1.51.2 the winner is the key at &lt;code&gt;0x23BF3897&lt;/code&gt; and the block at &lt;code&gt;0x26D860DC&lt;/code&gt;, the same two addresses the emulator had given. On 1.20 it is &lt;code&gt;0x15AB4A72&lt;/code&gt; and &lt;code&gt;0x17639883&lt;/code&gt;, with the list at &lt;code&gt;0x17F67139&lt;/code&gt;, and nothing had given those before. The round count is simply tried: 64 works on both.&lt;/p&gt;

&lt;p&gt;The oldest build I have, 0.01.2, breaks this search and in doing so shows a shorter one. Its list is found, it has five islands of 16 bytes and three of 8, and no pair of them decrypts anything. The cipher is the same: the constant &lt;code&gt;9E3779B9&lt;/code&gt; occurs exactly once in the file, in code the protection has to leave readable, since it is the code that makes everything else readable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;13A0885F  mov  dword ptr [rbp+10h], 40h          ; 64 cycles
13A08866  mov  dword ptr [rbp+14h], 9E3779B9h
13A0886D  lea  rax, [rip+88134Dh]                ; 14289BC1, the key
13A08874  mov  [rbp], rax
13A08878  lea  rax, [rip-11CBE387h]              ; 01D4A4F8, the starting block
13A0887F  mov  [rbp+8], rax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key is one of the five islands. The starting block is not in the protection's section at all. It is in the game's, which is why no pair matched. And these instructions are the same, frame offsets included, in 1.03, 1.20 and 1.51.2: the cipher's set-up is plain compiled code that names its cycle count, its key and its starting block. So the search by position became the fallback. The decryptor reads these instructions first and accepts what they name when it turns the head of the stream into code. On 1.51.2 they name the same three values the emulator had given.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every list, read backwards
&lt;/h2&gt;

&lt;p&gt;On 1.51.2 each writer's range list was found by running the writer and searching for the encoding of what it wrote. The backward reading that found the bootstrap's list finds these too, and without running anything, because every list in the protection ends the same way: with a gap that carries the cursor from somewhere inside the image to &lt;code&gt;0xFFFFFFFF&lt;/code&gt;. A gap that large is always five LEB128 bytes, and the last of them is &lt;code&gt;0D&lt;/code&gt;, &lt;code&gt;0E&lt;/code&gt; or &lt;code&gt;0F&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So every five-byte value of that shape is a possible ending. The cursor it implies is known, and the walk back from it either arrives at a gap equal to the whole remaining cursor, which is a list's first value, or falls apart within a few steps.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;1.51.2, decrypted&lt;/th&gt;
&lt;th&gt;1.20, first layer off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;possible endings&lt;/td&gt;
&lt;td&gt;105,690&lt;/td&gt;
&lt;td&gt;57,091&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;lists&lt;/td&gt;
&lt;td&gt;310&lt;/td&gt;
&lt;td&gt;191&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;time&lt;/td&gt;
&lt;td&gt;16 s&lt;/td&gt;
&lt;td&gt;14 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On 1.51.2 the 310 include the bootstrap's list and the range list of every one of the 100 writers. That is the check: the writer table was built by emulation, one routine at a time, and the file gives up the same lists to a scan.&lt;/p&gt;

&lt;p&gt;On 1.20 nothing was known beforehand. The lists come in sets with identical counts, seven of 855,858 ranges, three of about 178,232, just as the largest lists of 1.51.2 do (six of 1,175,684 ranges, three of about 121,290). And for the pages that are still noise:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;copies&lt;/th&gt;
&lt;th&gt;ranges&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;th&gt;of them on unreadable pages of game code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;296,965&lt;/td&gt;
&lt;td&gt;3,706,464&lt;/td&gt;
&lt;td&gt;1,263,502&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;109,294&lt;/td&gt;
&lt;td&gt;109,077&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;26&lt;/td&gt;
&lt;td&gt;30,673&lt;/td&gt;
&lt;td&gt;30,141&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;142&lt;/td&gt;
&lt;td&gt;23,987&lt;/td&gt;
&lt;td&gt;19,621&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first row is one run of 381 pages of 1.20's game code that are still noise after the first layer. On 1.51.2 a range that stays unreadable has a writer, a twin that hides it again, and guards that hash it, each with a copy of the list, so several copies are what one would expect here. Every one of the 191 lists also has an instruction that names it: a scan of the code for RIP-relative fields that land on a list's first byte finds at least one for each.&lt;/p&gt;

&lt;h2&gt;
  
  
  A key from the stash
&lt;/h2&gt;

&lt;p&gt;A writer's row has three more columns: where its encoded copy is, what its key is, and which of the four encodings it uses. The first is next to the list. In front of two of the four copies of the big list sits the same compiled setup code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;148758A0  lea rax, [rip+42ABA9Ah]      ; 18B21341
148758CF  lea rdx, [rip+2686562h]      ; 16EFBE38, a table of RVAs
148758F5  lea rax, [rip+0B53F5Fh]      ; 153C985B, the list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x18B21341&lt;/code&gt; is the first byte of a run of 905 pages of noise, and 3,706,464 bytes are 904.9 pages. The other copy names another run of the same length the same way. That is a writer and its twin.&lt;/p&gt;

&lt;p&gt;The key is a single dword, and a dword can be exhausted. A key search tries all 2^32 keys against each encoding, keeps a key while the first bytes it decodes look like code, and ranks the survivors by how much of the stash does. Tried on 1.51.2, where the rows are known, it taught two things in about five seconds each.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For a &lt;code&gt;MulRol&lt;/code&gt; stash it prints the row's key, &lt;code&gt;309F06A8&lt;/code&gt;, with 76.7% of the decoded bytes common code bytes. The runner-up has 27.5%.&lt;/li&gt;
&lt;li&gt;For an &lt;code&gt;XorAuto&lt;/code&gt; stash it prints a wrong key, and reports that 1.4 billion keys, a third of all there are, decode the first 80 bytes to something that passes for code. The reason is in the arithmetic: &lt;code&gt;p = e ^ k ; k += p&lt;/code&gt;. Bit 0 of the next &lt;code&gt;k&lt;/code&gt; is bit 0 of &lt;code&gt;e&lt;/code&gt; whatever &lt;code&gt;k&lt;/code&gt; was. Bit 1 then depends only on bit 0, and so on up. The running key forgets where it started within 32 words. The key it printed and the row's real key give decodes that differ in six bytes, all among the first twelve. Decoded with key 0 and with key &lt;code&gt;12345678&lt;/code&gt;, the same stash is the same code after its first few hundred bytes. An &lt;code&gt;XorAuto&lt;/code&gt; stash is readable without its key, apart from its first lines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On 1.20 neither of the two 3.7 MB blocks decodes this way. They are not &lt;code&gt;XorAuto&lt;/code&gt; (any key would have shown it) and not &lt;code&gt;Xor&lt;/code&gt; or &lt;code&gt;Sub&lt;/code&gt; (a fixed key leaves each of the four byte positions with a favourite value, and these are flat). And the search finds no &lt;code&gt;MulRol&lt;/code&gt; key for them. Exhausting keys answers "what is this stash's key" only when the stash is already in its final form, and the answer to why these two are not was in the code all along.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three instructions per writer
&lt;/h2&gt;

&lt;p&gt;Every writer is an instance of one compiled routine. On 1.51.2, where all 100 rows of the table are known, the instruction that refers to each row's stash, list and key can be looked up, and the hundred look alike.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;what&lt;/th&gt;
&lt;th&gt;how the writer names it&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;stash&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lea rax,[rip+S] ; mov [rbp+68h],rax&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;list&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lea rax,[rip+L] ; mov [rbp+48h],rax ; mov eax,1 ; test eax,eax&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, &lt;code&gt;Xor&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;xor eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax&lt;/code&gt;, in the loop&lt;/td&gt;
&lt;td&gt;36&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, &lt;code&gt;Sub&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;add eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax&lt;/code&gt;, in the loop&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, &lt;code&gt;XorAuto&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;mov eax,[rip+K] ; mov [rbp+8Ch],eax&lt;/code&gt;, before the set-up&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, &lt;code&gt;MulRol&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;mov eax,[rip+K] ; mov [rbp+88h],eax ; mov eax,[rip+K] ; and eax,1Fh&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So the encoding does not have to be guessed, and neither does the key. A loop with an &lt;code&gt;imul&lt;/code&gt; in it is &lt;code&gt;MulRol&lt;/code&gt;. One that xors &lt;code&gt;eax&lt;/code&gt; with a frame slot is &lt;code&gt;XorAuto&lt;/code&gt;. The other two name their key in the loop itself. 1.20 has the same shapes at other frame offsets, the list in &lt;code&gt;[rbp+50h]&lt;/code&gt; and the stash in &lt;code&gt;[rbp+70h]&lt;/code&gt;, so the shapes are what to match, not the offsets.&lt;/p&gt;

&lt;p&gt;Matching them takes the same care as everything else in this image, because the protection cuts the routine into pieces. The stash is named eighteen instructions before the list, and in half the writers a joining jump falls somewhere in between. So the instructions are read as a stream: joining jumps followed, disguised ones resolved, conditional branches taken both ways when looking for the loop.&lt;/p&gt;

&lt;p&gt;Read that way, the older builds show one more address in front of the stash, and it is worth a look for what it says about the protection's habits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  rax, [rip+X]            ; some byte of the image
mov  [rbp+48h], rax
mov  rax, [rip+Q]            ; a qword: X's address as the linker assigned it
neg  rax
add  rax, [rbp+48h]          ; where X is, minus where it was meant to be
mov  [rbp+48h], rax
mov  rax, [rbp+48h]
add  rax, [rbp+68h]
mov  [rbp+68h], rax          ; added to an address the writer carries
...
lea  rax, [rip+STASH]
mov  [rbp+70h], rax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The qword at &lt;code&gt;Q&lt;/code&gt; has no relocation entry, so the loader leaves it alone, and the difference is how far the image was moved. The writer does not ask the loader for the image base. It measures the slide itself, from one of its own bytes. The bootstrap cipher's set-up on 0.01.2 does the same and then adds a second unrelocated qword, &lt;code&gt;0x140000000&lt;/code&gt;, to arrive at the image base without ever asking for it. For a tool this means the stash is the &lt;em&gt;last&lt;/em&gt; address stored into the frame before the list's, not the first: the first is &lt;code&gt;X&lt;/code&gt;, and it is a different slot.&lt;/p&gt;

&lt;p&gt;With stash, list, key and encoding in hand, each candidate row can simply be decoded, and what comes out says what the row is.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;a row's output on 1.51.2&lt;/th&gt;
&lt;th&gt;rows&lt;/th&gt;
&lt;th&gt;bits per byte&lt;/th&gt;
&lt;th&gt;common code bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;code or data, different from what is in place: a decryptor&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;td&gt;4.9 to 6.7&lt;/td&gt;
&lt;td&gt;58% to 77%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;code, identical to what is in place: a repair writer&lt;/td&gt;
&lt;td&gt;33&lt;/td&gt;
&lt;td&gt;5.6 to 6.8&lt;/td&gt;
&lt;td&gt;37% to 70%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;neither&lt;/td&gt;
&lt;td&gt;87&lt;/td&gt;
&lt;td&gt;7.1 to 8.0&lt;/td&gt;
&lt;td&gt;16% to 24%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third group is two things. Most of it is the twins that hide a range again, and a twin does not have to write noise: on 1.20 some write the code back weakly encoded, at 7.3 bits per byte, which is why the share of code bytes decides and the entropy does not. The rest are the writers that prepare another writer's stash, and those are found by trying: apply one where it stands, see whether the row it feeds now decodes to code, and undo it if not. That is also what was wrong with the two big blocks. The 3.7 MB stash at &lt;code&gt;0x18B21341&lt;/code&gt; is itself the single range of an &lt;code&gt;XorAuto&lt;/code&gt; writer's list. Once that one has run, the block decodes with the &lt;code&gt;Sub&lt;/code&gt; key its own loop names. The other block is the twin's.&lt;/p&gt;

&lt;p&gt;The check is build 1.51.2 again. The decryptor finds the bootstrap's parameters in the file, the reading above builds a writer table from the code, and the decryptor applies it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;table built from emulated writers&lt;/th&gt;
&lt;th&gt;table read from the file&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;writers that prepare a stash&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;writers that produce code&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes those change&lt;/td&gt;
&lt;td&gt;3,188,708&lt;/td&gt;
&lt;td&gt;3,188,708&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes that differ between the two images&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0 of 297,288,240&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The ten rows the second table lacks are repair writers, which change nothing on a fresh image. Everything this post decrypted on 1.51.2 with an emulator, a memory dump and a hand-kept table now comes out of the shipped file alone, byte for byte the same.&lt;/p&gt;

&lt;p&gt;1.20 has no known-good image to compare with, so its measure is what is left unreadable.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;range lists&lt;/td&gt;
&lt;td&gt;191&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;writer set-ups&lt;/td&gt;
&lt;td&gt;82&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;encodings: &lt;code&gt;Xor&lt;/code&gt;, &lt;code&gt;Sub&lt;/code&gt;, &lt;code&gt;XorAuto&lt;/code&gt;, &lt;code&gt;MulRol&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;18, 20, 17, 27&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rows kept (of them preparing another row's stash)&lt;/td&gt;
&lt;td&gt;25 (12)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rows dropped: noise, repair, too short to judge&lt;/td&gt;
&lt;td&gt;22, 31, 4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes the kept writers produce&lt;/td&gt;
&lt;td&gt;3,983,307 in 297,337 ranges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pages of game code above 7.2 bits: shipped, after layer 1, after both&lt;/td&gt;
&lt;td&gt;15,836, 458, 0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  A writer in &lt;code&gt;.pdata&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The oldest build, 0.01.2 from September 2019, went the same way up to a point. 150 lists, 63 set-ups, 20 rows kept, and then 233 pages of game code still unreadable, where 1.20 had none.&lt;/p&gt;

&lt;p&gt;All but one of those pages lie in the ranges of a single list: 195,980 ranges, 2,705,700 bytes, about a third of them game code and the rest the protection's own. The image holds that list three times.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;copy&lt;/th&gt;
&lt;th&gt;named by&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x150BA81A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;a writer's set-up: &lt;code&gt;Sub&lt;/code&gt;, stash &lt;code&gt;0x15CB65F5&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x13725257&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;a guard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x146E90AF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;nothing in the executable sections&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Sub&lt;/code&gt; writer decodes to noise, and the key search finds no key of any encoding that would change that: it is the twin. The guard is a guard. And the third copy, the one a decryptor would use, has no reader.&lt;/p&gt;

&lt;p&gt;One more row stood out. A &lt;code&gt;MulRol&lt;/code&gt; writer whose list is a single range of exactly 2,705,700 bytes, at &lt;code&gt;0x14A2C367&lt;/code&gt;. A single range of another writer's size is what a stash-preparing writer looks like, except that no writer named &lt;code&gt;0x14A2C367&lt;/code&gt; as its stash. So the same search was run from the other end: apply that row to a copy of the image, and exhaust the keys on what it leaves at &lt;code&gt;0x14A2C367&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stash 14A2C367, 16384 bytes
  Xor     key 1AD6DCB2  common code bytes  60.4%
  Sub     key 1C231DC1  common code bytes  45.0%
  MulRol  key 70592C61  common code bytes  21.6%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Laid over the ranges of the unread list, the &lt;code&gt;Xor&lt;/code&gt; decode leaves one page of game code above the line. The writer exists, then, with its list, its stash, its encoding and its key all accounted for, and no code. Nothing in either code section refers to &lt;code&gt;0x146E90AF&lt;/code&gt; or &lt;code&gt;0x14A2C367&lt;/code&gt;, RIP-relative, absolute, or as a bare RVA.&lt;/p&gt;

&lt;p&gt;The reference search had only looked where code is supposed to be. Run over every section, it finds one reference to each address, 0x55 bytes apart, which is the distance between the stash and the list in every other set-up of this build. They are in the last page of &lt;code&gt;.pdata&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nasm"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pdata&lt;/span&gt;   &lt;span class="mi"&gt;12A47000&lt;/span&gt;  &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="mi"&gt;140200&lt;/span&gt;   &lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;
   &lt;span class="mi"&gt;12B87175&lt;/span&gt;   &lt;span class="k"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rip&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;…&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;; 14A2C367, the stash&lt;/span&gt;
   &lt;span class="mi"&gt;12B871CA&lt;/span&gt;   &lt;span class="k"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;rip&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;…&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;; 146E90AF, the list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.pdata&lt;/code&gt; is the table of unwind records, and the linker pads it to the end of its last page. The protection used the padding. On 1.51.2 it does the same with the tail of &lt;code&gt;.rdata&lt;/code&gt;; here &lt;code&gt;_RDATA&lt;/code&gt; and &lt;code&gt;.pdata&lt;/code&gt; hold pieces too, and the piece in &lt;code&gt;.pdata&lt;/code&gt; is the set-up of the one writer that decrypts 2.7 MB. Three jumps from the code sections land in that page. That is too few to stand out: thirty-five other &lt;code&gt;.pdata&lt;/code&gt; pages each have some byte pattern in the code that reads as a jump into them, so a search for "data pages that code jumps into" walks straight past it.&lt;/p&gt;

&lt;p&gt;So the tool now treats every read-only section as a place code may be. Nothing is decoded there unless a reference or a jump leads to it, so the cost is nothing and nothing new is mistaken for code.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;0.01.2&lt;/th&gt;
&lt;th&gt;code sections only&lt;/th&gt;
&lt;th&gt;all read-only sections&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;writer set-ups&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rows kept&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes the kept writers produce&lt;/td&gt;
&lt;td&gt;72,506&lt;/td&gt;
&lt;td&gt;2,778,206&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pages of game code above 7.2 bits after both layers&lt;/td&gt;
&lt;td&gt;233&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The new row's set-up names its key at &lt;code&gt;0x154AA25F&lt;/code&gt;. The dword there is &lt;code&gt;1AD6DCB2&lt;/code&gt;: the key the exhaustive search had already read out of the stash, found a second time by following the code. And 1.51.2, run again with the wider reading, still comes out with 0 bytes different.&lt;/p&gt;

&lt;p&gt;The one page left is a caution about the measure and not about the protection. "Unreadable" has meant more than 7.2 bits per byte. One page of 0.01.2 and three of 1.03 stay above that after everything has been decrypted, and all four disassemble from their first byte to their last: they are hashing code, long runs of &lt;code&gt;mov rcx, 64-bit constant ; imul ; shr ; xor&lt;/code&gt;, and constants are as dense as noise. They have 40% to 42% common code bytes where an encrypted page has 19% to 22%, so from here on a page counts as unreadable when it is above 7.2 bits &lt;em&gt;and&lt;/em&gt; under 30% common code bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The way back is a constant
&lt;/h2&gt;

&lt;p&gt;A routine writes three qwords on its caller's stack. The emulator recorded their values for all 730, so the question can be put to the file directly: where else does &lt;code&gt;0x166D60D49&lt;/code&gt; occur?&lt;/p&gt;

&lt;p&gt;Once. Every one of the three addresses of every routine is in the image exactly once, as an absolute qword with a relocation entry, sitting alone between pieces of code. Nothing jumps to the address and no instruction computes it. One instruction loads the qword, and what follows it is the end of the routine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25F7BB85  mov rcx, [rip-0DE7068h]      ; the qword at 25194B24: the continuation
25F7BB8C  mov [rdx+rax*8], rcx
28D0EF2E  mov rax, [rip-73AD73h]       ; another qword: a small number, the slot
28D0EF35  mov rdx, [rbp+170h]          ; the caller's frame
28D0EF3C  mov rcx, [rip-27394568h]     ; the address of a ret
28D0EF43  mov [rdx+rax*8], rcx
28D0EF47  mov rax, [rip-41FC769h]
290F38DD  mov rdx, [rbp+170h]
290F38E4  mov rcx, [rip-379E0AFh]      ; the address of the other ret
290F38EB  mov [rdx+rax*8], rcx
290F38EF  mov rbx, [rbp+178h]
290F38F6  lea rsp, [rbp+190h]          ; epilogue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the three stores that are "everything the calling code needs" are four instructions each, the same four in every routine, and both of their inputs are constants: a slot number and an address. A routine has several exits, and each has its own copy of the stores, loading the same qwords.&lt;/p&gt;

&lt;p&gt;That makes the table readable. Take every routine entry from the chains, walk the routine's code from there (joining jumps followed, disguised ones resolved, both ways through every branch, calls stepped over) and collect the stores on the way. Against the 730 measured routines of 1.51.2 the reading gives the same three addresses 730 times. It also gives the one routine the measurement had never reached. The walk of all 734 routines takes a few seconds.&lt;/p&gt;

&lt;p&gt;There is a second reading to hold it against. Where the three slots lie is known from the chain: just above its register save, &lt;code&gt;118h&lt;/code&gt; plus eight bytes per register pushed. The routine's own slot number has to say the same thing, and it does, in all 731, by the same one qword of difference.&lt;/p&gt;

&lt;p&gt;1.20 was the test of whether this is the protection or the build, and at first it looked like the build. Not one store of that form in 613 routines. Grouping every indexed qword store in the image by the instructions before it shows why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov rax,[rbp+D] ; add rax,[rip+I] ; mov rdx,[rip+Q] ; mov [rbp+rax*8+d],rdx     1,395 times
mov rax,[rip+Q] ; mov rdx,[rbp+D] ; add rdx,[rip+I] ; mov [rbp+rdx*8+d],rax     2,016 times
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is the same store, written by an older version of the protection, and written twice. The registers are different, the slots are addressed from the routine's own frame, the alignment is added at run time where 1.51.2 folds it into a pointer, and the stores come at the start of the routine where 1.51.2 has them in the epilogue. The two oldest builds have the same pair. That there are two spellings in one build says the kinds of routine are separately compiled functions there. What all three share is the part worth matching: a qword store with a scaled index, whose value was just loaded from a qword holding an address in the image, and whose index was just loaded from, or had added to it, a qword holding a small number.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;0.01.2&lt;/th&gt;
&lt;th&gt;1.03&lt;/th&gt;
&lt;th&gt;1.20&lt;/th&gt;
&lt;th&gt;1.51.2&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;routines&lt;/td&gt;
&lt;td&gt;551&lt;/td&gt;
&lt;td&gt;566&lt;/td&gt;
&lt;td&gt;616&lt;/td&gt;
&lt;td&gt;734&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;called, and returning (the start-up three)&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;jumped to, and three addresses read&lt;/td&gt;
&lt;td&gt;548&lt;/td&gt;
&lt;td&gt;563&lt;/td&gt;
&lt;td&gt;613&lt;/td&gt;
&lt;td&gt;731&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;slot number agrees with the chain&lt;/td&gt;
&lt;td&gt;548&lt;/td&gt;
&lt;td&gt;563&lt;/td&gt;
&lt;td&gt;613&lt;/td&gt;
&lt;td&gt;731&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;spellings of the store&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For 1.20 the reading was then put to the routines themselves. Each of the 613 was entered in an emulator the way its chain enters it, on the decrypted image, and everything it wrote above its entry &lt;code&gt;rsp&lt;/code&gt; was recorded. 595 returned, and wrote exactly the three qwords the table has, at the offsets the table has. The other 18 were still running when their ninety seconds ran out. Each of them had by then written the continuation, the one the table has, and nothing else above its frame.&lt;/p&gt;

&lt;p&gt;The replacement for a routine follows from the table as before, 53 bytes in the routine's own first instructions. One thing a measured replacement had that a read one does not: 52 routines of 1.51.2 leave a loop counter in &lt;code&gt;r8&lt;/code&gt;, and the measured stub copied even that. The table read from the code does not know leftovers. It says instead that &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt; and &lt;code&gt;r8&lt;/code&gt; to &lt;code&gt;r11&lt;/code&gt; may come back changed, and a construct is folded only where the ones its chain did not save are dead afterwards. That is no loss in correctness, since a leftover in a register the chain chose not to save is by construction something the game does not read.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the guards were going to say
&lt;/h2&gt;

&lt;p&gt;The second measured input was the 40 values the guards publish. Those never needed a guard. A check reads the published dword and does arithmetic on it with two constants of its own:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov r, [rip+P]      ; the published value
add r, [rip+A]
xor r, [rip+B]      ; or sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then uses the result as a stack adjustment, or as the condition of a branch, in a way that only works when the result is zero. So the value the guard has to have published is &lt;code&gt;B − A&lt;/code&gt;, and it is sitting in the check. Collected that way, the 40 values of 1.51.2 come out as the same file, byte for byte, as the one made from the guards' emulated runs.&lt;/p&gt;

&lt;p&gt;The three instructions are not always neighbours. The protection cuts its code wherever it likes, and in a few checks of every build a joining jump follows the &lt;code&gt;mov&lt;/code&gt;. Read through those jumps, the count is the same in all four builds: 53 checks on 40 published values, in September 2019 and in January 2022.&lt;/p&gt;

&lt;p&gt;Fed with a table and values that were read, the tool that plans the removal produces, on 1.51.2, the plan it produced from the measured ones: 303,768 patches, every one identical, and one fold fewer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code in the padding of every section
&lt;/h2&gt;

&lt;p&gt;One more thing had to be learned from 1.20 before its plans could be trusted. The walk has to know where code may be, and on 1.51.2 that was the two code sections plus the tail of &lt;code&gt;.rdata&lt;/code&gt; and one page of &lt;code&gt;.pdata&lt;/code&gt;, found as "data pages that many jumps land in". 1.20 has one such page. It also has this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;section&lt;/th&gt;
&lt;th&gt;bytes after its last directory&lt;/th&gt;
&lt;th&gt;jumps landing there&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.rdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;18,568&lt;/td&gt;
&lt;td&gt;44&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.pdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;364&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;_RDATA&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(last page)&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.rsrc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;232&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.reloc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;(last page)&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.idata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A section's content ends where it ends, and alignment leaves the rest of the section unused. The protection puts a piece of itself in each of those leftovers, the writable import section included, two to five jumps' worth at a time. None of them would be noticed by counting busy pages. They are found by what they are: the bytes after the last thing the PE header says the section holds, when a jump lands in them. 1.51.2 turns out to have three more of these than the two that were known, in &lt;code&gt;_RDATA&lt;/code&gt;, &lt;code&gt;.reloc&lt;/code&gt; and &lt;code&gt;.idata&lt;/code&gt;, the smallest 22 bytes long.&lt;/p&gt;

&lt;p&gt;The padding holds data of the protection's as well, and one kind of it matters to a walk: relocated pointers to code. A walk that starts wherever the image says code begins takes every relocated code pointer in a data section as such a place, and for the game's own tables that is right. 1.20 has 22 of them in the protection's padding that point into the middle of routines. Followed, the first one led through ten routines and onto the first instruction of one, where a replacement was about to be written. A pointer the protection keeps in its own space, to code only the protection reaches, is the protection's, and the walk now leaves those alone, as it already left alone the exception records laid over protection code.&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
      <category>gamedev</category>
      <category>windows</category>
    </item>
    <item>
      <title>Inside Arxan, Appendix B: taking the protection out, step by step</title>
      <dc:creator>odindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 01:25:32 +0000</pubDate>
      <link>https://dev.to/odindev/inside-arxan-appendix-b-taking-the-protection-out-step-by-step-50m5</link>
      <guid>https://dev.to/odindev/inside-arxan-appendix-b-taking-the-protection-out-step-by-step-50m5</guid>
      <description>&lt;p&gt;&lt;em&gt;This is Appendix B 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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The main article says what a removal has to supply and why a routine cannot simply be skipped. This article goes through every rewrite in turn, with the count of each, what could go wrong with it, and the emulator run that shows it did not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking the protection out
&lt;/h2&gt;

&lt;p&gt;A decrypted file still contains all of Arxan. Every routine is there, every call chain, every disguised jump, the injected start-up path and the second import table. The last step is to remove them and export what remains.&lt;/p&gt;

&lt;p&gt;A word on vocabulary first. This is usually called devirtualizing, but this build has no bytecode virtual machine to undo. What it has is the list in the previous sentence, and each item needs a different kind of removal.&lt;/p&gt;

&lt;h3&gt;
  
  
  What removal has to mean
&lt;/h3&gt;

&lt;p&gt;Deleting the protection would break the program, because the program has come to depend on it in three ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Decrypted bytes.&lt;/strong&gt; Covered already: both layers are static.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The start-up path.&lt;/strong&gt; Two of the three start-up routines do work the program needs. One makes memory writable. One fills the second import table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Published state.&lt;/strong&gt; A checksum guard writes its digest into a slot inside a code section before it compares, and other code reads those slots later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything else is protection for its own sake: the comparisons, the debugger probes, the clocks, the repair writes.&lt;/p&gt;

&lt;p&gt;So removal is not deletion. For the start-up path, the program needs the function the thunk was hiding, and one import table instead of two. For published state, it needs the values the guards would have written, already in place. Only then can the routines stop running.&lt;/p&gt;

&lt;h3&gt;
  
  
  The entry point gets its call back
&lt;/h3&gt;

&lt;p&gt;The entry-layout walker already knows how to follow the entry point's &lt;code&gt;call&lt;/code&gt; into its thunk and through the three chains. The instruction after the last chain is a jump to the function the &lt;code&gt;call&lt;/code&gt; originally named. So the fix is to write that function's address back into the &lt;code&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;site&lt;/th&gt;
&lt;th&gt;shipped&lt;/th&gt;
&lt;th&gt;restored&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;entry point, &lt;code&gt;0x70D9228&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;call 0x1B07FCB&lt;/code&gt; (thunk, three routines)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;call 0x70DD5F0&lt;/code&gt; (&lt;code&gt;__security_init_cookie&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TLS callback 0, &lt;code&gt;0xA80D10&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;jmp 0x1A274F7&lt;/code&gt; (three routines, then the body)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;jmp 0x2776055E&lt;/code&gt; (the body)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Eight bytes. Nothing else in the file refers to either thunk.&lt;/p&gt;

&lt;h3&gt;
  
  
  One import table
&lt;/h3&gt;

&lt;p&gt;The third start-up routine existed to copy 667 pointers into the old import table, for the 1,251 instructions that still use it. There are two ways to make that routine unnecessary. One is to do its copy somewhere else. The other is to make those instructions stop needing it: add the distance between an old slot and its twin in the loader's table to each instruction's displacement.&lt;/p&gt;

&lt;p&gt;Finding the instructions takes a little care, because a four-byte pattern is not an instruction. Scanning both code sections for any 32-bit field that would land on a copied slot if it were a RIP-relative displacement gives 1,485 candidates. Each is kept only if some instruction start in the eleven bytes before it decodes to an instruction whose displacement field is exactly that field. 1,251 survive: 1,226 &lt;code&gt;call&lt;/code&gt;, 21 &lt;code&gt;jmp&lt;/code&gt; and 4 &lt;code&gt;mov&lt;/code&gt;, the same count the census of both tables gave earlier. The other 234 are windows that straddle two instructions.&lt;/p&gt;

&lt;p&gt;After those 1,251 patches nothing in the image refers to the old table, and the import directory already describes the one everything uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking the start
&lt;/h3&gt;

&lt;p&gt;This much can be proven end to end in the emulator, on the exported image.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Entry point.&lt;/strong&gt; Run from the entry point with hooks on the thunk and on all three start-up routines. The trace is &lt;code&gt;__security_init_cookie&lt;/code&gt;, then &lt;code&gt;__scrt_common_main_seh&lt;/code&gt;, and no hook fires. The API calls made on the way are &lt;code&gt;GetSystemTimeAsFileTime&lt;/code&gt;, &lt;code&gt;GetCurrentThreadId&lt;/code&gt;, &lt;code&gt;GetCurrentProcessId&lt;/code&gt; and &lt;code&gt;QueryPerformanceCounter&lt;/code&gt;, which are exactly the four calls the compiler's cookie initialisation makes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TLS callback 0.&lt;/strong&gt; Called with the loader's arguments, it arrives at its own first instruction with every argument, every non-volatile register and the stack pointer as passed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Imports.&lt;/strong&gt; Each of the 1,251 retargeted instructions is executed alone and compared with the function its original named through the old table. 1,251 of 1,251 go to the same DLL and function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  615 chains at the doors
&lt;/h3&gt;

&lt;p&gt;The entry point and the callback were two of 205 entries that carry the three start-up calls. The other 203 are the exported functions, and their chains are still there.&lt;/p&gt;

&lt;p&gt;With the image stored decrypted and one import table, none of the three routines has anything left to do. A chain around a routine that does nothing saves a set of registers and restores the same set. So each of the 615 chains is replaced by a five-byte &lt;code&gt;jmp&lt;/code&gt; from its first instruction to the instruction after its last.&lt;/p&gt;

&lt;p&gt;One thing does change. A chain aligns the stack with &lt;code&gt;test rsp, 0Fh&lt;/code&gt; and undoes it with &lt;code&gt;add rsp, [rsp+8]&lt;/code&gt;, and unless it saves the flags, it leaves different flags behind than it found. Skipping it leaves the old flags. Following the code after each of the 615 exits until every status flag is either overwritten or read shows that nothing reads them: all 615 exits have dead flags.&lt;/p&gt;

&lt;h3&gt;
  
  
  A routine that only returns
&lt;/h3&gt;

&lt;p&gt;The 730 routines spliced into game code look like the same case, and the obvious experiment is the same: write &lt;code&gt;ret&lt;/code&gt; at a routine's entry and run the code around it.&lt;/p&gt;

&lt;p&gt;Here is one such place, in a game function at &lt;code&gt;0x7013EBC&lt;/code&gt;, laid out with the idioms resolved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;07013EBC  push 25842E17 ; push 27FEF75C ; ret
27FEF75C  push 040F51A4 ; push 041011FF ; ret
041011FF  push 03FEB276 ; push 00B03667 ; ret
00B03667  ; save context: 11 registers, 16 XMM
          ; align stack
24F7F1E3  push 250A7A89 ; push 250326B2 ; ret      ; call the routine
250A7A89  ; restore context
250A7B27  ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three "calls" are nested around the chain before it starts, so three return addresses lie on the stack above the saved registers. After the chain restores its context it executes &lt;code&gt;ret&lt;/code&gt;, which takes the innermost of them, &lt;code&gt;0x3FEB276&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the routine replaced by &lt;code&gt;ret&lt;/code&gt;, that is where the emulator ends up, 98 instructions later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;03FEB276  mov   r9, [rsp+48h]
03FEB27B  add   r9, 0E5h
03FEB282  mov   [rsp+0B0h], r9
03FEB28A  movzx eax, byte ptr [r9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the middle of an unrelated function, entered with a stack that is not its own. The other two addresses are no better. &lt;code&gt;0x40F51A4&lt;/code&gt; is a &lt;code&gt;jmp&lt;/code&gt; in the middle of some other function, and &lt;code&gt;0x25842E17&lt;/code&gt; is inside one of the protection's routines.&lt;/p&gt;

&lt;p&gt;The same experiment on 60 chains spread over the 730 gives the same answer 60 times. None arrives anywhere the game function could continue.&lt;/p&gt;

&lt;h3&gt;
  
  
  The routine is the return
&lt;/h3&gt;

&lt;p&gt;So the real routine does something the chain depends on, and it is not in any register: the chain restores those. Running the chain with the real routine and comparing the stack afterwards shows what it is. Three qwords above the chain's saved registers have changed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;slot&lt;/th&gt;
&lt;th&gt;pushed by the construct&lt;/th&gt;
&lt;th&gt;written by the routine&lt;/th&gt;
&lt;th&gt;what is there&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;first&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x3FEB276&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x2335316&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ret&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;second&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x40F51A4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x416FDBF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ret&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;third&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x25842E17&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x26D01BE3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;mov dword ptr [rsp+58h], 8&lt;/code&gt;, then more of the game function&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The three addresses the construct pushes are decoys. The routine overwrites all three before it returns. The chain's own &lt;code&gt;ret&lt;/code&gt; then goes to a &lt;code&gt;ret&lt;/code&gt;, which goes to another &lt;code&gt;ret&lt;/code&gt;, which goes to the third address, and that one is the next piece of the game function.&lt;/p&gt;

&lt;p&gt;This holds for every one of the 730. The slots are always the three above the register save: at &lt;code&gt;0x118 + 8 × registers saved&lt;/code&gt; from the routine's own entry &lt;code&gt;rsp&lt;/code&gt;, eight more when the alignment step pushed &lt;code&gt;0x18&lt;/code&gt; instead of &lt;code&gt;0x10&lt;/code&gt;. The first two always hold the address of a &lt;code&gt;ret&lt;/code&gt;, in one of two spellings, &lt;code&gt;C3&lt;/code&gt; or &lt;code&gt;lea rsp,[rsp+8] ; jmp [rsp-8]&lt;/code&gt;. The third is the continuation.&lt;/p&gt;

&lt;p&gt;So a routine in game code is not called &lt;em&gt;on the way&lt;/em&gt; to somewhere. On its success path it &lt;strong&gt;is&lt;/strong&gt; the way: the place where the function continues exists only as three writes inside the routine, and nothing in the calling construct names it. A disassembler that resolves every idiom perfectly, and then follows the constants it found, walks into a decoy. So does a patch that removes the routine or makes it return early.&lt;/p&gt;

&lt;p&gt;It also puts the failed check in a different light. A guard that fails does not need a separate mechanism to send the program somewhere wrong. It only has to write something else into slots it was going to write anyway.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fifty-three bytes
&lt;/h3&gt;

&lt;p&gt;Since those three writes are everything the calling code needs, a routine can be replaced by a function that performs them and nothing else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  rdx, [rsp+8]            ; the caller's frame, as the routine addresses it
mov  rax, [rdx+8]            ; the alignment push: 10h or 18h
shr  rax, 3
add  rax, idx                ; first slot, in qwords
lea  rcx, [rip+T2] ; mov [rdx+rax*8+10h], rcx
lea  rcx, [rip+T1] ; mov [rdx+rax*8+8],  rcx
lea  rcx, [rip+T0] ; mov [rdx+rax*8],    rcx
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is 53 bytes, position independent, and fits in the routine's own first instructions. The three addresses are read out of one emulated run of each routine, by watching what it writes to the stack above its frame.&lt;/p&gt;

&lt;p&gt;The order of the three stores is not free. Some chains save only twelve registers, and in those &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt; and &lt;code&gt;rdx&lt;/code&gt; come back to the game holding whatever the routine last put in them: a slot index, the address of a &lt;code&gt;ret&lt;/code&gt;, a stack address. To be indistinguishable from the routine, the replacement has to leave the same three leftovers, and it does only when it writes the slots in this order. 52 routines also leave a loop counter in &lt;code&gt;r8&lt;/code&gt;, and their replacement ends with one more &lt;code&gt;mov&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Each of the 730 chains was then run twice in the emulator, from the instruction that calls the routine to the continuation: once with the real routine, once with the 53 bytes. The comparison covers &lt;code&gt;rip&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt;, all sixteen general registers, the flags, and the live stack. All 730 pairs end in the same state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Values the guards leave behind
&lt;/h3&gt;

&lt;p&gt;The second thing the program needs from the routines is state. A guard publishes its digest into a slot in code memory before it compares, and code elsewhere reads such slots later.&lt;/p&gt;

&lt;p&gt;Searching both code sections for instructions that read anything a routine publishes, and discarding the instructions that the routines themselves execute, leaves very little: 55 short sequences, all of one family.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub  rsp, 8
mov  ecx, [P]            ; a published value
add  ecx, [A]
xor  ecx, [B]
add  rsp, rcx            ; zero when P is what a clean run leaves there
add  rsp, [rsp+8]
pop  rcx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variants end in a conditional jump or in a &lt;code&gt;cmov&lt;/code&gt; between two addresses instead of the &lt;code&gt;add rsp&lt;/code&gt;. All of them compute one number from a published value and two constants, and all of them are harmless only when that number is zero. They read 40 distinct values: 16 digest slots and 24 other words in code memory.&lt;/p&gt;

&lt;p&gt;The value each one needs follows from its own constants, since &lt;code&gt;(P + A) ^ B = 0&lt;/code&gt; gives &lt;code&gt;P = B - A&lt;/code&gt;. That makes a useful cross-check. For 25 of the 40 values the publisher was found and run in the emulator, either a guard or the start-up decryptor, and in 25 of 25 what it writes is exactly the value the arithmetic asks for. For the other 15 no routine that publishes them was reached, and the arithmetic is the only source.&lt;/p&gt;

&lt;p&gt;So the published state can be stored in the file. Forty dwords, 160 bytes, written where the guards would have written them; five of them already hold the right value in the shipped image.&lt;/p&gt;

&lt;p&gt;With those in place every input of every check is a constant, and a check with constant inputs has one outcome. Each of the 55 was emulated on the image with the values stored, from two random states, to the point where the stack is back where it began. All 55 come out the same way both times, and each is replaced by a &lt;code&gt;jmp&lt;/code&gt; to that point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Folding a nest
&lt;/h3&gt;

&lt;p&gt;An image with 730 replacement functions in it runs without the protection, but it does not look like it. Every chain is still there, saving and restoring up to fourteen registers and sixteen XMM registers around 53 bytes that write three constants.&lt;/p&gt;

&lt;p&gt;Those 53 bytes are simple enough to reason about without running them. The layout tool's symbolic stack already resolves &lt;code&gt;push R ; push G ; ret&lt;/code&gt;. Give it one more rule, "this routine stores these three addresses in these three slots", and it can evaluate a whole construct from its first instruction: the nested pushes, the chain, the three writes, the chain's &lt;code&gt;ret&lt;/code&gt; into a &lt;code&gt;ret&lt;/code&gt;, into another &lt;code&gt;ret&lt;/code&gt;, into the continuation. For the construct shown earlier the result is short.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; shipped
07013EBC  push 25842E17 ; push 27FEF75C ; ret      ; and everything after it

; exported
07013EBC  jmp  26D01BE3
26D01BE3  mov  dword ptr [rsp+58h], 8
26D01BEB  mov  dword ptr [rsp+44h], 0FFFFFFF8h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three wrapped calls, a context save, a routine and a context restore fold into one direct jump to the next piece of the game function, with the stack where it started.&lt;/p&gt;

&lt;p&gt;Often the continuation of one routine is the first instruction of another construct, and the evaluation simply carries on through it. The 731 chains in game code (one more than the routine census counted: it sits in spare room at the end of &lt;code&gt;.rdata&lt;/code&gt;, a place the next section comes back to) group into 311 such nests, each reached at its outermost &lt;code&gt;push&lt;/code&gt; from the game's own code. A nest runs between one and five routines before it lets the function go on: 127 run one, 85 run two, and 60 run five in a row. 309 of the nests become a single &lt;code&gt;jmp&lt;/code&gt;. The other two are entered one level in, with one or two decoys already on the stack, and become &lt;code&gt;lea rsp,[rsp+8k] ; jmp T&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A jump is not quite the same thing as the nest it replaces, and the difference has to be shown harmless before the fold is allowed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Registers.&lt;/strong&gt; The routine leaves a slot index, the address of a &lt;code&gt;ret&lt;/code&gt; and a stack address in &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt; and &lt;code&gt;rdx&lt;/code&gt;, and a chain that does not save those three hands them to the game. After a fold they hold whatever they held before. That is the same only if nothing reads them before writing them. So from each fold target the code is followed on every path until each such register is written or read. A direct &lt;code&gt;call&lt;/code&gt; on the way does not settle it either way: the callee is entered, because a function that overwrites &lt;code&gt;rcx&lt;/code&gt; before using it does not care what was in it. One apparent reader turned out to be &lt;code&gt;nop dword ptr [rax+rax]&lt;/code&gt;, a filler instruction whose operand is never accessed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flags.&lt;/strong&gt; A chain that does not save the flags leaves the ones its alignment arithmetic produced. The same walk is done for each status flag.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All 311 nests pass both tests.&lt;/p&gt;

&lt;p&gt;Then each fold was checked against the real routine, not against the 53 bytes. The emulator runs the shipped construct from its first instruction, with the protection's routine executing in full, until it reaches the fold's target. The exported image only has to execute one &lt;code&gt;jmp&lt;/code&gt; to get there. The two are compared on &lt;code&gt;rip&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt; and every register that is live at the target. The final export has 309 of the folds (two nests go back to their shipped form, for a reason a later section gives). All 309 end equal.&lt;/p&gt;

&lt;p&gt;They did not all get there in one pass. In the first, 260 were equal and 49 gave nothing to compare: the real routine ended in an illegal instruction, a breakpoint or a bad read before it reached the target, or was still running after ninety seconds. An illegal instruction and a breakpoint are two of the six debugger probes, and the timestamp decides which probe a routine runs. Run again from a new random state with a different pinned timestamp, 47 of the 49 reached the target at the first try. The last two only needed time: one takes 167 seconds of emulation to get through its routines, the other 87.&lt;/p&gt;

&lt;h3&gt;
  
  
  Straightening the jumps
&lt;/h3&gt;

&lt;p&gt;The three idioms that had to be resolved to &lt;em&gt;read&lt;/em&gt; the entry point are everywhere, and for an exported image they have to be resolved and written back.&lt;/p&gt;

&lt;p&gt;That needs a definition of where code is. The walk that supplies it starts from everything the file itself calls an entry: the 127,967 records of the exception directory, 56,756 relocated pointers in the data sections that point into code, the entry point, the six TLS callbacks, the 203 exports, the unwind handlers and &lt;code&gt;__except&lt;/code&gt; filters. From there it follows every direct branch, resolves every idiom into the branch it stands for, and follows that too.&lt;/p&gt;

&lt;p&gt;Two things nearly hid a large part of the code from it.&lt;/p&gt;

&lt;p&gt;The first is that code is not only in the code sections. 143 branches from walked code land in the last pages of &lt;code&gt;.rdata&lt;/code&gt;, between the end of the data and the end of the section, on instructions that look like everything else the protection writes. There is a little more after the end of the exception table in &lt;code&gt;.pdata&lt;/code&gt;. Those pages are walked and rewritten like the rest.&lt;/p&gt;

&lt;p&gt;The second is the compiler's &lt;code&gt;switch&lt;/code&gt;. MSVC dispatches a dense &lt;code&gt;switch&lt;/code&gt; through a table of offsets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  rdx, [__ImageBase]
mov  ecx, [rdx+rax*4+table]
add  rcx, rdx
jmp  rcx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A walker recognises this and reads the table to find the cases. In this build the last instruction is not &lt;code&gt;jmp rcx&lt;/code&gt;. It is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov  [rsp-8], rcx
lea  rsp, [rsp-8]
lea  rsp, [rsp+8]
jmp  [rsp-8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which pushes &lt;code&gt;rcx&lt;/code&gt;, pops nothing, and jumps to what it pushed. A walk that treats &lt;code&gt;jmp [rsp-8]&lt;/code&gt; as an unknown destination stops there and never sees a single case of the &lt;code&gt;switch&lt;/code&gt;. Reading those four instructions as &lt;code&gt;jmp rcx&lt;/code&gt; brought 4,520 jump tables and every block behind them into the walk, about 25,000 idioms that had been invisible.&lt;/p&gt;

&lt;p&gt;In the code the walk reaches there are 184,254 disguised branches: 106,468 conditional, 77,685 unconditional, 99 calls and the 2 partial nests. Each is replaced at its first instruction by what it means.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;idiom&lt;/th&gt;
&lt;th&gt;replacement&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jmp X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;jmp X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;jcc B&lt;/code&gt;, else &lt;code&gt;A&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;jcc B ; jmp A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;call G&lt;/code&gt;, return to &lt;code&gt;R&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;call G ; jmp R&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The tail of the entry point is the smallest example. Twenty-nine bytes in six instructions become five bytes in one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; shipped
28DDA0BA  lea  rsp, [rsp-8]
28DDA0BF  mov  [rsp], rbp
28DDA0C3  lea  rbp, [rip-21D0101Ah]      ; 070D90B0
28DDA0CA  xchg [rsp], rbp
28DDA0CE  lea  rsp, [rsp+8]
28DDA0D3  jmp  [rsp-8]

; exported
28DDA0BA  jmp  27854BC6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exported target is not &lt;code&gt;0x70D90B0&lt;/code&gt;, because &lt;code&gt;0x70D90B0&lt;/code&gt; is itself only a &lt;code&gt;jmp&lt;/code&gt;: the protection moved the body of &lt;code&gt;__scrt_common_main_seh&lt;/code&gt; into its own section and left a jump behind. Every rewritten branch is followed through such joining jumps to the first instruction that does something.&lt;/p&gt;

&lt;p&gt;The replacement has to fit where the idiom was, and an idiom is rarely in one place. Its instructions are spread over several chunks joined by &lt;code&gt;jmp rel32&lt;/code&gt;, and the first chunk is often a single instruction and its joining jump: ten bytes, one short of a conditional branch and its fall-through. In 5,983 cases the first chunk gets a plain &lt;code&gt;jmp&lt;/code&gt; to a longer chunk of the same idiom, and the real branch is written there.&lt;/p&gt;

&lt;p&gt;It also has to be safe to overwrite. A chunk may be rewritten only if nothing else enters it: no branch from live code into its interior, no &lt;code&gt;lea&lt;/code&gt; or &lt;code&gt;movabs&lt;/code&gt; of an address inside it, and no exception record that starts in the middle of it. A branch to the idiom's first byte is fine, since whoever arrives there gets the replacement. 62 idioms fail this test and 7 have no chunk with eleven free bytes. Those 69 stay as they are. They still work, because nothing they need is removed.&lt;/p&gt;

&lt;p&gt;Whether a conditional idiom can become a &lt;code&gt;jcc&lt;/code&gt; at its &lt;em&gt;first&lt;/em&gt; instruction depends on the flags surviving from there to the &lt;code&gt;cmov&lt;/code&gt; that used to read them. They do. Everything the idiom executes in between is &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;mov&lt;/code&gt;, &lt;code&gt;lea&lt;/code&gt;, &lt;code&gt;movabs&lt;/code&gt; or &lt;code&gt;xchg&lt;/code&gt;, and none of those writes a flag.&lt;/p&gt;

&lt;p&gt;One piece of bookkeeping is easy to miss. Every &lt;code&gt;movabs&lt;/code&gt; in a conditional idiom carried a 64-bit address, so every one has an entry in the relocation table. If the instruction goes and its entry stays, the loader adds the load offset to eight bytes in the middle of whatever is there now. So each rewrite also deletes the relocation entries under it: 164,289 of the 422,110 the file started with.&lt;/p&gt;

&lt;p&gt;The proof is the same kind as before. A rewritten idiom is run in the emulator from its first instruction to its target, in the decrypted image and in the patched one, from random register states, both ways for a conditional. Idioms that are byte-for-byte the same shape share one proof, so the runs cover one idiom of every shape and a random sample of the rest: 1,605 shapes, 4,585 idioms, 42,717 runs. All end in the same state.&lt;/p&gt;

&lt;h3&gt;
  
  
  One instruction, written as two
&lt;/h3&gt;

&lt;p&gt;With the branches rewritten, &lt;code&gt;jmp [rsp-8]&lt;/code&gt; should have disappeared from live code. Nearly 7,000 were still there, and grouping them by the instructions in front showed why. They are not branches. They are compiler epilogues in which every stack instruction has been written as a pair.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;written as&lt;/th&gt;
&lt;th&gt;means&lt;/th&gt;
&lt;th&gt;in live code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;lea rsp,[rsp-8] ; mov [rsp],r&lt;/code&gt; or &lt;code&gt;mov [rsp-8],r ; lea rsp,[rsp-8]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;push r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;52,085&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;mov r,[rsp] ; lea rsp,[rsp+8]&lt;/code&gt; or &lt;code&gt;lea rsp,[rsp+8] ; mov r,[rsp-8]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;pop r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;54,751&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;lea rsp,[rsp+8] ; jmp [rsp-8]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ret&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;6,755&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;push r&lt;/code&gt;, then the &lt;code&gt;ret&lt;/code&gt; pair&lt;/td&gt;
&lt;td&gt;&lt;code&gt;jmp r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The two halves of a pair are often in different chunks, with a joining jump between them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; shipped                                  ; exported
0041AA1F  lea  rsp, [rsp-8]                0041AA1F  push rdi
0041AA24  jmp  290B0D5F                    0041AA20  jmp  290B0D63
290B0D5F  mov  [rsp], rdi
290B0D63  ...                              290B0D63  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The one-instruction form is written over the first half, the joining jump is moved up behind it and pointed past the second half, and the freed bytes become filler. A pair is only taken when both halves are instructions the walk reached, nothing else enters the second half or the bytes between, and neither half belongs to an idiom. 18 pairs fail that and are left.&lt;/p&gt;

&lt;p&gt;The proof runs the shipped pair and its replacement from the same random state and compares everything afterwards: one pair of every distinct byte shape and a random sample of the rest, 24,610 in all, with no difference.&lt;/p&gt;

&lt;p&gt;None of these change what the program does, and they are not the protection's logic. But they are the protection's handwriting, and close to 114,000 of them are most of the reason a function in this build is hard to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where the walk ends
&lt;/h3&gt;

&lt;p&gt;Every plan so far rests on one definition: code is what the walk reaches. An inventory of the game's &lt;code&gt;.text&lt;/code&gt; after the rewrite shows where that definition runs out. 6,996,602 bytes there are neither reached by the walk nor touched by a plan, and 3,645 &lt;code&gt;jmp [rsp-8]&lt;/code&gt; sequences are among them. Decoded from the right offsets, part of it is ordinary code.&lt;/p&gt;

&lt;p&gt;Grouping the 3,645 by the exception record around them gives the reason. 2,980 lie in 27 records that each have a &lt;code&gt;jmp&lt;/code&gt; through a register among their walked instructions, and 1,912 of those are in a single record of 2.6 MB. The file has eight exception records larger than 1.8 MB, and the largest covers 8,034,120 bytes. These belong to the game's anti-cheat, which carries an obfuscation of its own: its blocks end in a register jump whose destination is computed at run time. A walk follows direct branches, idioms and jump tables. It does not follow a computed jump, and resolving those is outside the scope of this post. So the walk enters such a function, takes the blocks it can name, and stops.&lt;/p&gt;

&lt;p&gt;Arxan did not stop there. It treated this code like any other. Its idioms run all through it, and pieces of it were moved into the protection's section and joined to the rest with the usual &lt;code&gt;jmp rel32&lt;/code&gt;. That is why the edge matters for removal:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Not reached" cannot be read as "dead". A wipe of everything the walk missed in the protection's section would take pieces of these functions with it.&lt;/li&gt;
&lt;li&gt;A rewrite is safe only when every way into the rewritten bytes is known. Here some of the ways in cannot be seen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the code behind the computed jumps has to be found without walking to it, and the file's layout offers a way. A compiler emits blocks back to back, so the byte after an instruction that never falls through (&lt;code&gt;jmp&lt;/code&gt;, &lt;code&gt;ret&lt;/code&gt;, &lt;code&gt;int3&lt;/code&gt;) is where the next block would begin. Every such byte that the walk did not reach, inside an exception record whose first instruction is live, is a candidate. One block is decoded from it, up to its terminator, and accepted when two things hold.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every instruction is one a compiler writes for user-mode code: no &lt;code&gt;in&lt;/code&gt; or &lt;code&gt;out&lt;/code&gt;, no far return, no 32-bit stack pointer, no segment override, no operand that points outside the image.&lt;/li&gt;
&lt;li&gt;The block ends at a terminator inside the unwalked run, or exactly on the start of an instruction the walk already has.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An accepted block ends somewhere, and the byte after its end is a new candidate, so the sweep repeats until a round adds nothing. That took 39 rounds. 944,961 candidate blocks were decoded and 345,249 accepted: 264,600 run onto an instruction the walk already had, and 80,649 end at a terminator. Of the gaps still left at the end, 370,555 decode across the boundary of a walked instruction, 191,896 are padding, 83,079 do not decode, and 57,354 contain an &lt;code&gt;in&lt;/code&gt; or an &lt;code&gt;out&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Passing those tests makes a block plausible. It does not make it code, and it says nothing about who jumps into its middle. So accepted blocks are not rewritten. They become the roots of a second walk with a different rule, &lt;strong&gt;keep&lt;/strong&gt;: everything it reaches stays byte for byte.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No patch is planned in it.&lt;/li&gt;
&lt;li&gt;Nothing it executes, reads or branches to is wiped.&lt;/li&gt;
&lt;li&gt;A patch that was already planned over a byte it walks is taken back.&lt;/li&gt;
&lt;li&gt;Where it calls one of the protection's routines, the routine keeps its 53-byte replacement, since the code around the call still depends on the three writes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The keep walk covers 8,869,443 bytes and 1,491,036 instructions: 2,272,981 bytes in the game's &lt;code&gt;.text&lt;/code&gt; and the rest in the protection's section. 797 planned patches gave way to it, which returned 641 disguised branches, 101 stack pairs and two of the 311 nests to their shipped form. So the export has 309 folded nests and 183,544 rewritten branches.&lt;/p&gt;

&lt;p&gt;One more check closes the gap between "accepted" and "everything". The sweep rejects a block that does not end cleanly even when all its instructions pass, and such a block could still be code. So after the plans are final, every block still unwalked in a live record that passes the instruction test is scanned for a branch or a RIP-relative reference that lands on a byte about to be wiped. 202,704 blocks were scanned and 120,376 pass the test. Where one of them pointed into a wipe, its target was kept and the plans made again, until none did. 43 such targets do not decode as instructions at all, and each of those keeps the fifteen bytes an instruction there could occupy.&lt;/p&gt;

&lt;p&gt;The price is stated in the keep walk's own numbers. Inside it, exactly as shipped, are 60,869 disguised branches, 59,790 stack pairs and 344 routine call sites. The protection's handwriting stays in the code the walk could not prove it understood.&lt;/p&gt;

&lt;h3&gt;
  
  
  The exported file
&lt;/h3&gt;

&lt;p&gt;A plan changes bytes inside sections. The file around them has to follow, and the exporter does that part itself after the last plan is applied.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relocations.&lt;/strong&gt; An entry whose eight bytes were patched or wiped is turned into a padding entry, which the loader skips. An entry whose bytes no longer hold an address inside the image goes the same way. What is left is checked: every remaining entry still points at a value inside the image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The exception directory.&lt;/strong&gt; A record whose whole range was wiped is removed and the table closed up. A record that begins in the middle of something rewritten but still covers live code is left alone. The shipped table is not clean to begin with: 433 of its 127,967 records begin inside an instruction or inside one of the protection's own sequences, and about 20,000 more begin on one of its chunks. The protection laid its code over space that still had function records and never tidied the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Imports.&lt;/strong&gt; The protection has an import descriptor of its own, a second &lt;code&gt;KERNEL32.DLL&lt;/code&gt; with two functions, &lt;code&gt;AddVectoredExceptionHandler&lt;/code&gt; and &lt;code&gt;RemoveVectoredExceptionHandler&lt;/code&gt;. Its debugger probes use them and nothing else does. The descriptor is removed once no instruction left in the file loads either slot. In this export that condition is not met, for a reason the last section gives, and the descriptor stays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Signature and checksum.&lt;/strong&gt; The certificate table is cleared, since the signature cannot match any more, and the PE checksum is recomputed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Section protections are a separate question. The first start-up routine used to make every section writable and executable, and it is gone. An option of the export writes the same thing into the section headers instead. Whether anything left in the image still needs it is not something an emulator can answer, because an emulator has no page protections unless they are modelled, so the option exists and the comparison below does not depend on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking the whole
&lt;/h3&gt;

&lt;p&gt;Each kind of rewrite was proven on its own, in isolation. That leaves the question of whether the pieces fit: whether a function in the exported image, run from its entry, does what the same function does in the protected one.&lt;/p&gt;

&lt;p&gt;The comparison needs a reference that still has its protection, running. That is the decrypted image in the emulator with every routine executing, the start-up state as a cold start leaves it, the second import table filled, and each checksum guard held on its success path by the method of the main article: the expected digest is substituted for the computed one at the moment of publication. The candidate is the exported file, run as it is.&lt;/p&gt;

&lt;p&gt;What a function &lt;em&gt;does&lt;/em&gt;, as far as the rest of the world can tell, is the sequence of calls it makes to Windows and the state it leaves. So both sides are stopped at every import call and the function, its four register arguments, the stack pointer and the eight non-volatile registers are recorded. Calls the reference makes while one of the protection's routines is on its stack are left out, because the candidate has no routines. The two sequences must be equal. When both runs end the same way, the return value, the non-volatile registers, every changed page of every data section, the argument buffers and the heap must be equal too.&lt;/p&gt;

&lt;p&gt;Four details of the reference turned out to say something about the protection.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A path that starts in the middle of the program's life meets checks before guards.&lt;/strong&gt; A check adds &lt;code&gt;(P + A) ^ B&lt;/code&gt; to the stack pointer. If the guard that publishes &lt;code&gt;P&lt;/code&gt; has not run on this path, &lt;code&gt;P&lt;/code&gt; still has its at-rest value and the reference walks off into an address like &lt;code&gt;0xE094DA97CC&lt;/code&gt;. In the real program the guard ran long before. So the reference gets the same 40 published values the export has.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A routine has returned when its chain's return address executes, not when the stack says so.&lt;/strong&gt; The game can be in a deeper frame than the routine's was by the time it next calls Windows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An exception record is not always a function.&lt;/strong&gt; Some records begin three bytes into a &lt;code&gt;mov&lt;/code&gt;, or at the third instruction of a disguised jump. Entered there, the shipped code and the export differ, and nothing can enter there. Only records whose first byte the walk reached as the start of an instruction count as entries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A probe's random number has to be pinned where it is made.&lt;/strong&gt; The same function, run alone in the reference six times, returned three times and stopped at an &lt;code&gt;int 2Dh&lt;/code&gt; probe the other three, with every &lt;code&gt;0F 31&lt;/code&gt; in the image pinned. Two instruction traces of it are identical for 568 instructions and differ at the next one, which is the probe bundle assembling its &lt;code&gt;rdtsc&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  25854E37  lea  rdx, [rip+11D1F23h]       ; 26A26D61
  25854E3E  mov  eax, [rip-23C3D778h]
  25854E44  xor  eax, [rip-23CE1C47h]      ; eax = 310F9090
  25854E4A  mov  [rdx], eax
  25854E4C  jmp  26A26D61
  26A26D61  nop ; nop ; rdtsc              ; at rest: lea rdx, [rsp]
  26A26D65  jmp  273F0874
  273F0874  mov  [rbp+130h], eax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the file those four bytes are a harmless &lt;code&gt;lea&lt;/code&gt;, so a scan of the image for the opcode does not find them, and the timestamp that picks the probe came from the host. The reference now watches writes into code for the opcode and pins those as well. Two of the six probes raise an exception that this emulator does not deliver. A function whose reference ends at one of them is run again with the next of a fixed list of timestamp values, on both sides.&lt;/p&gt;

&lt;p&gt;Pinning them had one more consequence. The game has a timer base in &lt;code&gt;.data&lt;/code&gt;, set by the first caller from an &lt;code&gt;rdtsc&lt;/code&gt; of its own. Once the protection's reads moved the same counter, two functions ended with a different word there: the reference's routines had used up one or two ticks before the game looked, and the export has no routines. So reads made while a routine is on the stack draw from a counter of their own.&lt;/p&gt;

&lt;p&gt;One function shows what the filter on routine calls hides. At &lt;code&gt;0x5FB33E0&lt;/code&gt; both sides make one call of their own, &lt;code&gt;GetLastError&lt;/code&gt;. The reference makes seven more while routines are on its stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LoadLibraryA  GetProcAddress  GetCurrentProcess  NtQueryInformationProcess
FreeLibrary   AddVectoredExceptionHandler        CloseHandle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is one of the debugger-probe bundles, running in the middle of a game function. The export makes none of those calls and ends in the same state.&lt;/p&gt;

&lt;p&gt;The results on the final export:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;functions run&lt;/th&gt;
&lt;th&gt;how many&lt;/th&gt;
&lt;th&gt;equal&lt;/th&gt;
&lt;th&gt;no verdict&lt;/th&gt;
&lt;th&gt;different&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;the entry path&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;every function that reaches a routine call site, a folded nest or a folded check&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;td&gt;161&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a random sample of function entries&lt;/td&gt;
&lt;td&gt;3,000&lt;/td&gt;
&lt;td&gt;2,987&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a second sample, with arguments full of pointers&lt;/td&gt;
&lt;td&gt;3,000&lt;/td&gt;
&lt;td&gt;2,940&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every "no verdict" is the reference running out of time, 20 seconds for the first group and 6 for the samples: a routine in the middle of hashing megabytes of code is slow in an emulator. In the 170 targeted functions the reference ran 498 routine calls that the export does without, and seven of those functions needed a second timestamp value to get past a probe. 1,111 of the 6,089 equal runs went all the way to the function's return. The rest stopped at the same instruction in the same state on both sides, usually at a pointer the invented arguments could not satisfy.&lt;/p&gt;

&lt;p&gt;That last sentence is the limit of the whole comparison. An emulator with invented arguments walks the first stretch of a function and seldom its depths, and it never follows a computed jump the way the running game would. What the table says is that no path it could take tells the two images apart.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is left
&lt;/h3&gt;

&lt;p&gt;The exported image is not free of the protection's bytes, and the numbers say how far it is.&lt;/p&gt;

&lt;p&gt;72,268,708 bytes are filled with &lt;code&gt;int3&lt;/code&gt;, in 236,561 ranges. The relocation table goes from 422,110 entries to 208,359. The protection's section is 103,949,312 bytes, and its entropy tells the story in three numbers: 7.96 bits per byte as shipped, 6.71 decrypted, 2.48 exported. 32,063,205 of its bytes still hold something. What those 32 MB are, by the first reason that keeps each byte:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;kept because&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;the walk reaches it as code&lt;/td&gt;
&lt;td&gt;16,905,826&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the keep walk reaches it&lt;/td&gt;
&lt;td&gt;6,613,731&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;it is a rewritten instruction&lt;/td&gt;
&lt;td&gt;5,835,709&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a live instruction reads it as data&lt;/td&gt;
&lt;td&gt;1,503,125&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;it is the relocation table, which the protection keeps in its own section&lt;/td&gt;
&lt;td&gt;941,946&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;only exception records that begin on protection code reach it&lt;/td&gt;
&lt;td&gt;114,493&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a routine writes there&lt;/td&gt;
&lt;td&gt;109,001&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;an unwalked block that looks like code points at it&lt;/td&gt;
&lt;td&gt;38,788&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;other&lt;/td&gt;
&lt;td&gt;586&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first row is the largest and the least certain. Part of it is game code that the protection moved, like the body of &lt;code&gt;__scrt_common_main_seh&lt;/code&gt;. But a walk that follows every direct branch also follows the ones that are never taken, and some of what it reaches cannot be code. Among 27 million instruction starts in the walked set, 31,765 have a RIP-relative operand that points outside the image, 5,019 branch directly into bytes that are not code, and 87,683 overlap another instruction. Walking again and refusing to pass any instruction of the first two kinds loses 941,177 bytes of that first row, 5.6%. Refusing overlapping starts as well loses 8.0%. The rest shows none of the three symptoms, which is not proof that it is real.&lt;/p&gt;

&lt;p&gt;The second row has a similar caveat from the other side. The direct-flow closure of the 734 routine entries is 3.7 MB, and 45% of what the keep walk holds in the protection's section lies inside it. So the keep walk does not only hold the anti-cheat's blocks. It also holds routine bodies that those blocks, or the bytes taken for them, lead into, and it cannot tell which of the two it is looking at.&lt;/p&gt;

&lt;p&gt;What a reader of the file will still meet:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;in code the walk reaches&lt;/th&gt;
&lt;th&gt;in the whole file&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;routine call sites&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;426&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jmp [rsp-8]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;598&lt;/td&gt;
&lt;td&gt;55,304&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;instructions that load the protection's two import slots&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;1,308&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4 KB code pages above 7.2 bits per byte&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;97&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The one call site in walked code, &lt;code&gt;0x28630C77&lt;/code&gt;, hangs on an exception record at &lt;code&gt;0x708AE0D&lt;/code&gt; whose first instructions are the protection's own. Most of the 598 are tails of the branches the keep rule returned to their shipped form. Nearly all of the rest of the right-hand column is in code the keep walk holds.&lt;/p&gt;

&lt;p&gt;The third row decides one thing about the file. Because 1,308 instructions still load &lt;code&gt;AddVectoredExceptionHandler&lt;/code&gt; or &lt;code&gt;RemoveVectoredExceptionHandler&lt;/code&gt; through the protection's second &lt;code&gt;KERNEL32.DLL&lt;/code&gt; descriptor, the descriptor stays. Those instructions are the debugger probes. Whether any path the game takes still reaches one of them is a question about computed jumps, and the walk has no answer.&lt;/p&gt;

&lt;p&gt;Three assumptions are not proven by anything here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data extents.&lt;/strong&gt; A byte range is kept as data when a live instruction refers to it. A read with a stated operand size keeps that many bytes. An address that is taken keeps up to the next address taken, the next code byte, or 64 KB. About 8.3 MB of the protection's section is wiped on the strength of that rule and would have been kept by a looser one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Targets that are not instructions.&lt;/strong&gt; 43 direct branches go to bytes that do not decode, 41 of them from walked code. Fifteen bytes are kept at each, on the reading that such a branch is never taken.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Page protections.&lt;/strong&gt; The emulator has none. 378 instructions in walked code store to an address inside a code section. Some are plainly misdecoded bytes, but I did not establish that for all of them, and unless the export marks them writable those sections are read-only in a real process.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
      <category>gamedev</category>
      <category>windows</category>
    </item>
    <item>
      <title>Inside Arxan, Appendix A: the file on disk, in full</title>
      <dc:creator>odindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 01:25:23 +0000</pubDate>
      <link>https://dev.to/odindev/inside-arxan-appendix-a-the-file-on-disk-in-full-3mhp</link>
      <guid>https://dev.to/odindev/inside-arxan-appendix-a-the-file-on-disk-in-full-3mhp</guid>
      <description>&lt;p&gt;&lt;em&gt;This is Appendix A 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.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The main article gives the shape of the file and of its two layers. This article is the whole account, question by question, in the order the questions came up: how a file whose code is ciphertext starts at all, the cipher and its list, what the protection did to the entry point, the functions that stay encrypted after start-up, and the strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  The file on disk
&lt;/h2&gt;

&lt;p&gt;Everything above describes a process that is already running. The file that produces that process is a different object. It is 297 MB on disk against 693 MB mapped, and almost none of its code can be read.&lt;/p&gt;

&lt;p&gt;This article starts from the shipped file and works out how it becomes the running image the main article describes, without ever running it. The method is the same as before, with one more step at the end:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;look at the bytes and ask the smallest question they raise;&lt;/li&gt;
&lt;li&gt;answer it by running Arxan's own code in the emulator;&lt;/li&gt;
&lt;li&gt;replace the emulation with a plain re-implementation, and check that against the memory dump.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last step is what turns an observation into a tool. Each section below is one question, in the order the questions came up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two images of one build
&lt;/h3&gt;

&lt;p&gt;The first step is the obvious one. Map the shipped &lt;code&gt;modernwarfare.exe&lt;/code&gt; and the memory dump by RVA and compare them section by section.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;section&lt;/th&gt;
&lt;th&gt;size&lt;/th&gt;
&lt;th&gt;entropy on disk&lt;/th&gt;
&lt;th&gt;equal to the running image&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (game)&lt;/td&gt;
&lt;td&gt;120.1 MB&lt;/td&gt;
&lt;td&gt;7.991&lt;/td&gt;
&lt;td&gt;3.67%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.rdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5.3 MB&lt;/td&gt;
&lt;td&gt;5.936&lt;/td&gt;
&lt;td&gt;99.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.data&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;460.8 MB (mostly zero-fill)&lt;/td&gt;
&lt;td&gt;0.260&lt;/td&gt;
&lt;td&gt;n/a, written at run time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.pdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1.5 MB&lt;/td&gt;
&lt;td&gt;7.198&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.rsrc&lt;/code&gt;, &lt;code&gt;.reloc&lt;/code&gt;, &lt;code&gt;_RDATA&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;1.8 MB&lt;/td&gt;
&lt;td&gt;5.5 to 7.5&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (protection)&lt;/td&gt;
&lt;td&gt;103.9 MB&lt;/td&gt;
&lt;td&gt;7.959&lt;/td&gt;
&lt;td&gt;11.08%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Eight bits per byte is what random data measures. Both code sections are ciphertext, and nothing else is.&lt;/p&gt;

&lt;p&gt;That second half of the sentence matters as much as the first. The headers, the import directory, the relocation table, the TLS directory, the exception directory and the resources are all in the clear. Windows can map this file, relocate it, bind its imports, register its unwind data and call its TLS callbacks with no help from the protection. Only the code is locked, and the loader never needs to read code.&lt;/p&gt;

&lt;h3&gt;
  
  
  What has to be readable
&lt;/h3&gt;

&lt;p&gt;A file whose code is entirely ciphertext cannot start. So the next question is: what is the first instruction the process executes, and is it readable?&lt;/p&gt;

&lt;p&gt;The loader calls TLS callbacks first, then the entry point. On disk the entry point is nine plaintext bytes followed by noise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;070D9224  48 83 EC 28        sub  rsp, 28h
070D9228  E8 9E ED A2 FA     call 01B07FCB
070D922D  ...                ; ciphertext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TLS directory lists six callbacks. Five of them begin with bytes that decode to nothing. The first begins with a jump:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00A80D10  E9 E2 67 FA 00     jmp  01A274F7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both of those targets are readable, and they contain the same thing: fourteen register pushes, sixteen XMM saves, a stack alignment, and then the call stub every routine is entered through. The code that unlocks the file is not special. It is ordinary Arxan routines, reached through the ordinary stub, stored in plaintext.&lt;/p&gt;

&lt;p&gt;To see how much plaintext there is, compare the two images byte by byte and look for long runs that already agree. In the game's &lt;code&gt;.text&lt;/code&gt; there are 233 runs of 256 bytes or more, 901 KB in total. In the protection's section there are two large islands: 4.4 MB at &lt;code&gt;0x25A8CA90&lt;/code&gt;, and a 4.8 MB tail from &lt;code&gt;0x290FB97A&lt;/code&gt; that holds, among other things, the relocation table.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you relocate ciphertext?
&lt;/h3&gt;

&lt;p&gt;The image is built for address &lt;code&gt;0x140000000&lt;/code&gt; and is loaded somewhere else. The loader fixes that by adding the difference to every address stored in the image. The relocation table lists 422,110 such places. 34,282 are inside the game's code and 251,874 are inside the protection's.&lt;/p&gt;

&lt;p&gt;That is a problem for an encrypted file. The loader adds a number to eight bytes of &lt;em&gt;ciphertext&lt;/em&gt;, before any decryption has run. Decrypting afterwards would give garbage at every one of those places.&lt;/p&gt;

&lt;p&gt;Looking at the sites directly settles it. At each relocation site inside the code sections, the eight bytes on disk are a readable link-time pointer, and the bytes on either side are noise. The encryption steps around every relocation. So whatever decrypts this file must have a list of what to skip, and that list has at least 286,000 holes in it.&lt;/p&gt;

&lt;p&gt;The count for the protection's own section is worth a second look. A quarter of a million absolute addresses is a lot for code that is otherwise position-independent. Once the code is readable the reason is plain: 99.3% of those sites are the 64-bit immediate of a &lt;code&gt;movabs&lt;/code&gt;, the instruction one of the protection's jump idioms uses to name its targets. It appears further down this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cold-starting the file
&lt;/h3&gt;

&lt;p&gt;With two readable entry stubs, the file can be started in the emulator the way the loader would start it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Map the &lt;strong&gt;disk&lt;/strong&gt; file at its preferred base, section by section.&lt;/li&gt;
&lt;li&gt;Point every import at a stub that logs the call and returns something plausible.&lt;/li&gt;
&lt;li&gt;Call TLS callback 0 with the loader's arguments: the image base, &lt;code&gt;DLL_PROCESS_ATTACH&lt;/code&gt;, and zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first thing it does is ask Windows for access. &lt;code&gt;GetSystemInfo&lt;/code&gt; once, then a &lt;code&gt;VirtualQuery&lt;/code&gt; and a &lt;code&gt;VirtualProtect&lt;/code&gt; for the headers and for each of the nine sections, ten pairs in all. Every one of them asks for &lt;code&gt;PAGE_EXECUTE_READWRITE&lt;/code&gt;. That is 21 API calls, and there are no others for the rest of the callback.&lt;/p&gt;

&lt;p&gt;Then the bytes start changing, front to back, at about 13 MB per minute of emulation. Sampling the instruction pointer shows the emulator in two small loops: a byte-by-byte copy at &lt;code&gt;0x2905BDAE&lt;/code&gt;, and a longer loop at &lt;code&gt;0x28584D29&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The cipher
&lt;/h3&gt;

&lt;p&gt;The loop at &lt;code&gt;0x28584D29&lt;/code&gt; is plaintext on disk, so it can simply be read.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov eax,[rbp+1Ch] ; shl eax,4
mov edx,[rbp+1Ch] ; shr edx,5 ; xor eax,edx ; add eax,[rbp+1Ch]     ; ((v1&amp;lt;&amp;lt;4) ^ (v1&amp;gt;&amp;gt;5)) + v1
mov edx,[rbp+20h] ; and edx,3 ; mov rcx,[rbp] ; mov edx,[rcx+rdx*4] ; key[sum &amp;amp; 3]
add edx,[rbp+20h] ; xor eax,edx ; add eax,[rbp+18h] ; mov [rbp+18h],eax  ; v0 += ...
mov eax,[rbp+14h] ; add eax,[rbp+20h] ; mov [rbp+20h],eax           ; sum += delta
...                                    shr edx,0Bh ; and edx,3      ; key[(sum&amp;gt;&amp;gt;11) &amp;amp; 3]
add dword [rbp+24h],1 ; cmp [rbp+24h],[rbp+10h] ; jb loop           ; rounds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two shifts by 4 and 5, a key word chosen by the low two bits of a running sum, then the same again with bits 11 and 12. This is the XTEA block function, in the encipher direction. The round count and the constant added to the sum are arguments, and the key is behind a pointer.&lt;/p&gt;

&lt;p&gt;Knowing the block function is not knowing the cipher. The question that decides everything is what goes &lt;em&gt;into&lt;/em&gt; the block function and what happens to what comes out. A hook at the head of the loop and another at its tail, logging the frame for the first 600 blocks, answers it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;parameter&lt;/th&gt;
&lt;th&gt;value&lt;/th&gt;
&lt;th&gt;where it is stored&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;key&lt;/td&gt;
&lt;td&gt;&lt;code&gt;7B7006CF 0F833A93 A21A3354 13F7BA9F&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RVA &lt;code&gt;0x23BF3897&lt;/code&gt;, plaintext&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sum increment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x9E3779B9&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;immediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rounds&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;immediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;first input block&lt;/td&gt;
&lt;td&gt;&lt;code&gt;E8B9C3BC A0A8028D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RVA &lt;code&gt;0x26D860DC&lt;/code&gt;, plaintext&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The round count is twice the usual 32. The constant is the standard one.&lt;/p&gt;

&lt;p&gt;The inputs are the surprise. Every block's input is the previous block's output. No block ever takes its input from the image. And each byte that the copy loop stores is the byte that was already at that address, XORed with the next byte of the block outputs taken in order.&lt;/p&gt;

&lt;p&gt;So the cipher never touches the code. It enciphers its own 8-byte state over and over, and the successive states are a keystream that is XORed into the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;S(0)   = the stored block
S(i+1) = XTEA(S(i))
plaintext = ciphertext XOR (S(1) || S(2) || S(3) || ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is output-feedback mode, and it has three consequences.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encryption and decryption are the same operation.&lt;/strong&gt; The build tool that encrypted the file and the routine that decrypts it can be the same code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is strictly sequential.&lt;/strong&gt; Block &lt;em&gt;n&lt;/em&gt; cannot be computed without the &lt;em&gt;n&lt;/em&gt; − 1 before it. There is no seeking to the middle and no splitting the work across threads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Position is everything.&lt;/strong&gt; The keystream carries no trace of where it is being applied. Feed it the wrong list of addresses, or the right list in the wrong order, and every byte after the first mistake is wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first 312 bytes stored by the copy loop all followed the rule, and all 312 matched the running image.&lt;/p&gt;

&lt;h3&gt;
  
  
  The list
&lt;/h3&gt;

&lt;p&gt;The keystream ran straight across the boundary between two separate destination ranges without restarting. So the one thing still missing is the list of ranges, in order.&lt;/p&gt;

&lt;p&gt;The store log gives its first few entries: 16 bytes at &lt;code&gt;0x1000&lt;/code&gt;, then 28 at &lt;code&gt;0x7004&lt;/code&gt;, 28 at &lt;code&gt;0xD014&lt;/code&gt;, 28 at &lt;code&gt;0x13024&lt;/code&gt;. Those are exactly the gaps between the plaintext islands found earlier.&lt;/p&gt;

&lt;p&gt;A checksum guard describes what it hashes as a stream of LEB128 &lt;code&gt;(gap, length)&lt;/code&gt; pairs. If the bootstrap uses the same encoding, its list begins with &lt;code&gt;80 20 10&lt;/code&gt; (gap &lt;code&gt;0x1000&lt;/code&gt;, length 16) followed by &lt;code&gt;F4 BF 01 1C&lt;/code&gt; (gap &lt;code&gt;0x5FF4&lt;/code&gt;, length 28) repeated. Searching the file for those bytes gives one hit, at RVA &lt;code&gt;0x25A8CA90&lt;/code&gt;: the first byte of the 4.4 MB plaintext island in the protection's section.&lt;/p&gt;

&lt;p&gt;Decoded from there, the list is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2,774,020 bytes long;&lt;/li&gt;
&lt;li&gt;1,324,188 ranges;&lt;/li&gt;
&lt;li&gt;208,856,213 bytes covered. That is 96.69% of the game's code section, 89.15% of the protection's, and under 22,000 bytes spread across the data sections.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It ends with a gap that moves a 32-bit cursor from the end of the last range to exactly &lt;code&gt;0xFFFFFFFF&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The most common gap is 8 bytes. Of 23,166 eight-byte gaps sampled, 21,997 sit exactly on a relocation site. That is the answer to the relocation question: every relocated pointer is a hole in the list. The list also steps over the plaintext islands, the key, the starting block, and itself.&lt;/p&gt;

&lt;p&gt;More than a million ranges for two sections sounds excessive until the holes are counted. The code is not encrypted as a block with exceptions. It is encrypted as 1.3 million fragments that fit between the things the loader has to touch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decrypting without running anything
&lt;/h3&gt;

&lt;p&gt;Key, starting block, round count and list are all plaintext in the file. Nothing is derived from the machine, the time, or the process. So the layer can be removed by a program that never executes a byte of the game.&lt;/p&gt;

&lt;p&gt;That program is &lt;code&gt;mwdec&lt;/code&gt;. It maps the file by RVA, decodes the list, and runs one XTEA keystream across all 1.3 million ranges in order: 26,107,027 blocks, in 4.1 seconds. The emulator needs about fifteen minutes for the same work.&lt;/p&gt;

&lt;p&gt;Then comes the check: rebase the result to the dump's load address through the relocation table, and compare.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;section&lt;/th&gt;
&lt;th&gt;entropy before&lt;/th&gt;
&lt;th&gt;entropy after&lt;/th&gt;
&lt;th&gt;equal to the running image before&lt;/th&gt;
&lt;th&gt;after&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (game)&lt;/td&gt;
&lt;td&gt;7.991&lt;/td&gt;
&lt;td&gt;6.916&lt;/td&gt;
&lt;td&gt;3.67%&lt;/td&gt;
&lt;td&gt;99.05%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (protection)&lt;/td&gt;
&lt;td&gt;7.959&lt;/td&gt;
&lt;td&gt;6.743&lt;/td&gt;
&lt;td&gt;11.08%&lt;/td&gt;
&lt;td&gt;96.25%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because the keystream is positional, this result is stronger than the percentages suggest. One wrong range anywhere in the list would have turned everything after it into noise. Agreement at the far end of the second section means all 1.3 million ranges are right.&lt;/p&gt;

&lt;h3&gt;
  
  
  What a memory dump gets wrong
&lt;/h3&gt;

&lt;p&gt;That leaves 5.0 MB of code that differs, in 75,253 separate places. It is tempting to call that "still encrypted". Comparing the entropy of the two sides of each difference shows four different things instead.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Zero in the dump, code in the static image.&lt;/strong&gt; Whole pages, such as 64 KB at &lt;code&gt;0x3C0000&lt;/code&gt;. The static side is ordinary x64. The dumping tool could not read those pages, and the static image has them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code in the static image, noise in the dump.&lt;/strong&gt; For example 4,222 bytes at &lt;code&gt;0x5192A0&lt;/code&gt;: a normal function prologue statically, entropy 7.9 in the dump. The running process encrypted that function &lt;em&gt;after&lt;/em&gt; start-up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Noise on both sides, but different noise.&lt;/strong&gt; 3.2 MB in 53 runs, the largest 2.16 MB at &lt;code&gt;0x23D66DC2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Small edits.&lt;/strong&gt; About 70,000 runs of 8 to 64 bytes, 1.3 MB in total, with code on both sides.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the first two cases the static result is the better image of the two. A dump is a photograph of one moment in a program that keeps rewriting itself. The file, once the bootstrap layer is off, is the state every run starts from.&lt;/p&gt;

&lt;p&gt;The last two cases are the writer routines, seen from the other side. The next sections take them in turn, after the entry point.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the entry point is built
&lt;/h2&gt;

&lt;p&gt;With the code decrypted, the entry paths can be read instead of emulated. Reading them still takes a tool, because no path is contiguous. Each one is a trail of one or two instructions at a time, scattered across both code sections and joined by jumps, and the jumps themselves are disguised.&lt;/p&gt;

&lt;h3&gt;
  
  
  Three idioms
&lt;/h3&gt;

&lt;p&gt;Three patterns have to be undone before a path becomes a listing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; "jmp X"
lea rsp,[rsp-8] ; mov [rsp],rbp ; lea rbp,[rip+X] ; xchg [rsp],rbp
lea rsp,[rsp+8] ; jmp [rsp-8]

; "jne B", falling through to A
push rbp ; movabs rbp,A ; xchg [rsp],rbp          ; push A
push rcx ; push rdx
mov rcx,[rsp+10h] ; movabs rdx,B ; cmovne rcx,rdx ; mov [rsp+10h],rcx
pop rdx ; pop rcx ; ret

; "call G", returning to R
push R ; push G ; ret                              ; each push built as above
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first hides the target of a jump from anything that looks for &lt;code&gt;E9&lt;/code&gt; or &lt;code&gt;FF 25&lt;/code&gt;. The second replaces a conditional branch with a conditional &lt;em&gt;move&lt;/em&gt; between two absolute addresses, followed by a &lt;code&gt;ret&lt;/code&gt;. A disassembler following control flow sees no branch at all.&lt;/p&gt;

&lt;p&gt;The second idiom is also where the quarter of a million relocations come from. &lt;code&gt;movabs rbp, A&lt;/code&gt; holds a full 64-bit address, so the loader has to fix it up, so it needs a relocation entry, so the bootstrap's range list has to step around it. Of the 251,874 relocation sites in the protection's section, 250,178 are &lt;code&gt;movabs&lt;/code&gt; immediates.&lt;/p&gt;

&lt;p&gt;A small layout tool starts at each TLS callback and at the entry point, follows every jump, tracks a small symbolic stack to resolve these three idioms, and folds each block of register saves or restores into one line.&lt;/p&gt;

&lt;h3&gt;
  
  
  The entry point is the compiler's
&lt;/h3&gt;

&lt;p&gt;Here is the entry point, laid out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;070D9224  sub  rsp, 28h
070D9228  call 01B07FCB
070D922D  nop
28DDA0B6  add  rsp, 28h
28DDA0D3  jmp  070D90B0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anyone who has looked at an MSVC binary will recognise the shape. This is &lt;code&gt;mainCRTStartup&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub  rsp, 28h
call __security_init_cookie
add  rsp, 28h
jmp  __scrt_common_main_seh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x70D90B0&lt;/code&gt; begins &lt;code&gt;mov ecx,1 ; call … ; test al,al&lt;/code&gt; and then reads the CRT's startup-state global. It is &lt;code&gt;__scrt_common_main_seh&lt;/code&gt;. So the tail of the entry point is untouched, apart from being moved into the protection's section and having its &lt;code&gt;jmp&lt;/code&gt; rewritten in the first idiom.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;call&lt;/code&gt; is where the protection got in. It no longer goes to &lt;code&gt;__security_init_cookie&lt;/code&gt;. It goes to a thunk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;01B07FCB  ; save context: 14 registers, 16 XMM
          ; align stack
031206B6  call 0043907D
          ; restore context
          ; save context, align stack
27714CFC  call 258DC3DA
          ; restore context
          ; save context, align stack
27850B16  call 285C23FC
          ; restore context
2863455F  jmp  070DD5F0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x70DD5F0&lt;/code&gt; loads a global and compares it with &lt;code&gt;0x2B992DDFA232&lt;/code&gt;. That constant is MSVC's default stack cookie, and the function is &lt;code&gt;__security_init_cookie&lt;/code&gt;, the one the &lt;code&gt;call&lt;/code&gt; originally named.&lt;/p&gt;

&lt;p&gt;So the protection did not write an entry point. It took the compiler's, and redirected its first &lt;code&gt;call&lt;/code&gt; through a thunk that runs three routines and then tail-jumps to the original callee. Everything else about process start-up is stock.&lt;/p&gt;

&lt;p&gt;This also explains the nine plaintext bytes. &lt;code&gt;sub rsp,28h&lt;/code&gt; and the &lt;code&gt;call&lt;/code&gt; are the only two instructions of the entry point that can execute before decryption, so they are the only two left in the clear.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same three calls, at every way in
&lt;/h3&gt;

&lt;p&gt;TLS callback 0, laid out the same way, begins with the same three calls, each in its own save and restore, and then continues into the callback's own first instruction, &lt;code&gt;mov [rsp+8], rbx&lt;/code&gt;. Nothing tests the loader's reason code before them. The three routines are entered every time Windows calls the callback: at process attach, for every thread that starts, for every thread that ends, and at process detach. After the first time they have nothing left to do, and each returns at once without a call or a write.&lt;/p&gt;

&lt;p&gt;The other five callbacks have nothing prefixed, and none of the six is the protection's own. An unprotected build made from nearly the same source two weeks before 1.20 has 1.20's three callbacks in the same order, sharing their constants almost one for one (136 of 139 in the first, all 17 in the third). They belong to the game and its anti-cheat: the first reads a fresh copy of &lt;code&gt;ntdll.dll&lt;/code&gt; from disk, the others register DLL-load notifications or query memory when the process attaches, and return immediately for any other reason. What the protection did to them is what it does to any function it covers. Between 45% and 90% of each callback's instructions have been moved into the appended section (the fifth callback of 1.51.2, 43 instructions of plain compiled code, is the one left alone), their conditional jumps are written as constructs, their &lt;code&gt;ret&lt;/code&gt; is &lt;code&gt;lea rsp,[rsp+8] ; jmp [rsp-8]&lt;/code&gt;, and the bytes the moved blocks left behind are used again for pieces of other functions: routines, call chains, debugger probes, the stores that publish a guard's value. A scan of a callback's address range finds all of those. A walk of the callback's own paths reaches none of them, and an emulated run of every callback for every reason enters no routine except the three at the front.&lt;/p&gt;

&lt;p&gt;The loader runs TLS callbacks before the entry point, so in a normal start the callback does the work. The entry point's copy is the fallback, for a process whose first executed instruction is the entry point.&lt;/p&gt;

&lt;p&gt;Those are not the only two copies. The executable exports 204 symbols: the NGX SDK's entry points, the C runtime's &lt;code&gt;printf&lt;/code&gt; and &lt;code&gt;scanf&lt;/code&gt; families that a static library brought along, and one variable, &lt;code&gt;NvOptimusEnablement&lt;/code&gt;. Each of the 203 exported functions begins with a jump to its own copy of the three calls. With the entry point and the callback that makes 205 entries and 615 call chains, 205 for each routine. Whichever door into the image is used first, the code behind it has been decrypted by the time it runs.&lt;/p&gt;

&lt;p&gt;Running the three routines one at a time in the emulator, on the statically decrypted image, shows what each one is.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;routine&lt;/th&gt;
&lt;th&gt;API calls&lt;/th&gt;
&lt;th&gt;what it does to memory&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x43907D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;GetSystemInfo&lt;/code&gt;, then &lt;code&gt;VirtualQuery&lt;/code&gt; and &lt;code&gt;VirtualProtect&lt;/code&gt; for the headers and each section&lt;/td&gt;
&lt;td&gt;reads one byte and writes it back at the start of every 4 KB page: 169,353 pages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x258DC3DA&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;the XTEA pass over 1,324,188 ranges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x285C23FC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;copies 667 pointers from &lt;code&gt;.idata&lt;/code&gt; to the start of &lt;code&gt;.rdata&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Unlock.&lt;/strong&gt; The first routine makes the whole image readable, writable and executable, then touches every page. Reading a byte and writing the same byte back changes nothing, but it forces each page to become a private, committed copy before the decryptor starts writing. Nothing in the start-up path puts the protections back. From here on every Arxan routine simply writes to code, which is why no writer routine ever calls &lt;code&gt;VirtualProtect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decrypt.&lt;/strong&gt; The second is the cipher described above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connect the imports.&lt;/strong&gt; The third is the one a static decryptor would miss.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two import tables
&lt;/h3&gt;

&lt;p&gt;The file has two import address tables. The PE import directory describes one in &lt;code&gt;.idata&lt;/code&gt;, a section the protection added. That is the table the Windows loader fills in. The original table is still at the very start of &lt;code&gt;.rdata&lt;/code&gt;, but no directory entry mentions it and the loader never touches it.&lt;/p&gt;

&lt;p&gt;Counting the indirect calls in the decrypted code shows that both are in use.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;in the game's code&lt;/th&gt;
&lt;th&gt;through &lt;code&gt;.idata&lt;/code&gt; (loader's)&lt;/th&gt;
&lt;th&gt;through &lt;code&gt;.rdata&lt;/code&gt; (original)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;call [rip+d]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10,015&lt;/td&gt;
&lt;td&gt;1,226&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jmp [rip+d]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;338&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;distinct slots used&lt;/td&gt;
&lt;td&gt;600&lt;/td&gt;
&lt;td&gt;183&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Nine call sites in ten were rewritten to use the new table. The rest still go through the old one. Left alone, each of those 1,226 calls would jump through a slot that holds whatever the linker put there, which is not an address.&lt;/p&gt;

&lt;p&gt;The third routine is what keeps them working. It walks two range lists in step, one at &lt;code&gt;0x26D045AE&lt;/code&gt; and one at &lt;code&gt;0x23B163B3&lt;/code&gt;, in the same LEB128 encoding as everything else. The first list is 33 runs in &lt;code&gt;.idata&lt;/code&gt;. The second is 32 runs of the same sizes in &lt;code&gt;.rdata&lt;/code&gt;. Run by run, it copies the loader's pointers across: 667 of them, one run per imported DLL.&lt;/p&gt;

&lt;p&gt;The odd run out is the protection's own. The import directory has 33 descriptors, and the last is a second &lt;code&gt;KERNEL32.DLL&lt;/code&gt; with exactly two functions, &lt;code&gt;AddVectoredExceptionHandler&lt;/code&gt; and &lt;code&gt;RemoveVectoredExceptionHandler&lt;/code&gt;. The game does not import them. The debugger probes need them. They have no copy in the old table.&lt;/p&gt;

&lt;p&gt;The protection's own section never calls an import directly. It has 3,960 places that &lt;em&gt;load&lt;/em&gt; a pointer from &lt;code&gt;.idata&lt;/code&gt; into a register, and none that touch the old table.&lt;/p&gt;

&lt;p&gt;For a static image, the consequence is practical. A disassembler shown the decrypted file names the imports in &lt;code&gt;.idata&lt;/code&gt; and leaves 1,226 call sites pointing into a table of anonymous numbers. A tool that rebuilds imports from a memory dump finds two tables holding the same pointers and has to guess which one is real. Both are. The decryptor decodes the two lists and writes out every slot of both tables with its DLL and function name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flags in the padding
&lt;/h3&gt;

&lt;p&gt;If both the callback and the entry point call the same three routines, the second caller must not decrypt again. XORing the keystream in twice would re-encrypt the image.&lt;/p&gt;

&lt;p&gt;Each routine keeps a flag, and keeps it somewhere that looks like nothing: &lt;code&gt;CC&lt;/code&gt; bytes between functions. The unlock routine's flag is four &lt;code&gt;CC&lt;/code&gt; bytes at &lt;code&gt;0x1BF4DAF&lt;/code&gt;, which it sets to zero when it finishes. The import routine reads one &lt;code&gt;CC&lt;/code&gt; byte at &lt;code&gt;0x41B988&lt;/code&gt; on entry and writes zero to it on the way out. A flag that is still &lt;code&gt;CC&lt;/code&gt; means "not yet".&lt;/p&gt;

&lt;p&gt;The emulator confirms the rest. Comparing the memory left by a complete emulated run of the callback with the output of &lt;code&gt;mwdec&lt;/code&gt;, the two code sections differ in 25 bytes out of 224,083,968.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;RVA&lt;/th&gt;
&lt;th&gt;static&lt;/th&gt;
&lt;th&gt;after the real routines&lt;/th&gt;
&lt;th&gt;what it is&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x41B988&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;import routine's flag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x1BF4DAF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CC CC CC CC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;00 00 00 00&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;unlock routine's flag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;0xA8C97F&lt;/code&gt;, &lt;code&gt;0x1C176AE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;two dwords&lt;/td&gt;
&lt;td&gt;two different dwords&lt;/td&gt;
&lt;td&gt;state written by the decryptor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x23BF3897&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;CF 06 70 7B&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;92 78 77 22&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;first word of the XTEA key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x26D860DC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;BC C3 B9 E8 8D 02 A8 A0&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;18 77 80 CB 6C AA FD EF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the keystream state&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With those 25 bytes in place, calling each of the three routines again returns in a fraction of a second, with no API call and no write to code.&lt;/p&gt;

&lt;p&gt;The last two rows are worth a moment. The decryptor enciphers its state in place, so when it finishes, the starting block has been replaced by the final one. &lt;code&gt;mwdec&lt;/code&gt; ends on the same value, &lt;code&gt;CB807718 EFFDAA6C&lt;/code&gt;, which is a second, independent check on 26 million blocks. And the decryptor overwrites the first word of its own key on the way out.&lt;/p&gt;

&lt;p&gt;A memory dump of the running game therefore holds neither the key nor the starting block. Everything needed to decrypt the file is in the file, and after start-up it is nowhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions that stay encrypted
&lt;/h2&gt;

&lt;p&gt;After the bootstrap, 251 of the game's 29,329 code pages are still noise. They belong to 59 functions, the largest of them 454 KB. These are the functions that are decrypted on demand, and the obvious question is where their key is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Following one dword backwards
&lt;/h3&gt;

&lt;p&gt;The way to find a key is to take one decrypted value and walk back to where it came from. So: run a writer that is known to produce the function at &lt;code&gt;0x387DEC0&lt;/code&gt;, log every read and write, and look at the instruction that produced the first four bytes of code, &lt;code&gt;55 48 8B EC&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2583A98F  mov rax, [rbp+68h]        ; a pointer that starts at 0x2500ACF2
2583A993  mov eax, [rax]            ; e = 0x17A80096
2583A995  xor eax, [rip+...]        ; K = the dword at 0x415D1D = 0xFB2348C3
2583A99F  mov [rdx], eax            ; -&amp;gt; a 4-byte buffer, then memcpy to the function
2583A9A1  add qword ptr [rbp+68h], 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x17A80096 ^ 0xFB2348C3 = 0xEC8B4855&lt;/code&gt;. That is the code.&lt;/p&gt;

&lt;p&gt;What is missing from this listing is more interesting than what is in it. The writer never reads the function. The pointer walks through a block of data in the protection's own section, half a gigabyte of address space away from the function it is producing.&lt;/p&gt;

&lt;p&gt;So this is not decryption in the usual sense. The noise that fills the function at rest is not transformed into code. It is thrown away. Each writer carries its own encoded copy of the bytes it will write, a &lt;strong&gt;stash&lt;/strong&gt;, and overwrites the target with the decoded copy. One experiment confirms it: fill every target range with random bytes before running the writers, and the result is identical.&lt;/p&gt;

&lt;h3&gt;
  
  
  Four encodings
&lt;/h3&gt;

&lt;p&gt;Scanning both code sections for the shape of that decode block finds every variant of it. There are four.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;variant&lt;/th&gt;
&lt;th&gt;decode step&lt;/th&gt;
&lt;th&gt;notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;xor&lt;/td&gt;
&lt;td&gt;&lt;code&gt;p = e ^ K&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;66 sites&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;subtract&lt;/td&gt;
&lt;td&gt;&lt;code&gt;p = K - e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;51 sites&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;autokey&lt;/td&gt;
&lt;td&gt;&lt;code&gt;p = e ^ k ; k += p&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;45 sites; &lt;code&gt;k&lt;/code&gt; starts as &lt;code&gt;K&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;multiply-rotate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;a = rol(a, b &amp;amp; 31) ; p = e - a*b ; b = ~p ^ b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;34 sites; &lt;code&gt;a&lt;/code&gt; starts as &lt;code&gt;K&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; as &lt;code&gt;K &amp;amp; 31&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In every case &lt;code&gt;e&lt;/code&gt; is the next dword of the stash, &lt;code&gt;p&lt;/code&gt; is the next four bytes of output, and &lt;code&gt;K&lt;/code&gt; is a single dword. &lt;code&gt;K&lt;/code&gt; is not stored anywhere that looks like data. It sits in the filler bytes between two fragments of scattered code, for example &lt;code&gt;D9 0F 56 2A&lt;/code&gt; between one &lt;code&gt;jmp&lt;/code&gt; and the next instruction.&lt;/p&gt;

&lt;p&gt;Where the output goes is given by a range list, in the same LEB128 encoding as the guards and the bootstrap.&lt;/p&gt;

&lt;p&gt;So a writer is completely described by four addresses and a variant: range list, stash, key, and which of the four steps to use. None of the four depends on anything at run time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layers
&lt;/h3&gt;

&lt;p&gt;Run one at a time, some writers still produce noise. The model is right and the key is right, and the output is not code.&lt;/p&gt;

&lt;p&gt;The function at &lt;code&gt;0x471CCF0&lt;/code&gt; shows why. Two writers are involved. The first, a subtract writer, does not write code at all. It writes 8,000 bytes into the protection's own section, at &lt;code&gt;0x25673181&lt;/code&gt;. The second, an autokey writer, uses exactly that address as its stash. Run the second alone and it decodes a stash that has not been prepared. Run them in order and the function appears:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0471CCF0  mov  dword ptr [rsp+8], ecx
0471CCF4  sub  rsp, 608h
0471CCFB  movsxd rax, dword ptr [rsp+610h]
0471CD03  lea  rcx, [rip+1A3C0BAEh]
0471CD0A  mov  rax, [rcx+rax*8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some writers exist only to prepare another writer's stash. One of them prepares the stashes of four others at once. Working from the memory dump, the order is ten stash writers, then seventy-four code writers.&lt;/p&gt;

&lt;h3&gt;
  
  
  One more level, visible only from the file
&lt;/h3&gt;

&lt;p&gt;Applying those 84 writers to the statically decrypted file produced 43 of the 59 functions. The other 16 stayed noise, and all 16 depend on two stashes: 2.16 MB at &lt;code&gt;0x23D66DC2&lt;/code&gt; and 234 KB at &lt;code&gt;0x267730C9&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Those addresses had appeared before. They are two of the "noise on both sides, but different noise" regions left over after the bootstrap. The stash in the shipped file is not the stash in the dump.&lt;/p&gt;

&lt;p&gt;That is a statement about time. By the moment the dump was taken, something had already rewritten those two stashes. Seen from the dump they need no preparation, and seen from the file they do. The search for what rewrote them is mechanical: take every writer on record, including the ones filed as writing noise, decode each from the static image, and keep any whose target overlaps the stash and whose output equals what the dump holds there. Exactly one writer qualifies for each stash, and both are autokey writers.&lt;/p&gt;

&lt;p&gt;With those two run first, the count is 86 writers: 12 that prepare stashes and 74 that produce code. Up to three steps now separate the file from a function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The bootstrap keystream decrypts the feeder's stash.&lt;/li&gt;
&lt;li&gt;The feeder decodes that into the writer's stash.&lt;/li&gt;
&lt;li&gt;The writer decodes that into code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every step is a function of bytes in the file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Every range has two writers
&lt;/h3&gt;

&lt;p&gt;Those 86 are not all the writers there are. A function that is produced on demand is also taken away again: for every on-demand range a second routine exists, with the same range list and a stash of its own, whose output is noise. It is the same mechanism pointed the other way. Hiding a function is one more overwrite from an encoded copy, and the copy happens to be ciphertext.&lt;/p&gt;

&lt;p&gt;So the writers come in pairs, and a memory dump shows each range in whichever state the last of the two left it. For a 454 KB function it is obvious which writer produces code. For 259 bytes scattered over seven fragments, "does this decode?" is a weak question.&lt;/p&gt;

&lt;p&gt;The at-rest file allows a stronger one. Run every one of the 730 routines once on the statically decrypted image. For each, take the bytes it writes that differ from what the image already holds, leaving out its bookkeeping words, and measure two entropies over them: of what the routine writes, and of what is there.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;the routine writes&lt;/th&gt;
&lt;th&gt;the image holds&lt;/th&gt;
&lt;th&gt;routines&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;th&gt;what they are&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;about 8.0 bits per byte&lt;/td&gt;
&lt;td&gt;5.6 to 6.7&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;1,101,492&lt;/td&gt;
&lt;td&gt;re-encryptors: they turn code the image already has into noise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4.4 to 5.9&lt;/td&gt;
&lt;td&gt;7.2 to 8.0&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;17,717&lt;/td&gt;
&lt;td&gt;decryptors whose output the image does not have yet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7.6 to 7.8&lt;/td&gt;
&lt;td&gt;7.5 to 7.8&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;4,928&lt;/td&gt;
&lt;td&gt;re-encryptors of the ranges in the row above&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every other routine either writes nothing of this kind or writes what is already there. The table sorts itself: the first row is to be left alone for ever, the third row pairs off with seven of the second by range list, and the second row is fifteen writers still to apply.&lt;/p&gt;

&lt;h3&gt;
  
  
  A guard's list is not its writer's list
&lt;/h3&gt;

&lt;p&gt;One of the fifteen is the writer of &lt;code&gt;0x387DEC0&lt;/code&gt;, the function whose first dword was followed backwards above. Its stash and key are known. Its range list looked known as well: nine ranges, 84,761 bytes, which is to the byte what the checksum guard for that function hashes.&lt;/p&gt;

&lt;p&gt;Lay the decoded stash over those nine ranges and seven of them come out as code. The last two do not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;247A940F   2E 78 68 61 73 68 00 00 63 6F 6C 6C 69 73 69 6F   .xhash..collisio
25FC7169   61 69 6D 00 70 61 73 73 74 68 72 6F 75 67 68 2D   aim.passthrough-
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Text, in two places where the real routine, run in the emulator, leaves a function prologue and a register-restoring thunk. Plain text is not what a wrong key looks like. The stream is right and it is landing in the wrong place, which means a range is missing in front of it.&lt;/p&gt;

&lt;p&gt;The protection encodes a list as LEB128 &lt;code&gt;(gap, length)&lt;/code&gt; pairs, so a guess at the missing range can be encoded and searched for. The image holds three lists that begin with the same seven ranges. One has nine entries. The other two have ten, and the extra one sits eighth: 10,760 bytes at &lt;code&gt;0x72DAEB0&lt;/code&gt;, in &lt;code&gt;.rdata&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the ten-range list the last two ranges become the prologue and the thunk, and the block in &lt;code&gt;.rdata&lt;/code&gt; becomes 619 strings: &lt;code&gt;collisiontile&lt;/code&gt;, &lt;code&gt;passthrough-if/else&lt;/code&gt;, &lt;code&gt;j_shoulder_le&lt;/code&gt;, &lt;code&gt;tag_ik_target_left&lt;/code&gt;. They are the names of animation states, and at rest they are stored as noise.&lt;/p&gt;

&lt;p&gt;So a writer can carry data along with its function, and the guard that watches the function hashes the writer's list with the data left out. Two lists that start the same way are not copies of each other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading a writer's parameters from one run
&lt;/h3&gt;

&lt;p&gt;For seven of the fifteen, the only writer on record with their range list was the twin that re-encrypts, and its stash decodes to noise. Their own stash, key and variant had to come from somewhere.&lt;/p&gt;

&lt;p&gt;They come from watching one run. Hook every read and every write of the image while the routine executes, and then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the longest unbroken run of image bytes it read is the stash, whatever the access size;&lt;/li&gt;
&lt;li&gt;of the dwords it read anywhere else, exactly one turns that stash into the bytes it wrote under one of the four decode steps. That dword is &lt;code&gt;K&lt;/code&gt;, the address it was read from is where &lt;code&gt;K&lt;/code&gt; lives, and the step that worked is the variant;&lt;/li&gt;
&lt;li&gt;the ranges it wrote, encoded as a list, are found in the image by search.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nothing here requires understanding the routine, and nothing about it is specific to this build. Pointed at a writer whose four parameters were already known, it returns the same four. For the seven: five multiply-rotate, one autokey, one xor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fourteen entries that are not there
&lt;/h3&gt;

&lt;p&gt;Apart from the string block, what the fifteen writers produce is small: 6,957 bytes. Seventy of those bytes are the interesting ones. They are fourteen five-byte patches that sit close together in the game's code, between &lt;code&gt;0xAF8E10&lt;/code&gt; and &lt;code&gt;0xAFB4A0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At rest each of those fourteen places holds five bytes of noise. Decrypted, the five bytes are a &lt;code&gt;jmp&lt;/code&gt; into the protection's section, and everything else the writer produces is what the jump leads to: a call chain, its return thunk, its &lt;code&gt;ret&lt;/code&gt; gadgets, and a few instructions of game code that end in a return. Each is the entry of a small subroutine, between 258 and 892 bytes with its chain included, whose body is not in the game's code section at all and which has no record in the exception directory.&lt;/p&gt;

&lt;p&gt;They belong to a larger family. Writers target 76 such five-byte entries in that neighbourhood, and after the bootstrap layer only 7 of them are readable. The 86 writers account for most of the rest; these fourteen need the fifteen.&lt;/p&gt;

&lt;p&gt;The chain in front of each of the fourteen bodies runs a re-encryptor, and what it hides is not the subroutine it stands in. It hides the entry and the chain of &lt;em&gt;another&lt;/em&gt; member of the family. Running one of them makes a different one unreadable, in all fourteen cases.&lt;/p&gt;

&lt;p&gt;That is the most thorough hiding in the image. The entry is noise in the file, the body lives in the protection's section, nothing in the PE headers says a function is there, and the running process keeps closing the family behind itself as it goes.&lt;/p&gt;

&lt;p&gt;With the fifteen added the table has 100 writers: 12 that prepare stashes and 88 that produce code, one of which also produces strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking it
&lt;/h3&gt;

&lt;p&gt;There are two independent routes to the same 59 functions. One starts from the shipped file and uses only &lt;code&gt;mwdec&lt;/code&gt;. The other starts from the memory dump, applies the writers there, and was itself checked against the emulated output of each writer. They agree on 883,737 of 883,737 bytes.&lt;/p&gt;

&lt;p&gt;The fifteen writers of the last three sections have a check of their own, because the dump cannot provide one. The real routines, run in the emulator on the at-rest image, write 17,717 bytes that the image did not hold. The static model writes the same 17,717, with none missing and none extra.&lt;/p&gt;

&lt;p&gt;Counting pages of the game's code that still measure above 7.2 bits per byte:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;image&lt;/th&gt;
&lt;th&gt;noisy pages, of 29,329&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;shipped file&lt;/td&gt;
&lt;td&gt;29,094&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;after the bootstrap layer&lt;/td&gt;
&lt;td&gt;251&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;after the writers&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;memory dump of the running game&lt;/td&gt;
&lt;td&gt;254&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The static image ends with 248 fewer unreadable pages than a dump of the game that was actually running. The six that remain look the same in the dump, and no writer that was found targets them.&lt;/p&gt;

&lt;p&gt;The 88 code writers write 3.8 MB in all, in 151,337 ranges. Less than a quarter of that is the 59 functions. The rest is small change: the tails of scattered fragments and five-byte jump patches, in the game's code and in the protection's own. That is the population of "small edits" from the comparison with the dump.&lt;/p&gt;

&lt;p&gt;Two practical consequences follow from the stash model.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A byte changed inside one of these functions is not detected by its writer and does not corrupt anything. It is simply overwritten the next time the writer runs.&lt;/li&gt;
&lt;li&gt;There is nothing to gain from attacking the noise in the function body. Nothing reads it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Strings
&lt;/h2&gt;

&lt;p&gt;Text is hidden in this executable in three ways. Two of them are Arxan's.&lt;/p&gt;

&lt;h3&gt;
  
  
  The protection's own: two pads
&lt;/h3&gt;

&lt;p&gt;Arxan needs two names at run time, &lt;code&gt;ntdll.dll&lt;/code&gt; and &lt;code&gt;NtQueryInformationProcess&lt;/code&gt;, for the debugger probe described earlier. It stores neither.&lt;/p&gt;

&lt;p&gt;Each place that needs a name carries two byte arrays of the same length and a length dword, all three inside a code section. The name is the XOR of the two arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  r, [rip + padA]   ; mov [frame], r
lea  r, [rip + padB]   ; mov [frame], r
mov  r32, [rip + len]
...                    ; out[i] = padA[i] ^ padB[i]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 544 pairs: 276 decode to &lt;code&gt;ntdll.dll&lt;/code&gt; and 268 to &lt;code&gt;NtQueryInformationProcess&lt;/code&gt;. Every site has its own pair of pads, so no two sites share a single byte even though they produce the same nine or twenty-five characters. A search for the bytes of one finds none of the others.&lt;/p&gt;

&lt;p&gt;The pads are inside the bootstrap's ranges, so in the shipped file they are under the keystream as well. After the first layer they are plain data, and finding them is a matter of following the scattered instructions above from each candidate &lt;code&gt;lea&lt;/code&gt;. A scan of the decrypted image does exactly that.&lt;/p&gt;

&lt;h3&gt;
  
  
  A table that travels with a function
&lt;/h3&gt;

&lt;p&gt;The second is the block met in the previous section: 619 animation-state names, 10,760 bytes at &lt;code&gt;0x72DAEB0&lt;/code&gt; in &lt;code&gt;.rdata&lt;/code&gt;. The writer that produces the function at &lt;code&gt;0x387DEC0&lt;/code&gt; produces the table in the same pass, and the routine that hides the function hides the table with it.&lt;/p&gt;

&lt;p&gt;It is the only data the writers handle. Of everything the 88 code writers write, this block is the one range that is not code: the rest of what they put outside the two code sections is 694 bytes of the protection's own fragments, which spill into the tail of &lt;code&gt;.rdata&lt;/code&gt; and the last page of &lt;code&gt;.pdata&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The engine's: keyed blobs
&lt;/h3&gt;

&lt;p&gt;The game engine has its own scheme, and it is much larger: 17,205 strings in &lt;code&gt;.data&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+0x0   8 bytes   key
+0x8   3 bytes   length
+0xB   1 byte    1 = encrypted, 0 = decrypted
+0xC   length    text, each byte XORed with key[i &amp;amp; 7]
       1 byte    a plain zero
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each string has an inlined getter. The first time the string is wanted, the getter XORs it in place and clears the byte at &lt;code&gt;+0xB&lt;/code&gt;. After that it is an ordinary C string with a twelve-byte header.&lt;/p&gt;

&lt;p&gt;Because the key travels with the blob, the shipped file alone is enough. &lt;code&gt;mwdec&lt;/code&gt; scans &lt;code&gt;.data&lt;/code&gt; on 8-byte boundaries for the header shape, requires the terminating zero and readable text, and counts the code that refers to each blob as a cross-check. The decryptor writes the plaintext and clears the flag, which leaves every blob in exactly the state the game itself would leave it in.&lt;/p&gt;

&lt;p&gt;The cross-check has a pleasing detail. With only the bootstrap layer removed, about 440 of the blobs have no reference from code at all. With the writers applied, every one of the 17,205 does. The difference is 402 strings that are used &lt;em&gt;only&lt;/em&gt; from inside the on-demand functions of the previous section: &lt;code&gt;dw_match_id&lt;/code&gt;, &lt;code&gt;noShowReport&lt;/code&gt;, &lt;code&gt;teamsValidated&lt;/code&gt;, &lt;code&gt;totalGameCount&lt;/code&gt;, &lt;code&gt;winningTeamIndex&lt;/code&gt;. In a memory dump those strings look unused, because the code that uses them is noise. They also say something about what was thought worth the extra layer.&lt;/p&gt;

&lt;p&gt;The dump gives a direct check here too. By the time it was taken the game had already decrypted 9,144 of these strings, and all 9,144 match the static result byte for byte. The remaining eight thousand had never been used in that run. They exist in readable form only in the static image.&lt;/p&gt;

&lt;p&gt;Some of what turns up: &lt;code&gt;armadillo initialization faillure&lt;/code&gt;, &lt;code&gt;tact_client_create_error&lt;/code&gt;, &lt;code&gt;Crash Report in progress...&lt;/code&gt;, &lt;code&gt;HARDWARE\DESCRIPTION\System\CentralProcessor\0&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is not there
&lt;/h3&gt;

&lt;p&gt;Two searches came back empty, which is worth recording. There are no stack strings: 1,765 runs of immediate stores to the stack contain control-flow state values, not text. And there are no single-byte XOR loops over text among 87,070 candidate loops.&lt;/p&gt;

&lt;p&gt;Console-variable names are a separate matter. The build replaces each name with a ten-letter code that spells a 32-bit number, and the original names are not in the file in any form. That is hashing, not encryption, and there is nothing to decrypt.&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
      <category>gamedev</category>
      <category>windows</category>
    </item>
    <item>
      <title>Inside Arxan in Modern Warfare (2019)</title>
      <dc:creator>odindev</dc:creator>
      <pubDate>Sun, 22 Nov 2026 01:25:14 +0000</pubDate>
      <link>https://dev.to/odindev/inside-arxan-in-modern-warfare-2019-1cob</link>
      <guid>https://dev.to/odindev/inside-arxan-in-modern-warfare-2019-1cob</guid>
      <description>&lt;p&gt;Arxan is the code protection that ships inside the Call of Duty executables. It does not stop you from reading the game's code. Its job is to notice when that code has been changed, and to make the consequences hard to trace back to the change.&lt;/p&gt;

&lt;p&gt;This is an account of taking that system apart in Modern Warfare (2019), and then taking it out, told in the order it happened. It starts with a memory dump and an emulator, because that is what I started with. It ends with ten versions of the game running their start-up side by side with their cleaned twins. In between are the places where I was wrong in a way that says something about the protection: a routine that cannot be skipped, a check that is there to fail, and a cleaned build that passed seven hundred comparisons and would not open its window.&lt;/p&gt;

&lt;p&gt;If you want the mechanism before the story, the section after this introduction is the whole protection on one page. The rest of the post covers, in order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the 730 routines Arxan adds to the game, and what each kind does;&lt;/li&gt;
&lt;li&gt;how a checksum guard works, instruction by instruction, and what it does when the checksum is wrong, which is not a crash;&lt;/li&gt;
&lt;li&gt;how code is repaired, decrypted and hidden again while the game runs;&lt;/li&gt;
&lt;li&gt;how it looks for a debugger, and what it does not look for;&lt;/li&gt;
&lt;li&gt;one structural weakness, and a way to use it that changes no code at all;&lt;/li&gt;
&lt;li&gt;how the shipped file is encrypted, and how to decrypt it without running it;&lt;/li&gt;
&lt;li&gt;what the protection did to the entry point, and what runs before the game's first instruction;&lt;/li&gt;
&lt;li&gt;what the program still needs from the protection, and what it takes to export an image that runs without it;&lt;/li&gt;
&lt;li&gt;how to know that such an image is right: an emulator used as a measuring instrument, a real start, and what a memory dump can and cannot tell you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything about the running process was established by executing Arxan's own machine code in an emulator. Nothing is inferred from watching the game crash. Everything about the file was then re-implemented from what the emulator showed, and checked byte for byte: against the emulated routines, against memory dumps of the running game, and in the end against a second, a third and a tenth build that none of it had been fitted to.&lt;/p&gt;

&lt;p&gt;I have kept the order in which things were found, wrong turns included where they taught me something. Each section says what I looked at, what came back, and what question that raised, because the route is more reusable than the addresses.&lt;/p&gt;

&lt;p&gt;Part of the idea came from Maurice Heumann's &lt;a href="https://momo5502.com/posts/2022-11-17-reverse-engineering-integrity-checks-in-black-ops-3/" rel="noopener noreferrer"&gt;Reverse Engineering Integrity Checks in Black Ops 3&lt;/a&gt;, which follows one of this protection's checksum checks in an older title.&lt;/p&gt;

&lt;p&gt;The developed PoC projects are available at &lt;a href="https://github.com/IDARETW/MWHook" rel="noopener noreferrer"&gt;IDARETW/MWHook&lt;/a&gt; and &lt;a href="https://github.com/IDARETW/MWDec" rel="noopener noreferrer"&gt;IDARETW/MWDec&lt;/a&gt; for further analysis and experimentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arxan on one page
&lt;/h2&gt;

&lt;p&gt;Before the story, the mechanism, stated flat. Everything in this section is shown later with the listing and the numbers behind it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does to the executable.&lt;/strong&gt; Arxan runs after the linker. It appends a code section of its own, about 100 MB here, and then treats the game's code as raw material:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it cuts game functions into pieces, moves many of the pieces into its own section, and joins them with jumps;&lt;/li&gt;
&lt;li&gt;it rewrites branches, calls, returns and stack instructions in a small set of disguised spellings, so that no disassembler follows them;&lt;/li&gt;
&lt;li&gt;it splices 730 small routines of its own into game functions, each reached through an obfuscated call;&lt;/li&gt;
&lt;li&gt;it encrypts both code sections, and leaves everything the Windows loader reads in the clear.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On disk.&lt;/strong&gt; The file is a valid PE whose code is ciphertext. There is one cipher: XTEA in output-feedback mode, a single keystream XORed over 1.3 million fragments that fit between the relocated pointers. The key, the starting block and the list of fragments are stored in the file in plain bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At start-up.&lt;/strong&gt; The entry point is the compiler's, with its first &lt;code&gt;call&lt;/code&gt; redirected through three routines. The first TLS callback and every exported function carry the same three. They make every section writable and executable, decrypt the code, and copy the import pointers into a second import table that a tenth of the game's call sites still use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;While the game runs.&lt;/strong&gt; The 730 routines are of four kinds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;writers&lt;/strong&gt; (554) overwrite code from encoded copies they carry: some repair bytes that should not have changed, and some produce functions that are stored as noise and exist only while they are needed. Every such function has a second writer that overwrites it with noise again;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;checksum guards&lt;/strong&gt; (106) hash ranges of code, write the digest into a slot in the code section, and compare it with an expected value. Most of what they hash is the protection itself;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;clocks&lt;/strong&gt; (52) sample time, and gate how often the guards do their work;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;debugger probes&lt;/strong&gt; come in a bundle of six, stamped into the image 294 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When something is wrong.&lt;/strong&gt; Nothing crashes where the check is. A guard that fails overwrites its own return address with a value that lands exactly 1 GiB above a real address, or leaves a wrong number in a state word that some unrelated function later adds to its stack pointer. Fifty-three checks elsewhere in the game's code read the values the guards publish, and their failing path computes a return address from a table of wrong ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it cannot simply be cut out.&lt;/strong&gt; A routine spliced into a function is not a detour on the way to somewhere. The code around it pushes three plausible addresses, and the routine replaces all three while it runs: the place where the function continues exists only as three writes inside the routine. Skip the routine and the function continues in the middle of some other function. And some checks are placed &lt;em&gt;before&lt;/em&gt; the guard that satisfies them, so that the program depends on their failing first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does not have.&lt;/strong&gt; No bytecode virtual machine. No key derived from the machine or the run. No anti-debugging beyond the six probes, and no code that asks what protection a page has. Everything it knows is a constant in the file, which is what makes the rest of this post possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  How this is laid out
&lt;/h2&gt;

&lt;p&gt;This article is the whole argument, with the listings and the numbers that carry it. Four more articles in this series, the appendices, hold the long tails: every step, every count, and the cases that went sideways.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;in this article&lt;/th&gt;
&lt;th&gt;in full&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The running process&lt;/td&gt;
&lt;td&gt;routines, guards, the failed check, writers, debugger probes, the weakness&lt;/td&gt;
&lt;td&gt;here&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The file on disk&lt;/td&gt;
&lt;td&gt;the cipher, the entry point, functions that stay encrypted, strings&lt;/td&gt;
&lt;td&gt;Appendix A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Taking the protection out&lt;/td&gt;
&lt;td&gt;what the program needs, why a routine cannot be skipped, the rewrites&lt;/td&gt;
&lt;td&gt;Appendix B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Doing it without an emulator&lt;/td&gt;
&lt;td&gt;how every table is read from the shipped file alone&lt;/td&gt;
&lt;td&gt;Appendix C&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checking the result&lt;/td&gt;
&lt;td&gt;emulated comparison, a real start, memory dumps, the emulated start&lt;/td&gt;
&lt;td&gt;Appendix D&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;The target is an archived build of the game, 1.51.2 from January 2022, studied offline on my own machine. This post covers Arxan only. The game also carries an anti-cheat with an obfuscation of its own; it appears here where the protection touches it, it is left exactly as shipped, and nothing in this post takes it apart. This is not a guide to cheating or to running a game you do not own.&lt;/p&gt;

&lt;p&gt;Two limits apply throughout, and I repeat them where they matter. The mitigation at the end of the first part is proven against Arxan's real code in emulation and by a native test of the implementation, and it has been run in real starts of the game, where it corrected a guard that had found five changed bytes; the guards that hash the game's own code did not run in those starts, so for them the proof is still the emulator. The cleaned images of the last part were compared with their protected originals in an emulator, function by function and from the entry point into &lt;code&gt;main&lt;/code&gt;, on ten versions of the game; one of them was also started as a real process.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Part 1: The running process&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method: run it where it cannot fight back
&lt;/h2&gt;

&lt;p&gt;I started with what I had, which was a memory dump of the running game. The usual way to study an integrity check is to put a hardware breakpoint on some code and wait for something to read it. That needs a live process, and it shows one check at a time. I did not want one check at a time. I wanted to know what every routine the protection added does, so I took the dump and executed its routines in an emulator.&lt;/p&gt;

&lt;p&gt;The dump is a 693 MB snapshot with the code already unpacked. Map it lazily into &lt;a href="https://www.unicorn-engine.org/" rel="noopener noreferrer"&gt;Unicorn&lt;/a&gt; at its recorded base, give it a stack and a fake TEB and PEB, point &lt;code&gt;rip&lt;/code&gt; at a routine, and record three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;every data &lt;strong&gt;read&lt;/strong&gt; that lands in an executable section;&lt;/li&gt;
&lt;li&gt;every &lt;strong&gt;write&lt;/strong&gt; that does;&lt;/li&gt;
&lt;li&gt;where control ends up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This works far better than it has any right to. The first guard I tried ran to completion with no hand-built state: 53,178 basic blocks, one loop that read 16,982 bytes of code, three 4-byte writes into the code section, and a clean return.&lt;/p&gt;

&lt;p&gt;The reason is in Arxan's design. Each routine computes the image base from its own address, reads its tables out of the code section, and keeps its state in its own stack frame. It has to be self-contained, because it must work no matter which game function it is spliced into. The same property makes every routine runnable in isolation.&lt;/p&gt;

&lt;p&gt;Two details matter for repeatable results. Routines that call Windows need small models of the handful of APIs they use. And &lt;code&gt;rdtsc&lt;/code&gt; has to be pinned to a chosen value, because some routines branch on the timestamp.&lt;/p&gt;

&lt;p&gt;With that in place, an experiment is cheap: run a routine, change one byte, run it again, and compare everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  730 routines, one calling convention
&lt;/h2&gt;

&lt;p&gt;Arxan's routines are entered through an obfuscated call. It is easy to recognise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;push 0x18                         ; how many bytes the return thunk will drop
sub  rsp, 8                       ; 48 81 EC 08 00 00 00 - a scratch slot, in the long encoding
lea  rsp, [rsp-8]                 ; --+
mov  [rsp], rbp                   ;   | push &amp;lt;return thunk&amp;gt;
lea  rbp, [rip+R]                 ;   |
xchg [rsp], rbp                   ; --+
push rbp                          ; --+
lea  rbp, [rip+G]                 ;   | push &amp;lt;routine&amp;gt;
xchg [rsp], rbp                   ; --+
ret                               ; "call" G, which returns to R
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The return address and the target are both planted on the stack with &lt;code&gt;lea&lt;/code&gt;/&lt;code&gt;xchg&lt;/code&gt;, so no &lt;code&gt;call&lt;/code&gt; instruction and no direct reference to the routine exists. The routine &lt;code&gt;G&lt;/code&gt; preserves the non-volatile registers and returns to the thunk &lt;code&gt;R&lt;/code&gt;, which restores the saved context.&lt;/p&gt;

&lt;p&gt;Where the thunk goes after that is not written in the thunk. Its last transfers go through three stack slots that the routine filled in while it ran. The third holds the address of the code that continues the game function, starting with whatever instruction the splice displaced. Those three slots come back when the protection is taken out, because they decide what it takes to remove a routine.&lt;/p&gt;

&lt;p&gt;Chains of these stubs are spliced into game functions. In the case traced end to end below, the function's first five bytes are a &lt;code&gt;jmp&lt;/code&gt; into the chain, and its original first instruction sits in the last thunk.&lt;/p&gt;

&lt;p&gt;There are 730 stubs in the image, and each calls a different routine. Once one of them ran, I ran all of them and sorted them by what they did. That gives four kinds:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Kind&lt;/th&gt;
&lt;th&gt;Count&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Writer&lt;/td&gt;
&lt;td&gt;554&lt;/td&gt;
&lt;td&gt;Copies, repairs or decrypts code. More than half write eight bytes or fewer.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checksum guard&lt;/td&gt;
&lt;td&gt;106&lt;/td&gt;
&lt;td&gt;Reads code as data, publishes a digest, compares it with an expected value.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clock&lt;/td&gt;
&lt;td&gt;52&lt;/td&gt;
&lt;td&gt;Samples the system time.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Other&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Includes the debugger probes.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(Three more did not finish inside the instruction budget, and seven faulted on state the emulator does not model.)&lt;/p&gt;

&lt;p&gt;Control flow inside every routine is cut into chunks of a few instructions, scattered over a 100 MiB section that Arxan appends to the executable. Chunks are joined by &lt;code&gt;jmp rel32&lt;/code&gt; or by computed transfers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;movabs rbp, &amp;lt;target A&amp;gt;            ; planted on the stack
...
movabs rax, &amp;lt;target B&amp;gt;
cmove  rbx, rax                   ; choose between A and B
...
jmp    qword ptr [rsp-8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last form is a conditional branch. A disassembler sees an indirect jump and stops; an emulator just follows it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Anatomy of a checksum guard
&lt;/h2&gt;

&lt;p&gt;A guard is an ordinary function with an &lt;code&gt;rbp&lt;/code&gt; frame. It does seven things.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Find the image
&lt;/h3&gt;

&lt;p&gt;There are no relocations and no imports involved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov rax, [rip+...]        ; 0x140000000, the link-time image base
mov [rbp+0x28], rax
lea rax, [rip-...]        ; my own address, right now
mov rax, [rip+...]        ; my own address at link time
neg rax
add rax, [rbp+0x30]       ; load delta
add rax, [rbp+0x28]       ; runtime base
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Decode what to hash
&lt;/h3&gt;

&lt;p&gt;The ranges are a LEB128 stream of &lt;code&gt;(gap, length)&lt;/code&gt; pairs stored in the code section. Each range starts where the previous one ended, plus the gap. The stream ends when the start reaches &lt;code&gt;0xFFFFFFFF&lt;/code&gt;. For the larger guards the stream is TEA-encrypted with a per-guard key and decrypted first.&lt;/p&gt;

&lt;p&gt;The ranges are sparse. The guard above hashes 16,982 bytes in 83 ranges. Many ranges are a single byte, or exactly five bytes (one &lt;code&gt;jmp rel32&lt;/code&gt;). The gaps skip everything that legitimately changes while the game runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Hash the code
&lt;/h3&gt;

&lt;p&gt;This loop is the only place where code is read as data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;movzx eax, byte ptr [rax]         ; one byte of code
xor   eax, [rbp+0x60]
...                               ; h = (h ^ b) * 0x01000193
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is FNV-1a with a non-standard seed. Two other hashes are in use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a rotate-and-add over dwords (&lt;code&gt;h += rol(w, r); r = (h ^ r) &amp;amp; 31&lt;/code&gt;) with a byte tail;&lt;/li&gt;
&lt;li&gt;full SHA-1, with its round constants split across two tables and XORed together at run time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Hash the relocations
&lt;/h3&gt;

&lt;p&gt;A second list names the pointers that lie inside the hashed ranges. Each is read, converted back to its link-time value, and folded into the hash. The digest therefore does not depend on where the image was loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Publish the digest
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea rdx, [rip+slot]
mov [rdx+rcx*4], eax              ; 89 04 8A
add dword ptr [rbp+0x70], -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard copies its digest, one dword per iteration, into a slot in the code section: a few dead bytes between two chunks. There are 184 of these stores in this image.&lt;/p&gt;

&lt;p&gt;Most of these slots are write-only. About one in nine is read back later, by unrelated code, in the form &lt;code&gt;slot + c1 - c2 == 0&lt;/code&gt;, where &lt;code&gt;c2 - c1&lt;/code&gt; is the expected digest. The published value is a second opinion, taken far from the guard.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Compare
&lt;/h3&gt;

&lt;p&gt;The guard keeps two adjacent pointers in its frame. One points at the digest it just computed, on its stack. The other points at the expected digest, a table of one or five dwords in the code section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov rdx, [rbp+0x40]               ; computed
mov rbx, [rbp+0x48]               ; expected
mov eax, [rdx+rax*4]
mov edx, [rbx+rcx*4]
cmp eax, edx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comparison reads the &lt;strong&gt;stack&lt;/strong&gt; copy, not the published one. Changing &lt;code&gt;eax&lt;/code&gt; at the store in step 5 changes what is published and nothing else.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Act
&lt;/h3&gt;

&lt;p&gt;On a match the guard performs one more store, refreshing a state word elsewhere in the code section, and returns to its thunk. What it does on a mismatch gets its own section.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the guards cover
&lt;/h2&gt;

&lt;p&gt;Only the two executable sections are ever hashed. &lt;code&gt;.rdata&lt;/code&gt;, &lt;code&gt;.data&lt;/code&gt; and the import table never are.&lt;/p&gt;

&lt;p&gt;Coverage is lopsided. Measured over the guards that could be emulated:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Bytes hashed&lt;/th&gt;
&lt;th&gt;Share&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;The game's own code&lt;/td&gt;
&lt;td&gt;about 465 KB&lt;/td&gt;
&lt;td&gt;0.4%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Arxan's appended section&lt;/td&gt;
&lt;td&gt;about 11.3 MB&lt;/td&gt;
&lt;td&gt;10.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The guards mostly guard each other:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;40 of 105 guard entry points lie inside some other guard's ranges.&lt;/li&gt;
&lt;li&gt;A third of the expected-digest words do.&lt;/li&gt;
&lt;li&gt;A quarter of the publish instructions do, and at least 29 of the 106 guards hash one or more of them. An inline hook on a publish instruction is itself a modification that other guards see.&lt;/li&gt;
&lt;li&gt;About 2 MB is hashed by two or three guards at once.&lt;/li&gt;
&lt;li&gt;No publish slot lies inside any hashed range. Guards do not chain by hashing each other's results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One more property follows from the next section but belongs here. Run against the dump, 36 of the 106 guards compute a digest that does not match. Every one of them hashes code that the writer routines decrypt just before use and encrypt again afterwards. The dump caught those functions at rest. Such a guard only passes when it runs at the right moment in the sequence: after the writer that prepares its code, and before the one that hides it again.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a failed check does
&lt;/h2&gt;

&lt;p&gt;This was the experiment I had wanted the emulator for. Take a guard that passes. Flip one bit in one byte it hashes. Run it again and compare the path, the writes, the registers and where it returns.&lt;/p&gt;

&lt;p&gt;Inside the hash, nothing changes except the value. The first difference is after the comparison. Of 65 guards tested, 55 respond the same way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov eax, [rip+K]                  ; a constant; for this guard 0x448E92B0
mov eax, eax
add rax, [rbp+0x20]               ; + runtime image base
mov rcx, [rbp+0x28]
sub rcx, 8
mov [rcx], rax                    ; my own return address := base + K
mov eax, 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard overwrites &lt;strong&gt;its own return address&lt;/strong&gt;, skips the state refresh, and returns normally. The fault happens on the &lt;code&gt;ret&lt;/code&gt;, in the guard's epilogue, at an address 1 GiB above the image, with a stack that no longer records who called.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where bit 30 comes from
&lt;/h3&gt;

&lt;p&gt;Crashes caused by this response always have the form &lt;em&gt;valid code address + &lt;code&gt;0x40000000&lt;/code&gt;&lt;/em&gt;. It looks like a flag bit. It is not.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;K&lt;/code&gt; is stored in the code as &lt;code&gt;0x1_4000_0000 + rva&lt;/code&gt;: a link-time virtual address, against the preferred image base. About 3,000 such constants are embedded in the code, with no relocation entry. Truncate one to 32 bits and you get &lt;code&gt;rva + 0x40000000&lt;/code&gt;. Add the real runtime base, as the failure path does, and you land exactly 1 GiB above a real address.&lt;/p&gt;

&lt;p&gt;No instruction ever sets bit 30. It is what remains of &lt;code&gt;0x140000000&lt;/code&gt; after a rebase that is done wrong on purpose.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;rva&lt;/code&gt; is not the guard's legitimate return address. It is the place in game code where the guard chain was spliced in. In the case I could run end to end, that is the first five bytes of the protected function.&lt;/p&gt;

&lt;h3&gt;
  
  
  The quieter responses
&lt;/h3&gt;

&lt;p&gt;Not every guard poisons its return.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Four of the 65 return to their thunk normally and simply &lt;strong&gt;skip the state refresh&lt;/strong&gt; they perform on success.&lt;/li&gt;
&lt;li&gt;A separate failure helper &lt;strong&gt;overwrites a state word&lt;/strong&gt; with a scrambled copy of its correct value: &lt;code&gt;y = x ^ (x &amp;gt;&amp;gt; 2)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;State words are consumed elsewhere. This is one consumer, in a return thunk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov edx, [rip+D]                  ; the state word
add edx, [rip+A]
xor edx, [rip+B]
add rsp, rdx                      ; zero when D is right
add rsp, [rsp+8]
pop rdx
jmp body
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the right value, &lt;code&gt;(D + A) ^ B&lt;/code&gt; is zero and the instruction does nothing. With the scrambled value it is &lt;code&gt;0x1150787F&lt;/code&gt;: the stack pointer moves 277 MiB. The next stack access faults, and the exception cannot be delivered, because there is no stack to build the frame on.&lt;/p&gt;

&lt;p&gt;The same word is also tested by a conditional branch somewhere else entirely.&lt;/p&gt;

&lt;p&gt;So the check and the penalty are separated in both space and time. The guard that detects a change does not crash. It leaves a wrong number behind, and a different function, later, uses it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A poisoned return cannot be undone afterwards
&lt;/h2&gt;

&lt;p&gt;My first idea for living with a poisoned return was to treat it as recoverable: catch the execute fault, subtract &lt;code&gt;0x40000000&lt;/code&gt; from &lt;code&gt;rip&lt;/code&gt;, continue. The emulator shows what that does.&lt;/p&gt;

&lt;p&gt;One guard protects a function whose entry is still the five-byte &lt;code&gt;jmp&lt;/code&gt; into its guard chain. Start at that entry with known registers and a marker for a return address, and see whether execution reaches the function's body.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Run&lt;/th&gt;
&lt;th&gt;Reaches the body&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;rsp&lt;/code&gt; vs entry&lt;/th&gt;
&lt;th&gt;Registers&lt;/th&gt;
&lt;th&gt;Return address&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Clean&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;all preserved&lt;/td&gt;
&lt;td&gt;intact&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One byte flipped&lt;/td&gt;
&lt;td&gt;no, faults at entry + &lt;code&gt;0x40000000&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;−416&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt; clobbered&lt;/td&gt;
&lt;td&gt;gone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped, fault redirected to the unbiased address&lt;/td&gt;
&lt;td&gt;never&lt;/td&gt;
&lt;td&gt;−416 per attempt&lt;/td&gt;
&lt;td&gt;clobbered&lt;/td&gt;
&lt;td&gt;gone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same, all clocks advanced 10 s per attempt&lt;/td&gt;
&lt;td&gt;never&lt;/td&gt;
&lt;td&gt;−416 per attempt&lt;/td&gt;
&lt;td&gt;clobbered&lt;/td&gt;
&lt;td&gt;gone&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The unbiased address is the entry of the protected function, which is the jump into the guard chain. The guard runs again, fails again, and returns to the same poisoned address.&lt;/p&gt;

&lt;p&gt;Every attempt leaves 416 bytes of dead frame behind, because the guard never unwound, and the argument registers hold whatever the guard left in them. Advancing every clock the routines can read changes nothing, so this is not a schedule waiting to expire. A 1 MiB stack lasts about 2,520 attempts.&lt;/p&gt;

&lt;p&gt;The response is not a detour that can be stepped around. By the time the fault happens, the state needed to continue is already gone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writers: repair and decryption
&lt;/h2&gt;

&lt;p&gt;Guards are a seventh of the routines. Three quarters are writers.&lt;/p&gt;

&lt;p&gt;They are built around about 340 copies of one small &lt;code&gt;memcpy&lt;/code&gt;: a dword loop and a byte tail. The same helper is used three ways.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read.&lt;/strong&gt; Copy code into a stack buffer, eight bytes at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repair.&lt;/strong&gt; Write a range back to the bytes it should hold. The good bytes come from an encoded copy in the protection's own section, the stash, decoded a dword at a time with a key hidden in filler bytes between instructions. The ranges are another LEB128 &lt;code&gt;(gap, length)&lt;/code&gt; stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decrypt.&lt;/strong&gt; The same operation, pointed at a function that is not yet code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fifty-nine game functions are stored unreadable and produced on demand, and so are the entries of a family of much smaller subroutines. The chapter on the file takes that mechanism apart, including the part that is easy to get wrong: a writer never reads what it overwrites.&lt;/p&gt;

&lt;p&gt;On disk the picture is starker. All but 235 of the game's 29,329 code pages are noise, and the file has to unpack itself before its first instruction can run. That is where the next chapter starts.&lt;/p&gt;

&lt;p&gt;Two facts about how the writes happen matter later:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One start-up routine walks the image with &lt;code&gt;VirtualQuery&lt;/code&gt; and &lt;code&gt;VirtualProtect&lt;/code&gt; and makes every section readable, writable and executable.&lt;/li&gt;
&lt;li&gt;After that, no routine changes page protection around its own writes. It just writes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For anyone placing a hook: ordinary function prologues are not rewritten, but anything inside an on-demand function will be overwritten the next time its writer runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clocks and scheduling
&lt;/h2&gt;

&lt;p&gt;The 52 clock routines read interrupt time and system time straight from &lt;code&gt;KUSER_SHARED_DATA&lt;/code&gt;, with no API call.&lt;/p&gt;

&lt;p&gt;Guards have scheduling gates in front of their hash loops: counters stored in the code section, and timestamps. A guard does not hash every time it is reached. Presumably this keeps megabytes of hashing from costing a frame. It also means the moment a change is noticed is not the moment it was made.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debugger probes
&lt;/h2&gt;

&lt;p&gt;Everything Arxan does that looks outward at the process is one bundle of six probes, stamped into the image 294 times:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Probe&lt;/th&gt;
&lt;th&gt;How "no debugger" is established&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PEB.BeingDebugged&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the byte is zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NtQueryInformationProcess(ProcessDebugObjectHandle)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the query fails with "port not set"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;CloseHandle(0x12345)&lt;/code&gt; under a vectored handler&lt;/td&gt;
&lt;td&gt;the invalid-handle exception a debugger would cause never arrives&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ud2&lt;/code&gt; under a vectored handler&lt;/td&gt;
&lt;td&gt;the handler runs and steps over it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;int 2Dh&lt;/code&gt; under a vectored handler&lt;/td&gt;
&lt;td&gt;the handler runs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;int3&lt;/code&gt; under &lt;code&gt;SetUnhandledExceptionFilter&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;the filter runs, which only happens with no debugger attached&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One instance, run end to end:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It picks a probe from &lt;code&gt;rdtsc % 5&lt;/code&gt;. It assembles the &lt;code&gt;rdtsc&lt;/code&gt; on the fly, storing &lt;code&gt;90 90 0F 31&lt;/code&gt; into the code section and then executing it.&lt;/li&gt;
&lt;li&gt;For the query it calls &lt;code&gt;LoadLibraryA("ntdll.dll")&lt;/code&gt; and &lt;code&gt;GetProcAddress(..., "NtQueryInformationProcess")&lt;/code&gt;, then frees the library again. Both strings are built on the stack from two XOR pads and wiped after use. They are the only two protected strings in the whole protection.&lt;/li&gt;
&lt;li&gt;When the query reports a debug object, the routine answers with the same poisoned return the checksum guards use. There is one response primitive for everything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The vectored handlers exist only for the duration of a probe: registered first in the chain, removed straight after. They read &lt;code&gt;CONTEXT.Rax&lt;/code&gt; and &lt;code&gt;Rip&lt;/code&gt; and nothing else. Arming a hardware breakpoint or setting &lt;code&gt;PEB.BeingDebugged&lt;/code&gt; changes the outcome of none of the exception probes. They test that exceptions are &lt;strong&gt;delivered&lt;/strong&gt; to the process, not what the debug registers hold.&lt;/p&gt;

&lt;p&gt;That has a consequence for tool authors. A first-chance handler that swallows illegal-instruction or breakpoint exceptions breaks these probes, and the probe then reports a debugger that is not there.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it does not do
&lt;/h3&gt;

&lt;p&gt;Searched for and not found in Arxan's code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;thread hiding;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;NtGlobalFlag&lt;/code&gt; or heap-flag checks;&lt;/li&gt;
&lt;li&gt;timing checks (every &lt;code&gt;rdtsc&lt;/code&gt; feeds a random gate; none is subtracted from another);&lt;/li&gt;
&lt;li&gt;debug-register inspection;&lt;/li&gt;
&lt;li&gt;kernel-debugger checks;&lt;/li&gt;
&lt;li&gt;process, window or module enumeration;&lt;/li&gt;
&lt;li&gt;reads of system DLL code;&lt;/li&gt;
&lt;li&gt;direct system calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Its whole API vocabulary is nine functions: &lt;code&gt;Add&lt;/code&gt;- and &lt;code&gt;RemoveVectoredExceptionHandler&lt;/code&gt;, &lt;code&gt;SetUnhandledExceptionFilter&lt;/code&gt;, &lt;code&gt;CloseHandle&lt;/code&gt;, &lt;code&gt;GetCurrentProcess&lt;/code&gt;, &lt;code&gt;LoadLibraryA&lt;/code&gt;, &lt;code&gt;GetProcAddress&lt;/code&gt;, &lt;code&gt;FreeLibrary&lt;/code&gt; and &lt;code&gt;NtQueryInformationProcess&lt;/code&gt;. The imported ones are reached with &lt;code&gt;push rax; ret&lt;/code&gt;, never with a &lt;code&gt;call&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A weakness: every guard publishes before it judges
&lt;/h2&gt;

&lt;p&gt;By now the options had narrowed. Hooks are code, and code is what the guards hash. The response cannot be undone after the fact. That left the moment in between, and I went back to the guard's seven steps to look for it.&lt;/p&gt;

&lt;p&gt;Step 5 happens before step 6. Every guard writes its digest into the code section &lt;strong&gt;before&lt;/strong&gt; it compares. A write is something a process can be notified about without changing any code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzqy3frb0fons5un6txsl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzqy3frb0fons5un6txsl.png" alt="Figure 1. A guard publishes its digest into a code page before it compares. A page fault on that one store is enough to see the guard's frame at the right moment." width="799" height="277"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 1. A guard publishes its digest into a code page before it compares. A page fault on that one store is enough to see the guard's frame at the right moment.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Two things make that usable. Sections are made writable once, at start-up. And Arxan's routines cannot notice a change afterwards: nothing in their nine-function vocabulary queries or sets page protection. So take the pages that hold the 184 publish slots (171 pages) and set them back to read-and-execute. Nothing in the image is modified. The next time a guard publishes, the store faults, and the handler sees a live guard frame at exactly the right moment:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;rcx&lt;/code&gt; is the index and &lt;code&gt;eax&lt;/code&gt; is the computed digest word.&lt;/li&gt;
&lt;li&gt;Scan the frame for the two adjacent pointers from step 6: one outside the image whose dword at that index equals &lt;code&gt;eax&lt;/code&gt;, followed by one into an executable section. That is the guard's own &lt;code&gt;{computed*, expected*}&lt;/code&gt; pair.&lt;/li&gt;
&lt;li&gt;If the expected word differs, write it over the computed word on the guard's stack and put it in &lt;code&gt;eax&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Complete the store by hand, skip the three-byte instruction, and continue.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The guard goes on to compare two equal values. It takes its normal pass path, performs its state refresh, and returns to its thunk. Everything downstream sees a guard that passed, because the guard itself believes it passed.&lt;/p&gt;

&lt;p&gt;The correction itself is the obvious one for a checksum guard: replace the computed checksum with the expected one in the guard's own context. What is unusual is the delivery, a page fault instead of a patch. Because no code byte changes, there is nothing for any other guard to notice.&lt;/p&gt;

&lt;p&gt;The core of the handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rip&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;page_protected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rcx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;computed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rax&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;computed_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;expected_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;computed_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expected_ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;expected_ptr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;computed_ptr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// what the guard is about to compare&lt;/span&gt;
            &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;store_through&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// the write the guard asked for&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rip&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// past mov [rdx+rcx*4], eax&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;EXCEPTION_CONTINUE_EXECUTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other stores land on those pages too: writer routines, and the guards' own state refreshes. The handler completes plain &lt;code&gt;mov&lt;/code&gt; stores from the instruction bytes and lets anything more exotic run once with the page writable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Proof by emulation
&lt;/h3&gt;

&lt;p&gt;The emulator can model this faithfully, because the handler needs nothing a real one would not have: the instruction bytes at &lt;code&gt;rip&lt;/code&gt;, the registers, and readable memory. Pages are write-protected inside Unicorn, the write fault is handed to a Python port of the handler, and the guard's real code runs on either side of it.&lt;/p&gt;

&lt;p&gt;For every guard that passes on the dump, four runs from its real call stub: clean, one byte flipped, and both again with the protection armed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Guards&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Tested&lt;/td&gt;
&lt;td&gt;65&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped byte is in code the guard verifies&lt;/td&gt;
&lt;td&gt;59&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;...detected with no mitigation&lt;/td&gt;
&lt;td&gt;59 of 59 (55 poisoned return, 4 withheld refresh)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;...armed: same outcome as the clean run&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;59 of 59&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped byte is in the guard's own range table&lt;/td&gt;
&lt;td&gt;3 (the guard decodes garbage and never publishes, armed or not)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flipped byte has no visible effect&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Clean code, armed: same outcome as unarmed&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;65 of 65&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;"Same outcome" means the same return thunk, the same stack pointer, every general register equal, and every code slot the routine wrote holding the same final value.&lt;/p&gt;

&lt;p&gt;The handler corrected one digest word for 40 guards and five for 19 (the SHA-1 family). It never failed to identify the context.&lt;/p&gt;

&lt;p&gt;The function-entry experiment from earlier, with the protection armed:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Run&lt;/th&gt;
&lt;th&gt;Reaches the body&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;rsp&lt;/code&gt; vs entry&lt;/th&gt;
&lt;th&gt;Registers&lt;/th&gt;
&lt;th&gt;Handler&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Clean&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;all preserved&lt;/td&gt;
&lt;td&gt;digest &lt;code&gt;0x73008F99&lt;/code&gt; matched&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One byte flipped&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;yes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;all preserved&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;0x73009799&lt;/code&gt; corrected to &lt;code&gt;0x73008F99&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The handler knows no addresses. It finds the publish stores by their instruction bytes, so the same experiment can be put to a build it was never looked at on. In 1.20, a build twenty months older, there are 200 stores of that shape, 166 of them with their slot named by the &lt;code&gt;lea&lt;/code&gt; in front, on 168 pages. Ten functions whose path runs through a guard that passes were each run three ways with the protection's routines executing: clean, with one bit flipped in one code byte the guard hashes, and flipped with the correction applied at the store.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20, ten functions&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;flipped: ends at the function's own entry + &lt;code&gt;0x40000000&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;flipped and corrected: ends where the clean run ends, in the same state&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The first of them is the function at &lt;code&gt;0x15A7200&lt;/code&gt;. With the byte at &lt;code&gt;0x14DD8F1&lt;/code&gt; flipped, its guard leaves five digest words wrong and the run ends on a fetch from image + &lt;code&gt;0x415A7200&lt;/code&gt;. With the correction, five words are corrected and the run ends at the instruction the clean one ends at. It is the same poisoned return, with the same missing bit of &lt;code&gt;0x140000000&lt;/code&gt;, in a build from May 2020.&lt;/p&gt;

&lt;p&gt;The handler also exists as native code. Its test builds a routine with the guard's exact publish-and-compare shape in a buffer, confirms that the routine detects a wrong digest on its own, then arms the protection and confirms that the routine can no longer tell.&lt;/p&gt;

&lt;h3&gt;
  
  
  In the running game
&lt;/h3&gt;

&lt;p&gt;Then I put the native handler into a real start of the shipped 1.20, loaded inside the game process before the protection's start-up has run. Its own scan of the running image finds the 200 stores and names all 200 slots: the 34 that have no &lt;code&gt;lea&lt;/code&gt; in front of the store are named by the &lt;code&gt;lea&lt;/code&gt; that ends the block whose jump lands on it. That makes 200 pages to protect.&lt;/p&gt;

&lt;p&gt;The first start ended at 18.6 seconds on a write fault in a byte-copy loop of the protection, one page past a protected one. The start-up pass copies code into the game's pages one byte at a time, and it runs straight across page boundaries. Completing a store at the end of a protected page had left the page after it read-only too, and that page was not one of the 200. The whole image is writable at that point, so the loop's next byte there faulted with nobody to handle it. A protected page has to give every neighbour back exactly the protection it had.&lt;/p&gt;

&lt;p&gt;The same copy pass is also what the handler costs. It writes through almost every page that holds a slot. Armed as soon as the stores were decrypted, the handler completed about 700,000 of its single-byte stores before the splash screen, and the game window came up eight seconds late. Armed once the slot pages have stopped changing, which is about two and a half seconds later, it completes about 52,000, and the splash and the window come up when they do without it. In the menus it goes on completing 2,000 to 4,000 unrelated stores a second, which I estimate at about 3% of one core.&lt;/p&gt;

&lt;p&gt;The running process also has a moment the dump never showed. From about five seconds into the start, runs of the code pages cannot be read at all, and a scan that reads the sections from end to end faults. So the scan walks only the pages that can be read, and the handler only ever takes write access away: a page the program has made inaccessible is left that way.&lt;/p&gt;

&lt;p&gt;The handler logs each store the first time it publishes. Fifty stores first publish after the splash. The emulator had reached 27 of them, and what those guards read says what they hash: every one hashes the protection's own section, one of them a single run of 6.6 MB, and none hashes the game's code. The guards that do hash the game's code, up to 16.4 MB of it, did not run in the menus at all.&lt;/p&gt;

&lt;p&gt;The last test was a change for them to find. Right at the splash, I overwrote five bytes of that 6.6 MB run with the bytes an inline hook writes, &lt;code&gt;E9&lt;/code&gt; and a 32-bit displacement, and put them back ten seconds later.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;1.20, shipped, five hook bytes in hashed code&lt;/th&gt;
&lt;th&gt;handler off&lt;/th&gt;
&lt;th&gt;handler armed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;what followed&lt;/td&gt;
&lt;td&gt;a poisoned return 0.86 s later&lt;/td&gt;
&lt;td&gt;a guard published five wrong words 3.3 s later; all five corrected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the process&lt;/td&gt;
&lt;td&gt;crashed at 14.3 s, before its window&lt;/td&gt;
&lt;td&gt;splash and window on time, still running at 90 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;publishes seen&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;171 matched, 5 corrected, none without a context&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It was not the guard I had picked from the emulator that caught it. Another guard, one the emulator had never reached, hashes the same bytes and ran first. Coverage overlaps, and the handler does not need to know which guard it is.&lt;/p&gt;

&lt;p&gt;This build cannot reach its online services, and it quits on its own after one to two minutes, with the handler armed or not, so every start was compared with one without it.&lt;/p&gt;

&lt;p&gt;The handler carries no table, so I started ten other versions the same way, from September 2019 to the newest: each build's own shipped executable, run on 1.20's game data, gets as far as its splash screen before it meets data that is not its own. The store shape is in every one of them, and every store has its slot named:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;version&lt;/th&gt;
&lt;th&gt;stores with their slot&lt;/th&gt;
&lt;th&gt;pages&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.01.2&lt;/td&gt;
&lt;td&gt;166&lt;/td&gt;
&lt;td&gt;167&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.07.0&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.20&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.23.2&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.30.0&lt;/td&gt;
&lt;td&gt;201&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.31.5&lt;/td&gt;
&lt;td&gt;201&lt;/td&gt;
&lt;td&gt;204&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.39.3&lt;/td&gt;
&lt;td&gt;202&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.41.2&lt;/td&gt;
&lt;td&gt;202&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.51.2&lt;/td&gt;
&lt;td&gt;184&lt;/td&gt;
&lt;td&gt;176&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.64.0&lt;/td&gt;
&lt;td&gt;185&lt;/td&gt;
&lt;td&gt;184&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;183&lt;/td&gt;
&lt;td&gt;182&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Where guards published before a build gave up, every digest was already right: 87 publishes in 0.01.2, 17 in 1.23.2. 1.69.0 stops before its splash on 1.20's data, armed or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In the running game it has been put to one change, in the menus.&lt;/strong&gt; The guards that ran there and that the emulator reached hash the protection's own section. The guards that hash the game's own code did not run in the menus, so for those the correction is still shown only in the emulator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only 65 of the 106 guards could be tested against a clean baseline.&lt;/strong&gt; The rest hash on-demand code that was at rest in the dump. They publish with the same instruction the mitigation keys on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The 730 stubs reach 101 of the 184 publish instructions.&lt;/strong&gt; How the guards behind the other 83 are entered is not known. The mitigation covers them by their instruction shape; the tests do not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Five routines publish with a different instruction&lt;/strong&gt; and are not covered.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It does not address the debugger probes.&lt;/strong&gt; Those are a separate mechanism with the same response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There is a window.&lt;/strong&gt; While the handler has a page writable to complete a store, another thread's publish to the same page goes through unseen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It costs something.&lt;/strong&gt; The protection keeps writing to the pages that hold slots, and every such store is a fault. At start-up that is avoided by arming late; in the menus it is a few thousand faults a second.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Part 2: The file on disk&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A file that cannot be read
&lt;/h2&gt;

&lt;p&gt;By this point I had a working model of the protection in a running process, and all of it had come from one memory dump. A dump is one moment of a program that keeps rewriting itself. It could not tell me how the process had got into that state, and it is not something to hand to a disassembler and trust. The file that produces the process is a different object: 297 MB on disk against 693 MB mapped, and almost none of its code can be read.&lt;/p&gt;

&lt;p&gt;So the second stretch of the work started from the shipped file, with the aim of arriving at the running image without ever running anything. The method stayed the same, with one more step at the end:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;look at the bytes and ask the smallest question they raise;&lt;/li&gt;
&lt;li&gt;answer it by running Arxan's own code in the emulator;&lt;/li&gt;
&lt;li&gt;replace the emulation with a plain re-implementation, and check that against the memory dump.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last step is what turns an observation into a tool, and the tool grew one question at a time. It is a static decryptor I called &lt;code&gt;mwdec&lt;/code&gt;: it reads a file and writes a file, and it never loads or runs the game.&lt;/p&gt;

&lt;p&gt;I began with the obvious comparison: the shipped executable and the memory dump, mapped by RVA, section by section.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;section&lt;/th&gt;
&lt;th&gt;size&lt;/th&gt;
&lt;th&gt;entropy on disk&lt;/th&gt;
&lt;th&gt;equal to the running image&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (game)&lt;/td&gt;
&lt;td&gt;120.1 MB&lt;/td&gt;
&lt;td&gt;7.991&lt;/td&gt;
&lt;td&gt;3.67%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.rdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5.3 MB&lt;/td&gt;
&lt;td&gt;5.936&lt;/td&gt;
&lt;td&gt;99.7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.data&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;460.8 MB (mostly zero-fill)&lt;/td&gt;
&lt;td&gt;0.260&lt;/td&gt;
&lt;td&gt;n/a, written at run time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.pdata&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1.5 MB&lt;/td&gt;
&lt;td&gt;7.198&lt;/td&gt;
&lt;td&gt;100.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.rsrc&lt;/code&gt;, &lt;code&gt;.reloc&lt;/code&gt;, &lt;code&gt;_RDATA&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;1.8 MB&lt;/td&gt;
&lt;td&gt;5.5 to 7.5&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (protection)&lt;/td&gt;
&lt;td&gt;103.9 MB&lt;/td&gt;
&lt;td&gt;7.959&lt;/td&gt;
&lt;td&gt;11.08%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Eight bits per byte is what random data measures. Both code sections are ciphertext, and nothing else is. The headers, the import directory, the relocation table, the TLS directory, the exception directory and the resources are all in the clear. Windows can map this file, relocate it, bind its imports, register its unwind data and call its TLS callbacks with no help from the protection. Only the code is locked, and the loader never needs to read code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftc6xvjuygn6h4e87hm9p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftc6xvjuygn6h4e87hm9p.png" alt="Figure 2. The shipped file. Everything the Windows loader reads is plain; the two code sections are ciphertext except for the few islands the first instructions need." width="800" height="307"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 2. The shipped file. Everything the Windows loader reads is plain; the two code sections are ciphertext except for the few islands the first instructions need.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What has to be readable
&lt;/h3&gt;

&lt;p&gt;A file whose code is entirely ciphertext cannot start, so my next question was what the first executed instruction is, and whether it can be read. The loader calls TLS callbacks first, then the entry point. On disk the entry point is nine plaintext bytes followed by noise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;070D9224  48 83 EC 28        sub  rsp, 28h
070D9228  E8 9E ED A2 FA     call 01B07FCB
070D922D  ...                ; ciphertext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The TLS directory lists six callbacks. Five begin with bytes that decode to nothing. The first begins with &lt;code&gt;jmp 01A274F7&lt;/code&gt;. Both targets are readable, and they contain the same thing: fourteen register pushes, sixteen XMM saves, a stack alignment, and then the call stub every routine is entered through. The code that unlocks the file is not special. It is ordinary Arxan routines, reached through the ordinary stub, stored in plaintext.&lt;/p&gt;

&lt;p&gt;Comparing the file with the dump for long runs that already agree shows how much plaintext there is: 233 runs of 256 bytes or more in the game's code, 901 KB in total, and two large islands in the protection's section, 4.4 MB at &lt;code&gt;0x25A8CA90&lt;/code&gt; and a 4.8 MB tail that holds, among other things, the relocation table.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you relocate ciphertext?
&lt;/h3&gt;

&lt;p&gt;The image is built for &lt;code&gt;0x140000000&lt;/code&gt; and loaded somewhere else, and the loader fixes that by adding the difference to every address stored in the image. The relocation table lists 422,110 such places: 34,282 inside the game's code and 251,874 inside the protection's.&lt;/p&gt;

&lt;p&gt;That raised a question I had to answer before going any further, because it looks impossible for an encrypted file. The loader adds a number to eight bytes of &lt;em&gt;ciphertext&lt;/em&gt;, before any decryption has run, and decrypting afterwards would give garbage at every one of those places. Looking at the sites directly settled it. At each relocation site inside the code sections, the eight bytes on disk are a readable link-time pointer, and the bytes on either side are noise. The encryption steps around every relocation. So whatever decrypts this file must have a list of what to skip, and that list has at least 286,000 holes in it.&lt;/p&gt;

&lt;h2&gt;
  
  
  One keystream, cut into 1.3 million pieces
&lt;/h2&gt;

&lt;p&gt;With two readable entry stubs I could start the file in the emulator the way the loader would: map the disk file section by section, point every import at a stub that logs the call, and call TLS callback 0 with the loader's arguments.&lt;/p&gt;

&lt;p&gt;The first thing it does is ask Windows for access: &lt;code&gt;GetSystemInfo&lt;/code&gt;, then a &lt;code&gt;VirtualQuery&lt;/code&gt; and a &lt;code&gt;VirtualProtect&lt;/code&gt; for the headers and each of the nine sections, every one asking for &lt;code&gt;PAGE_EXECUTE_READWRITE&lt;/code&gt;. That is 21 API calls, and there are no others for the rest of the callback. Then the bytes start changing, front to back, and sampling the instruction pointer shows the emulator in two small loops: a byte-by-byte copy, and a longer loop that is plaintext on disk and can simply be read.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov eax,[rbp+1Ch] ; shl eax,4
mov edx,[rbp+1Ch] ; shr edx,5 ; xor eax,edx ; add eax,[rbp+1Ch]     ; ((v1&amp;lt;&amp;lt;4) ^ (v1&amp;gt;&amp;gt;5)) + v1
mov edx,[rbp+20h] ; and edx,3 ; mov rcx,[rbp] ; mov edx,[rcx+rdx*4] ; key[sum &amp;amp; 3]
add edx,[rbp+20h] ; xor eax,edx ; add eax,[rbp+18h] ; mov [rbp+18h],eax  ; v0 += ...
mov eax,[rbp+14h] ; add eax,[rbp+20h] ; mov [rbp+20h],eax           ; sum += delta
...                                    shr edx,0Bh ; and edx,3      ; key[(sum&amp;gt;&amp;gt;11) &amp;amp; 3]
add dword [rbp+24h],1 ; cmp [rbp+24h],[rbp+10h] ; jb loop           ; rounds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two shifts by 4 and 5, a key word chosen by the low two bits of a running sum, then the same again with bits 11 and 12. This is the XTEA block function, in the encipher direction. Knowing the block function is not knowing the cipher, though. The question that decides everything is what goes &lt;em&gt;into&lt;/em&gt; it and what happens to what comes out, and a hook at the head of the loop and another at its tail, logging the frame for the first 600 blocks, answers it.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;parameter&lt;/th&gt;
&lt;th&gt;value&lt;/th&gt;
&lt;th&gt;where it is stored&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;key&lt;/td&gt;
&lt;td&gt;&lt;code&gt;7B7006CF 0F833A93 A21A3354 13F7BA9F&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RVA &lt;code&gt;0x23BF3897&lt;/code&gt;, plaintext&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sum increment&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x9E3779B9&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;immediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rounds&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;immediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;first input block&lt;/td&gt;
&lt;td&gt;&lt;code&gt;E8B9C3BC A0A8028D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;RVA &lt;code&gt;0x26D860DC&lt;/code&gt;, plaintext&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The inputs are the surprise. Every block's input is the previous block's output. No block ever takes its input from the image. And each byte the copy loop stores is the byte that was already at that address, XORed with the next byte of the block outputs taken in order. So the cipher never touches the code. It enciphers its own 8-byte state over and over, and the successive states are a keystream that is XORed into the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;S(0)   = the stored block
S(i+1) = XTEA(S(i))
plaintext = ciphertext XOR (S(1) || S(2) || S(3) || ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is output-feedback mode, and it has three consequences. Encryption and decryption are the same operation. It is strictly sequential: block &lt;em&gt;n&lt;/em&gt; cannot be computed without the &lt;em&gt;n&lt;/em&gt; − 1 before it. And position is everything: the keystream carries no trace of where it is being applied, so the right list of addresses in the wrong order makes every byte after the first mistake wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  The list
&lt;/h3&gt;

&lt;p&gt;The keystream ran straight across the boundary between two separate destination ranges without restarting, so the one thing still missing is the list of ranges, in order. The store log gives its first entries: 16 bytes at &lt;code&gt;0x1000&lt;/code&gt;, then 28 at &lt;code&gt;0x7004&lt;/code&gt;, 28 at &lt;code&gt;0xD014&lt;/code&gt;, 28 at &lt;code&gt;0x13024&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A checksum guard describes what it hashes as a stream of LEB128 &lt;code&gt;(gap, length)&lt;/code&gt; pairs. If the bootstrap uses the same encoding, its list begins with &lt;code&gt;80 20 10&lt;/code&gt; (gap &lt;code&gt;0x1000&lt;/code&gt;, length 16) followed by &lt;code&gt;F4 BF 01 1C&lt;/code&gt; repeated. I searched the file for those bytes and got one hit, at RVA &lt;code&gt;0x25A8CA90&lt;/code&gt;: the first byte of the 4.4 MB plaintext island. Decoded from there the list is 2,774,020 bytes long and has 1,324,188 ranges covering 208,856,213 bytes: 96.69% of the game's code section, 89.15% of the protection's, and under 22,000 bytes spread across the data sections. It ends with a gap that moves a 32-bit cursor to exactly &lt;code&gt;0xFFFFFFFF&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The most common gap is 8 bytes, and of 23,166 eight-byte gaps sampled, 21,997 sit exactly on a relocation site. That is the answer to the relocation question. The code is not encrypted as a block with exceptions. It is encrypted as 1.3 million fragments that fit between the things the loader has to touch, and the list also steps over the plaintext islands, the key, the starting block, and itself.&lt;/p&gt;

&lt;p&gt;Key, starting block, round count and list are all plaintext in the file. Nothing is derived from the machine, the time or the process. So the first version of &lt;code&gt;mwdec&lt;/code&gt; did exactly one thing: it ran one XTEA keystream across all 1.3 million ranges in order, 26,107,027 blocks, in 4.1 seconds, where the emulator needs about fifteen minutes for the same work. Rebased to the dump's load address and compared:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;section&lt;/th&gt;
&lt;th&gt;entropy before&lt;/th&gt;
&lt;th&gt;after&lt;/th&gt;
&lt;th&gt;equal to the running image before&lt;/th&gt;
&lt;th&gt;after&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (game)&lt;/td&gt;
&lt;td&gt;7.991&lt;/td&gt;
&lt;td&gt;6.916&lt;/td&gt;
&lt;td&gt;3.67%&lt;/td&gt;
&lt;td&gt;99.05%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.text&lt;/code&gt; (protection)&lt;/td&gt;
&lt;td&gt;7.959&lt;/td&gt;
&lt;td&gt;6.743&lt;/td&gt;
&lt;td&gt;11.08%&lt;/td&gt;
&lt;td&gt;96.25%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because the keystream is positional, this is stronger than the percentages suggest. One wrong range anywhere in the list would have turned everything after it into noise. Agreement at the far end of the second section means all 1.3 million ranges are right. What still differs, 5.0 MB in 75,253 places, turns out not to be "still encrypted" at all, and the section on memory dumps near the end comes back to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the protection did to the entry point
&lt;/h2&gt;

&lt;p&gt;With the code decrypted I wanted to read the entry paths instead of emulating them. That still took a tool, because no path is contiguous. Each is a trail of one or two instructions at a time, scattered across both code sections and joined by jumps, and the jumps themselves are disguised. Three spellings have to be undone before a path becomes a listing, and they matter for everything that follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; "jmp X"
lea rsp,[rsp-8] ; mov [rsp],rbp ; lea rbp,[rip+X] ; xchg [rsp],rbp
lea rsp,[rsp+8] ; jmp [rsp-8]

; "jne B", falling through to A
push rbp ; movabs rbp,A ; xchg [rsp],rbp          ; push A
push rcx ; push rdx
mov rcx,[rsp+10h] ; movabs rdx,B ; cmovne rcx,rdx ; mov [rsp+10h],rcx
pop rdx ; pop rcx ; ret

; "call G", returning to R
push R ; push G ; ret                              ; each push built as above
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first hides the target of a jump from anything that looks for &lt;code&gt;E9&lt;/code&gt; or &lt;code&gt;FF 25&lt;/code&gt;. The second replaces a conditional branch with a conditional &lt;em&gt;move&lt;/em&gt; between two absolute addresses, followed by a &lt;code&gt;ret&lt;/code&gt;, so a disassembler following control flow sees no branch at all. It is also where the quarter of a million relocations in the protection's section come from: &lt;code&gt;movabs rbp, A&lt;/code&gt; holds a full 64-bit address, so the loader has to fix it up, so it needs a relocation entry, so the bootstrap's list has to step around it. 250,178 of the 251,874 are &lt;code&gt;movabs&lt;/code&gt; immediates.&lt;/p&gt;

&lt;p&gt;Followed through those idioms with a small symbolic stack, the entry point reads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;070D9224  sub  rsp, 28h
070D9228  call 01B07FCB
070D922D  nop
28DDA0B6  add  rsp, 28h
28DDA0D3  jmp  070D90B0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anyone who has looked at an MSVC binary will recognise &lt;code&gt;mainCRTStartup&lt;/code&gt;: &lt;code&gt;sub rsp, 28h ; call __security_init_cookie ; add rsp, 28h ; jmp __scrt_common_main_seh&lt;/code&gt;. The tail is untouched, apart from being moved into the protection's section. The &lt;code&gt;call&lt;/code&gt; is where the protection got in. It no longer goes to &lt;code&gt;__security_init_cookie&lt;/code&gt;. It goes to a thunk that saves the full context three times over and makes three calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;01B07FCB  ; save context: 14 registers, 16 XMM; align stack
031206B6  call 0043907D
          ; restore, save, align
27714CFC  call 258DC3DA
          ; restore, save, align
27850B16  call 285C23FC
          ; restore
2863455F  jmp  070DD5F0        ; compares a global with 2B992DDFA232h: __security_init_cookie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the protection did not write an entry point. It took the compiler's and redirected its first &lt;code&gt;call&lt;/code&gt; through a thunk that runs three routines and then tail-jumps to the original callee. That also explains the nine plaintext bytes: &lt;code&gt;sub rsp,28h&lt;/code&gt; and the &lt;code&gt;call&lt;/code&gt; are the only two instructions of the entry point that can execute before decryption.&lt;/p&gt;

&lt;p&gt;TLS callback 0 begins with the same three calls and then continues into its own first instruction. So do all 203 exported functions of the executable. With the entry point and the callback that makes 205 doors and 615 call chains: whichever way into the image is used first, the code behind it has been decrypted by the time it runs. None of the six TLS callbacks is the protection's own; they belong to the game and its anti-cheat, and the protection treats them like any other function it covers.&lt;/p&gt;

&lt;p&gt;Run one at a time on the statically decrypted image, the three routines are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;routine&lt;/th&gt;
&lt;th&gt;API calls&lt;/th&gt;
&lt;th&gt;what it does to memory&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x43907D&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;GetSystemInfo&lt;/code&gt;, then &lt;code&gt;VirtualQuery&lt;/code&gt; and &lt;code&gt;VirtualProtect&lt;/code&gt; for the headers and each section&lt;/td&gt;
&lt;td&gt;makes everything read-write-execute, then reads one byte and writes it back at the start of every 4 KB page: 169,353 pages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x258DC3DA&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;the XTEA pass over 1,324,188 ranges&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;0x285C23FC&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;copies 667 pointers from &lt;code&gt;.idata&lt;/code&gt; to the start of &lt;code&gt;.rdata&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Nothing in the start-up path puts the page protections back. From here on every Arxan routine simply writes to code, which is why no writer routine ever calls &lt;code&gt;VirtualProtect&lt;/code&gt;, and why the weakness of the first part works at all.&lt;/p&gt;

&lt;p&gt;The third routine is the one a static decryptor would miss. The file has &lt;strong&gt;two import address tables&lt;/strong&gt;. The PE import directory describes one in &lt;code&gt;.idata&lt;/code&gt;, a section the protection added, and that is the table the Windows loader fills in. The original table is still at the very start of &lt;code&gt;.rdata&lt;/code&gt;, but no directory entry mentions it. Counting indirect calls in the decrypted game code shows both in use: 10,015 &lt;code&gt;call [rip+d]&lt;/code&gt; go through the loader's table and 1,226 through the original. Nine call sites in ten were rewritten, and the rest would jump through slots holding whatever the linker left there. The third routine keeps them working by walking two range lists in step, in the same LEB128 encoding as everything else, and copying the loader's pointers across, one run per imported DLL. The odd run out is the protection's own: a 33rd import descriptor, a second &lt;code&gt;KERNEL32.DLL&lt;/code&gt; with exactly two functions, &lt;code&gt;AddVectoredExceptionHandler&lt;/code&gt; and &lt;code&gt;RemoveVectoredExceptionHandler&lt;/code&gt;, which the debugger probes need and the game does not import.&lt;/p&gt;

&lt;p&gt;One more detail closes the start-up. If the callback and the entry point both make the same three calls, the second caller must not decrypt again, since XORing the keystream in twice would re-encrypt the image. Each routine keeps a flag, and keeps it somewhere that looks like nothing: &lt;code&gt;CC&lt;/code&gt; bytes between functions, which it zeroes when it has run. Comparing the memory left by a complete emulated run of the callback with the static result, the two code sections differ in 25 bytes out of 224,083,968: those flags, two state dwords, the final keystream state where the starting block was, and the first word of the XTEA key, which the decryptor overwrites on its way out. A memory dump of the running game therefore holds neither the key nor the starting block. Everything needed to decrypt the file is in the file, and after start-up it is nowhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions that stay encrypted
&lt;/h2&gt;

&lt;p&gt;After the bootstrap, 251 of the game's 29,329 code pages are still noise. They belong to 59 functions, the largest of them 454 KB: the functions the first part called decrypted on demand. I went looking for their key the way one usually finds a key: take one decrypted value and walk back to where it came from. I ran a writer that was known to produce the function at &lt;code&gt;0x387DEC0&lt;/code&gt;, logged every read and write, and looked at the instruction that produced its first four bytes, &lt;code&gt;55 48 8B EC&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2583A98F  mov rax, [rbp+68h]        ; a pointer that starts at 0x2500ACF2
2583A993  mov eax, [rax]            ; e = 0x17A80096
2583A995  xor eax, [rip+...]        ; K = the dword at 0x415D1D = 0xFB2348C3
2583A99F  mov [rdx], eax            ; -&amp;gt; a 4-byte buffer, then memcpy to the function
2583A9A1  add qword ptr [rbp+68h], 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0x17A80096 ^ 0xFB2348C3 = 0xEC8B4855&lt;/code&gt;. That is the code. What is missing from this listing is more interesting than what is in it: the writer never reads the function. The pointer walks through a block of data in the protection's own section, half a gigabyte of address space away from the function it is producing.&lt;/p&gt;

&lt;p&gt;So I had been asking the wrong question. This is not decryption in the usual sense, and the function has no key. The noise that fills it at rest is not transformed into code. It is thrown away. Each writer carries its own encoded copy of the bytes it will write, a &lt;strong&gt;stash&lt;/strong&gt;, and overwrites the target with the decoded copy. One experiment confirmed it: I filled every target range with random bytes before running the writers, and the result was identical.&lt;/p&gt;

&lt;p&gt;Scanning both code sections for the shape of that decode block finds every variant of it. There are four, and in each &lt;code&gt;e&lt;/code&gt; is the next dword of the stash, &lt;code&gt;p&lt;/code&gt; the next four bytes of output, and &lt;code&gt;K&lt;/code&gt; a single dword hidden in the filler bytes between two fragments of scattered code.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;variant&lt;/th&gt;
&lt;th&gt;decode step&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;xor&lt;/td&gt;
&lt;td&gt;&lt;code&gt;p = e ^ K&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;subtract&lt;/td&gt;
&lt;td&gt;&lt;code&gt;p = K - e&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;autokey&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;p = e ^ k ; k += p&lt;/code&gt;, with &lt;code&gt;k&lt;/code&gt; starting as &lt;code&gt;K&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;multiply-rotate&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;a = rol(a, b &amp;amp; 31) ; p = e - a*b ; b = ~p ^ b&lt;/code&gt;, with &lt;code&gt;a&lt;/code&gt; starting as &lt;code&gt;K&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; as &lt;code&gt;K &amp;amp; 31&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Where the output goes is a range list in the usual encoding. So a writer is completely described by four addresses and a variant: range list, stash, key, and which step to use. None of the four depends on anything at run time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Layers, and pairs
&lt;/h3&gt;

&lt;p&gt;Run one at a time, some writers still produced noise, with the right model and the right key. The function at &lt;code&gt;0x471CCF0&lt;/code&gt; showed why. Two writers are involved, and the first does not write code at all: it writes 8,000 bytes into the protection's own section, at exactly the address the second uses as its stash. Some writers exist only to prepare another writer's stash, and from the file there is one more level than the memory dump shows, because two large stashes had already been rewritten by the time the dump was taken. Up to three steps separate the file from a function, and every one is a function of bytes in the file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvs8s58pk4u9elx3k3d6c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvs8s58pk4u9elx3k3d6c.png" alt="Figure 3. From the file to a function: the bootstrap keystream uncovers a feeder's stash, the feeder decodes that into a writer's stash, and the writer overwrites the function with the decoded copy. A twin with the same range list overwrites it with noise again." width="799" height="281"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 3. From the file to a function: the bootstrap keystream uncovers a feeder's stash, the feeder decodes that into a writer's stash, and the writer overwrites the function with the decoded copy. A twin with the same range list overwrites it with noise again.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A function that is produced on demand is also taken away again. For every on-demand range a second routine exists, with the same range list and a stash of its own, whose output is noise: hiding a function is one more overwrite from an encoded copy, and the copy happens to be ciphertext. So writers come in pairs, and a memory dump shows each range in whichever state the last of the two left it. Telling the two apart on the at-rest file is a measurement: run every routine once, and compare the entropy of what it writes with the entropy of what is there.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;the routine writes&lt;/th&gt;
&lt;th&gt;the image holds&lt;/th&gt;
&lt;th&gt;routines&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;th&gt;what they are&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;about 8.0 bits per byte&lt;/td&gt;
&lt;td&gt;5.6 to 6.7&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;1,101,492&lt;/td&gt;
&lt;td&gt;re-encryptors: they turn code the image already has into noise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4.4 to 5.9&lt;/td&gt;
&lt;td&gt;7.2 to 8.0&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;17,717&lt;/td&gt;
&lt;td&gt;decryptors whose output the image does not have yet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7.6 to 7.8&lt;/td&gt;
&lt;td&gt;7.5 to 7.8&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;4,928&lt;/td&gt;
&lt;td&gt;re-encryptors of the ranges in the row above&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In all, the table for this build has 100 writers: 12 that prepare stashes and 88 that produce code. Applied in order to the statically decrypted file, they leave 6 of 29,329 pages of game code above 7.2 bits per byte, where a dump of the game that was actually running has 254. Two independent routes to the 59 functions, one from the file and one from the dump, agree on 883,737 of 883,737 bytes.&lt;/p&gt;
&lt;h3&gt;
  
  
  A protected range is not only code
&lt;/h3&gt;

&lt;p&gt;The writer that taught me the most was one I thought I already had. Its stash and key were known, and its range list looked known as well: nine ranges, to the byte what the checksum guard for that function hashes. Laid over those nine ranges, the decoded stash came out as code in seven places and as this in the last two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;247A940F   2E 78 68 61 73 68 00 00 63 6F 6C 6C 69 73 69 6F   .xhash..collisio
25FC7169   61 69 6D 00 70 61 73 73 74 68 72 6F 75 67 68 2D   aim.passthrough-
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Text, where the real routine leaves a function prologue. Plain text is not what a wrong key looks like. The stream is right and landing in the wrong place, which means a range is missing in front of it. The image holds three lists that begin with the same seven ranges: one with nine entries and two with ten, and the extra one is 10,760 bytes in &lt;code&gt;.rdata&lt;/code&gt;. With the ten-range list the last two ranges become code and the block in &lt;code&gt;.rdata&lt;/code&gt; becomes 619 strings, the names of animation states, stored as noise at rest. A writer can carry data along with its function, and the guard that watches the function hashes the writer's list with the data left out.&lt;/p&gt;

&lt;p&gt;Much later, on build 1.30.0, the same thing turned up at a larger scale, as a comparison between two images that would not come out equal: one list covering 48,843 bytes of functions and 51,128 bytes of &lt;code&gt;.rdata&lt;/code&gt; holding 2,797 names (&lt;code&gt;yaw&lt;/code&gt;, &lt;code&gt;movetype&lt;/code&gt;, &lt;code&gt;fov&lt;/code&gt;, &lt;code&gt;start_aim&lt;/code&gt;, &lt;code&gt;goalradius&lt;/code&gt;...), where the functions are the ones that register those names, each a long run of &lt;code&gt;lea rcx,[rip+name] ; call ; mov [rip+slot],eax&lt;/code&gt;. A strings dump of the file does not show the names, and neither does a search for code that refers to them, because the references are in the same ciphertext. That writer also defeats the simple way of telling a decryptor from its twin, since code that is one address load after another is mostly displacement bytes: 34% common code bytes, halfway between code (58% and up) and noise (20%). Entropy settles it, at 5.7 bits per byte against 8.0. Nothing that is still encrypted has two bits of slack per byte.&lt;/p&gt;

&lt;p&gt;The most thorough hiding in the image is a family of 76 small subroutines near &lt;code&gt;0xAF8E10&lt;/code&gt;. At rest each entry is five bytes of noise. Decrypted, the five bytes are a &lt;code&gt;jmp&lt;/code&gt; into the protection's section, where the body lives; the exception directory has no record for it; and the chain in front of each body runs a re-encryptor that hides the entry of &lt;em&gt;another&lt;/em&gt; member of the family. The running process keeps closing the family behind itself as it goes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strings
&lt;/h3&gt;

&lt;p&gt;Text is hidden three ways, and two are Arxan's. The protection needs two names at run time, &lt;code&gt;ntdll.dll&lt;/code&gt; and &lt;code&gt;NtQueryInformationProcess&lt;/code&gt;, and stores neither: each of 544 sites carries two byte arrays inside a code section, and the name is their XOR. Every site has its own pair of pads, so no two share a byte even though they produce the same nine or twenty-five characters. The second is the name tables that travel with their functions, above. The third is the game engine's own and much larger: 17,205 strings in &lt;code&gt;.data&lt;/code&gt;, each an 8-byte key, a length, a flag byte and the text XORed with the key, decrypted in place by an inlined getter on first use. Because the key travels with the blob the file alone is enough, and the dump gives a direct check: the game had decrypted 9,144 of them by the time it was taken, and all 9,144 match. One detail is pleasing. 402 of those strings are referred to &lt;em&gt;only&lt;/em&gt; from inside on-demand functions (&lt;code&gt;dw_match_id&lt;/code&gt;, &lt;code&gt;teamsValidated&lt;/code&gt;, &lt;code&gt;winningTeamIndex&lt;/code&gt;), so in a memory dump they look unused, because the code that uses them is noise. They also say something about what was thought worth the extra layer.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Part 3: Taking the protection out&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What removal has to mean
&lt;/h2&gt;

&lt;p&gt;A decrypted file still contains all of Arxan. Every routine is there, every call chain, every disguised jump, the injected start-up path and the second import table. Once I could read the file, the next thing I wanted was the same program without any of that in it.&lt;/p&gt;

&lt;p&gt;This is usually called devirtualizing, but this build has no bytecode virtual machine to undo. What it has is the list in the previous paragraph, and each item needs a different kind of removal. I started by writing down what the program would miss if the protection were simply deleted, because by now it depends on it in three ways.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Decrypted bytes.&lt;/strong&gt; Covered already: both layers are static.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The start-up path.&lt;/strong&gt; Two of the three start-up routines do work the program needs. One makes memory writable. One fills the second import table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Published state.&lt;/strong&gt; A checksum guard writes its digest into a slot inside a code section before it compares, and other code reads those slots later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything else is protection for its own sake: the comparisons, the debugger probes, the clocks, the repair writes. So removal is not deletion. For the start-up path, the program needs the function the thunk was hiding, and one import table instead of two. For published state, it needs the values the guards would have written, at the moment they would have written them. Only then can the routines stop running. Appendix B has every step in full; what follows is the shape of each and the thing that made it harder than it looks.&lt;/p&gt;

&lt;h3&gt;
  
  
  The start-up path
&lt;/h3&gt;

&lt;p&gt;The instruction after the last of the three start-up chains is a jump to the function the entry point's &lt;code&gt;call&lt;/code&gt; originally named. So the fix is to write that address back.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;site&lt;/th&gt;
&lt;th&gt;shipped&lt;/th&gt;
&lt;th&gt;restored&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;entry point, &lt;code&gt;0x70D9228&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;call 0x1B07FCB&lt;/code&gt; (thunk, three routines)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;call 0x70DD5F0&lt;/code&gt; (&lt;code&gt;__security_init_cookie&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;TLS callback 0, &lt;code&gt;0xA80D10&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;jmp 0x1A274F7&lt;/code&gt; (three routines, then the body)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;jmp 0x2776055E&lt;/code&gt; (the body)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third start-up routine existed to copy 667 pointers into the old import table for the instructions that still use it. Those instructions can be made to stop needing it: add the distance between an old slot and its twin in the loader's table to each instruction's displacement. Finding them takes a little care, because a four-byte pattern is not an instruction. Scanning for any 32-bit field that would land on a copied slot if it were a RIP-relative displacement gives 1,485 candidates, and each is kept only if some instruction start in the eleven bytes before it decodes to an instruction whose displacement is exactly that field. 1,251 survive: 1,226 &lt;code&gt;call&lt;/code&gt;, 21 &lt;code&gt;jmp&lt;/code&gt; and 4 &lt;code&gt;mov&lt;/code&gt;. Executed one at a time in the emulator, 1,251 of 1,251 go to the DLL and function their original named.&lt;/p&gt;

&lt;p&gt;With the image stored decrypted and one import table, none of the three routines has anything left to do, and a chain around a routine that does nothing saves a set of registers and restores the same set. Each of the 615 chains at the 205 doors is replaced by a five-byte &lt;code&gt;jmp&lt;/code&gt; from its first instruction to the instruction after its last. One thing does change: a chain that does not save the flags leaves the flags of its own stack-alignment arithmetic behind. Following the code after each exit until every status flag is either overwritten or read shows that nothing reads them. All 615 exits have dead flags.&lt;/p&gt;

&lt;h2&gt;
  
  
  A routine cannot be skipped
&lt;/h2&gt;

&lt;p&gt;The start-up path had come out so cleanly that I expected the 730 routines spliced into game code to be the same case, and I tried the same thing: write &lt;code&gt;ret&lt;/code&gt; at a routine's entry and run the code around it. Here is one such place, in a game function at &lt;code&gt;0x7013EBC&lt;/code&gt;, with the idioms resolved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;07013EBC  push 25842E17 ; push 27FEF75C ; ret
27FEF75C  push 040F51A4 ; push 041011FF ; ret
041011FF  push 03FEB276 ; push 00B03667 ; ret
00B03667  ; save context: 11 registers, 16 XMM
          ; align stack
24F7F1E3  push 250A7A89 ; push 250326B2 ; ret      ; call the routine
250A7A89  ; restore context
250A7B27  ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three "calls" are nested around the chain before it starts, so three return addresses lie on the stack above the saved registers. After the chain restores its context it executes &lt;code&gt;ret&lt;/code&gt;, which takes the innermost of them, &lt;code&gt;0x3FEB276&lt;/code&gt;. With the routine replaced by &lt;code&gt;ret&lt;/code&gt;, that is where the emulator ends up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;03FEB276  mov   r9, [rsp+48h]
03FEB27B  add   r9, 0E5h
03FEB282  mov   [rsp+0B0h], r9
03FEB28A  movzx eax, byte ptr [r9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the middle of an unrelated function, entered with a stack that is not its own. The other two addresses are no better: one is a &lt;code&gt;jmp&lt;/code&gt; in the middle of some other function, and one is inside a routine of the protection. I repeated the experiment on 60 chains spread over the 730 and got the same answer 60 times. None arrives anywhere the game function could continue.&lt;/p&gt;

&lt;p&gt;So the real routine does something the chain depends on, and it is not in any register, because the chain restores those. I ran the chain with the real routine and compared the stack afterwards. Three qwords above the chain's saved registers have changed.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;slot&lt;/th&gt;
&lt;th&gt;pushed by the construct&lt;/th&gt;
&lt;th&gt;written by the routine&lt;/th&gt;
&lt;th&gt;what is there&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;first&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x3FEB276&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x2335316&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ret&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;second&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x40F51A4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x416FDBF&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ret&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;third&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x25842E17&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x26D01BE3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;mov dword ptr [rsp+58h], 8&lt;/code&gt;, then more of the game function&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The three addresses the construct pushes are decoys. The routine overwrites all three before it returns. The chain's own &lt;code&gt;ret&lt;/code&gt; then goes to a &lt;code&gt;ret&lt;/code&gt;, which goes to another &lt;code&gt;ret&lt;/code&gt;, which goes to the third address, and that one is the next piece of the game function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgunp7edg9o8mscfyqana.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgunp7edg9o8mscfyqana.png" alt="Figure 4. A routine spliced into a game function. The construct pushes three plausible addresses, the routine replaces all three while it runs, and the chain's return falls through two bare returns into the real continuation." width="800" height="341"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 4. A routine spliced into a game function. The construct pushes three plausible addresses, the routine replaces all three while it runs, and the chain's return falls through two bare returns into the real continuation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This holds for every one of the 730. On its success path a routine in game code is not called &lt;em&gt;on the way&lt;/em&gt; to somewhere. It &lt;strong&gt;is&lt;/strong&gt; the way: the place where the function continues exists only as three writes inside the routine, and nothing in the calling construct names it. A disassembler that resolves every idiom perfectly and then follows the constants it found walks into a decoy. So does a patch that removes the routine or makes it return early. And it puts the failed check of the first part in a different light. A guard that fails does not need a separate mechanism to send the program somewhere wrong. It only has to write something else into slots it was going to write anyway.&lt;/p&gt;
&lt;h3&gt;
  
  
  Fifty-three bytes
&lt;/h3&gt;

&lt;p&gt;Since those three writes are everything the calling code needs, a routine can be replaced by a function that performs them and nothing else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lea  rdx, [rsp+8]            ; the caller's frame, as the routine addresses it
mov  rax, [rdx+8]            ; the alignment push: 10h or 18h
shr  rax, 3
add  rax, idx                ; first slot, in qwords
lea  rcx, [rip+T2] ; mov [rdx+rax*8+10h], rcx
lea  rcx, [rip+T1] ; mov [rdx+rax*8+8],  rcx
lea  rcx, [rip+T0] ; mov [rdx+rax*8],    rcx
ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is 53 bytes, position independent, and fits in the routine's own first instructions. The order of the three stores is not free. Some chains save only twelve registers, and in those &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt; and &lt;code&gt;rdx&lt;/code&gt; come back to the game holding whatever the routine last put in them: a slot index, the address of a &lt;code&gt;ret&lt;/code&gt;, a stack address. To be indistinguishable from the routine, the replacement has to leave the same leftovers. Each of the 730 chains was run twice in the emulator, from the instruction that calls the routine to the continuation, once with the real routine and once with the 53 bytes, and compared on &lt;code&gt;rip&lt;/code&gt;, &lt;code&gt;rsp&lt;/code&gt;, all sixteen general registers, the flags and the live stack. All 730 pairs end in the same state.&lt;/p&gt;

&lt;p&gt;The three addresses came from one emulated run of each routine, watching what it writes above its own frame. Seven hundred and thirty emulated runs is a workable way to build a table for one build. It stopped being workable the moment there was a second build, and the next chapter is about what replaced it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Values the guards leave behind
&lt;/h2&gt;

&lt;p&gt;The second thing the program needs from the routines is state. Searching both code sections for instructions that read anything a routine publishes, and discarding the instructions the routines themselves execute, leaves very little: a few dozen short sequences, all of one family.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov  ecx, [P]            ; a published value
add  ecx, [A]
xor  ecx, [B]            ; or sub
add  rsp, rcx            ; zero when P is what a clean run leaves there
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variants end in a conditional jump or a &lt;code&gt;cmov&lt;/code&gt; between two addresses instead of the &lt;code&gt;add rsp&lt;/code&gt;. All of them compute one number from a published value and two constants of their own, and all are harmless only when that number is zero. The value each one needs therefore follows from its own constants: &lt;code&gt;(P + A) ^ B = 0&lt;/code&gt; gives &lt;code&gt;P = B − A&lt;/code&gt;. For 25 of the values the publisher was found and run in the emulator, a guard or the start-up decryptor, and in 25 of 25 what it writes is exactly what the arithmetic asks for. Read through the joining jumps the protection cuts its code with, the count later turned out to be the same in every build I looked at: 53 checks on 40 published dwords.&lt;/p&gt;

&lt;p&gt;What such a check does with a value it does not like is worth seeing once, because it is the failed guard again with no routine in sight. This one is from build 1.20.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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. Decoded for one such check in build 1.30.0, the four table entries are link-time addresses in the game's code, and what the path does with them is a rebase by hand done with two anchors that are not the same place, so every entry lands &lt;code&gt;0x466A27B&lt;/code&gt; short of where it points: the middle of an instruction in an unrelated function three times out of four, and 38 MB below the image the fourth. 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.&lt;/p&gt;

&lt;p&gt;The natural conclusion is that the 40 values can be stored in the file, and that every check then has one outcome and can be folded into a jump. That is what I did first, and every comparison I ran in the emulator agreed with it. It is wrong, and the reason only showed when a cleaned build was started for real; that story comes two chapters on. The rule that came out of it: a published value has a before and an after. Only values that hold from the start are written into the file. Where a guard publishes a value at run time, its construct stays, its replacement stores the value at the moment the guard would have, and the checks that read it are left exactly as they are, both branches.&lt;/p&gt;

&lt;h2&gt;
  
  
  Folding, straightening, and where it stops
&lt;/h2&gt;

&lt;p&gt;At this point I had an image with 730 replacement functions in it. It runs without the protection, but it does not look like it. Every chain is still there, saving and restoring up to fourteen registers and sixteen XMM registers around 53 bytes that write three constants.&lt;/p&gt;

&lt;p&gt;Those 53 bytes are simple enough to reason about without running them. Give the symbolic stack that resolves &lt;code&gt;push R ; push G ; ret&lt;/code&gt; one more rule, "this routine stores these three addresses in these three slots", and it can evaluate a whole construct from its first instruction: the nested pushes, the chain, the three writes, the chain's &lt;code&gt;ret&lt;/code&gt; into a &lt;code&gt;ret&lt;/code&gt;, into another &lt;code&gt;ret&lt;/code&gt;, into the continuation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; shipped
07013EBC  push 25842E17 ; push 27FEF75C ; ret      ; and everything after it

; exported
07013EBC  jmp  26D01BE3
26D01BE3  mov  dword ptr [rsp+58h], 8
26D01BEB  mov  dword ptr [rsp+44h], 0FFFFFFF8h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three wrapped calls, a context save, a routine and a context restore fold into one direct jump to the next piece of the game function. Often the continuation of one routine is the first instruction of another construct, and the evaluation carries on through it: the chains in game code group into 311 such nests, each running between one and five routines before it lets the function go on.&lt;/p&gt;

&lt;p&gt;A jump is not quite the same thing as the nest it replaces, and the difference has to be shown harmless before the fold is allowed. The routine leaves values in &lt;code&gt;rax&lt;/code&gt;, &lt;code&gt;rcx&lt;/code&gt; and &lt;code&gt;rdx&lt;/code&gt;, and a chain that does not save those hands them to the game; after a fold they hold whatever they held before. That is the same only if nothing reads them before writing them, so from each fold target the code is followed on every path, into callees, until each such register is written or read. The same walk is done for each status flag. Then every fold was checked against the real routine: the emulator runs the shipped construct from its first instruction, with the protection's routine executing in full, until it reaches the fold's target, and the two sides are compared on every register that is live there. They did not all get there in one pass. In the first, 49 of 309 gave nothing to compare, because the real routine ended in an illegal instruction or a breakpoint: two of the six debugger probes, chosen by the timestamp. Run again with a different pinned timestamp, 47 of the 49 went through, and the last two only needed time, 167 and 87 seconds of emulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Straightening the jumps
&lt;/h3&gt;

&lt;p&gt;The three idioms that had to be resolved to &lt;em&gt;read&lt;/em&gt; the entry point are everywhere, and for an exported image they have to be resolved and written back. That needs a definition of where code is. The walk that supplies it starts from everything the file itself calls an entry: 127,967 records of the exception directory, 56,756 relocated pointers in the data sections that point into code, the entry point, the TLS callbacks, the exports, the unwind handlers. From there it follows every direct branch, resolves every idiom into the branch it stands for, and follows that too.&lt;/p&gt;

&lt;p&gt;Two things nearly hid a large part of the code from it. Code is not only in the code sections: the protection's pieces in section padding have to be walked like the rest. And the compiler's &lt;code&gt;switch&lt;/code&gt; dispatch does not end in &lt;code&gt;jmp rcx&lt;/code&gt; in this build. It ends in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov  [rsp-8], rcx
lea  rsp, [rsp-8]
lea  rsp, [rsp+8]
jmp  [rsp-8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which pushes &lt;code&gt;rcx&lt;/code&gt;, pops nothing, and jumps to what it pushed. A walk that treats &lt;code&gt;jmp [rsp-8]&lt;/code&gt; as an unknown destination stops there and never sees a single case. Reading those four instructions as &lt;code&gt;jmp rcx&lt;/code&gt; brought 4,520 jump tables and every block behind them into the walk.&lt;/p&gt;

&lt;p&gt;In the code the walk reaches there are 184,254 disguised branches, and each is replaced at its first instruction by what it means.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;idiom&lt;/th&gt;
&lt;th&gt;replacement&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;jmp X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;jmp X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;jcc B&lt;/code&gt;, else &lt;code&gt;A&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;jcc B ; jmp A&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;call G&lt;/code&gt;, return to &lt;code&gt;R&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;call G ; jmp R&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The replacement has to fit where the idiom was, and an idiom is rarely in one place: its instructions are spread over several chunks joined by &lt;code&gt;jmp rel32&lt;/code&gt;. It also has to be safe to overwrite. A chunk may be rewritten only if nothing else enters it: no branch from live code into its interior, no &lt;code&gt;lea&lt;/code&gt; or &lt;code&gt;movabs&lt;/code&gt; of an address inside it, no exception record that starts in the middle of it. And one piece of bookkeeping is easy to miss. Every &lt;code&gt;movabs&lt;/code&gt; in a conditional idiom carried a 64-bit address, so every one has an entry in the relocation table. If the instruction goes and its entry stays, the loader adds the load offset to eight bytes in the middle of whatever is there now. Each rewrite also deletes the relocation entries under it: 164,289 of the 422,110 the file started with.&lt;/p&gt;

&lt;p&gt;With the branches rewritten I expected &lt;code&gt;jmp [rsp-8]&lt;/code&gt; to have disappeared from live code. Nearly 7,000 were still there, and grouping them by the instructions in front showed why. They are not branches. They are compiler epilogues in which every stack instruction has been written as a pair: &lt;code&gt;lea rsp,[rsp-8] ; mov [rsp],r&lt;/code&gt; for &lt;code&gt;push r&lt;/code&gt;, &lt;code&gt;mov r,[rsp] ; lea rsp,[rsp+8]&lt;/code&gt; for &lt;code&gt;pop r&lt;/code&gt;, &lt;code&gt;lea rsp,[rsp+8] ; jmp [rsp-8]&lt;/code&gt; for &lt;code&gt;ret&lt;/code&gt;, often with a joining jump between the two halves. There are close to 114,000 of them. None changes what the program does, and they are not the protection's logic. But they are its handwriting, and they are most of the reason a function in this build is hard to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  A no-op with a hole in it
&lt;/h3&gt;

&lt;p&gt;One number had been nagging at the end of every run: a handful of function pieces, four to ten a build, that still read as noise after both layers were off. I had been carrying them as "still encrypted", and that was wrong. Held against a memory dump of the running game they are byte for byte the same. No writer has them in its list. And read as instructions from their first byte, most of them are this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00001010  pushfq
00001011  push   rbx
00001012  movabs rbx, 0EFECB3E81FEBAEF6h   ; 48 BB F6 AE EB 1F E8 B3 EC EF
0000101C  call   00001016                  ; back into that immediate, onto the bytes EB 1F
00001021  add    rsp, 3E10h                ; never executed: a believable epilogue,
00001028  pop    rbp                       ;   then a few random bytes
00001029  ret
00001037  pop    rbx                       ; the call's return address, thrown away
00001038  xor    ebx, 0DC7421E6h
0000103E  pop    rbx
0000103F  popfq
00001040  call   00AF27C0                  ; the function goes on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flags and one register are saved. The register is loaded with a constant whose middle two bytes are &lt;code&gt;EB 1F&lt;/code&gt;. The &lt;code&gt;call&lt;/code&gt; goes &lt;em&gt;back&lt;/em&gt;, into the middle of that instruction, where &lt;code&gt;EB 1F&lt;/code&gt; is a short jump over what follows. What it lands on throws the return address away, does a little more arithmetic on the scratch register, and puts the register and the flags back. Between thirty-four and forty-seven bytes, and nothing has changed. What it is for is the stretch nobody executes: a disassembler reading straight through meets &lt;code&gt;add rsp ; pop rbp ; ret&lt;/code&gt;, decides the function has ended, and decodes the random bytes after it as the start of the next one. It also explained why a walk that follows every call and every fall-through had been reaching bytes that could not be code.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;constructs&lt;/th&gt;
&lt;th&gt;bytes they occupy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;a build of the game that was never protected&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.01.2&lt;/td&gt;
&lt;td&gt;12,578&lt;/td&gt;
&lt;td&gt;about 0.6 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.20&lt;/td&gt;
&lt;td&gt;19,064&lt;/td&gt;
&lt;td&gt;about 0.9 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.51.2&lt;/td&gt;
&lt;td&gt;23,674&lt;/td&gt;
&lt;td&gt;about 1.1 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;22,429&lt;/td&gt;
&lt;td&gt;about 1.1 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every one of them is in the game's own code, and the protection's section has none. The fifty variants differ in the order of the two pushes, the register and the arithmetic. Removing them takes less trust than the branches did, because the claim is so narrow. Each candidate is checked instruction by instruction: the pushes are exactly the flags and one register, everything in between writes only that register and the flags, the pops undo the pushes in order, and no arithmetic comes after either has been put back. All 19,064 and all 23,674 pass. Six hundred of them, at least one of every variant, were then run in the emulator from random registers and flags, and at the end everything is as it was. Each becomes a two-byte jump over itself, and the bytes behind it are filled with &lt;code&gt;int3&lt;/code&gt; unless some branch encoded anywhere in the image could land in them, which keeps a few hundred as they are.&lt;/p&gt;

&lt;p&gt;The construct changes between versions, and the two ends of the range spell it differently. The oldest builds save only the register:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;push   rbx
sub    ebx, 3D1EEB6Bh       ; immediate bytes 6B EB 1E 3D
call   &amp;lt;back onto EB 1E&amp;gt;
...                         ; never executed
pop    rbx                  ; the return address
movabs rbx, ...
pop    rbx                  ; no popfq: the flags keep what the sub left
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the construct is not a no-op. The &lt;code&gt;sub&lt;/code&gt; changes the flags and nothing puts them back, so it can only go where the flags it leaves are never looked at. For each of these I followed every path forward from the construct until all six status flags had been written: by an instruction that sets them, or by a call or a &lt;code&gt;ret&lt;/code&gt;, across which the Windows x64 convention keeps no flags. A path that reads a flag first, takes an indirect jump or runs too long keeps its construct. On 0.01.2 all 12,578 come out clean, 12,568 of them at an instruction that writes the flags and ten at a call. The newest build goes the other way and adds something between the two pops: a &lt;code&gt;nop&lt;/code&gt; of any length, an &lt;code&gt;xchg&lt;/code&gt; or &lt;code&gt;mov&lt;/code&gt; of a register with itself at 64 or 16 bits, or &lt;code&gt;lea r64, [r64]&lt;/code&gt;. All of these change nothing, and I accept only those. The 32-bit forms are left out on purpose, because writing a 32-bit register clears its upper half. That is what 21,751 of 1.69.0's constructs look like, and it raises the number of shapes from fifty to two hundred. The same emulator check holds for both forms, with the flags left out of the comparison for the older one, whose flags are covered by the proof above.&lt;/p&gt;

&lt;p&gt;In 1.51.2 four of the ten pieces were this construct. The other six are what is left where a function used to be. Each begins with a &lt;code&gt;jmp&lt;/code&gt; into the protection's section, where the function's body now lives, and what follows in the old address range is fragments of the protection's own code or bytes nothing executes; one record begins a byte early, in front of such a jump. The largest, 806 bytes that recur in five versions, is noise in the running game too: guards hash it and no writer writes it. None of them is something a decryptor would produce. Each is, byte for byte, what the running game holds.&lt;/p&gt;

&lt;p&gt;Each kind of rewrite has its own proof of the same shape: the shipped form and its replacement, run from the same random state, compared on everything afterwards. Idioms that are byte for byte the same shape share one proof, so the runs cover one of every shape and a random sample of the rest: 42,717 runs for the branches, 24,610 for the pairs, no difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where the walk ends
&lt;/h3&gt;

&lt;p&gt;Every rewrite rests on one definition: code is what the walk reaches. I took an inventory of the game's code section after the rewrite to see where that definition runs out. Close to seven megabytes of the game's code section are neither reached nor touched, and decoded from the right offsets, part of it is ordinary code. Grouped by the exception record around it, almost all of it lies in a handful of enormous records, the largest covering eight megabytes. These belong to the game's anti-cheat, whose blocks end in a register jump with a destination computed at run time. A walk follows direct branches, idioms and jump tables. It does not follow a computed jump, and resolving those is outside the scope of this post.&lt;/p&gt;

&lt;p&gt;Arxan did not stop there. It treated that code like any other: its idioms run all through it, and pieces of it were moved into the protection's section. That is why the edge matters. "Not reached" cannot be read as "dead", and a rewrite is safe only when every way into the rewritten bytes is known. So the code behind the computed jumps is found without walking to it. A compiler emits blocks back to back, so the byte after an instruction that never falls through is where the next block would begin; every such byte the walk did not reach is decoded as a candidate block and accepted when every instruction is one a compiler writes for user-mode code and the block ends cleanly. Accepted blocks are not rewritten. They become the roots of a second walk with a different rule, &lt;strong&gt;keep&lt;/strong&gt;: everything it reaches stays byte for byte, nothing it executes, reads or branches to is wiped, and a patch already planned over a byte it walks is taken back.&lt;/p&gt;

&lt;p&gt;The price is stated in that walk's own numbers. Inside it, exactly as shipped, are about 61,000 disguised branches, 60,000 stack pairs and 344 routine call sites. The protection's handwriting stays in the code the walk could not prove it understood.&lt;/p&gt;

&lt;p&gt;Two things about that edge are worth being exact about. The first is whose obfuscation the computed jumps are. A build of the same game that was never protected, linked two weeks before 1.20, already has these functions in this form: its largest exception records are 3.1 MB, 1.7 MB and 0.75 MB, where the protected 1.20 has 3.1, 2.2 and 1.9 MB. The flattening was in the code before Arxan saw it. Arxan's part is what it lays over everything alike: moved blocks, disguised branches, junk constructs, and the routines it splices in. That is also why this post leaves those functions exactly as shipped and does not take their dispatch apart: it belongs to the game's anti-cheat, not to the protection.&lt;/p&gt;

&lt;p&gt;The second is how much of Arxan's own writing stays behind because of it, in code the walk does reach. For the ten versions of the last chapter:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;version&lt;/th&gt;
&lt;th&gt;disguised branches left as shipped&lt;/th&gt;
&lt;th&gt;routine call sites left in live code&lt;/th&gt;
&lt;th&gt;routine replacements kept in place&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.01.2&lt;/td&gt;
&lt;td&gt;686&lt;/td&gt;
&lt;td&gt;62&lt;/td&gt;
&lt;td&gt;630&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.07.0&lt;/td&gt;
&lt;td&gt;729&lt;/td&gt;
&lt;td&gt;67&lt;/td&gt;
&lt;td&gt;630&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.23.2&lt;/td&gt;
&lt;td&gt;795&lt;/td&gt;
&lt;td&gt;69&lt;/td&gt;
&lt;td&gt;611&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.30.0&lt;/td&gt;
&lt;td&gt;1,281&lt;/td&gt;
&lt;td&gt;74&lt;/td&gt;
&lt;td&gt;1,009&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.31.5&lt;/td&gt;
&lt;td&gt;1,220&lt;/td&gt;
&lt;td&gt;83&lt;/td&gt;
&lt;td&gt;1,214&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.39.3&lt;/td&gt;
&lt;td&gt;1,397&lt;/td&gt;
&lt;td&gt;86&lt;/td&gt;
&lt;td&gt;1,199&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.41.2&lt;/td&gt;
&lt;td&gt;993&lt;/td&gt;
&lt;td&gt;76&lt;/td&gt;
&lt;td&gt;1,088&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.51.2&lt;/td&gt;
&lt;td&gt;1,449&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;908&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.64.0&lt;/td&gt;
&lt;td&gt;1,442&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;td&gt;932&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;1,404&lt;/td&gt;
&lt;td&gt;78&lt;/td&gt;
&lt;td&gt;907&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Against the fifty to two hundred thousand branches rewritten in each of those versions, about one in a hundred is left, and every routine a kept call site reaches has its three-write replacement, so none of the protection's routines runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is left
&lt;/h3&gt;

&lt;p&gt;For 1.51.2, the exported image has 72,268,708 bytes filled with &lt;code&gt;int3&lt;/code&gt;, in 236,561 ranges, and its relocation table goes from 422,110 entries to 208,359. The protection's section tells the story in three numbers: 7.96 bits per byte as shipped, 6.71 decrypted, 2.48 exported. About 32 of its 104 MB still hold something: game code the protection moved there, the kept blocks, rewritten instructions, data that live instructions read, and the relocation table itself, which the protection keeps in its own section. The protection's own import descriptor stays as well, because the kept code still contains debugger probes that load its two slots, and whether any path the game takes still reaches one of them is a question about computed jumps that the walk cannot answer.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Part 4: A second build&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How much of it was the build?
&lt;/h2&gt;

&lt;p&gt;Everything so far was measured on one build, with an emulator and a memory dump at hand, and by now the tables behind the removal had been filled in one emulated routine at a time. I did not know how much of what I had was Arxan and how much was 1.51.2. The honest test was a build none of it had been fitted to, so I took an older one, 1.20 from May 2020, and resolved to use nothing but the shipped file. Appendix C follows every step. The short version is that each table the protection uses is either named by plain compiled code or pinned down by the file's own structure, and the emulator had been a slow way of reading what was written down all along.&lt;/p&gt;

&lt;h3&gt;
  
  
  The cipher's three parameters
&lt;/h3&gt;

&lt;p&gt;On 1.51.2 the bootstrap's list was found by watching the cipher store its first few ranges. It can be found without watching anything, because the file says where some of its entries must be. The cipher steps around every relocated qword, so between two relocation sites that lie close together the list has to contain &lt;code&gt;gap 8, length (distance − 8)&lt;/code&gt;. I took six consecutive entries of the file's own relocation table, encoded the six pairs as LEB128, and searched the file for those bytes. A hit is somewhere in the middle of the list, with the cursor at that point known. And LEB128 reads backwards without ambiguity, since a value ends at a byte with the top bit clear and the top-bit-set bytes in front of it are its own. So the list can be walked back, value by value, to the gap that equals the whole remaining cursor, which is its beginning.&lt;/p&gt;

&lt;p&gt;The key and the starting block I first found by position: both must be readable before anything is decrypted, so the cipher must skip them, and no relocation explains those skips. Trying every 16-byte island with every 8-byte island on the head of the stream, and scoring the output by how much of it looks like compiled code, takes a few seconds and leaves one pair standing at 54% to 59% against 23% for the runner-up.&lt;/p&gt;

&lt;p&gt;Then the oldest build I have, 0.01.2 from September 2019, broke that search and in doing so showed a shorter one. No pair of its islands decrypts anything. But the constant &lt;code&gt;9E3779B9&lt;/code&gt; occurs exactly once in the file, in code the protection has to leave readable, since it is the code that makes everything else readable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;13A0885F  mov  dword ptr [rbp+10h], 40h          ; 64 cycles
13A08866  mov  dword ptr [rbp+14h], 9E3779B9h
13A0886D  lea  rax, [rip+88134Dh]                ; 14289BC1, the key
13A08874  mov  [rbp], rax
13A08878  lea  rax, [rip-11CBE387h]              ; 01D4A4F8, the starting block
13A0887F  mov  [rbp+8], rax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The starting block of that build is not in the protection's section at all, which is why no pair matched. And these instructions are the same, frame offsets included, in 1.03, 1.20 and 1.51.2. The cipher's set-up is plain compiled code that names its cycle count, its key and its starting block. I had built a clever search for something the file states outright.&lt;/p&gt;

&lt;h3&gt;
  
  
  Every list, and every writer
&lt;/h3&gt;

&lt;p&gt;The backward reading that found the bootstrap's list finds all the others, because every list in the protection ends the same way: with a gap that carries the cursor from somewhere inside the image to &lt;code&gt;0xFFFFFFFF&lt;/code&gt;. A gap that large is always five LEB128 bytes, and the last of them is &lt;code&gt;0D&lt;/code&gt;, &lt;code&gt;0E&lt;/code&gt; or &lt;code&gt;0F&lt;/code&gt;. Every five-byte value of that shape is a possible ending, and the walk back from it either arrives at a list's first value or falls apart within a few steps. On 1.51.2 that turns 105,690 candidates into 310 lists in about a second, the bootstrap's and all 100 writers' among them: the table I had built by emulation, one routine at a time, given up by the file to a scan.&lt;/p&gt;

&lt;p&gt;A writer needs three more things: its stash, its key and its encoding. My first attempt at the key was brute force, since a key is one dword and a dword can be exhausted. It taught me something I would not have found otherwise. For a multiply-rotate stash, trying all 2^32 keys prints the right one with 76.7% of the decoded bytes common code bytes against 27.5% for the runner-up. For an autokey stash it prints a wrong key and reports that 1.4 billion keys, a third of all there are, decode the first 80 bytes to something that passes for code. The reason is in the arithmetic: &lt;code&gt;p = e ^ k ; k += p&lt;/code&gt;. Bit 0 of the next &lt;code&gt;k&lt;/code&gt; is bit 0 of &lt;code&gt;e&lt;/code&gt; whatever &lt;code&gt;k&lt;/code&gt; was, bit 1 then depends only on bit 0, and so on up. The running key forgets where it started within 32 words. An autokey stash is readable without its key, apart from its first lines.&lt;/p&gt;

&lt;p&gt;Brute force was the wrong tool all the same, and the answer was again in the code. Every writer is an instance of one compiled routine, and three instruction shapes name its parameters.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;what&lt;/th&gt;
&lt;th&gt;how the writer names it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;stash&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lea rax,[rip+S] ; mov [rbp+68h],rax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;list&lt;/td&gt;
&lt;td&gt;&lt;code&gt;lea rax,[rip+L] ; mov [rbp+48h],rax ; mov eax,1 ; test eax,eax&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, xor&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;xor eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax&lt;/code&gt;, in the loop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, subtract&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;add eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax&lt;/code&gt;, in the loop&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, autokey&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;mov eax,[rip+K] ; mov [rbp+8Ch],eax&lt;/code&gt;, before the set-up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;key, multiply-rotate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;mov eax,[rip+K] ; mov [rbp+88h],eax ; mov eax,[rip+K] ; and eax,1Fh&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The frame offsets differ between builds, so the shapes are what to match. Matching them takes the same care as everything else in this image, because the protection cuts the routine into pieces: the instructions have to be read as a stream, joining jumps followed and disguised ones resolved. With all four parameters in hand each candidate row is simply decoded, and what comes out says what the row is: code that differs from what is in place is a decryptor, code identical to what is in place is a repair writer, noise is a twin or a writer that prepares another's stash.&lt;/p&gt;

&lt;p&gt;The oldest build had one more lesson. After everything, 233 pages of its game code were still unreadable, nearly all in the ranges of a single list of 2.7 MB. The image holds that list three times: one copy named by a writer that decodes to noise, one by a guard, and one that nothing in either code section refers to. The writer that should use the third copy seemed not to exist. It does. Its set-up is in the last page of &lt;code&gt;.pdata&lt;/code&gt;, in the padding the linker leaves after the table of unwind records. The reference search had only looked where code is supposed to be. Reading every read-only section as a place code may be took the unreadable pages of that build from 233 to 1, and the one is hashing code whose constants are as dense as noise.&lt;/p&gt;

&lt;p&gt;The check was the first build again. With every parameter read from the file and nothing taken from the emulator, the decrypted image of 1.51.2 differs from the earlier one in 0 of 297,288,240 bytes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The way back is a constant
&lt;/h3&gt;

&lt;p&gt;The removal leaned on two things the calling code does not contain: where each routine sends its caller, and what the guards publish. On 1.51.2 both had been measured by running every routine. Running hundreds of routines again for every build was one way to go on. The other was to ask where a routine gets its three addresses from, so I put the question to the file: where else does one of those addresses occur?&lt;/p&gt;

&lt;p&gt;Once. Every one of the three addresses of every routine is in the image exactly once, as an absolute qword with a relocation entry, sitting alone between pieces of code. One instruction loads the qword, and what follows is the end of the routine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25F7BB85  mov rcx, [rip-0DE7068h]      ; the qword at 25194B24: the continuation
25F7BB8C  mov [rdx+rax*8], rcx
28D0EF2E  mov rax, [rip-73AD73h]       ; another qword: a small number, the slot
28D0EF35  mov rdx, [rbp+170h]          ; the caller's frame
28D0EF3C  mov rcx, [rip-27394568h]     ; the address of a ret
28D0EF43  mov [rdx+rax*8], rcx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both inputs of each store are constants. Walking every routine from its entry and collecting those stores gives the same three addresses as the 730 measured runs, 730 times, in a few seconds, and adds the one routine the measurement had never reached.&lt;/p&gt;

&lt;p&gt;1.20 was the test of whether that is the protection or the build, and at first it looked like the build: not one store of that form in 613 routines. Grouping every indexed qword store in the image by the instructions before it showed the same store written by an older version of the protection, in two spellings, addressed from the routine's own frame and placed at the start of the routine where 1.51.2 has it in the epilogue. What all versions share is the part worth matching: a qword store with a scaled index, whose value was just loaded from a qword holding an address in the image. Entered in the emulator the way their chains enter them, 595 of 1.20's 613 routines returned having written exactly the three qwords the table has, and the other 18 ran out of time having written the continuation and nothing else.&lt;/p&gt;

&lt;p&gt;The published values never needed a guard at all. A check does arithmetic on the published dword with two constants of its own and uses the result in a way that only works when it is zero, so the value the guard has to have published is &lt;code&gt;B − A&lt;/code&gt;, and it is sitting in the check. Collected that way, the 40 values of 1.51.2 are the same file, byte for byte, as the one made from the guards' emulated runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.20 without its protection
&lt;/h3&gt;

&lt;p&gt;With those tables read from the file, the whole removal ran on a build it had never been fitted to: 613 routine replacements, 537 start-up chains skipped, 275 of 275 constructs folded to a jump, 56,302 disguised branches rewritten, 55 of 55 checks folded, 65 MB wiped, and no function piece left that reads as noise.&lt;/p&gt;

&lt;p&gt;Those are counts, and counts were not enough. Compared against the protected image in the emulator, function by function from the same state, the first export was wrong in two places. One function returned in the protected image and jumped out of the image in the export: its path goes through a check whose first instruction is separated from the rest by a joining jump, so the value it reads had not been recognised. The other stopped at an &lt;code&gt;int3&lt;/code&gt;: a return thunk that lives in the padding of a data section, whose address is only ever &lt;em&gt;pushed&lt;/em&gt; and never jumped to, had not been recognised as code, and what it leads to had been wiped. Both were fixed, and then the comparison read 107 of 116 targeted functions and 599 of 600 sampled ones equal, the rest out of time, none different.&lt;/p&gt;

&lt;p&gt;By every measure I had, the 1.20 export was right. So it was time to do the one thing an emulator cannot do for a file, and start it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Part 5: Finding out whether it is right&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An emulator as a measuring instrument
&lt;/h2&gt;

&lt;p&gt;A cleaned file is a claim about a program: that it does what the protected one does. Each kind of rewrite had been proven on its own, in isolation. That leaves the question of whether the pieces fit, and I wanted an answer that did not depend on my understanding of any single piece.&lt;/p&gt;

&lt;p&gt;The comparison I settled on needs a reference that still has its protection, running. That is the decrypted image in the emulator with every routine executing and each checksum guard held on its success path. The candidate is the exported file, run as it is. What a function &lt;em&gt;does&lt;/em&gt;, as far as the rest of the world can tell, is the sequence of calls it makes to Windows and the state it leaves. So both sides are entered at the same function with the same registers, stack and memory, both are stopped at every import call, and the call, its arguments, the stack pointer and the non-volatile registers are recorded. Calls the reference makes while one of the protection's routines is on its stack are left out, because the candidate has no routines. The two sequences must be equal, and when both runs end the same way, so must the return value, the registers, every changed byte of every data section and the heap.&lt;/p&gt;

&lt;p&gt;Getting the reference to behave took longer than building the comparison, and most of what went wrong said something about the protection. The best example is a function that, run alone in the reference six times, returned three times and stopped at an &lt;code&gt;int 2Dh&lt;/code&gt; debugger probe the other three, with every &lt;code&gt;rdtsc&lt;/code&gt; in the image pinned to a fixed value. Two instruction traces of it are identical for 568 instructions and differ at the next one, which is a probe bundle assembling its own &lt;code&gt;rdtsc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25854E37  lea  rdx, [rip+11D1F23h]       ; 26A26D61
25854E3E  mov  eax, [rip-23C3D778h]
25854E44  xor  eax, [rip-23CE1C47h]      ; eax = 310F9090
25854E4A  mov  [rdx], eax
25854E4C  jmp  26A26D61
26A26D61  nop ; nop ; rdtsc              ; at rest: lea rdx, [rsp]
26A26D65  jmp  273F0874
273F0874  mov  [rbp+130h], eax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the file those four bytes are a harmless &lt;code&gt;lea&lt;/code&gt;, so a scan of the image for the opcode does not find them, and the timestamp that picks the probe came from the host. The reference now watches writes into code for the opcode and pins those too.&lt;/p&gt;

&lt;p&gt;One function shows what the filter on routine calls hides. At &lt;code&gt;0x5FB33E0&lt;/code&gt; both sides make one call of their own, &lt;code&gt;GetLastError&lt;/code&gt;. The reference makes seven more while routines are on its stack: &lt;code&gt;LoadLibraryA&lt;/code&gt;, &lt;code&gt;GetProcAddress&lt;/code&gt;, &lt;code&gt;GetCurrentProcess&lt;/code&gt;, &lt;code&gt;NtQueryInformationProcess&lt;/code&gt;, &lt;code&gt;FreeLibrary&lt;/code&gt;, &lt;code&gt;AddVectoredExceptionHandler&lt;/code&gt;, &lt;code&gt;CloseHandle&lt;/code&gt;. That is a debugger-probe bundle running in the middle of a game function. The export makes none of those calls and ends in the same state.&lt;/p&gt;

&lt;p&gt;The results on 1.51.2:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;functions run&lt;/th&gt;
&lt;th&gt;how many&lt;/th&gt;
&lt;th&gt;equal&lt;/th&gt;
&lt;th&gt;no verdict&lt;/th&gt;
&lt;th&gt;different&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;the entry path&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;every function that reaches a routine call site, a folded nest or a folded check&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;td&gt;161&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a random sample of function entries&lt;/td&gt;
&lt;td&gt;3,000&lt;/td&gt;
&lt;td&gt;2,987&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a second sample, with arguments full of pointers&lt;/td&gt;
&lt;td&gt;3,000&lt;/td&gt;
&lt;td&gt;2,940&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every "no verdict" is the reference running out of time: a routine in the middle of hashing megabytes of code is slow in an emulator. 1,111 of the 6,089 equal runs went all the way to the function's return. The rest stopped at the same instruction in the same state on both sides, usually at a pointer the invented arguments could not satisfy. That is the limit of this kind of comparison, and I understood it less well than I thought. An emulator with invented arguments walks the first stretch of a function and seldom its depths, and what the table says is that no path it could take tells the two images apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Started for real
&lt;/h2&gt;

&lt;p&gt;The Windows loader is the one reader of the file the emulator never plays. So the 1.20 export was started once as a process, from the game's folder, under its own file name.&lt;/p&gt;

&lt;p&gt;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. 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.&lt;/p&gt;

&lt;p&gt;Windows kept a dump, and the first surprise was that the fault had nothing to do with anything I had changed. The faulting call is &lt;code&gt;call qword ptr [rsp+1EC8h]&lt;/code&gt; in static initialiser 30 of 6,442, through a local variable that held an address in private memory, and next to it the frame held &lt;code&gt;CreateFileW&lt;/code&gt;, &lt;code&gt;GetFileSize&lt;/code&gt;, &lt;code&gt;VirtualAlloc&lt;/code&gt; and three pointers that look like the arrays of an export directory. That initialiser reads &lt;code&gt;gdi32.dll&lt;/code&gt; from disk, lays it out as a second image of its own, and calls the copy's &lt;code&gt;GdiDllInitialize&lt;/code&gt;. To watch it do so the emulator needed something it had never had, a module list and the real DLL files mapped around the image, and given those the function runs the same twenty-one calls in both images to the same end. On a current Windows that export is a jump through a delay-load slot, and Windows will not resolve a delay-load import for an image it has never heard of. 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. With a handler in the process that recognises that one fault, the shipped file shows its splash screen after ten seconds and its window after fourteen.&lt;/p&gt;

&lt;p&gt;The export, in the same folder under the same name, did three other things, a different one from start to start. Each was a rule I had wrong.&lt;/p&gt;

&lt;h3&gt;
  
  
  One function, 256 ways through
&lt;/h3&gt;

&lt;p&gt;The first was an &lt;code&gt;int3&lt;/code&gt;, a wiped byte, executed inside static initialiser 39. The dump has the path: &lt;code&gt;push rax ; ret&lt;/code&gt; with &lt;code&gt;rax = 0x3DD537&lt;/code&gt;, and at that address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3DD532  90 66 2B 16 76        junk
3DD537  E9 8C C3 96 15        jmp 15D498C8
3DD53C  48 BD 28 78 ...       junk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A disassembler that starts at &lt;code&gt;0x3DD532&lt;/code&gt; reads &lt;code&gt;nop ; sub dx,[rsi] ; jbe …&lt;/code&gt; and never sees the jump, and nothing in the image names &lt;code&gt;0x3DD537&lt;/code&gt;: no &lt;code&gt;lea&lt;/code&gt;, 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 &lt;code&gt;jmp block&lt;/code&gt; 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 direct branches reach and no others, and I had wiped the rest as unreachable.&lt;/p&gt;

&lt;p&gt;Why had the emulator never seen it? The initialiser opens like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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. My emulator's counter was pinned so that two images would see the same values, and its first read always ended in zero. I had made the comparison repeatable and, in the same stroke, blind to 255 of 256 ways through this function.&lt;/p&gt;

&lt;p&gt;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 the walk did not read and no rewrite wipes, a &lt;code&gt;jmp&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;jcc&lt;/code&gt; encoded at any offset whose target is a wiped byte keeps that target, and a second walk keeps what the target reaches. Build 1.20 has 1,651 of these and 799 pointers of the same kind, and keeping their targets costs 150 KB of the 65 MB that was wiped.&lt;/p&gt;

&lt;h3&gt;
  
  
  A value has a before
&lt;/h3&gt;

&lt;p&gt;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 TLS callbacks and all 6,442 initialisers run in order, the export does the same thing, and the log shows where: &lt;code&gt;WinMain&lt;/code&gt; makes no call at all. Its first lines, with the disguises taken off:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          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
          ...
          &amp;lt;a guard's construct&amp;gt;        ; publishes P
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;P&lt;/code&gt; is one of the 40 dwords a guard publishes, and the first three instructions are one of the 53 checks. Everything I had said about checks holds here except the one thing I had 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 &lt;code&gt;WinMain&lt;/code&gt; with &lt;code&gt;P&lt;/code&gt; still holding the value it has in the file. The check fails, the failing branch sets the flag, the flag lets &lt;code&gt;WinMain&lt;/code&gt; continue, and the very next construct is the guard that publishes &lt;code&gt;P&lt;/code&gt;. From then on &lt;code&gt;P&lt;/code&gt; is right, and the other check that reads it, one whose failing branch is the poisoned return, passes.&lt;/p&gt;

&lt;p&gt;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. &lt;code&gt;WinMain&lt;/code&gt; returns zero, the C runtime runs the destructors, and the process ends politely.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9eduv2wyfy9jcirdhgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh9eduv2wyfy9jcirdhgl.png" alt="Figure 5. A check placed before the guard that publishes its value. In the shipped program the first check fails every time, and its failing branch is the way forward. A cleaned file that stores the value ahead of time turns that branch off." width="800" height="297"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 5. A check placed before the guard that publishes its value. In the shipped program the first check fails every time, and its failing branch is the way forward. A cleaned file that stores the value ahead of time turns that branch off.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;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. 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,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov  dword ptr [rip+P], 671CF959h      ; ten bytes, no register, no flag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so the value appears when the guard would have run. Checks that read such a value are left exactly as they are, both branches.&lt;/p&gt;

&lt;p&gt;That last sentence I learned from the third ending. With the fold merely withdrawn after planning, the game drew its window and then reported &lt;code&gt;Fatal Error … 0xc0000005&lt;/code&gt; itself. A snapshot of the process with the box still up has the faulting thread's context on its stack: a &lt;code&gt;ret&lt;/code&gt; popping &lt;code&gt;0x19C8E1FCDFD36281&lt;/code&gt;, with the alignment words &lt;code&gt;10h&lt;/code&gt; and &lt;code&gt;18h&lt;/code&gt; above it. That is the poisoned return of a check, live. The check's guard had never run, because a jump I had written earlier was aimed at the end of the chain of jumps after another check and looked straight through the guard to its continuation. Deciding that a construct stays has to come before any jump target is computed.&lt;/p&gt;

&lt;h3&gt;
  
  
  A window
&lt;/h3&gt;

&lt;p&gt;With the three rules in place the counts for 1.20 read differently, and more honestly: 258 of 275 constructs folded and 17 left in place because their guard publishes a value; 31 of 55 checks folded and 24 left as shipped, both branches. 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.&lt;/p&gt;

&lt;p&gt;Much later, after the junk constructs had been taken out as well (19,064 more rewrites in this build), I did not take the emulator's word for it a second time. A fresh export of 1.20 went through the comparison, 398 of 411 functions equal and none different, then the start in the emulator, 6,450 of 6,450 stretches the same, and then one more real start: the splash two seconds after the process began, the game's window drawing by the fourteenth second, the same name lookups, no fault and no error event, and still running when I stopped it after seventy-five seconds.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  From the entry point, in the emulator
&lt;/h2&gt;

&lt;p&gt;So the comparison grew a second form, and it is the one I trust most now. Start both images 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 &lt;code&gt;main&lt;/code&gt; 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.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;build&lt;/th&gt;
&lt;th&gt;TLS callbacks&lt;/th&gt;
&lt;th&gt;stretches&lt;/th&gt;
&lt;th&gt;the same in both&lt;/th&gt;
&lt;th&gt;API calls up to the wait&lt;/th&gt;
&lt;th&gt;routines entered, protected side&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1.07.0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;2,681&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.20&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;6,450&lt;/td&gt;
&lt;td&gt;6,450&lt;/td&gt;
&lt;td&gt;4,855&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.30.0&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;4,942&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;5,785&lt;/td&gt;
&lt;td&gt;154&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Getting past 1.20 meant playing more of Windows, because the game's start-up asks more of it in each generation, and an emulator that answers "success" and writes nothing sends both images down the same wrong path. 1.30.0 reads a system DLL from disk through &lt;code&gt;NtCreateFile&lt;/code&gt; and &lt;code&gt;NtReadFile&lt;/code&gt; and then executes the &lt;code&gt;syscall&lt;/code&gt; instruction itself, with a service number it has just read out of that file; an emulator that steps over &lt;code&gt;syscall&lt;/code&gt; hands back a null pointer as success, and both images die on it at initialiser 43 of 6,539. 1.69.0 loads the number and jumps to the &lt;code&gt;syscall ; ret&lt;/code&gt; at the end of a stub inside the loaded &lt;code&gt;ntdll&lt;/code&gt;. Answered from the same table the program reads, both go through. That code is the game's and its anti-cheat's, and I treated it as a black box: the emulator answers what it asks and looks no further.&lt;/p&gt;

&lt;p&gt;The last obstacle was the protection's own. In 1.69.0 the path through &lt;code&gt;main&lt;/code&gt; enters 154 of its routines before the first wait, against two dozen in the older builds, and one of them is a debugger probe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A processor that executes &lt;code&gt;ud2&lt;/code&gt; 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 &lt;code&gt;rax&lt;/code&gt; it finds in the context, moves the context's &lt;code&gt;rip&lt;/code&gt; 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 &lt;code&gt;main&lt;/code&gt;, the handler answers it, and the protected image arrives at the same wait as the cleaned one after the same 5,785 calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  What a function entered cold cannot tell you
&lt;/h3&gt;

&lt;p&gt;Running the function comparison over many builds produced a steady trickle of functions that differed, and almost every one turned out to be a property of entering a function with no history. They are worth listing because the protection is what makes each of them visible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A check at the door.&lt;/strong&gt; A function entered cold can meet a check before the guard that publishes its value has run. The protected image then dies on the check's failing path, a jump through four wrong addresses, and the cleaned one dies somewhere else, because that table is the failing path's alone and a program that works never reads it. Where the value is one a kept routine publishes at run time, both images fail the same check, each through its own wrong address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The protection opens and closes its own code.&lt;/strong&gt; Pieces of the protection sit in the unused tail of data sections, and one was different after a run: 168 bytes that one routine writes as readable instructions and a later one covers with noise again. That is a writer and its twin bracketing one use of a piece of protection code. A cleaned image has nobody left to close it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Routines leave their registers on the stack.&lt;/strong&gt; When the game 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 two bytes of a register a routine saved. Fill the stack below the function with another value, run the cleaned side again, and exactly those bytes change with it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The protection is 90 megabytes of plausible neighbours.&lt;/strong&gt; A function given filler for arguments can index a table with it and land in the protection's section, where the protected side reads protection code and the cleaned side reads what replaced it. 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not every record is a function.&lt;/strong&gt; The exception directory has records that begin in the middle of one of the protection's constructs, some of them on a 16-byte boundary by chance. A record whose unwind data is chained to another is a later piece of a function, not an entry.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of those is now recognised by what happened on the path, not by how the path ended, and each gets its own verdict instead of "different".&lt;/p&gt;

&lt;h2&gt;
  
  
  What a memory dump holds
&lt;/h2&gt;

&lt;p&gt;A dump of the running game is the other way to get this code out from under its protection, so it was 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 &lt;code&gt;0x7FF6D7950000&lt;/code&gt;, and its pointers are relocated to that address. Read both by RVA and they agree on where everything is. For 1.51.2:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;at the same RVA in both&lt;/th&gt;
&lt;th&gt;same&lt;/th&gt;
&lt;th&gt;of&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;sections&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;function records in the exception directory&lt;/td&gt;
&lt;td&gt;127,967&lt;/td&gt;
&lt;td&gt;127,967&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pointers in read-only data, each side's base subtracted&lt;/td&gt;
&lt;td&gt;78,973&lt;/td&gt;
&lt;td&gt;78,973&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes of game code&lt;/td&gt;
&lt;td&gt;117,293,940 (97.6%)&lt;/td&gt;
&lt;td&gt;120,134,656&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes of game code with the dump moved by 16 bytes&lt;/td&gt;
&lt;td&gt;4.5%&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;97.6% against 4.5% leaves no doubt about the addresses. 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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;of the 1,475,362 code bytes where the dump is not the decrypted image&lt;/th&gt;
&lt;th&gt;bytes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;exactly what one writer writes, over 115,632 ranges&lt;/td&gt;
&lt;td&gt;805,560&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exactly what three other writers write&lt;/td&gt;
&lt;td&gt;194,164&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exactly what smaller writers write&lt;/td&gt;
&lt;td&gt;4,734&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;inside a writer's ranges, in another state&lt;/td&gt;
&lt;td&gt;224,770&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;in no writer's ranges&lt;/td&gt;
&lt;td&gt;246,134&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;None of those writers decrypts anything. What each of them writes is noise: 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.&lt;/p&gt;

&lt;p&gt;A second dump of the same build, made by another tool at another time, shows how much the moment matters.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;the same build, two dumps&lt;/th&gt;
&lt;th&gt;taken early&lt;/th&gt;
&lt;th&gt;taken late&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;pages of game code present&lt;/td&gt;
&lt;td&gt;12,845 of 29,329&lt;/td&gt;
&lt;td&gt;29,329 of 29,329&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes identical to the decrypted image, in the pages present&lt;/td&gt;
&lt;td&gt;99.6%&lt;/td&gt;
&lt;td&gt;98.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;of the bytes writers decrypt, still encrypted&lt;/td&gt;
&lt;td&gt;99.2%&lt;/td&gt;
&lt;td&gt;17.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;noise a hiding writer left&lt;/td&gt;
&lt;td&gt;under 8,000 bytes&lt;/td&gt;
&lt;td&gt;1,004,458 bytes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The early one is the game seconds into its life: the outer layer off, almost nothing opened yet, nothing hidden again yet.&lt;/p&gt;

&lt;p&gt;A dump of the last version, 1.69.0, caught an earlier moment still, and I only noticed because its numbers looked wrong. Its addresses agree with the cleaned file like all the others, section for section and record for record, but only two thirds of its code bytes do. Held against the &lt;em&gt;shipped&lt;/em&gt; file instead, the reason is a single address. The game's code is decrypted up to &lt;code&gt;0x54DE87A&lt;/code&gt;, and from the next byte on the dump is the shipped file's ciphertext, byte for byte, to the end of the section and through the whole of the protection's section behind it. The dumper stopped the process 73% 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ten versions
&lt;/h2&gt;

&lt;p&gt;The last thing I did was run the whole removal, with every check above, on versions spread across the life of the game, from the September 2019 build to the last one. Nothing in the tools knows any of them. Each row is one shipped executable taken through decryption, removal and export, then compared with its protected original function by function, started in the emulator from its entry point beside it, and held against a memory dump of the same version where I had one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;version&lt;/th&gt;
&lt;th&gt;routines&lt;/th&gt;
&lt;th&gt;junk constructs removed&lt;/th&gt;
&lt;th&gt;functions compared&lt;/th&gt;
&lt;th&gt;equal&lt;/th&gt;
&lt;th&gt;out of time or no verdict&lt;/th&gt;
&lt;th&gt;different&lt;/th&gt;
&lt;th&gt;start: stretches&lt;/th&gt;
&lt;th&gt;the same&lt;/th&gt;
&lt;th&gt;memory dump&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0.01.2&lt;/td&gt;
&lt;td&gt;551&lt;/td&gt;
&lt;td&gt;12,578&lt;/td&gt;
&lt;td&gt;396&lt;/td&gt;
&lt;td&gt;383&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,181&lt;/td&gt;
&lt;td&gt;6,181&lt;/td&gt;
&lt;td&gt;none to compare&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.07.0&lt;/td&gt;
&lt;td&gt;567&lt;/td&gt;
&lt;td&gt;12,868&lt;/td&gt;
&lt;td&gt;402&lt;/td&gt;
&lt;td&gt;391&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;6,294&lt;/td&gt;
&lt;td&gt;none to compare&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.23.2&lt;/td&gt;
&lt;td&gt;616&lt;/td&gt;
&lt;td&gt;18,538&lt;/td&gt;
&lt;td&gt;410&lt;/td&gt;
&lt;td&gt;396&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,505&lt;/td&gt;
&lt;td&gt;6,505&lt;/td&gt;
&lt;td&gt;none to compare&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.30.0&lt;/td&gt;
&lt;td&gt;621&lt;/td&gt;
&lt;td&gt;20,774&lt;/td&gt;
&lt;td&gt;411&lt;/td&gt;
&lt;td&gt;396&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;6,539&lt;/td&gt;
&lt;td&gt;none to compare&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.31.5&lt;/td&gt;
&lt;td&gt;621&lt;/td&gt;
&lt;td&gt;20,730&lt;/td&gt;
&lt;td&gt;411&lt;/td&gt;
&lt;td&gt;397&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,551&lt;/td&gt;
&lt;td&gt;6,551&lt;/td&gt;
&lt;td&gt;same addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.39.3&lt;/td&gt;
&lt;td&gt;625&lt;/td&gt;
&lt;td&gt;21,085&lt;/td&gt;
&lt;td&gt;411&lt;/td&gt;
&lt;td&gt;394&lt;/td&gt;
&lt;td&gt;17&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,027&lt;/td&gt;
&lt;td&gt;6,027&lt;/td&gt;
&lt;td&gt;same addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.41.2&lt;/td&gt;
&lt;td&gt;626&lt;/td&gt;
&lt;td&gt;21,183&lt;/td&gt;
&lt;td&gt;412&lt;/td&gt;
&lt;td&gt;393&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,032&lt;/td&gt;
&lt;td&gt;6,032&lt;/td&gt;
&lt;td&gt;none to compare&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.51.2&lt;/td&gt;
&lt;td&gt;734&lt;/td&gt;
&lt;td&gt;23,674&lt;/td&gt;
&lt;td&gt;407&lt;/td&gt;
&lt;td&gt;392&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,463&lt;/td&gt;
&lt;td&gt;6,463&lt;/td&gt;
&lt;td&gt;same addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.64.0&lt;/td&gt;
&lt;td&gt;744&lt;/td&gt;
&lt;td&gt;23,341&lt;/td&gt;
&lt;td&gt;408&lt;/td&gt;
&lt;td&gt;393&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,995&lt;/td&gt;
&lt;td&gt;6,995&lt;/td&gt;
&lt;td&gt;same addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1.69.0&lt;/td&gt;
&lt;td&gt;735&lt;/td&gt;
&lt;td&gt;22,429&lt;/td&gt;
&lt;td&gt;406&lt;/td&gt;
&lt;td&gt;392&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;6,950&lt;/td&gt;
&lt;td&gt;same addresses&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ten of ten export. No function differs in any of them, and every stretch of every start is the same in both images, between six and seven thousand stretches a version. The functions without a verdict are the ones where the protected side ran out of emulated time, plus one to four per version that fell into the cold-entry cases above. Where I had a memory dump of the version, every section, every function record and every pointer in read-only data sits at the same address in the dump as in the cleaned file.&lt;/p&gt;

&lt;p&gt;The table also shows how little the protection moved in two and a half years. The routine count grows from 551 to 744 and the code around it more than doubles, but every one of these versions has the same 53 checks on 40 published values, the same three start-up calls at every door, and the same three writes inside every routine.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Part 6: Looking back&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The tool
&lt;/h2&gt;

&lt;p&gt;What came out of all this is one static tool, &lt;code&gt;mwdec&lt;/code&gt;, and it is smaller than the story. It reads a shipped executable and never loads or runs the game. From the file alone it finds the bootstrap cipher's parameters and its list, every writer with its stash, key and encoding, both import tables, the three addresses each routine writes and the values the checks need, and it produces one of two things.&lt;/p&gt;

&lt;p&gt;The first is a decrypted image, for a disassembler: every byte readable, every string in plain text, and the protection still in it, inert, because its start-up flags are unset and its guards would be hashing code that is no longer in the state they expect. On 1.51.2 that takes under six seconds, four of them the keystream.&lt;/p&gt;

&lt;p&gt;The second is the cleaned image, meant to stand on its own. The removal is kept apart from the writing of the file: the analysis produces plans, each a list of byte patches, relocation entries to delete and ranges to fill with &lt;code&gt;int3&lt;/code&gt;, and the exporter only applies plans. That keeps every decision inspectable, since a plan can be printed, compared with another, or applied alone to see which one a difference comes from. Planning is the slow part, a walk of 27 million instructions several times over. Everything runs in one process from the shipped file, nothing kept from an earlier run, and the walks that do not depend on each other run side by side. The whole export, shipped file to cleaned file, takes about half a minute for the oldest build, one minute for 1.30.0 and under two minutes for 1.69.0, the largest.&lt;/p&gt;

&lt;p&gt;The evidence is of different kinds, and it is worth keeping them apart.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;what&lt;/th&gt;
&lt;th&gt;compared with&lt;/th&gt;
&lt;th&gt;result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;bootstrap layer&lt;/td&gt;
&lt;td&gt;memory left by the real routines, run to completion in the emulator&lt;/td&gt;
&lt;td&gt;25 bytes differ in 224,083,968, all of them the routines' own bookkeeping&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bootstrap layer&lt;/td&gt;
&lt;td&gt;memory dump of the running game&lt;/td&gt;
&lt;td&gt;99.05% of the game's code equal, up from 3.67%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;writer layer&lt;/td&gt;
&lt;td&gt;the same writers applied to the dump&lt;/td&gt;
&lt;td&gt;883,737 of 883,737 bytes equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;615 start-up chains skipped&lt;/td&gt;
&lt;td&gt;the chain, run from its first instruction to its exit&lt;/td&gt;
&lt;td&gt;615 equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;730 routine replacements&lt;/td&gt;
&lt;td&gt;the real routine, from its call to the continuation&lt;/td&gt;
&lt;td&gt;730 equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;309 nests folded&lt;/td&gt;
&lt;td&gt;the shipped nest with the real routines running&lt;/td&gt;
&lt;td&gt;309 equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;183,544 branches rewritten&lt;/td&gt;
&lt;td&gt;the shipped idiom, one of each of 1,605 shapes and a sample&lt;/td&gt;
&lt;td&gt;42,717 runs, all equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;113,572 stack pairs&lt;/td&gt;
&lt;td&gt;the shipped pair, over 22,202 shapes&lt;/td&gt;
&lt;td&gt;24,610 pairs, all equal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;whole functions&lt;/td&gt;
&lt;td&gt;the decrypted image with its protection running&lt;/td&gt;
&lt;td&gt;6,089 equal, 0 different, 82 out of time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;the start, entry point to &lt;code&gt;main&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;the same, protection running&lt;/td&gt;
&lt;td&gt;every stretch equal, on all ten versions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;one cleaned build, as a process&lt;/td&gt;
&lt;td&gt;the shipped file, as a process&lt;/td&gt;
&lt;td&gt;the same window, four seconds sooner&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Arxan in this game is smaller than its footprint suggests. Under a hundred megabytes of scattered chunks there are four kinds of routine, three hash functions, six debugger probes and one response primitive. Under 209 MB of ciphertext there is one keystream, one list, and four ways of encoding a dword. What makes it hard is not any one of those. It is that each of them is arranged so that the obvious next step is slightly wrong.&lt;/p&gt;

&lt;p&gt;The design choices I would remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One list format for everything.&lt;/strong&gt; What a guard hashes, what the bootstrap decrypts, where a writer writes, and which import slots get copied are all the same LEB128 &lt;code&gt;(gap, length)&lt;/code&gt; stream with the same terminator. Recognise it once and every table in the protection becomes readable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encrypt around the loader, not against it.&lt;/strong&gt; The file is a valid PE that Windows can map, relocate and bind unaided. The cipher is cut into 1.3 million pieces to fit between the relocations. The entry point is the compiler's, with one &lt;code&gt;call&lt;/code&gt; redirected.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing is derived at run time.&lt;/strong&gt; The key, the starting block, every range list, every stash and every stash key are bytes in the file. So is every address a routine sends its caller to, and every value a check expects. That is why the whole of it can be undone statically, and why the decryptor destroys its key after use: the file is the only place it ever needed to be.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Copies, not ciphers.&lt;/strong&gt; An on-demand function is not decrypted. It is overwritten from an encoded copy kept somewhere else, sometimes through two more copies, and overwritten with noise again when it is done. The noise at rest is a placeholder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Self-containment.&lt;/strong&gt; A routine carries its own base calculation, tables and frame, so it can be spliced anywhere. The same property lets each one be run in isolation, which is what made all of this possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutual coverage.&lt;/strong&gt; A tenth of the protection's own section is hashed, against less than half a percent of the game's. The guards are there mainly to protect each other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separation of check and penalty.&lt;/strong&gt; A failed guard poisons its own return or leaves a wrong number behind. The damage appears later, in code that never compared anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the things that taking it out taught me, mostly by being wrong about them first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The way on is inside the routine.&lt;/strong&gt; A routine spliced into a function does not return to where its caller says. The construct around it pushes three believable addresses, and the routine replaces all three while it runs. Nothing that skips the routine can find where the function continues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decoys are real code.&lt;/strong&gt; Each of the three pushed addresses is a valid instruction in some other function. A patched-out routine does not crash at once. It carries on somewhere plausible with the wrong stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What the program needs is small.&lt;/strong&gt; One function the entry point's &lt;code&gt;call&lt;/code&gt; used to name, one import table instead of two, and forty dwords of published state, each at the right time. Everything else the protection does at run time is for itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Most of the bulk is handwriting.&lt;/strong&gt; 184,000 disguised branches and 114,000 stack instructions written as pairs carry no secret at all. They are a fixed set of spellings, and undoing them is a table lookup plus the care not to overwrite a byte something else jumps to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A static walk has an edge.&lt;/strong&gt; Removal is exact for the code a walk can reach, and the honest thing to do with the rest is to leave it and everything it touches alone. Finding the rest is a search, not a walk: a computed jump still has to land on bytes the file spells out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A check can be there to fail.&lt;/strong&gt; A check placed before its guard takes its failing branch in the shipped program every time, and the program is written to need that. Supplying the guard's answer early switches the branch off, and nothing crashes to say so.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A dump is a moment, and so is a function entered cold.&lt;/strong&gt; The running process has functions hidden again, stashes rewritten and holes punched in its code; a function run on its own has no history. Both are true pictures of something that is not quite the program.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Looking back over the order of it, the pattern is that the emulator kept being right about less than I thought. It was right about every routine it ran, and that was enough to model the protection. It was right about every function it compared, and that was not enough to start a process, because a process has a history and a function entered in the middle of one does not. Each time, the fix was the same: stop fitting to the sample I had and go to the next larger thing, a second build, a real start, the whole run from the entry point, ten versions. The protection's values have a time as well as a place, and a program can be built to need the moment before one of its own checks passes. The listing had been in front of me the whole time. It took a window that did not open to make me read it that way.&lt;/p&gt;

&lt;p&gt;The weakness from the first part, publishing before judging, held up in a running process. A guard found five changed bytes, the handler gave it the digest it expected at the moment it published, and the game went on as if nothing had changed, where without the handler the protection answered within a second and the process died. What is still open is the same experiment against the guards that hash the game's own code, which did not run in the part of the game I could reach.&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverseengineering</category>
      <category>gamedev</category>
      <category>windows</category>
    </item>
  </channel>
</rss>
