51
Introduction to Exploitation th!nkh@ck-hackartist

Basic of Exploitation

Embed Size (px)

DESCRIPTION

This slides describe the basic of exploitation.

Citation preview

Page 1: Basic of Exploitation

Introduction to Exploitationth!nkh@ck-hackartist

Page 2: Basic of Exploitation

Outline

• What is exploitation

• Study Points

• CPU Registers & Instructions

• Flow of Function

• Managing Memory

Page 3: Basic of Exploitation

What is exploitation?

• The kind of exploitation is various For software, hardware, system and etc.

• What is the goal of the exploitation? The exploitation is getting actions you want it to do.

• What is needed?Skill of computer languages Knowledge of Operating System Understanding architecture.

Page 4: Basic of Exploitation

Study Points

• We will focus on :CPU registers, Understanding stack of computer, A glance about assembly and C

• Why we study those Most of exploitations come from memory corruption.

Page 5: Basic of Exploitation

CPU Registers & Instructions

• Cores of CPUCPU consists of registers, instructions.

• What does the different architecture of CPU means?It means that they have the different instructions each others.

• Are you hard to get assembly?Do not afraid to face it. Because assembly was made for us with human friendly representation of opcodes.

• Special instructionsEBP, ESP, EIP

Page 6: Basic of Exploitation

CPU Registers & Instructions

• 16 bits data registers can be accessed to high and low half bits by AL&AH, BL&BH and etc.

16 Bits 32 Bits 64 Bits Description

AX EAX RAX Accumulator

BX EBX RBX Base Index

CX ECX RCX Counter

DX EDX RDX Data

BP EBP RBP Base Pointer

SP ESP RSP Stack Pointer

IP EIP RIP Instruction Pointer

SI ESI RSI Source Index Pointer

DI EDI RDI Destination Index Pointer

Page 7: Basic of Exploitation

Composition of a Functionvoid func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Name

Parameters

Body

Return type

Page 8: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 9: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 10: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 11: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 12: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 13: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 14: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 15: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 16: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 17: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 18: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 19: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 20: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 21: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 22: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 23: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 24: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

func2(buffer);

Page 25: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 26: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 27: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 28: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

func1(argv[1]);

Page 29: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 30: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 31: Basic of Exploitation

Variation of Stack memory

void func2(char *x) { printf(“You entered: %s\n", x); }

void func1(char *str) { char buffer[16]; strcpy(buffer, str); func2(buffer); }

int main(int argc, char *argv[]) { if (argc > 1) func1(argv[1]); else printf(“No arguments!\n"); }

Page 32: Basic of Exploitation

What is Stack?

• Stack is one of data structure. LIFO - Last In First Out

• Imagine reading book and stacking them. After reading them, we simply take over the top of books. In the circumstances, it is difficult to get a book in middle of the tower.

Page 33: Basic of Exploitation

Managing Memory

• One of role of Operating System is managing memory. In order to manage memory, the memory is segmented into several parts. The way memory is segmented is depends on OS. However, main memory is existed and important in all OS.

• Main memory is represented to “logical” structure and consists of several stack frames.

• Stack frame is a significantly important part. The stack frame is represented by two pointers:Base pointer(EBP or RBP) indicates start of current stack frame.Stack pointer(ESP or RSP) indicates end of current stack frame.

Page 34: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 35: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBPESP

Page 36: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBPESP

Page 37: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 38: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBPESP

Page 39: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBPESP

Page 40: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBP ESP

Page 41: Basic of Exploitation

Back to Previous Position

Empty memory

Top of memory

Start of memory

Newer stack frame

EBP ESP?

Page 42: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 43: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBPESP

Page 44: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 45: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 46: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 47: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 48: Basic of Exploitation

Back to Previous Position

Empty memory

Saved EBP

Top of memory

Start of memory

Newer stack frame

EBP

ESP

Page 49: Basic of Exploitation

x86 calling conventions

• There are three types for calling function. stdcall : The default call type of WinAPI32. A callee is responsible for cleaning stack frame for arguments.cdecl : The default call type of C. A caller is responsible for cleaning stack frame.

• There are many call types.

Page 50: Basic of Exploitation

Difference between thempush <arg2>push <arg1>

call <callee>

push ebpmov ebp esppush <local variables>

pop ebpret <args size>

push <arg2>push <arg1>

call <callee>

push ebpmov ebp esppush <local variables>

pop ebpret 0

add esp <args size>

Caller

Callee

Caller

cdecl stdcall

Page 51: Basic of Exploitation

Thank you for listening.