Towards Ultimate Deobfuscation

wail.ly/secday-2015-slides.html

wail.ly/secday-2015-slides.html

Who I am ?

Plan

What is obfus cation ?

Main strcmp

ida-main.png
Main strcmp

With OLLVM

ida-main-ollvm.png
Obfuscated (OLLVM (4))

Protect code and data

Code impénétrable as Jiss

Code protection example

Data protection example

Reverse Engineering

Find the functional equivalent

Basic RE algo [2]

basic-re-algo.png
Basic RE algorithm

How to slow a reverse engineer ?

Multiple techniques [1]

How to deob fuscate ?

Idea

Find the high-level semantic (pseudo-code)

Goal

We need an automated tool easily supporting new architectures

Bad idea #1: Reimplement the assembly semantic

Create your own translator

Reimplement the semantic

BAP add rax, rbx

addr 0x0 @asm "add    %rax,%rbx"
label pc_0x0
T_t1:u64 = R_RBX:u64
T_t2:u64 = R_RAX:u64
R_RBX:u64 = R_RBX:u64 + T_t2:u64
R_CF:bool = R_RBX:u64 < T_t1:u64
R_OF:bool = high:bool((T_t1:u64 ^ ~T_t2:u64) & (T_t1:u64 ^ R_RBX:u64))
R_AF:bool = 0x10:u64 == (0x10:u64 & (R_RBX:u64 ^ T_t1:u64 ^ T_t2:u64))
R_PF:bool =
  ~low:bool(let T_acc:u64 := R_RBX:u64 >> 4:u64 ^ R_RBX:u64 in
            let T_acc:u64 := T_acc:u64 >> 2:u64 ^ T_acc:u64 in
            T_acc:u64 >> 1:u64 ^ T_acc:u64)
R_SF:bool = high:bool(R_RBX:u64)
R_ZF:bool = 0:u64 == R_RBX:u64

Solution

What is THE generic program that convert ASM to atomic operations ?

The T iny C ode G enerator

Example with Qemu

ret
Getting target map
000000: \xc3                                              .
 qemu_ld_i64 tmp0,rsp,leq,$0x0
 movi_i64 tmp11,$0x8
 add_i64 tmp3,rsp,tmp11
 mov_i64 rsp,tmp3
 st_i64 tmp0,env,$0x80
 exit_tb $0x0
 end

Why ?

But

Bad idea #2: Intermediate Representation IR

Create your own IR

And reimplement compiler optimizations

Why ?

You will be faced with ancient compilers problems

dragons.jpg

Bad idea #3: Optimize with Regexp

Pattern matching

Solution

Use a supported IR with native optimizations: LLVM

LLVM ret

  %Lgv1 = load i64* @rsp
  %Ildq = inttoptr i64 %Lgv1 to i64*
  %Ldq = load i64* %Ildq
  %Oarith = add i64 %Lgv1, 8
  store i64 %Oarith, i64* @rsp
  store i64 %Ldq, i64* @rip
  ret i64 0

Bad idea #42

!

troll.jpg
Oops

So ?

We all waited it :

the-only-way.jpg

Is it possible ?

graal.jpg
Is it possible ?

Panda @moyix

All the madness started here

But

Based on a very specific version of Qemu, updating will be hard (Aarch64 and Cpp)

Idea

blockdiag-idea.png

Our engine

About the source code

nROP cons

nROP cons 2

Demo time

Still a long way to go

Future work

Interested ?

aurelien.wailly () orange.com

Conclusion

Merci!

Resources