ios apps reverse engineering - hacking-lab livecd€¦ · › launch gdb › set a breakpoint ......

61
iOS applications reverse engineering Julien Bachmann – [email protected] 1

Upload: truongcong

Post on 02-Apr-2018

240 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

iOS applications reverse engineering

Julien Bachmann – [email protected]

1

Page 2: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

2

Agenda› Motivations› The architecture

› Mach-O

› Objective-C

› ARM

› AppStore binaries› Find'em

› Decrypt'em

› Reverse'em

› What to look for› Where to start

› Remote connections

› Data protection

› Conclusion

Page 3: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

3

Preamble● Security engineer @ SCRT● Areas of interest focused on reverse engineering, software vulnerabilities and OS internals

● Not an Apple fanboy but like all the cool kids... ;)

● Goals of this presentation is to give a state of the art, in 45minutes, of my knowledge about iOS applications reverse engineering

● Motivate people to do more research in user/kernel-land iOS reverse engineering

Page 4: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

4

Motivations

Page 5: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

5

A few numbers

› +160 millions iOS users› +400 000 applications available› +10 billion downloads

→ (modestly) large user base

Page 6: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

6

e-banking applications

Page 7: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

7

Applications review

› Apple defined a review process› 10% of the applications are classified as dangereous

› Cases of applications not « compliant » with their description

Page 8: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

8

Storm8 case

Page 9: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

9

Now, what if you want to...

› check an external app ?› verify that your application is secure ?› check what kind of information an attacker

can get from your application ?

Page 10: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

10

Best reason ever...

› Because it's fun to learn how to reverse new things !

Page 11: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

11

The architecture

Page 12: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

12

Mach-O

› File format for› Executables› Libraries› Core dumps

Page 13: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

13

Mach-O

› Contains three parts› Header› Load commands› Data

Page 14: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

14

Mach-O

› Header

Page 15: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

15

Mach-O

Page 16: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

16

Mach-O

› Load commands› Indicates memory layout› Locates symbols table› Main thread context› Shared libraries

Page 17: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

17

Mach-O

› Data› Segments containing sections› __PAGEZERO› __TEXT

› Executable code and r--

› __DATA› rw-

› __OBJC› ...

Page 18: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

18

Mach-O

› objdump ?› Forget about it› Introducing : otool !

Page 19: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

19

Mach-O

› Universal / FAT files› Supports multiples architectures› For OSX

› Universal

› PowerPC, x86 and x86_64

› For iOS› FAT

› armv6, armv7

Page 20: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

20

Objective-C

› Programming language› Superset of the C language› Object oriented

› Class method calls differ from C++

Page 21: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

21

Calling methods

› C++› ObjectPointer->Method(param1, param2)

› Obj-C› [ObjectPointer Method:param1 param2Name:param2]

Page 22: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

22

Looking more closely

› [ObjectPointer Method]› objc_msgSend(ObjectPointer, @selector(Method))

› Selector› C string› objc_msgSend(ObjectPointer, "Method")

Page 23: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

23

ARM

› RISC› load-store architecture› Fixed-length 32-bit instructions› 3-address instruction formats

Page 24: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

24

Registers

› User-level programs› 15 general-purpose 32-bit registers : r0 → r14› PC = r15› Current program status register (N, Z, C, V flags, etc.)

Page 25: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

25

Load-store architecture

› Instructions can be classified into 3 groups› Data transfer (load-store)› Data processing› Control flow

Page 26: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

26

Data transfer instructions

› Load from memory› LDR r0, [r1] → r0 = mem[r1]

› Store to memory› STR r0, [r1] → mem[r1] = r0

Page 27: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

27

Data processing instructions

› Simple› ADD r0, r1, r2 → r0 = r1 + r2

› Immediate operands› ADD r1, r1, #1 → r1 = r1 + 1

› Shifted register operands› ADD r3, r2, r1, LSL #3 → r3 = r2 + (r1 << 3)

Page 28: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

28

Control flow instructions

› Branch instructions› B LABEL› BAL LABEL

› Conditional branches› BXX LABEL

› BEQ, BNE, BPL, BMI, …

› Conditional execution› CMP r0, #5 → if (r0!= 5)› ADDNE r1, r1, r0 r1 = r1 + r0

Page 29: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

29

Control flow instructions

› Branch and link instructions› BL SUBROUTINE → r14 = @next instr + jmp SUBR› PUSH {r0-r5, LR}› …› POP {r0-r5, PC}

Page 30: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

30

Calling convention

› Arguments values› r0 → r3

› Local variables› r4 → r11

› Return value› r0

Page 31: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

31

Summing it up

› Objective-C› [ObjectPointer Method:42]

› C++› ObjectPointer->Method(42)

› Pseudo C› objc_msgSend(ObjectPointer, "Method", 42)

› ARM assembly›

Page 32: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

32

AppStore binaries

Page 33: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

33

First of all

› Forget about the simulator› Binaries compiled for x86 not ARM› Need to use a jailbroken iOS device› Tools to install

› SSH

› GDB

› ...

Page 34: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

34

Find'em

› Downloaded from the AppStore as .ipa› ZIP file› ~/Music/iTunes/iTunes Music/Mobile Applications/

› On iOS devices› /var/mobile/Applications/<UUID>/<AppName>.app/

Page 35: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

35

Content of <AppName>.app*

*after download from the device to workstation. Owner set to mobile:mobile on iOS

Page 36: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

36

FAT binaries

› Binary might contain multiple versions› Need to extract the one corresponding to our device

Page 37: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

37

Decrypt'em

› Encrypted using "FairPlay like" method› Each executable page is encrypted with AES and a MD5

checksum is computed

› How to know if a binary is encrypted ?› LC_ENCRYPTION_INFO

› cryptid → 1 if the binary is encrypted

› cryptoffset → offset of the encrypted data

› cryptsize → size of the encrypted data

Page 38: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

38

LC_ENCRYPTION_INFO

Page 39: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

39

Unpack the binary

› Use a script that automates the process› crackulous› Not leet enough;)

› "unpack your app in 5 steps and achieve peace"

› Launch GDB› Set a breakpoint› Run the application› Extract the unencrypted executable code› Patch the architecture specific binary

Page 40: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

40

Where do I set the breakpoint ?

› Execution steps› FAT binary is run› Architecture specific binary is mapped in memory› Executable code is decrypted› Branch to start symbol

› Get start's address

Page 41: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

41

GDB, set, run

Page 42: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

42

« Breakpoint reached capt'ain »

Page 43: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

43

Extract the executable code

› Useful information› start› cryptsize

Page 44: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

44

Patch the architecture specific binary

› Locate LC_ENCRYPTION_INFO› Mach-O header parser› Hexadecimal editor

› Replace cryptid› 1 → 0

› Replace encrypted code with unpacked one

Page 45: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

45

Locate LC_ENCRYPTION_INFO

› Mach-O header parser

› Search for the load command in the binary

Page 46: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

46

Locate LC_ENCRYPTION_INFO

Page 47: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

47

Modified LC_ENCRYPTION_INFO

Page 48: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

48

Replace encrypted code

Page 49: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

49

Reverse'em

› Retrieve classes declarations› class-dump

› Resolve objc_msgSend calls› Useless call graph› Need to patch the disassembly

Page 50: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

50

class-dump

Page 51: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

51

First look at the disassembly

Page 52: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

52

objc_msgSend

› As stated before› objc_msgSend(<ref to object>, @selector(method), …)› ARM calling convention

› arg1 → r0

› arg2 → r1

› Backtrace calls to objc_msgSend› By hand› Using Zynamics IDAPython scripts

Page 53: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

53

objc_helper.py

Page 54: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

54

What to look for

Page 55: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

55

Where to start

› Locate the main class› UIApplicationDelegate

› applicationDidFinishLaunching

› ApplicationDidFinishLaunchingWithOptions

› Views› UI*ViewController

› viewDidLoad

Page 56: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

56

applicationDidFinishLaunching

Page 57: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

57

Remote connections

› HTTP(S)› NSURL› ...

› Sockets› CFSocketCreate› ...

Page 58: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

58

Data protection

› Accessing the KeyChain using JB tools› Lost iPhone ? Lost Passwords ! *

› Protect KeyChain content› Using passcode› setAttributes ofItemAtPath → NSFileProtectionComplete› SecItemAdd → kSecAttrAccessibleWhenUnlocked

* http://www.sit.fraunhofer.de/forschungsbereiche/projekte/Lost_iPhone.jsp

Page 59: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

59

Data protection

Page 60: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

60

Conclusion

Page 61: iOS apps reverse engineering - Hacking-Lab LiveCD€¦ · › Launch GDB › Set a breakpoint ... iOS apps reverse engineering Author: Julien Bachmann Created Date: 4/12/2011 8:42:10

61

Conclusion

› This is a revolution !› This presentation was only an introduction

› Lot of work/ideas around iOS› Grab your debugger and disassembler and work on it› I'm open to discuss it around a few beers