Rev101 spritzers - CTF team spritz.math.unipd.it/spritzers.html - - PowerPoint PPT Presentation

rev101
SMART_READER_LITE
LIVE PREVIEW

Rev101 spritzers - CTF team spritz.math.unipd.it/spritzers.html - - PowerPoint PPT Presentation

Rev101 spritzers - CTF team spritz.math.unipd.it/spritzers.html Disclaimer All information presented here has the only purpose of teaching how reverse engineering works. Use your mad skillz only in CTFs or other situations in which you are


slide-1
SLIDE 1

Rev101

spritzers - CTF team spritz.math.unipd.it/spritzers.html

slide-2
SLIDE 2

All information presented here has the only purpose of teaching how reverse engineering works. Use your mad skillz only in CTFs or other situations in which you are legally allowed to do so. Do not hack the new Playstation. Or maybe do, but be prepared to get legal troubles (I’m looking at you, geohot).

Disclaimer

slide-3
SLIDE 3

But seriously, if you do pls tell me. It’d be awesome.

Disclaimer

slide-4
SLIDE 4

Reversing in CTFs

In reversing challenges you have to understand how a program works, but you don’t have its source code. You typically have to reverse an algorithm (encryption?) to get the flag. Most of the time, solving a challenge is a bit time consuming but straightforward. ...Unless obfuscation is involved.

slide-5
SLIDE 5

Reversing IRL

A lot of cool stuff, but legally it’s a gray area.

slide-6
SLIDE 6

Reverse Engineering?

slide-7
SLIDE 7

What it is

Final Product Design Information

Not limited to software

slide-8
SLIDE 8

(Binary) Software Reverse Engineering

slide-9
SLIDE 9

Compiling Software

int main() { puts(“YAY”); return 0; }

COMPILER

000100100100 ...

Source code Binary

slide-10
SLIDE 10

Reversing Software

int main() { puts(“YAY”); return 0; } 000100100100 ...

Source code Binary

slide-11
SLIDE 11

Reversing Software - The Truth

int main() { puts(“YAY”); return 0; }

00010010 ... mov eax, 3 call func ret

slide-12
SLIDE 12

Why is it relevant?

  • You don’t always have access to source code
  • Vulnerability assessment
  • Malware analysis
  • Pwning
  • Algorithm reversing (default WPA anyone?)
  • Interoperability (SMB/Samba, Windows/Wine)
  • Hacking embedded devices
slide-13
SLIDE 13

Can’t I just use a decompiler?

  • Can speed up the reversing, but...
  • Decompiling is (generally) undecidable
  • Fails in many cases
  • Sometimes you want to work at the ASM

level (pwning)

slide-14
SLIDE 14

Why should I do it?

  • Sometimes it’s fun

This is straight from the Wii’s game signature checking. (Credits: https://hackmii.com/)

slide-15
SLIDE 15

The Tools

slide-16
SLIDE 16

Disassembler

00010010 ...

Disassembler

mov eax, 3 call func ret Binary ASM

slide-17
SLIDE 17

Disassembler

  • IDA Pro (https://www.hex-rays.com/products/ida/)

○ GUI ○ Industry standard ○ $$$$$

  • Binary Ninja (https://binary.ninja/)

○ GUI ○ Very nice scripting features + has “undo” functionality ○ $$

  • Radare2 (https://github.com/radare/radare2)

○ CLI (experimental GUI @ https://github.com/radareorg/cutter/releases) ○ Opensource

  • Objdump

○ Seriously, don’t

slide-18
SLIDE 18

Hex Editor

slide-19
SLIDE 19

Hex Editor

  • Patch programs
  • Inspect file formats
  • Change content of files

Many different options here (hexedit, biew, etc…)

slide-20
SLIDE 20

Introduction to x86 ASM (yay)

slide-21
SLIDE 21
slide-22
SLIDE 22

Quick recap: a process’ memory

Stack Heap Main executable Libraries

0x0000...

.text .rodata .got .data .bss .plt

Code Zero-init’ed data Read/write data Read-only data Imports stuff Imports stuff Credits: abiondo

slide-23
SLIDE 23

Introduction to x86 ASM

  • Only architecture supported by IDA/Binja demo :(
  • Your computer probably runs on x86_64

○ x86 still supported ○ 32 bit vs 64 bit

  • This is NOT supposed to be a complete ASM lesson

(booooring)

slide-24
SLIDE 24

RAX

(some)

x86_64 Registers

EAX AH AL AX RBX EBX BH BL BX RCX ECX CH CL CX RDX EDX DH DL DX RSI ESI RSP ESP RBP EBP

General Purpose Stack Pointer Base Pointer

RIP EIP

Instruction Ptr

64 bit 32 bit 16 bit

slide-25
SLIDE 25

Instructions - MOV <dst>, <src>

  • Copy <src> into <dst>
  • MOV EAX, 16

○ EAX = 16

  • MOV EAX, [ESP+4]

○ EAX = *(ESP+4)

  • MOV AL, ‘a’

○ AL = 0x61

slide-26
SLIDE 26

Instructions - LEA <dst>, <src>

  • Load Effective Address of <src> into <dst>
  • Used to access elements from a buffer/array
  • Used to perform simple math operations
  • LEA ECX, [EAX+3]

○ ECX = EAX + 3

  • LEA EAX, [EBX+2*ESI]

○ EAX = EBX+2*ESI

slide-27
SLIDE 27

Instructions - PUSH <src>

  • Decrement ESP and put <src> onto the stack (push)
  • PUSH EAX

○ ESP -= 4 ○ *ESP = (dword) EAX

  • PUSH CX

○ ESP -= 2 ○ *ESP = (word) CX

slide-28
SLIDE 28

Instructions - POP <dst>

  • <dst> takes the value on top of the stack, ESP gets

incremented

  • POP EAX

○ EAX = *ESP ○ ESP += 4

  • POP CX

○ CX = *ESP ○ ESP += 2

slide-29
SLIDE 29

PUSH/POP example

PUSH EAX POP EBX = MOV EBX, EAX

slide-30
SLIDE 30

Instructions - ADD <dst>, <src>

  • <dst> += <src>
  • ADD EAX, 16

○ EAX += 16

  • ADD AH, AL

○ AH += AL

  • ADD ESP, 0x10

○ Remove 16 bytes from the stack

slide-31
SLIDE 31

Instructions - SUB <dst>, <src>

  • <dst> -= <src>
  • SUB EAX, 16

○ EAX -= 16

  • SUB AH, AL

○ AH -= AL

  • SUB ESP, 0x10

○ Allocate 16 bytes of space on the stack

slide-32
SLIDE 32

Flags

  • x86 instructions can modify a special register

called FLAGS

  • FLAGS contains 1-bit flags:

○ Ex: OF, SF, ZF, AF, PF, and CF

  • ZF = Zero Flag
  • SF = Sign Flag
  • CF = Carry Flag
slide-33
SLIDE 33

Flags

  • Zero Flag

○ set if the result of last operation was zero

  • Sign Flag

○ set if the result of last operation was negative (dst - src <s 0)

  • Carry Flag

○ set if integer underflow (dst <u src)

  • See https://stackoverflow.com/questions/8965923/carry-overflow-subtraction-in-x86
slide-34
SLIDE 34

Flags - Example

MOV RAX, 666 SUB RAX, 666 => ZF = 1 SF = 0 CF = 0

slide-35
SLIDE 35

Flags - Example

MOV RAX, 123 SUB RAX, 666 => ZF = 0 SF = 1 CF = 1

slide-36
SLIDE 36

Flags - Example

MOV AL, 0xFF SUB AL, 0x01 => ZF = 0 SF = 1 (-1 - 1 = -2 < 0) CF = 0 (255 - 1 = 254 > 0)

slide-37
SLIDE 37

Instructions - CMP <dst>, <src>

  • CoMPare
  • Perform a SUB but throw away the result
  • Used to set flags
  • CMP EAX, 13

○ EAX value doesn’t change ○ TMP = EAX - 13 ○ Update the FLAGS according to TMP

slide-38
SLIDE 38

Instructions - JMP <dst>

  • JuMP to <dst>
  • JMP RAX

○ Jump to the address saved in RAX

  • JMP 0x1234

○ Jump to address 0x1234

slide-39
SLIDE 39

Instructions - Jxx <dst>

  • Conditional jump
  • Used to control the flow of a program (ex.: IF

expressions)

  • JZ/JE => jump if ZF = 1
  • JNZ/JNE => jump if ZF = 0
  • JB, JA => Jump if <dst> Below/Above <src> (unsigned)
  • JL, JG => Jump if <dst> Less/Greater than <src> (signed)
  • Many others
  • See http://unixwiz.net/techtips/x86-jumps.html
slide-40
SLIDE 40

Jxx - Example: Password length == 16?

MOV RAX, password_length CMP RAX, 0x10 JZ ok JMP exit

  • k:

...print ‘yay’...

slide-41
SLIDE 41

Jxx - Example: Given number >= 11?

MOV RAX, integer_user_input CMP RAX, 11 JB fail JMP ok fail: ...print ‘too short’...

  • k: ...print ‘OK’...
slide-42
SLIDE 42

Instructions - XOR <dst>, <src>

  • Perform a bitwise XOR between <dst> and <src>
  • XOR EAX, EBX

○ EAX ^= EBX

  • Truth table:

1 1 1 1

slide-43
SLIDE 43

Instructions - CALL <dst>

  • CALL a subroutine
  • CALL 0x123456

○ Push return address on the stack ○ RIP = 0x123456

  • Function parameters passed in many different ways
slide-44
SLIDE 44

Instructions - RET

  • RETurn from a subroutine
  • RET

○ Pop return address from stack ○ Jump to it

slide-45
SLIDE 45

CALL / RET

0x123456: ... RET ... CALL 0x123456 ...

slide-46
SLIDE 46

How are function parameters passed around?

  • On x86, there are many calling conventions
  • Sometimes parameters are passed in registers
  • Sometimes on the stack
  • Return value usually in RAX/EAX
  • You should take some time to look at them

https://en.wikipedia.org/wiki/X86_calling_conventions

slide-47
SLIDE 47

Calling Convention - cdecl

slide-48
SLIDE 48

Calling Convention - cdecl

EBP+04: return address EBP+00: saved EBP EBP+08: arg1 EBP+10: arg3 EBP+0C: arg2

EBP ESP 0xFFFFFFFF 0x00000000

slide-49
SLIDE 49

Calling Convention - cdecl - Local vars

EBP+04: return address EBP+00: saved EBP EBP+08: arg1 EBP+10: arg3 EBP+0C: arg2 EBP-04: local var #1 EBP-08: local var #2 sub esp, 8

EBP ESP

mov esp, ebp

0xFFFFFFFF 0x00000000

slide-50
SLIDE 50

Other useful instructions

NOP - Single-byte instruction that does nothing RET - Return from a function MOVZX - Move and zero extend MOVSX - Move and sign extend

slide-51
SLIDE 51

Now the (slightly) less boring part :D

slide-52
SLIDE 52

...a small introduction to reversing and binja

slide-53
SLIDE 53

ASM - Linear View

slide-54
SLIDE 54

ASM - Graph View (CFG)

slide-55
SLIDE 55

Graph View - IF

slide-56
SLIDE 56

Graph View - Loop

slide-57
SLIDE 57

Binja - Some shortcuts

g - Go to address / symbol <spacebar> - Switch between linear and graph view n - Rename symbol y - Change symbol type ; - Comment (super useful!) * - Follow pointer

slide-58
SLIDE 58

Welcome to cracking reversing 101

slide-59
SLIDE 59

crackme v0

  • You are given an expensive program
  • But you don’t have any money
  • You don’t need the license
  • You can patch the license check so that

every number is correct

slide-60
SLIDE 60

DEMO

slide-61
SLIDE 61

crackme v1

  • Same program
  • We don’t want to patch the binary
  • We want to build a keygen
slide-62
SLIDE 62

DEMO

slide-63
SLIDE 63

crackme_remote

  • Similar to crackme
  • Running on spritz ctf
  • Find a valid key to get the flag
  • CRACKME_FLAG=ASD ./crackme_remote
  • nc 207.154.238.179 5222
slide-64
SLIDE 64

The End

slide-65
SLIDE 65

Some pointers

  • https://www.hex-rays.com/products/ida/index.shtml
  • https://binary.ninja/
  • http://www.radare.org/r/
  • https://github.com/radareorg/cutter/releases
  • http://hopperapp.com/ (only for Mac)
  • https://github.com/wtsxDev/reverse-engineering
  • https://azeria-labs.com/