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.

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.

Three parameters from the file

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.

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 gap 8, length (distance − 8). 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.

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.

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.

1.51.2 1.20
ranges in the list 1,324,188 851,351
bytes covered 208,856,213 139,805,245
islands in the protection's section 1,097 2,675
islands of exactly 16 bytes 3 4
islands of exactly 8 bytes 7 10
pairs to try 21 40

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, mov, lea, call, a few ModRM bytes, 00, CC. Fifty byte values, so noise scores about 20%.

1.51.2 1.20
best pair 59.2% 54.1%
second best 22.7% 22.9%
time, whole search 2.5 s 12 s

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

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 9E3779B9 occurs exactly once in the file, in code the protection has to leave readable, since it is the code that makes everything else readable.

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 

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.

Every list, read backwards

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 0xFFFFFFFF. A gap that large is always five LEB128 bytes, and the last of them is 0D, 0E or 0F.

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.

1.51.2, decrypted 1.20, first layer off
possible endings 105,690 57,091
lists 310 191
time 16 s 14 s

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.

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:

copies ranges bytes of them on unreadable pages of game code
4 296,965 3,706,464 1,263,502
4 22 109,294 109,077
4 26 30,673 30,141
3 142 23,987 19,621

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.

A key from the stash

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:

148758A0 lea rax, [rip+42ABA9Ah] ; 18B21341 148758CF lea rdx, [rip+2686562h] ; 16EFBE38, a table of RVAs 148758F5 lea rax, [rip+0B53F5Fh] ; 153C985B, the list 

0x18B21341 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.

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.

  • For a MulRol stash it prints the row's key, 309F06A8, with 76.7% of the decoded bytes common code bytes. The runner-up has 27.5%.
  • For an XorAuto 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: p = e ^ k ; k += p. Bit 0 of the next k is bit 0 of e whatever k 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 12345678, the same stash is the same code after its first few hundred bytes. An XorAuto stash is readable without its key, apart from its first lines.

On 1.20 neither of the two 3.7 MB blocks decodes this way. They are not XorAuto (any key would have shown it) and not Xor or Sub (a fixed key leaves each of the four byte positions with a favourite value, and these are flat). And the search finds no MulRol 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.

Three instructions per writer

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.

what how the writer names it rows
stash lea rax,[rip+S] ; mov [rbp+68h],rax 100
list lea rax,[rip+L] ; mov [rbp+48h],rax ; mov eax,1 ; test eax,eax 100
key, Xor xor eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax, in the loop 36
key, Sub add eax,[rip+K] ; mov rdx,[rbp+78h] ; mov [rdx],eax, in the loop 23
key, XorAuto mov eax,[rip+K] ; mov [rbp+8Ch],eax, before the set-up 19
key, MulRol mov eax,[rip+K] ; mov [rbp+88h],eax ; mov eax,[rip+K] ; and eax,1Fh 22

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

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.

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.

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 

The qword at Q 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, 0x140000000, to arrive at the image base without ever asking for it. For a tool this means the stash is the last address stored into the frame before the list's, not the first: the first is X, and it is a different slot.

With stash, list, key and encoding in hand, each candidate row can simply be decoded, and what comes out says what the row is.

a row's output on 1.51.2 rows bits per byte common code bytes
code or data, different from what is in place: a decryptor 78 4.9 to 6.7 58% to 77%
code, identical to what is in place: a repair writer 33 5.6 to 6.8 37% to 70%
neither 87 7.1 to 8.0 16% to 24%

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 0x18B21341 is itself the single range of an XorAuto writer's list. Once that one has run, the block decodes with the Sub key its own loop names. The other block is the twin's.

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.

table built from emulated writers table read from the file
writers that prepare a stash 12 12
writers that produce code 88 78
bytes those change 3,188,708 3,188,708
bytes that differ between the two images 0 of 297,288,240

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.

1.20 has no known-good image to compare with, so its measure is what is left unreadable.

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

A writer in .pdata

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.

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.

copy named by
0x150BA81A a writer's set-up: Sub, stash 0x15CB65F5
0x13725257 a guard
0x146E90AF nothing in the executable sections

The Sub 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.

One more row stood out. A MulRol writer whose list is a single range of exactly 2,705,700 bytes, at 0x14A2C367. A single range of another writer's size is what a stash-preparing writer looks like, except that no writer named 0x14A2C367 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 0x14A2C367.

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% 

Laid over the ranges of the unread list, the Xor 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 0x146E90AF or 0x14A2C367, RIP-relative, absolute, or as a bare RVA.

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 .pdata.

.pdata 12A47000 size 140200 read-only data 12B87175 lea rax, [rip+…] ; 14A2C367, the stash 12B871CA lea rax, [rip+…] ; 146E90AF, the list 

.pdata 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 .rdata; here _RDATA and .pdata hold pieces too, and the piece in .pdata 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 .pdata 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.

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.

0.01.2 code sections only all read-only sections
writer set-ups 63 64
rows kept 20 22
bytes the kept writers produce 72,506 2,778,206
pages of game code above 7.2 bits after both layers 233 1

The new row's set-up names its key at 0x154AA25F. The dword there is 1AD6DCB2: 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.

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 mov rcx, 64-bit constant ; imul ; shr ; xor, 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 and under 30% common code bytes.

The way back is a constant

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 0x166D60D49 occur?

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:

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 

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.

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.

There is a second reading to hold it against. Where the three slots lie is known from the chain: just above its register save, 118h 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.

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:

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 

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.

0.01.2 1.03 1.20 1.51.2
routines 551 566 616 734
called, and returning (the start-up three) 3 3 3 3
jumped to, and three addresses read 548 563 613 731
slot number agrees with the chain 548 563 613 731
spellings of the store 2 2 2 1

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 rsp 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.

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 r8, and the measured stub copied even that. The table read from the code does not know leftovers. It says instead that rax, rcx, rdx and r8 to r11 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.

What the guards were going to say

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:

mov r, [rip+P] ; the published value add r, [rip+A] xor r, [rip+B] ; or sub 

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 B − A, 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.

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 mov. 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.

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.

Code in the padding of every section

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 .rdata and one page of .pdata, found as "data pages that many jumps land in". 1.20 has one such page. It also has this:

section bytes after its last directory jumps landing there
.rdata 18,568 44
.pdata 364 3
_RDATA (last page) 3
.rsrc 232 5
.reloc (last page) 2
.idata 500 4

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 _RDATA, .reloc and .idata, the smallest 22 bytes long.

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.