Closer look at the native constant mutation in ConfuserEx

In my Dissecting ConfuserEx – x86 switch predicates paper I quickly went over the actual code used in order to understand the Switch jump flow. But I simply debugged it to see the return value, and didn’t go into detail about what the code does. I just thought it wouldn’t be that interesting or revelant to the paper. That’s why I decided to create this blog entry in order to properly explain it. You will need to read the paper in order to understand what I’m talking about here.

Let’s start by looking at an obfuscated piece of C# code;

Image

and the IL equivalent:

Image

Let’s follow the native method with RVA 20F0 call at:

IL_0011: call int32 <Module>::(int32)

in OllyDbg. We’ll find:

003420F0 /. 89E0 MOV EAX,ESP
003420F2 |. 53 PUSH EBX
003420F3 |. 57 PUSH EDI
003420F4 |. 56 PUSH ESI
003420F5 |. 29E0 SUB EAX,ESP
003420F7 |. 83F8 18 CMP EAX,18
003420FA |.- 74 07 JE SHORT 00342103 <-- THIS JUMP SHOULD NOT BE TAKEN
003420FC |. 8B4424 10 MOV EAX,DWORD PTR SS:[ESP+10]
00342100 |. 50 PUSH EAX
00342101 |.- EB 01 JMP SHORT 00342104
00342103 |> 51 PUSH ECX
00342104 |> B8 6D739303 MOV EAX,393736D
00342109 |. 81C0 BEBDB45E ADD EAX,5EB4BDBE
0034210F |. 59 POP ECX
00342110 |. 69C9 2538C0C9 IMUL ECX,ECX,-363FC7DB
00342116 |. 69C9 E5FC94FD IMUL ECX,ECX,-26B031B
0034211C |. 29C8 SUB EAX,ECX
0034211E |. 81C0 B2C98459 ADD EAX,5984C9B2
00342124 |. 5E POP ESI
00342125 |. 5F POP EDI
00342126 |. 5B POP EBX
00342127 \. C3 RETN

This is the code that returns the value deciding where the switch should jump next. We can skip instructions up until:

003420FC |. 8B4424 10 MOV EAX,DWORD PTR SS:[ESP+10]
00342100 |. 50 PUSH EAX

This is where it moves the data of the passed method parameter into the EAX register, in our case: 0x2515CA13. Right after it pushes the value onto stack. It then does an unconditional jump to:

00342104 |> B8 6D739303 MOV EAX,393736D
00342109 |. 81C0 BEBDB45E ADD EAX,5EB4BDBE

This should be quite self-explanatory. It moves an immediate value of 0x393736D into EAX, then adds 0x5EB4BDBE to it. EAX now contains: (0x393736D + 0x5EB4BDBE) == 0x6248312B

0034210F |. 59 POP ECX

Put whatever value is on top of stack into ECX, in our case it will load 0x2515CA13, pushed from: 00342100 PUSH EAX.

00342110 |. 69C9 2538C0C9 IMUL ECX,ECX,-363FC7DB
00342116 |. 69C9 E5FC94FD IMUL ECX,ECX,-26B031B

Here it does some more arithmetic. It first multiplies whatever is in ECX with -0x363FC7DB and stores it in ECX. It then multiplies whatever is in ECX with -0x26B031B and stores it in ECX once again. ECX now contains: (0x2515CA13 * -0x363FC7DB * -0x26B031B).

0034211C |. 29C8 SUB EAX,ECX

Subtract whatever is in EAX with ECX. EAX now contains: 0x6248312B – (0x2515CA13 * -0x363FC7DB * -0x26B031B).

0034211E |. 81C0 B2C98459 ADD EAX,5984C9B2

Add 0x5984C9B2 to EAX. EAX now contains: (0x6248312B – (0x2515CA13 * -0x363FC7DB * -0x26B031B)) + 0x5984C9B2. And now to the beautiful part. All this complicated looking math is actually equal to 2. Knowing this we can follow the jump flow of the switch as shown in the image below:

Image

This might all seem a bit over complicated for such a simple task of hiding a constant value. But it really isn’t. Splitting a constant up into parts and reassembling it at runtime causes an array of different problems for automatic deobfuscation tools. In my project ConfuserDeobfuscator I created a simple IL emulator in order to fold these “mutated” constants into one value. But doing this in x86 assembly makes it more difficult to emulate. Additionally, these methods are “randomly” generated from a small set of opcodes. The ones showed in this example (IMUL, ADD, SUB) aren’t all. Here’s a list of the possible opcodes:

public enum x86OpCode
 {
          MOV,
          ADD,
          SUB,
          IMUL,
          DIV,
          NEG,
          NOT,
          XOR,
          POP
 }

But all that aside, here’s what the x86 assembly code above could look like if it was implemented in C#:

Image

 

I hope this gave you some more insight on what the native code actually does. Is there something incorrect in the text? Do you have any questions or feedback? Post it in the comments below. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s