Slide 1

Slide 1 text

Reversing WTF Vladimir “Farcaller” Pouzanov

Slide 2

Slide 2 text

00002160          b580   push   {r7,  lr} 00002162          466f   mov   r7,  sp 00002164          b099   sub   sp,  #100 00002166          9017   str   r0,  [sp,  #92] 00002168          9116   str   r1,  [sp,  #88] 0000216a          4610   mov   r0,  r2 0000216c          930e   str   r3,  [sp,  #56] 0000216e   f000ef42   blx   0x2ff4   @  symbol  stub  for:  _objc_retain 00002172          9015   str   r0,  [sp,  #84] 00002174          980e   ldr   r0,  [sp,  #56] 00002176   f000ef3e   blx   0x2ff4   @  symbol  stub  for:  _objc_retain 0000217a          9014   str   r0,  [sp,  #80] 0000217c          9817   ldr   r0,  [sp,  #92] 0000217e          4967   ldr   r1,  [pc,  #412]   (0x231c) 00002180          4479   add   r1,  pc 00002182          6809   ldr   r1,  [r1,  #0] 00002184          4a64   ldr   r2,  [pc,  #400]   (0x2318) 00002186          447a   add   r2,  pc 00002188          6812   ldr   r2,  [r2,  #0] 0000218a          900d   str   r0,  [sp,  #52] 0000218c          4608   mov   r0,  r1 0000218e          4611   mov   r1,  r2 00002190   f000ef28   blx   0x2fe4   @  symbol  stub  for:  _objc_msgSend 00002194          495f   ldr   r1,  [pc,  #380]   (0x2314) 00002196          4479   add   r1,  pc 00002198          6809   ldr   r1,  [r1,  #0] 0000219a          4a5d   ldr   r2,  [pc,  #372]   (0x2310) 0000219c          447a   add   r2,  pc 0000219e          6812   ldr   r2,  [r2,  #0] 000021a0          900c   str   r0,  [sp,  #48] 000021a2          4608   mov   r0,  r1 000021a4          4611   mov   r1,  r2 000021a6   f000ef1e   blx   0x2fe4   @  symbol  stub  for:  _objc_msgSend 000021aa          463f   mov   r7,  r7 000021ac   f000ef24   blx   0x2ff8   @  symbol  stub  for:  _objc_retainAutoreleasedReturnValue 000021b0          2100   movs   r1,  #0 ТОЛЬКО АССЕМБЛЕР! ТОЛЬКО ХАРДКОР!

Slide 3

Slide 3 text

Зачем мне это?

Slide 4

Slide 4 text

Fun

Slide 5

Slide 5 text

Profit

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

We have to go deeper!

Slide 11

Slide 11 text

Mach-O

Slide 12

Slide 12 text

Load Command

Slide 13

Slide 13 text

Сегменты

Slide 14

Slide 14 text

__TEXT

Slide 15

Slide 15 text

__DATA

Slide 16

Slide 16 text

__TEXT.__text

Slide 17

Slide 17 text

00002388  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  application:didFinishLaunchingWithOptions:] 00002588  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationDidBecomeActive:] 00002570  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationDidEnterBackground:] 0000257c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillEnterForeground:] 00002564  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillResignActive:] 00002594  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillTerminate:] 0000231c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  dealloc] 00002600  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  navigationController] 0000261c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  setNavigationController:] 000025bc  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  setWindow:] 000025a0  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  window] 00002e60  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  configureView] 00002d80  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  dealloc] 00003040  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  detailDescriptionLabel] 00003024  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  detailItem] 00002f78  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  initWithNibName:bundle:] 0000305c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  setDetailDescriptionLabel:] 00002dec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  setDetailItem:] 00002f58  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController   shouldAutorotateToInterfaceOrientation:] 00002ed8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  viewDidLoad] 00002f18  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  viewDidUnload] 00002730  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  dealloc] 00002d20  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  detailViewController] 00002660  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  initWithNibName:bundle:] 000028c8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  insertNewObject:] 000029d8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  numberOfSectionsInTableView:] 00002d3c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  setDetailViewController:] 000028a8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController   shouldAutorotateToInterfaceOrientation:] 00002b30  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:canEditRowAtIndexPath:] 00002a1c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:cellForRowAtIndexPath:] 00002b4c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController   tableView:commitEditingStyle:forRowAtIndexPath:] 00002bec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:didSelectRowAtIndexPath:] 000029ec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:numberOfRowsInSection:] 0000279c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  viewDidLoad] 00002878  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  viewDidUnload]

Slide 18

Slide 18 text

00002388  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  application:didFinishLaunchingWithOptions:] 00002588  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationDidBecomeActive:] 00002570  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationDidEnterBackground:] 0000257c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillEnterForeground:] 00002564  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillResignActive:] 00002594  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  applicationWillTerminate:] 0000231c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  dealloc] 00002600  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  navigationController] 0000261c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  setNavigationController:] 000025bc  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  setWindow:] 000025a0  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[AppDelegate  window] 00002e60  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  configureView] 00002d80  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  dealloc] 00003040  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  detailDescriptionLabel] 00003024  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  detailItem] 00002f78  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  initWithNibName:bundle:] 0000305c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  setDetailDescriptionLabel:] 00002dec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  setDetailItem:] 00002f58  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController   shouldAutorotateToInterfaceOrientation:] 00002ed8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  viewDidLoad] 00002f18  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[DetailViewController  viewDidUnload] 00002730  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  dealloc] 00002d20  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  detailViewController] 00002660  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  initWithNibName:bundle:] 000028c8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  insertNewObject:] 000029d8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  numberOfSectionsInTableView:] 00002d3c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  setDetailViewController:] 000028a8  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController   shouldAutorotateToInterfaceOrientation:] 00002b30  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:canEditRowAtIndexPath:] 00002a1c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:cellForRowAtIndexPath:] 00002b4c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController   tableView:commitEditingStyle:forRowAtIndexPath:] 00002bec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:didSelectRowAtIndexPath:] 000029ec  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  tableView:numberOfRowsInSection:] 0000279c  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  viewDidLoad] 00002878  (__TEXT,__text)  non-­‐external  [Thumb]  -­‐[MasterViewController  viewDidUnload] 00002ed8 (__TEXT,__text) non-­‐external [Thumb] -­‐[DetailViewController  viewDidLoad]

Slide 19

Slide 19 text

ARM

Slide 20

Slide 20 text

ARMARM

Slide 21

Slide 21 text

15 регистров

Slide 22

Slide 22 text

13 – общего назначения

Slide 23

Slide 23 text

32 бита

Slide 24

Slide 24 text

SP, LR, PC

Slide 25

Slide 25 text

Адресное пространство

Slide 26

Slide 26 text

232 4 294 967 296

Slide 27

Slide 27 text

01234567 Big-endian

Slide 28

Slide 28 text

76543210 Little-endian

Slide 29

Slide 29 text

4 набора инструкций

Slide 30

Slide 30 text

ARM 32 бита

Slide 31

Slide 31 text

Thumb 16-32 бита

Slide 32

Slide 32 text

ThumbEE Jazelle

Slide 33

Slide 33 text

UAL

Slide 34

Slide 34 text

UAL 426 инструкций

Slide 35

Slide 35 text

Поехали!

Slide 36

Slide 36 text

00002388          b590   push   {r4,  r7,  lr} 0000238a          af01   add   r7,  sp,  #4 0000238c          b097   sub   sp,  #92 0000238e   f2400900   movw   r9,  #0   @  0x0 00002392   f2c00900   movt   r9,  #0   @  0x0

Slide 37

Slide 37 text

00002396          9016   str   r0,  [sp,  #88] 00002398          9115   str   r1,  [sp,  #84] 0000239a          9214   str   r2,  [sp,  #80] 0000239c          9313   str   r3,  [sp,  #76] r0  =    (id)self r1  =  (SEL)_cmd r2  =  ... r3  =  ...

Slide 38

Slide 38 text

00002396          9016   str   r0,  [sp,  #88] 00002398          9115   str   r1,  [sp,  #84] 0000239a          9214   str   r2,  [sp,  #80] 0000239c          9313   str   r3,  [sp,  #76] 0000239e          9816   ldr   r0,  [sp,  #88] r0  =    (id)self r1  =  (SEL)_cmd r2  =  ... r3  =  ...

Slide 39

Slide 39 text

000023a0          495f   ldr   r1,  [pc,  #380]  (0x2520) 000023a2          4479   add   r1,  pc 000023a4          6809   ldr   r1,  [r1,  #0] _OBJC_CLASS_$_UIWindow

Slide 40

Slide 40 text

000023a0          495f   ldr   r1,  [pc,  #380]  (0x2520) 000023a2          4479   add   r1,  pc 000023a4          6809   ldr   r1,  [r1,  #0] _OBJC_CLASS_$_UIWindow 000023a6          4a5d   ldr   r2,  [pc,  #372]  (0x251c) 000023a8          447a   add   r2,  pc 000023aa          6812   ldr   r2,  [r2,  #0] “alloc”

Slide 41

Slide 41 text

000023ac          900d   str   r0,  [sp,  #52] 000023ae          4608   mov   r0,  r1 000023b0          4611   mov   r1,  r2 000023b2   f8cd9030   str.w   r9,  [sp,  #48] 000023b6   f001ee1c   blx   0x3ff0 52: self

Slide 42

Slide 42 text

000023ac          900d   str   r0,  [sp,  #52] 000023ae          4608   mov   r0,  r1 000023b0          4611   mov   r1,  r2 000023b2   f8cd9030   str.w   r9,  [sp,  #48] 000023b6   f001ee1c   blx   0x3ff0 0000238e   f2400900   movw   r9,  #0   @  0x0 52: self 48: 0

Slide 43

Slide 43 text

000023ac          900d   str   r0,  [sp,  #52] 000023ae          4608   mov   r0,  r1 000023b0          4611   mov   r1,  r2 000023b2   f8cd9030   str.w   r9,  [sp,  #48] 000023b6   f001ee1c   blx   0x3ff0 [UIWindow alloc]

Slide 44

Slide 44 text

000023ba          4957   ldr   r1,  [pc,  #348]  (0x2518) 000023bc          4479   add   r1,  pc 000023be          6809   ldr   r1,  [r1,  #0] _OBJC_CLASS_$_UIScreen 000023c0          4a54   ldr   r2,  [pc,  #336]  (0x2514) 000023c2          447a   add   r2,  pc 000023c4          6812   ldr   r2,  [r2,  #0] “mainScreen”

Slide 45

Slide 45 text

000023c6          900b   str   r0,  [sp,  #44] 000023c8          4608   mov   r0,  r1 000023ca          4611   mov   r1,  r2 000023cc   f001ee10   blx   0x3ff0 52: self 48: 0 44: window [UIScreen mainScreen]

Slide 46

Slide 46 text

000023d0          494f   ldr   r1,  [pc,  #316]  (0x2510) 000023d2          4479   add   r1,  pc 000023d4          680a   ldr   r2,  [r1,  #0] “bounds”

Slide 47

Slide 47 text

000023d6          990c   ldr   r1,  [sp,  #48] 000023d8          4288   cmp   r0,  r1 000023da          900a   str   r0,  [sp,  #40] 000023dc          9209   str   r2,  [sp,  #36] 000023de          d005   beq.n   0x23ec 48: 0 40: screen 36: “bounds” [UIScreen mainScreen] == 0?

Slide 48

Slide 48 text

000023e0          a80f   add   r0,  sp,  #60 000023e2          990a   ldr   r1,  [sp,  #40] 000023e4          9a09   ldr   r2,  [sp,  #36] 000023e6   f001ee08   blx   0x3ff8 000023ea          e005   b.n   0x23f8 60: frameRect 40: screen 36: “bounds” [screen frame] (objc_msgSend_stret) [UIScreen mainScreen] != 0

Slide 49

Slide 49 text

000023ec          a80f   add   r0,  sp,  #60 000023ee          2100   movs   r1,  #0 000023f0          60c1   str   r1,  [r0,  #12] 000023f2          6081   str   r1,  [r0,  #8] 000023f4          6041   str   r1,  [r0,  #4] 000023f6          6001   str   r1,  [r0,  #0] 60: frameRect 40: screen 36: “bounds” CGRect {0, 0, 0, 0} [UIScreen mainScreen] == 0

Slide 50

Slide 50 text

000023f8          2001   movs   r0,  #1 000023fa   f2c00000   movt   r0,  #0   @  0x0 000023fe   f24221de   movw   r1,  #8926   @  0x22de 00002402   f2c00100   movt   r1,  #0   @  0x0 00002406          4479   add   r1,  pc 00002408          6809   ldr   r1,  [r1,  #0] “initWithFrame:”

Slide 51

Slide 51 text

0000240a          9a0f   ldr   r2,  [sp,  #60] 0000240c          9b10   ldr   r3,  [sp,  #64] 0000240e   f8dd9044   ldr.w   r9,  [sp,  #68] 00002412   f8ddc048   ldr.w   r12,  [sp,  #72] 60: frameRect.origin.x 64: frameRect.origin.y 68: frameRect.size.width 72: frameRect.size.height

Slide 52

Slide 52 text

00002416   f8dde02c   ldr.w   lr,  [sp,  #44] 0000241a          466c   mov   r4,  sp 0000241c   f8c49000   str.w   r9,  [r4] 00002420   f8c4c004   str.w   ip,  [r4,  #4] 00002424          9008   str   r0,  [sp,  #32] 00002426          4670   mov   r0,  lr 44: window

Slide 53

Slide 53 text

00002416   f8dde02c   ldr.w   lr,  [sp,  #44] 0000241a          466c   mov   r4,  sp 0000241c   f8c49000   str.w   r9,  [r4] 00002420   f8c4c004   str.w   ip,  [r4,  #4] 00002424          9008   str   r0,  [sp,  #32] 00002426          4670   mov   r0,  lr 44: window 32: 1 000023f8          2001   movs   r0,  #1

Slide 54

Slide 54 text

00002416   f8dde02c   ldr.w   lr,  [sp,  #44] 0000241a          466c   mov   r4,  sp 0000241c   f8c49000   str.w   r9,  [r4] 00002420   f8c4c004   str.w   r12,  [r4,  #4] 00002424          9008   str   r0,  [sp,  #32] 00002426          4670   mov   r0,  lr 44: window 00002428   f001ede2   blx   0x3ff0 [window initWithFrame:{.., .., .., ..}]

Slide 55

Slide 55 text

r0 – r3

Slide 56

Slide 56 text

r0 – r4 r0: self r1: _cmd r2: arg1 r3: arg2 r4: * *1: arg3 *2: arg4 ... Стек

Slide 57

Slide 57 text

0000242c          494c   ldr   r1,  [pc,  #304]  (0x2560) 0000242e          4479   add   r1,  pc 00002430          6809   ldr   r1,  [r1,  #0] 00002432   f001edde   blx   0x3ff0 [window autorelease]

Slide 58

Slide 58 text

00002436          4949   ldr   r1,  [pc,  #292]  (0x255c) 00002438          4479   add   r1,  pc 0000243a          6809   ldr   r1,  [r1,  #0] 0000243c          9a0d   ldr   r2,  [sp,  #52] 0000243e          9007   str   r0,  [sp,  #28] 00002440          4610   mov   r0,  r2 00002442          9a07   ldr   r2,  [sp,  #28] 00002444   f001edd4   blx   0x3ff0 [self setWindow:window] 52: self 28: window2

Slide 59

Slide 59 text

00002448          4843   ldr   r0,  [pc,  #268]  (0x2558) 0000244a          4478   add   r0,  pc 0000244c          6800   ldr   r0,  [r0,  #0] 0000244e          4941   ldr   r1,  [pc,  #260]  (0x2554) 00002450          4479   add   r1,  pc 00002452          6809   ldr   r1,  [r1,  #0] 00002454   f001edcc   blx   0x3ff0 [MasterViewController alloc]

Slide 60

Slide 60 text

00002458          493d   ldr   r1,  [pc,  #244]  (0x2550) 0000245a          4479   add   r1,  pc 0000245c          6809   ldr   r1,  [r1,  #0] 0000245e          2300   movs   r3,  #0 00002460   f242420c   movw   r2,  #9228   @  0x240c 00002464   f2c00200   movt   r2,  #0   @  0x0 00002468          447a   add   r2,  pc 0000246a   f001edc2   blx   0x3ff0 [controller initWithNibName:@”MasterViewController” bundle:0]

Slide 61

Slide 61 text

00002458          493d   ldr   r1,  [pc,  #244]  (0x2550) 0000245a          4479   add   r1,  pc 0000245c          6809   ldr   r1,  [r1,  #0] 0000245e          2300   movs   r3,  #0 00002460   f242420c   movw   r2,  #9228   @  0x240c 00002464   f2c00200   movt   r2,  #0   @  0x0 00002468          447a   add   r2,  pc 0000246a   f001edc2   blx   0x3ff0 ___CFConstantStringClassReference: @"MasterViewController"

Slide 62

Slide 62 text

...

Slide 63

Slide 63 text

00002506          9808   ldr   r0,  [sp,  #32] 00002508          b240   sxtb   r0,  r0 0000250a          b017   add   sp,  #92 0000250c          bd90   pop   {r4,  r7,  pc} 32: 1

Slide 64

Slide 64 text

0000250e          bf00   nop 00002510          230e   movs   r3,  #14 00002512          0000   lsls   r0,  r0,  #0 00002514          231a   movs   r3,  #26 00002516          0000   lsls   r0,  r0,  #0 00002518          23f0   movs   r3,  #240 0000251a          0000   lsls   r0,  r0,  #0 0000251c          2330   movs   r3,  #48 0000251e          0000   lsls   r0,  r0,  #0 ........

Slide 65

Slide 65 text

-­‐  (BOOL)application:(UIApplication  *)application                didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions {        self.window  =  [[[UIWindow  alloc]  initWithFrame:                                      [[UIScreen  mainScreen]  bounds]]  autorelease];   MasterViewController  *masterViewController  =  [[[MasterViewController  alloc]   initWithNibName:@"MasterViewController"  bundle:nil]  autorelease];   self.navigationController  =  [[[UINavigationController  alloc]   initWithRootViewController:masterViewController]  autorelease];   self.window.rootViewController  =  self.navigationController;        [self.window  makeKeyAndVisible];        return  YES; }

Slide 66

Slide 66 text

Главное – замечать общие шаблоны. Это легко* * не сложнее, чем матан

Slide 67

Slide 67 text

Казалось бы, вот и всё Но это только начало