hitb lab: arm exploitation lab (part 1) · stack smashing! • alephone’s 1996 smashing the stack...

52
HITB LAB: ARM Exploitation Lab (Part 1) 1

Upload: others

Post on 22-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

HITB LAB: ARM Exploitation Lab (Part 1)

!1

Page 2: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Outline Session 1

• Introduction to the ARM Architecture

• ARM Assembly • Lab 1: Writing Shellcode • Solution

Session 2 • Stack Overflows • Introduction to Ret2Libc • Lab 2: Buffer Overflow Exploit without NX

• Introduction to NX Exploit Mitigation

• Lab 3: Buffer Overflow Exploit with NX Bypass

!2

Page 3: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

OutlineDownload Lab Workbooks here: https://azeria-labs.com/downloads/HITB-ARM-Lab1+2.pdf

Download slides here: https://azeria-labs.com/downloads/HITB-Lab1.pdf https://azeria-labs.com/downloads/HITB-Lab1.pdf

Download VM here: https://drive.google.com/file/d/1dzyLfUrAN1HIT5yuYPIGIFBtWi-MKZWw/view?usp=sharing

!3

Page 4: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Stack-based buffer Overflows

!4

Page 5: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Stack Smashing!

• AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are classic introductions to stack-smashing techniques and trampolining.

• Murat Balaban [3] first described the technique of storing the executable code in an environment variable.

[1] in Phrack 49 at www.phrack.org/show.php?p=49&a=14 [2] www.cultdeadcow.com/cDc_files/cDc-351/ [3] www.enderunix.org/docs/eng/bof-eng.txt

Page 6: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Stack Frames

!6

Page 7: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!7

Page 8: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Non-Leaf Functions

Page 9: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Imagine a Stack

Page 10: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Imagine a Stack

[19]

[0]

Saved Frame Pointer

Saved Return Address

MemoryAddresses

StackGrowth

Page 11: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Imagine a Stack

Page 12: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Imagine a Stack

Page 13: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!13

Page 14: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!14

Page 15: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!15

Page 16: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!16

Page 17: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!17

Page 18: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Debugging with GDB

!18

Page 19: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!19

$ export test=$(./exploit.py)

Page 20: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!20

Page 21: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!21

Page 22: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!22

Page 23: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!23

Page 24: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!24

Page 25: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Examine Memory

!25

Page 26: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!26

• Use “help” to display categories

• Use “help all” to view all commands

• Use “apropos <cmd>” or “help <cmd>” to display the description of that command

Page 27: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!27

Page 28: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!28

Page 29: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Ropper 101

Page 30: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Lab 2: without NX

!30

Page 31: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

NX exploit mitigation

!31

Page 32: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

NX – Never eXecute

• Makes certain regions, e.g. stack, non-executable

• Simply putting your shellcode on the stack and branching to it won’t work anymore, since nothing on the stack can be executed anymore

• Bypass technique: Ret2Libc (ROP light)

• Bypass technique: Mprotect ROP chain, making a specific stack region executable again to execute shellcode.

Page 33: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

NX – Never eXecute

Page 34: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!34

Page 35: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Return to Libc

• Return-into-libc attacks were pioneered by Solar Designer in 1997 [1] and refined by Rafal Wojtczuk. [2]

• Bypass technique for the non-executable stack

• Using libc library gadgets to construct a ROP chain without executing shellcode on the stack.

[1] www.securityfocus.com/archive/1/7480 [2] 1998’s Defeating Solar Designer’s Non-executable Stack Patch at www.insecure.org/sploits/non-executable.stack.problems.html, and 2001’s The Advanced return-into-lib(c) Exploits in Phrack 58 at www.phrack.org/show.php?p=58&a=4

Page 36: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!36

Page 37: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!37

Page 38: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!38

Page 39: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!39

Page 40: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!40

Page 41: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!41

Page 42: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Invoking System

• System(“/bin/sh”)

• R0 —> /bin/sh

• PC: system() address

POP { R3, PC} <system address> MOV R0, SP; BLX R3

!42

Page 43: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

The Simple return to system

Page 44: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

The Simple return to system

Page 45: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

The Simple return to system

Page 46: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

The Simple return to system

Page 47: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!47

Page 48: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

What if we can’t find mov r0, sp?

•We need to make R0 point to our /bin/sh string in memory.

•We can’t use this gadget

• Find a way to make r0 point to /bin/sh and execute system without the “perfect gadget”

• Check which registers you control and where they point to

!48

Page 49: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

!49

CTRL+X —> Split terminal vertically CTRL+O —> Split terminal horizontally CTRL+X —> Maximize selected window CTRL+W —> Close selected window

ARM environment (ssh arm) for editing exploits

Ubuntu host for Gadget hunting with Ropper

ARM environment for GDB

Page 50: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Getting Started

• Disable ASLR

• user@arm:~# sudo sh -c "echo 0 > /proc/sys/kernel/randomize_va_space” 1. Open Challenge2 in GDB: gdb challenge2 2. Set breakpoint at main: b func1 3. Run the program: run 4. Check binary sections: vmmap 5. Check security properties: checksec

!50

Page 51: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

LAB 3: nx bypass

!51

Page 52: HITB LAB: ARM Exploitation Lab (Part 1) · Stack Smashing! • AlephOne’s 1996 Smashing the Stack for Fun and Profit [1] and DilDog’s The Tao of Windows Buffer Overruns [2] are

Session 2 /end :)More resources at https://azeria-labs.com Twitter: @Fox0x01

!52