“smashing the stack” is a type of buffer most common buffer...

16

Upload: others

Post on 16-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom
Page 2: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

  “Smashing the Stack” is a type of buffer overflow attack - overwriting the return address to redirect control to attack code

  Most common buffer overflow error since it is the easiest to make and take advantage of

2

Page 3: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

  No one would do something like this, right?

3

Page 4: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

  First example of a high speed worm (previously only existed in theory)

  Infected a total of 75,000 hosts in about 30 minutes

  Infected 90% of vulnerable hosts in 10 min   Exploited a vulnerability in MS SQL Server

Resolution Service, for which a patch had been available for 6 months

4

Page 5: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

  Code randomly generated an IP address and sent out a copy of itself

  Used UDP - limited by bandwidth, not network latency (TCP handshake).

  Packet was just 376 bytes long…   Spread doubled every 8.5 seconds   Max scanning rate (55 million scans/second)

reached in 3 minutes

5

Page 6: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

6

Page 7: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

7

Page 8: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

8

Executable Code (.text)

Data (.data)

Heap

Stack

Low

er M

emor

y A

ddre

sses

Assumptions

• Stack grows down

• Stack pointer points to the last address on the stack

Page 9: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

9

void function(int a, int b, int c){ char buf[16];

}

int main(){ function(1,2,3);

}

Let us consider how the stack of this program would look:

Page 10: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

10

Return Address

Saved registers

Local Variables (char buf[4])

Higher M

emory A

ddresses

function prolog sw $ra, -4(sp) sw $s0, -8(sp)

addi $sp, $sp, -24 Allocates space for stack frame

Page 11: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

11

4

ra s0 buf

4 16

Top of mem

ory B

ottom of stack B

otto

m o

f mem

ory

Top

of st

ack

Page 12: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

12

void function(char *str){ char buf [16]; strcpy(buf, str);

}

int main(){ char large_string[32]; int i; for (i = 0; i < 31; i++){ large_string[i] = ‘A’; } function(large_string);

}

Page 13: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

13

When this program is run, it results in an exception

4 4

ra s0 buf

16

Top of mem

ory B

ottom of stack

Bot

tom

of m

emor

y To

p of

stac

k

A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A

A A A A

The return address is overwritten with ‘AAAA’ (0x41414141)

Function exits and goes to execute instruction at 0x41414141…..

Page 14: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

14

Can we take advantage of this to execute code, instead of crashing? void function(int a, int b, int c){

char buf[4]; int *r; r = buf + 20; (*r) += 8;

}

int main(){ int x = 0; function(1,2,3); x = 1; printf(“%d\n”, x);

}

Page 15: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

15

Top of mem

ory B

ottom of stack

Bot

tom

of m

emor

y To

p of

stac

k

buf + 20

This causes it to skip the assignment of 1 to x, and prints out 0 for the value of x

4

ra s0 buf

4 16

Page 16: “Smashing the Stack” is a type of buffer Most common buffer …courses.cs.washington.edu/.../lectures/BufferOverflows.pdf · 2008. 10. 17. · Bottom of memory op of stack Bottom

  We have seen how we can overwrite the return address of our own program to crash it or skip a few instructions - basically just writing a buggy program

  How can these principles be used by an attacker to hijack the execution of a program?

  Attacker can use some kind of user/network input to inject attack code into such a buffer

16