Buffer Overflow for fun and pr0fit

47
Buffer Overflow for fun and pr0fit Facundo M. de la Cruz (@_tty0) [email protected]

description

Brief introduction to Buffer Overflow vulnerability explotation and protections for the 8.8 Computer Security Conference at Santiago de Chile on 18 and 19 October 2012.

Transcript of Buffer Overflow for fun and pr0fit

Page 1: Buffer Overflow for fun and pr0fit

Buffer Overflowfor fun and pr0fit

Facundo M. de la Cruz (@_tty0) [email protected]

Page 2: Buffer Overflow for fun and pr0fit

➔ Brief introduction to the Intel x86/x86_64 architecture

➔ Integer overflow

➔ Stack based buffer overflows

➔ Attacking a format string

➔ Shellcodes: The ASM cocktail

➔ OS Protections

AGENDA

Page 3: Buffer Overflow for fun and pr0fit

Why do I need to learn about Buffers Overflow?

- A common cause could be: Do you want to impress your girlfriend and be cool and sexy?.

1)Prepare your latest IE or Mozilla Firefox 0day.2)Send a email containing:

«Hey! Checkout this amazing news talking about naked photos of Rihanna» alongside with a link pointing to an url where the exploit is located.

3)Wait him to click.4)????5)Pr0fit!

- Secure your own software or the company software.

- Or simply you are just a curious, and it's cool :-)

NOTES

Page 4: Buffer Overflow for fun and pr0fit

1

Intel Architecture

Page 5: Buffer Overflow for fun and pr0fit

➔ Real mode- 20 bits segmented memory address space.- Only 1 MB of memory can be addressed. - Direct access to BIOS.

➔ Protected mode - Provide protected memory.- Memory paging support.- Global Descriptor Table (GDT) and Local Descriptor Table (LDT).

➔ Virtual 8086 mode- Hybrid operating mode for backward compatibility.- Allows real mode programs run under Protected mode. - Only available in 32 bits CPU's.

➔ Long mode- 64 bits address: 16 EB of memory address (16 billion of GB)- 64 bits instructions and registers. - 16 and 32 bits programs are executed in a sub mode.

- Extension of the 32-bit instruction set, but unlike the 16–to–32-bit transition.

CPU operations modes

Page 6: Buffer Overflow for fun and pr0fit

➔ Mechanisms to protect data and functionality from faults.

➔ Supervisor mode is a hardware-mediated flag which can be changed by code running in system-level software.

CPU RINGS

Page 7: Buffer Overflow for fun and pr0fit

Internal CPU structures used for store only one world or value for time.

➔ General purpose registers

➔ Control registers

➔ Offset registers

➔ Others registers

CPU Registers

Page 8: Buffer Overflow for fun and pr0fit

Memory sections

Page 9: Buffer Overflow for fun and pr0fit

THE STACK

Page 10: Buffer Overflow for fun and pr0fit

System calls

Page 11: Buffer Overflow for fun and pr0fit

System calls

➔ Our exit program in C

➔ The same program in Intel x86 ASM

Page 12: Buffer Overflow for fun and pr0fit

➔ The same program in Intel x86 ASM

From asm/unistd_32.h

System calls

Page 13: Buffer Overflow for fun and pr0fit

System Calls

➔ The same program in Intel x86 ASM

Argument (exit status)

From asm/unistd_32.h

Page 14: Buffer Overflow for fun and pr0fit

Switch from userspace to supervisor

➔ The same program in Intel x86 ASM

Argument (exit status)

From asm/unistd_32.h

System calls

Page 15: Buffer Overflow for fun and pr0fit

Explotation

Page 16: Buffer Overflow for fun and pr0fit

Integer Overflow

An arithmetic operation may produce a result larger than the maximum representable value, a potential error condition may result.

In the ISO C99 standard, signed integer overflow causes undefined behavior.

Page 17: Buffer Overflow for fun and pr0fit

Pacman Kill screen

The game's level counter was a single 8-bit byte and could therefore store only 256 distinct values (0–255). Reaching the 256th level causes an integer overflow in the counter...

Page 18: Buffer Overflow for fun and pr0fit

Integer Overflow

➔ From /usr/include/limits.h

➔ Our own example

Page 19: Buffer Overflow for fun and pr0fit

Integer Overflow

➔ From /usr/include/limits.h

➔ Our own example

0x7fffffff

Page 20: Buffer Overflow for fun and pr0fit

Integer Overflow

➔ From /usr/include/limits.h

➔ Our own example

0x7fffffff

0x7fffffff + 0x1

Page 21: Buffer Overflow for fun and pr0fit

Integer Overflow

➔ From /usr/include/limits.h

➔ Our own example

0x7fffffff

0x7fffffff + 0x1    0x80000000

Page 22: Buffer Overflow for fun and pr0fit

Demo time...

Page 23: Buffer Overflow for fun and pr0fit

Integer Overflow

This is INT_MAX + 1

Page 24: Buffer Overflow for fun and pr0fit

Integer Overflow

This is INT_MAX + 1 Our format string: %d\n

Page 25: Buffer Overflow for fun and pr0fit

Integer Overflow

This is INT_MAX + 1 Our format string: %d\n

We call to <printf@plt>

Page 26: Buffer Overflow for fun and pr0fit

Everytime you made an overflow God kills a kitten.

Page 27: Buffer Overflow for fun and pr0fit

Stack overflow

Page 28: Buffer Overflow for fun and pr0fit

Stack overflow

Page 29: Buffer Overflow for fun and pr0fit

Stack overflow

Page 30: Buffer Overflow for fun and pr0fit

Stack overflow

Page 31: Buffer Overflow for fun and pr0fit

Stack overflow

Page 32: Buffer Overflow for fun and pr0fit

Stack overflow

Page 33: Buffer Overflow for fun and pr0fit

Demo time...

Page 34: Buffer Overflow for fun and pr0fit

Format string exploits can be used to crash a program or to execute harmful code. The problem stems from the use of unchecked user input as the format string parameter in certain C functions that perform formatting, such as printf().

Format Strings

Page 35: Buffer Overflow for fun and pr0fit

Format string exploits can be used to crash a program or to execute harmful code. The problem stems from the use of unchecked user input as the format string parameter in certain C functions that perform formatting, such as printf().

Format Strings

Missing format string

Page 36: Buffer Overflow for fun and pr0fit

Adjacent Memory corruption

Page 37: Buffer Overflow for fun and pr0fit

Adjacent Memory corruption

strncpy(vuln_array, argv[1], sizeof(vuln_array) ­ 1);

Page 38: Buffer Overflow for fun and pr0fit

Demo time...

Page 39: Buffer Overflow for fun and pr0fit

Protections

Page 40: Buffer Overflow for fun and pr0fit

An operative system with support for the NX bit may mark certain areas of memory as Non eXecutable. The CPU will refuse any code residing in these areas of memory.

Intel markets the feature as the XD bit for eXecute Disable.AMD uses the name Enhanced Virus Protection.

NX STACK

Page 41: Buffer Overflow for fun and pr0fit

An operative system with support for the NX bit may mark certain areas of memory as Non eXecutable. The CPU will refuse any code residing in these areas of memory.

Intel markets the feature as the XD bit for eXecute Disable.AMD uses the name Enhanced Virus Protection.

NX STACK

Non executable stack CPU flag present.

Page 42: Buffer Overflow for fun and pr0fit

W^X (write XOR eXecute) is a OpenBSD security feature, it's a memory protection policy whereby every page in a process address space is either writable or executable, but not both simultaneously.

W^X first appeared in OpenBSD 3.3, released May 2003.

Similar features are available for other operating systems, including the PaX and Exec Shield patches for Linux, and NetBSD 4+'s implementation of PaX.

WX Memory

Page 43: Buffer Overflow for fun and pr0fit

ASLR (Address Space Layout Randomization) involves randomly arranging the position of key data areas, usually incluing in the base of the executable and position of libraries, heap and stack.

In a process's address space. It is more effective when more entropy is present in the random offset. Linux enable it by default since 2.6.12 kernel version.

ASLR

Page 44: Buffer Overflow for fun and pr0fit

AAAS (ASCII Armored Address Space) loads the shares libraries in memory address that start with NULL bytes (0x00).

AAAS

Page 45: Buffer Overflow for fun and pr0fit

AAAS (ASCII Armored Address Space) loads the shares libraries in memory address that start with NULL bytes (0x00).

AAAS

Start with nulls (0x00)

Page 46: Buffer Overflow for fun and pr0fit

The cookies is a 32 bits or 64 bits value inserted between the buffer and sensitive data (0x00007fff3a115000 for example).

Whenever the canary is modified, the program jumps into an execution handler, usually causing it to crash.

Cookies

Page 47: Buffer Overflow for fun and pr0fit