24
Buffer Overflow – Offensive Point of View Toe Khaing Oo Myanmar Security Forum

BufferOverflow - Offensive point of View

Embed Size (px)

Citation preview

Page 1: BufferOverflow - Offensive point of View

Buffer Overflow – Offensive Point of View

Toe Khaing Oo

Myanmar Security Forum

Page 2: BufferOverflow - Offensive point of View

About Me

- Final year student at UCSY.

- Moderator at Myanmar Security Forum.

- Core member at Fedora Project Myanmar Community.

Page 3: BufferOverflow - Offensive point of View

Buffer Overflow Basics

What is buffer?

- Any area of memory where more than one piece of data is stored.

- Where CPU can store data temporarily.

- Data may be retrieved from input.

Page 4: BufferOverflow - Offensive point of View

Buffer Overflow

- Well known form of Software security vulnerability.

- Errors occur when operating on buffers of char type.

- Difficult to discover, difficult to exploit.

- Exploit for fun and profit.

Page 5: BufferOverflow - Offensive point of View

How Overflow Works

Overflow occurs when program receives more data than it expects.

When data is written, it can overwrite other data in memory. Attacker can inject malicious code instead of overwritten data.

Attacker can take control target computer.

Page 6: BufferOverflow - Offensive point of View

Buffer Overflow Types

Mostly two types ..- Stack based overflow.- Heap based overflow.

Other types ... - Integer Overflows- Format String attack

Page 7: BufferOverflow - Offensive point of View

Stack Based Overflow

Occur when variable size data is copied into fixed length buffers located in stack without any bounds checking.

Data is written past the end of buffers allocated on the stack.

Page 8: BufferOverflow - Offensive point of View

Example Program

#include <unistd.h>void sto(){char buff[5];printf("Some input : ");gets(buff);puts(buff);}int main (int argc, char *argv[]){sto();return 0;}-----------------------------------------------------------------------------------------------------------Sample C Program save as sto.c

Page 9: BufferOverflow - Offensive point of View

Add 4 char “A” and we have return data correctly.

Page 10: BufferOverflow - Offensive point of View

When adding large input data such as “A” * 20, got an error as “Segmentation Fault”

Page 11: BufferOverflow - Offensive point of View

Heap Overflow

● Heap is a memory segment used for storing dynamically allocated data and global variables.

● Consists :– Boundry tags

– Memory management information

Page 12: BufferOverflow - Offensive point of View

How Heap Overflow Occurs

● Overwriting a function pointer that happens to be allocated on the heap.

● Like Stack-Based overflow, input strings are larger than expected.

● Unlike stack-based overflow, results are different.

Page 13: BufferOverflow - Offensive point of View

Example

int main(int argc, char *argv[])

{

...

vulnerable(argv[1]);

return 0;

}

int vulnerable(char *buf)

{

HANDLE hp = HeapCreate(0, 0, 0);

HLOCAL chunk = HeapAlloc(hp, 0, 260);

strcpy(chunk, buf); ''' Vulnerability

……..

return 0;

}

Page 14: BufferOverflow - Offensive point of View

When a heap based buffer is overflowed,the control information is overwritten so when the buffer (allocated block) is freed and it comes to updating the pointers in array there’s going to be an access violation.

Page 15: BufferOverflow - Offensive point of View

Format Strings Attack

● Occurs when the input string data is evaluated as a command by the application.

● Attacker can execute code, read the stack, or cause a segmentation fault in the running application, causing new behaviors that could compromise the security or the stability of the system.

Page 16: BufferOverflow - Offensive point of View

● Function like printf, fprintf convert the variable (String)

● Format String parameter .. %x, %s define conversion.

● Attacker insert a sequence of format strings making the program to read illegal address.

Page 17: BufferOverflow - Offensive point of View

Discovering Buffer Overflow

● Blackbox testing ● Graybox testing● Whitebox testing

Page 18: BufferOverflow - Offensive point of View

Blackbox testing

● Input the bad character to the in put box and check the memory registers with debuggers.

● Check this register can overwrite.

Page 19: BufferOverflow - Offensive point of View

Graybox Testing

● Reviewing Code.● Check binary files.

Page 20: BufferOverflow - Offensive point of View

Whitebox Testing

● Reading source code.● Static analyzer tools.● Fuzzing (eg. Spike fuzzer)

Page 21: BufferOverflow - Offensive point of View

Exploitation

● Check pattern.

● Check offset number.

● Check is it necessary to inject junk code.

● Generate shell code and inject.

Page 22: BufferOverflow - Offensive point of View

Avoiding Buffer Overflow

● Check Program with Static code analyzer.

● Check bounds checking.

● Check buffers in char type.

Page 23: BufferOverflow - Offensive point of View

● Use high level language like java, C#

● Some other IDE has default bound checking.

Page 24: BufferOverflow - Offensive point of View

Thanks ;)