+ All Categories
Home > Documents > Semi automatic binary deprotection -...

Semi automatic binary deprotection -...

Date post: 29-May-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
60
Metasm Structural manipulation Challenge T2 2007 Optimization Semi automatic binary deprotection Alexandre Gazet Yoann Guillot A. Gazet & Y. Guillot Semi automatic binary deprotection 1/57
Transcript
Page 1: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Semi automatic binary deprotection

Alexandre Gazet

Yoann Guillot

A. Gazet & Y. Guillot Semi automatic binary deprotection 1/57

Page 2: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Plan

1 Metasm

2 Structural manipulation

3 Challenge T2 2007

4 Optimization

A. Gazet & Y. Guillot Semi automatic binary deprotection 2/57

Page 3: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Plan

1 MetasmOther disassemblersBindingBacktracing

2 Structural manipulation

3 Challenge T2 2007

4 Optimization

A. Gazet & Y. Guillot Semi automatic binary deprotection 3/57

Page 4: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Metasm

A. Gazet & Y. Guillot Semi automatic binary deprotection 4/57

Page 5: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Metasm

A. Gazet & Y. Guillot Semi automatic binary deprotection 5/57

Page 6: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Disassemblers

The reference: IDA Pro

Very good on unobfuscated code: compiled binaries (Microsoft)

Not useful on obfuscated binaries

No code interpretation

Heavy hypothesis on code behavior

Hypothesis

Both branches of conditionnal jumps are taken

No overlapping instructions

The call instruction always returns

A. Gazet & Y. Guillot Semi automatic binary deprotection 6/57

Page 7: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Disassemblers

The reference: IDA Pro

Very good on unobfuscated code: compiled binaries (Microsoft)

Not useful on obfuscated binaries

No code interpretation

Heavy hypothesis on code behavior

Hypothesis

Both branches of conditionnal jumps are taken

No overlapping instructions

The call instruction always returns

A. Gazet & Y. Guillot Semi automatic binary deprotection 6/57

Page 8: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Hypothesys: call returns

. t e x t :00403 E9F loc 403E9F : ; CODE XREF: .text : loc_40CDEF

. t e x t :00403 E9F push ebp

. t e x t :00403EA0 push ecx

. t e x t :00403EA1 push ebp

. t e x t :00403EA2 c a l l sub 40BECD

. t e x t :00403EA7 outsb

. t e x t :00403EA8 cmp edx , esp

. t e x t :00403EAA push esp

. t e x t :00403EAB i n c e s i

A. Gazet & Y. Guillot Semi automatic binary deprotection 7/57

Page 9: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Failure

. t e x t :0040BECD sub 40BECD proc near ; CODE XREF: .text :00403EA2

. t e x t :0040BECD cmp eax , ebp

. t e x t :0040BECF add dword p t r [ esp +0] , 1

. t e x t :0040BED4 te s t ebx , 1E2h

. t e x t :0040BEDA re tn 0Ch

. t e x t :0040BEDA sub 40BECD endp

A. Gazet & Y. Guillot Semi automatic binary deprotection 8/57

Page 10: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Binding

Definition

This is how we call an instruction’s semantics, through an array ofsymbolic expressions.

Instruction ADD:

a = d i . i n s t r u c t i o n . a rg s . s ymbo l i c

r e s = Exp re s s i on [ [ a [ 0 ] , :& , mask ] , :+ , [ a [ 1 ] , :& , mask ] ]

b ind ing [ a [ 0 ] ] = Exp re s s i on [ res , :& , mask ]b ind ing [ : e f l a g z ] = Exp re s s i on [ [ res , :& , mask ] , :==, 0 ]b ind ing [ : e f l a g s ] = s i g n ( r e s )b ind ing [ : e f l a g c ] = Exp re s s i on [ res , :> , mask ]b ind ing [ : e f l a g o ] = Exp re s s i on [ [ s i g n (a [ 0 ] ) , :==, s i g n (a [ 1 ] ) ] ,

: ’&&’ , [ s i g n (a [ 0 ] ) , : ’!=’ , s i g n ( r e s ) ] ]

A. Gazet & Y. Guillot Semi automatic binary deprotection 9/57

Page 11: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Binding

Instruction CALL:

a dd r r e t = Exp re s s i on [ d i . address , :+ , d i . b i n l e n g th ] . reduce }b ind ing = {

: esp => Exp re s s i on [ : esp , :− , opsz ] ,I n d i r e c t i o n [ : esp , opsz ] => a dd r r e t

}

For exemple:

dword p t r [ esp ] = 0x4010CEesp = esp−4

Instruction RDTSC:

b ind ing = {: eax => Exp re s s i on : : Unknown ,: edx => Exp re s s i on : : Unknown

}

A. Gazet & Y. Guillot Semi automatic binary deprotection 10/57

Page 12: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Binding

Instruction CALL:

a dd r r e t = Exp re s s i on [ d i . address , :+ , d i . b i n l e n g th ] . reduce }b ind ing = {

: esp => Exp re s s i on [ : esp , :− , opsz ] ,I n d i r e c t i o n [ : esp , opsz ] => a dd r r e t

}

For exemple:

dword p t r [ esp ] = 0x4010CEesp = esp−4

Instruction RDTSC:

b ind ing = {: eax => Exp re s s i on : : Unknown ,: edx => Exp re s s i on : : Unknown

}

A. Gazet & Y. Guillot Semi automatic binary deprotection 10/57

Page 13: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Binding

Instruction CALL:

a dd r r e t = Exp re s s i on [ d i . address , :+ , d i . b i n l e n g th ] . reduce }b ind ing = {

: esp => Exp re s s i on [ : esp , :− , opsz ] ,I n d i r e c t i o n [ : esp , opsz ] => a dd r r e t

}

For exemple:

dword p t r [ esp ] = 0x4010CEesp = esp−4

Instruction RDTSC:

b ind ing = {: eax => Exp re s s i on : : Unknown ,: edx => Exp re s s i on : : Unknown

}

A. Gazet & Y. Guillot Semi automatic binary deprotection 10/57

Page 14: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Backtracing, the theory

Definition

Symbolic emulation by walking the instruction flow backwards

A. Gazet & Y. Guillot Semi automatic binary deprotection 11/57

Page 15: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Backtracing, the facts

Flot d’execution:

c a l l loc 40becdh ; @403ea2h e826800000

cmp eax , ebp ; @40becdh 39e8

add dword p t r [ esp +0] , 1 ; @40becfh 8344240001

t e s t ebx , 1e2h ; @40bed4h f7c3e2010000

r e t 0ch ; @40bedah c20c00

Backtracing x dword ptr [esp] for 40bedah ret 0ch

1 backtrace 40becfh add dword ptr [esp+0], 1

dword ptr [esp] => dword ptr [esp]+1

2 backtrace up 40becdh->403ea2h dword ptr [esp]+1

3 backtrace 403ea2h call loc 40becdh

dword ptr [esp]+1 ⇒ 403ea8h

4 backtrace result: 403ea8h

A. Gazet & Y. Guillot Semi automatic binary deprotection 12/57

Page 16: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

Other disassemblersBindingBacktracing

Metasm

Assembler listing produced:

l o c 403e9 fh :push ebp ; @403e9fh 55

push ecx ; @403ea0h 51

push ebp ; @403ea1h 55

c a l l loc 40becdh ; @403ea2h e826800000 noreturn

db 6eh ; @403ea7h

// Xre f s : 40bedahloc 403ea8h :

cmp edx , esp ; @403ea8h 39e2

push esp ; @403eaah 54

[ . . . ]− − − − − − − − − − − − − − − − − − − − − − − −// Xre f s : 403 ea2hloc 40becdh :

cmp eax , ebp ; @40becdh 39e8

add dword p t r [ esp +0] , 1 ; @40becfh 8344240001

t e s t ebx , 1e2h ; @40bed4h f7c3e2010000

r e t 0ch ; @40bedah c20c00 x: loc_403ea8h

A. Gazet & Y. Guillot Semi automatic binary deprotection 13/57

Page 17: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Plan

1 Metasm

2 Structural manipulationIntroductionControl graph complexificationNeutral element insertionUnprotection

3 Challenge T2 2007

4 Optimization

A. Gazet & Y. Guillot Semi automatic binary deprotection 14/57

Page 18: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Securitech 2006 - Challenge 10

Poeut.exe

Massively obfuscated binary

IDA overwhelmed

Metasm disassembles correctly, but:

Binary blocks are randomely moved in the binary

⇒ need to write a graphic front-end

yEd - Graph Editor

Visualise graphs

Needs a graphml file as input

We’ll translate Metasm internal InstructionBlock representation tothis format

A. Gazet & Y. Guillot Semi automatic binary deprotection 15/57

Page 19: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Raw graph

A. Gazet & Y. Guillot Semi automatic binary deprotection 16/57

Page 20: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Obscur predicates

Obscur predicates

The predicate function always return true

Using a way not easily statically analysed

The conditionnal jump is in fact not conditionnal

i f ( xˆ4 * ( x−5)ˆ2 >= 0){goto r e a l c od e ;

} e l s e {goto no where ;

}

f s t p qword p t r [ esp+8]f s t p qword p t r [ esp ]c a l l thunk powf s t p qword p t r [ ebp−0x20 ]mov eax , dword p t r [ ebp−0ch ]sub eax , 5push eaxf i l d dword p t r [ esp ]l e a esp , dword p t r [ esp+4]f l d qword p t r [ xre f 8048590h ]f s t p qword p t r [ esp+8]f s t p qword p t r [ esp ]c a l l thunk powf l d qword p t r [ ebp−20h ]fmulp ST(1 )

A. Gazet & Y. Guillot Semi automatic binary deprotection 17/57

Page 21: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Obscur predicates

Full randomisation

The predicate function returns randomly true or false

Both branches after the conditionnal jump are equivallent

i f ( rand ( ) %2 ) {r ea l code A ;

} e l s e {r ea l code B ;

}

A. Gazet & Y. Guillot Semi automatic binary deprotection 18/57

Page 22: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Flow duplication

Massive use of random predicates:

diamond shaped graph.

A. Gazet & Y. Guillot Semi automatic binary deprotection 19/57

Page 23: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Neutral elements

Definition

Instruction (or group of) having empty semantics : no effect on theexecution context.

A. Gazet & Y. Guillot Semi automatic binary deprotection 20/57

Page 24: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Neutral elements: apparent randomization

Implementation:

t e s t esp , ebx ; @402039h 85e3

cmp ebx , edx ; @40203bh 39d3

mov byte p t r [ edx ] , a l ; @40203dh 8802

add dword p t r [ esp +0] , 6 ; @40203fh 8344240006

i n c edx ; @402044h 42

jmp loc 40b25dh ; @402045h e913920000

Solving the problem

Use the instructions’ binding

Incoherent use of the processor flags seen in the dataflow :

Two successive writings,

or written but never read

A. Gazet & Y. Guillot Semi automatic binary deprotection 21/57

Page 25: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Neutral elements: fake subfunctions

Implementation :push eax ; @408dadh 50

push ecx ; @408daeh 51

push ebp ; @408dafh 55

[ . . . ]c a l l l o c 4037 f2h ; @40932fh

[ . . . ]push esp ; @4037f4h 54

push ecx ; @4037f5h 51

[ . . . ]add dword p t r [ esp +8] , 9 ; @4037f9h 8344240809

add esp , 8 ; @403800h 83 c408

[ . . . ]r e t 0ch ; @403808h c20c00 x: loc_40933dh

Solution

Execution flow reconstruction

Stack emulation

We have a pattern: return address modification

A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57

Page 26: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Epilogue (raw)

A. Gazet & Y. Guillot Semi automatic binary deprotection 23/57

Page 27: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Execution flow analysis

Solution

1 Walk the intenal tree of InstructionBlock

2 Inline functions if needed

3 Scan for a diamond pattern

4 Construction, cleaning and comparison of flows

5 Factorisation

A. Gazet & Y. Guillot Semi automatic binary deprotection 24/57

Page 28: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Epilogue (factorized)

A. Gazet & Y. Guillot Semi automatic binary deprotection 25/57

Page 29: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Epilogue (reordered)

A. Gazet & Y. Guillot Semi automatic binary deprotection 26/57

Page 30: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Epilogue (final)

A. Gazet & Y. Guillot Semi automatic binary deprotection 27/57

Page 31: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionControl graph complexificationNeutral element insertionUnprotection

Last touch

Cleanup of the whole program graph

Output of a clean asm source

Used to reassemble an unprotected binary

A. Gazet & Y. Guillot Semi automatic binary deprotection 28/57

Page 32: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Plan

1 Metasm

2 Structural manipulation

3 Challenge T2 2007IntroductionObfuscationVirtual machineResolution

4 Optimization

A. Gazet & Y. Guillot Semi automatic binary deprotection 29/57

Page 33: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

T2 2007

t207.exe

http://www.t2.fi/challenge/

Goal: find the password to unlock the program

Very simple binary [demo/1]

Loads an obfuscated driver [demo/2]

A. Gazet & Y. Guillot Semi automatic binary deprotection 30/57

Page 34: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Deobfuscation

Obfuscation types

Junk code

Obfuscated arithmetics

Ring3 detection

Code duplication

A. Gazet & Y. Guillot Semi automatic binary deprotection 31/57

Page 35: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Junk code

junk

r o r ed i , 0dhxchg ebx , e d ir o r ebx , 13hxchg ebx , e d i

A. Gazet & Y. Guillot Semi automatic binary deprotection 32/57

Page 36: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Obfuscated arithmetics

bit rotation

push eaxpush ecxr o l dword p t r [ esp +4] , c lpop ecxpop eax

A. Gazet & Y. Guillot Semi automatic binary deprotection 33/57

Page 37: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Ring3 detection

test ring0

push fdpush eaxxo r eax , eaxmov ax , c scmp eax , 9j l e l o c 131d5hr d t s cimu l eax , ecxjmp eax ; x : unknown

l o c 131d5h :pop eaxpopfd

A. Gazet & Y. Guillot Semi automatic binary deprotection 34/57

Page 38: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Code duplication

duplication

push e s ipush ebxpush fdr d t s cimu l ecx , ebxcmp c l , 7 fhjnb l o c 21abapopfdpop ebxpop e s i

l o c 21abah :popfdpop ebxpop e s i

A. Gazet & Y. Guillot Semi automatic binary deprotection 35/57

Page 39: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Virtual machine

Handler structure

Simple operations

Similar blueprint

Operations controlled by [ebp]

[demo/3]

A. Gazet & Y. Guillot Semi automatic binary deprotection 36/57

Page 40: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Virtual addition

An addition handler

l o c 15336 :mov ecx , dword p t r [ ebp+0ch ]xo r ecx , 842 b1208hmov ecx , dword p t r [ ebx+ecx ]mov eax , dword p t r [ ebp+8]xo r eax , 842 b1208hadd dword p t r [ ebx+eax ] , ecx

A. Gazet & Y. Guillot Semi automatic binary deprotection 37/57

Page 41: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Inter-handler transition

transition to the next handler

mov ecx , dword p t r [ ebp+0]xo r ecx , 149 f0c63hmov ebp , dword p t r [ ebp+4]xo r ebp , 842 b1208hadd ebp , dword p t r [ ebx+14h ]add ecx , dword p t r [ ebx+14h ]jmp ecx ; x : unknown

A. Gazet & Y. Guillot Semi automatic binary deprotection 38/57

Page 42: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Virtual machine architecture

next_handler next_instr arg0

key_1

key_2

handler

next_handler next_instr arg0

key_1

key_2

handler

arg1

ciphered instruct ion

ciphered instruct ion

mov eax, [ebp+8]xor eax, h1_key_2mov [ebx+eax] , 0mov eax, [ebp]xor eax, h1_key_1mov ebp, [ebp+4]xor ebp, h1_key_2add eax, [ebx+14h]add ebp, [ebx+14h]jmp eax

mov eax, [ebp+8]xor eax, h2_key_2mov ecx, [ebp+12]xor ecx, h2_key_2mov ecx, [ebx+ecx]add [ebx+eax] , ecx

A. Gazet & Y. Guillot Semi automatic binary deprotection 39/57

Page 43: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Handler enumeration

Direct enumeration impossible

No “handler table”

Need to follow the virtual code flow step by step

Binary size implies numerous handlers

Auto-analyse every handler behavior using backtracking

[demo/4]

A. Gazet & Y. Guillot Semi automatic binary deprotection 40/57

Page 44: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Analyse result

Handler binding

hand l e r 13491h :// ” reg0 ” <− Exp r e s s i o n [ ” reg0 ” , :+ , ” reg1 ” ]// hand l e r type : add reg , r egmov eax , dword p t r [ ebp+0ch ]xo r eax , 8 d3f5d8bhmov eax , dword p t r [ ebx+eax ]mov ecx , dword p t r [ ebp+8]xo r ecx , 8 d3f5d8bhadd dword p t r [ ebx+ecx ] , eax

A. Gazet & Y. Guillot Semi automatic binary deprotection 41/57

Page 45: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Raw virtual code

The first virtual instructions

e n t r y p o i n t 2 1 9 f e h 2 1 e a6h :nopmov r68 , 28hadd r68 , h o s t e s pmov r64 , dword p t r [ r68 ]mov dword p t r [ esp ] , r64mov r64 , 4add esp , r64mov r68 , 2chadd r68 , h o s t e s pmov r64 , dword p t r [ r68 ]mov dword p t r [ esp ] , r64mov r64 , 4add esp , r64t r a p

A. Gazet & Y. Guillot Semi automatic binary deprotection 42/57

Page 46: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Virtual macro-instructions

Higher abstraction level

Through pattern recognition

Reconstruction of a higher level assembler

Apparition of functions (call, ret)

A. Gazet & Y. Guillot Semi automatic binary deprotection 43/57

Page 47: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Virtual macro-code

The first instructions (again)

e n t r y p o i n t 2 1 9 f e h 2 1 e a6h :mov dword p t r [ esp ] , dword p t r [ h o s t e s p+28h ]add esp , 4mov dword p t r [ esp ] , dword p t r [ h o s t e s p+2ch ]add esp , 4mov ebp , espadd esp , 234hmov r64 , dword p t r [ ebp+200h ]xo r r64 , 1j r z l o c 2d630h 2d8 f fh , r64s y s c a l l a l l o c p t r r64 , 0chmov dword p t r [ ebp+200h ] , r64

l o c 2 d6 3 0h 2d8 f f h :

A. Gazet & Y. Guillot Semi automatic binary deprotection 44/57

Page 48: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Decompilation

The instruction semantic is very simple

The code patterns reminds C

Check it out

A. Gazet & Y. Guillot Semi automatic binary deprotection 45/57

Page 49: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionObfuscationVirtual machineResolution

Validation

Enabled us to solve the challenge by a pure static approach

A. Gazet & Y. Guillot Semi automatic binary deprotection 46/57

Page 50: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Plan

1 Metasm

2 Structural manipulation

3 Challenge T2 2007

4 OptimizationIntroductionPOCResultsProspects

A. Gazet & Y. Guillot Semi automatic binary deprotection 47/57

Page 51: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Facts

What we have done so far

Stress was put on binary manipulation : coderewritting

Add effective methods in Metasm to cleanobfuscation on the fly

At various level: filtering processor, graphmanipulation . . .

Drawbacks

Lack of a higher level of abstraction

Analysis is still time-consuming and painfull

Mostly target-specific

A. Gazet & Y. Guillot Semi automatic binary deprotection 48/57

Page 52: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Concepts

Objectives

Generic rewritting rules

Obfuscated code as input

Clean (obfuscation free) code as output

Means

What we want to do looks like optimization !

Extensive litterature on the subject

Can be easily applied at assembly level using Metasm: wehave methods to work on basic blocks & instructions

A. Gazet & Y. Guillot Semi automatic binary deprotection 49/57

Page 53: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Proof of concept

Implemented optimizations

Declaration cleaning: remove useless assignments

Constant propagation

Constant, operation folding: apply basic rules of arithmetic

Peephole: replace knwon patterns with a reduced form

Each of those optimization amouts to one or many rewritting rules,possibly associated with a condition.

We apply them localy, on each basic bloc, spaghetti code has beenpreviously merged.s

A. Gazet & Y. Guillot Semi automatic binary deprotection 50/57

Page 54: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Example: constant propagation

Before

100 bb5cfh mov a l , 12h100 bed67h mov c l , 46h100 bed69h xo r c l , a l

After

100 bb5cfh mov a l , 12h100bed67h mov c l , 46h100bed69h xo r c l , 12h

The constant (0x12) has been propagated through al

A. Gazet & Y. Guillot Semi automatic binary deprotection 51/57

Page 55: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Example 2: constant folding

Before

100 bb5cfh mov a l , 12h100 bed67h mov c l , 46h100 bed69h xo r c l , 12h

After

100 bb5cfh mov a l , 12h100bed67h mov c l , 54h

cl is now assigned with 0x46 xor 0x12h

A. Gazet & Y. Guillot Semi automatic binary deprotection 52/57

Page 56: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Does it work?

A. Gazet & Y. Guillot Semi automatic binary deprotection 53/57

Page 57: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Does it work ?

A. Gazet & Y. Guillot Semi automatic binary deprotection 54/57

Page 58: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Does it work ? Actually yes

A. Gazet & Y. Guillot Semi automatic binary deprotection 55/57

Page 59: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

Conclusion and prospect

Conclusion

Using optimization to defeat obfuscation is very promising

The kind of obfuscation used in the protection is too weak:mainly based on local constants expansion and affinefunctions.

We don’t have a real intermediate representation

Prospect

We are working on decompilation problems

We will study the opportunity to use jointly Metasm andLLVM

A. Gazet & Y. Guillot Semi automatic binary deprotection 56/57

Page 60: Semi automatic binary deprotection - UniTrentore-trust.dit.unitn.it/.../Gazet_Guillot-Semiautomated_deobfuscation.pdf · A. Gazet & Y. Guillot Semi automatic binary deprotection 22/57.

MetasmStructural manipulation

Challenge T2 2007Optimization

IntroductionPOCResultsProspects

The end

Thank you for your attention !

Any questions ?

A. Gazet & Y. Guillot Semi automatic binary deprotection 57/57


Recommended