47
Obfuscator reloaded Pascal Junod HEIG-VD Application Security Forum - 2012 Western Switzerland 7-8 novembre 2012 - Y-Parc / Yverdon-les-Bains https://www.appsec-forum.ch Julien Rinaldini – HEIG-VD Marc Romanens - EIA-FR Jean-Roland Schuler – EIA-FR

ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Embed Size (px)

DESCRIPTION

Le projet Obfuscator, mené conjointement par des équipes de l’HEIG-VD et de l’EIA-FR, vise à explorer des techniques de durcissement de logiciel, sous forme de code source ou d’exécutable, contre le reverse-engineering. Le but de cette présentation est de décrire les résultats obtenus. Cet exposé sera structurée en plusieurs parties: - Rapide présentation de la problématique de la sécurité du logiciel par rapport au reverse-engineering; - Description de quelques techniques d’obfuscation implémentées sous forme de “passes” pour le compilateur LLVM (substitution de code, injection de branchements factices, applatissement de code, …), et présentation des résultats obtenus ainsi que des difficultés rencontrées. - Présentation de quelques techniques d’obfuscation de binaires pour processeurs ARM. Méthodes d’insertions de code dans un fichier binaire au format ELF. Présentation de méthodes d’obfuscation sur processeur ARM avec leurs particularités d’implémentation.

Citation preview

Page 1: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Obfuscator reloaded

Pascal Junod – HEIG-VD

Application Security Forum - 2012Western Switzerland

7-8 novembre 2012 - Y-Parc / Yverdon-les-Bains

https://www.appsec-forum.ch

Pascal Junod – HEIG-VD

Julien Rinaldini – HEIG-VD

Marc Romanens - EIA-FR

Jean-Roland Schuler – EIA-FR

Page 2: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Agenda

� Context of the « Obfuscator » project

� LLVM-based obfuscation

� Playing with ARM binaries

2

Page 3: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …cÄxtáx áxx à{|á tá âÇÑÜÉàxvàxw áÉyàãtÜx4

3

Page 4: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

4

Page 5: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …cÄxtáx áxx à{|á tá Éuyâávtàxw áÉyàãtÜx4

5

Page 6: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

äáA

6

äáA

Page 7: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

`tÄ|v|Éâá exäxÜáx@XÇz|ÇxxÜ|Çz

7

Page 8: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

W|z|àtÄ e|z{àá `tÇtzxÅxÇà

8

Page 9: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

VÄÉâw VÉÅÑâà|Çz

9

Page 10: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

• A typical attack can be decomposed in three

phases:

– Program analysis

� Extraction of an algorithm, secret, design

10

� Extraction of an algorithm, secret, design

– Code modification

� Removal of license check mechanisms

– Distribution

� Violation of intellectual property

Page 11: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

• Defense possibilities (aka « software protection »)

– Confusion

• Obfuscation

– Tamper-resistance

11

– Tamper-resistance

• SW/HW-based protections, node-locking, …

– Watermark

• SW watermarking, fingerprinting, …

Page 12: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

• According to Wikipedia, «obfuscated code is

source of machine code that has been made

difficult to understand »

• «difficult» …

12

• «difficult» …

= costly

= time-consuming

� not necessarily

impossible

Page 13: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …return (int) ((((x - 2) * (x - 3) * (x - 4) * (x - 5) * (x - 6) *

(x - 7) * (x - 8) * (x - 9) * (x - 10) * (x - 11) *(x - 12) * 31) /

((x - 2) * (x - 3) * (x - 4) * (x - 5) * (x - 6) *(x - 7) * (x - 8) * (x - 9) * (x - 10) * (x - 11) *(x - 12) + .00001)) +

(((x - 3) * (x - 4) * (x - 5) * (x - 6) * (x - 7) *(x - 8) * (x - 9) * (x - 10) * (x - 11) * (x - 12) *(28 + z)) /

((x - 3) * (x - 4) * (x - 5) * (x - 6) * (x - 7) *(x - 8) * (x - 9) * (x - 10) * (x - 11) * (x - 12) +.00001)) +

(((x - 4) * (x - 5) * (x - 6) * (x - 7) * (x - 8) *(x - 9) * (x - 10) * (x - 11) * (x - 12) * 31) /

((x - 4) * (x - 5) * (x - 6) * (x - 7) * (x - 8) *(x - 9) * (x - 10) * (x - 11) * (x - 12) + .00001)) +

(((x - 5) * (x - 6) * (x - 7) * (x - 8) * (x - 9) *(x - 10) * (x - 11) * (x - 12) * 30) /

13

(x - 10) * (x - 11) * (x - 12) * 30) /((x - 5) * (x - 6) * (x - 7) * (x - 8) * (x - 9) *

(x - 10) * (x - 11) * (x - 12) + .00001)) +(((x - 6) * (x - 7) * (x - 8) * (x - 9) * (x - 10) *

(x - 11) * (x - 12) * 31) /((x - 6) * (x - 7) * (x - 8) * (x - 9) * (x - 10) *

(x - 11) * (x - 12) + .00001)) +

(((x - 7) * (x - 8) * (x - 9) * (x - 10) * (x - 11) *(x - 12) * 30) /

((x - 7) * (x - 8) * (x - 9) * (x - 10) * (x - 11) *(x - 12) + .00001)) +

(((x - 8) * (x - 9) * (x - 10) * (x - 11) * (x - 12) *31) /

((x - 8) * (x - 9) * (x - 10) * (x - 11) * (x - 12) +.00001)) +

(((x - 9) * (x - 10) * (x - 11) * (x - 12) * 31) /((x - 9) * (x - 10) * (x - 11) * (x - 12) + .00001)) +

(((x - 10) * (x - 11) * (x - 12) * 30) /((x - 10) * (x - 11) * (x - 12) + .00001)) +

(((x - 11) * (x - 12) * 31) /((x - 11) * (x - 12) + .00001)) +

(((x - 12) * 30) / ((x - 12) + .00001)) + 31 + .1) -y;

Page 14: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtonatsuJ";sub p{@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$ f^ord

14

[$ f^ord($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&close$_}%p;waituntil$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

Page 15: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

void primes(int cap) {int i, j, composite;for(i = 2; i < cap; ++i) {

composite = 0;for(j = 2; j * j <= i; ++j)

composite += !(i % j);if(!composite)

printf("%d\t", i);}

}

_(__,___,____,_____){___/__<=_____?_(__,___+_____,____,_____):!(___%__)?_(__,___+_____,___%__,__

15

int main(void) { primes(100);

}

___%__)?_(__,___+_____,___%__,_____):___%__==___/__&&!____?(printf("%d\t",___/__),_(__,___+_____,____,_____)):(___%__>_____&&___%__<___/__)?_(__,___+_____,____,_____+!(___/__%(___%__))):___<__*__?_(__,___+_____,____,_____):0;}main(void){_(100,0,0,1);}

Page 16: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

16

Page 17: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

� Different capabilities:

– Code source vs. binary

– Supported languages

• .NET, C#, Java, Javascript, C/C++, …

17

• .NET, C#, Java, Javascript, C/C++, …

– Costs

• Size, speed

– Security towards RE

Page 18: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

In a Nutshell …

� Usual techniques

– Packing

– Addition of junk code

– Code transformations

18

– Code transformations

– Opaque predicates

– White-box cryptography

– Anti-debugging tricks

– Virtualization

– …

Page 19: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Context of « Obfuscator »

� Project funded by HES-SO (about CHF 160K, time

span 2010-2012)

– HEIG-VD

• Pascal Junod

19

• Pascal Junod

• Gregory Ruch

• Julien Rinaldini

– EIA-FR

• Jean-Roland Schuler

• Marc Romanens

• Adrien Giner

Page 20: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Context of «Obfuscator»

� Goal:

– create knowledge and know-how in the domain of

software obfuscation

– Develop prototype tools

20

– Develop prototype tools

• HEIG-VD: focusing on source code obfuscation

• EIA-FR: focusing on binary obfuscation

Page 21: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

� No satisfactory open-source tool able to

obfuscate C/C++

� Cool project to play with!

� RE is hard, protecting against RE in an efficient

21

� RE is hard, protecting against RE in an efficient

way is even harder!

Page 22: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

� Several «false starts» (but that’s research ;-)

22

� Sébastien Bischof : parsing C with Python

� Grégory Ruch: hacking the LLVM Clang API

Page 23: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

23

� LLVM

– Compiler infrastructure

– Project initiated by the University of Illinois in 2000

– Since 2005, pushed by Apple Inc. – Since 2005, pushed by Apple Inc.

– Front-ends:

• C/C++, Objective C, Fortran, Ada, Haskell, Python, Ruby, …

– Back-ends:

• x86, x86-64, PowerPC, PowerPC-64, ARM, Thumb, Sparc,

Alpha, CellSPU, MIPS, MSP430, SystemZ, XCore.

Page 24: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

24

Page 25: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

25

� Available obfuscation passes

– Code substitution

• A ^ B = (A & ~B) | (~A & B)

• A + B = A - ( - B)• A + B = A - ( - B)

• …

Page 26: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

26

� Available obfuscation passes

– Insertion of fake branches with opaque predicates

(bachelor thesis of Julie Michielin)

Page 27: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

27

� Available obfuscation passes

– Code flattening

Page 28: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

28

� Code flattening: case of IF-THEN-ELSE

Page 29: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

29

� Code flattening: case of a FOR loop

Page 30: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

30

� Code flattening: case of a SWITCH

Page 31: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

31

� Another example:

Page 32: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

� Test procedure

– Run test suite of the cryptographic library

libtomcrypt

– Run test suite of the graphical library ImageMagick

32

– Run test suite of the graphical library ImageMagick

• Found a routine of 6000+ C lines in it !!

– Run test suite of the MySQL database

• A single test is still failing �

� Todo

– Flatten try -catch constructs as well

– Test, debug, test, debug, test !

Page 33: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

LLVM-based Obfuscation

33

by vÉâÜáx? ãx âáxw

Page 34: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Part III Arm obfuscation

� About me

– Marc Romanens

– Scientific collaborator at EIA-FR

– Works on a “Exploration Project” on binary

34

– Works on a “Exploration Project” on binary

obfuscation for processor ARM.

Page 35: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Agenda

File format ELF

Code insertion into ELF file

ARM obfuscation

Opaque predicate

35

Opaque predicate

Junk code

Page 36: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

File format ELF

Summary I

� 3 headers tables

– Header file

• Entry point

• Position of others headers

36

• Position of others headers

• Architecture

– Program header (define the segments)

• Flags (RWE)

• Offset / virtual addresses

– Section header (define the sections (optional))

Page 37: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

File Format Elf

Summary II

37

Page 38: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Memory allocation of segment

38

Image source: GNU Linux Magazine HS 32 Septembre Octobre 2007

Page 39: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Insertion of code

� Why ?

– Obfuscation needs space

� How ?

– Add a new segment

39

– Add a new segment

– Increase size of old segment

Page 40: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Obfuscation technique :

Opaque predicate

� Example :

– (X2 + X) % 2 == 0 is always true

– (3X3 + 2 X2 + X) % 6 == 0 is always true

40

– (3X3 + 2 X2 + X) % 6 == 0 is always true

� Opaque predicate aims at:

– Creating false branch

– Improving complexity of serial number

– Watermarking

Page 41: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Obfuscation technique :

Junk code

� Junk code aims at:

– Hiding the useful part of code

� Implementation :

41

� Implementation :

– Use condition code in ARM

– Use opaque predicate for enforcing the stealth

Page 42: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

42

Page 43: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

43

Page 44: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

44

Page 45: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Junk code

Optimisation

� Mix junk code and real code

� Change condition code with data processing

instructions

� Add more predicates

45

� Add more predicates

Page 46: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Questions?

46

Page 47: ASFWS 2012 - Obfuscator, ou comment durcir un code source ou un binaire contre le reverse-engineering par Pascal Junod / Jean-Roland Schuler

Merci/Thank you!

Contact:

[email protected]

@cryptopathe

47

@cryptopathe

http://crypto.junod.info

[email protected]

Slides:

http://slideshare.net/ASF-WS/presentations