77
The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Embed Size (px)

Citation preview

Page 1: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The OWASP Top 10and Buffer Overflow Attacks

Tom ChothiaComputer Security, Lecture 14

Page 2: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

OWASP top 10.

The Open Web Application Security Project

Open public effort to improve web security:– Many useful documents.– Open public meetings & events.

There “10 top” lists the current biggest web threats.

Page 3: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A1: Injection

• Server side command injection, e.g., SQL injection.

• Not just SQL injection, any command language can be injected.

• E.g. PHP, shell commands, XML processing commands, …

Page 4: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

PHP injection

• Get password

• Create command executer

Page 5: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A2: Broken Auth.

Many web developers implement their own log in systems. Often broken, e.g.

• No session time outs.

•Passwords not hashed– E.g. password shame list.

Page 6: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Password shame list

Page 7: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A3: XXS

• Cross Side Scripting attacks, as discussed.

• A1 injection is command injection on the server side.

• This is JavaScript injection on the client side.

Page 8: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A4: Insecure Direct Object Reference

Problem: the server trusts the client to request only the resources it should. E.g.

http://site.com/view?user=alice

which we could replace with:

http://site.com/view?user=bob

Also common with cookie values.

Page 9: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Path Transversal

• The user can type anything they want into the URL bar, or even form the request by hand.

http://nameOfHost/dir/file.html

Page 10: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Path Transversal

• The user can type anything they want into the URL bar, or even form the request by hand.

http://nameOfHost/../../../etc/shadow

Page 11: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Path Transversal

• The user can type anything they want into the URL bar, or even form the request by hand.

http://nameOfHost/../../../etc/shadow

• If the webserver is running with root permission this will give me the password file.

Page 12: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Path Transversal: Fix

• Use access control settings to stop Path Transversal.

• Best practice, make a specific user account for the webserver.

• Only give that account access to public files.

Page 13: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A5: Security Misconfiguration

Make sure your security settings don’t give an attacker an advantage, e.g.

– Error Messages: should not be made public.

– Directory Listings: It should not be possible to see the files in a directory.

– Admin panels should not be publically accessible.

Page 14: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

• Robots.txt

Page 15: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A6: Sensitive Data Exposure

All sensitive data should be protected at all times.

•Is SSL used everywhere?

•Credit card numbers not encrypted:– CC no. should be encrypted in database. PHP

page should decrypt these, if needed.– This means that the hacker needs to attack the

page and the database.

Page 16: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A7: Missing Function Level Access Control

Query strings are used to tell dynamic webpages what to do

http://myWebShop.com/index.php?account=tpc&action=add

http://myWebShop.com/index.php?account=tpc&action=show

What if the attacker tries: http://myWebShop.com/index.php?

account=admin&action=delete

Page 17: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

URL hacking

• The user can type anything they want into the URL bar, or even form the request by hand.

http://nameOfHost/filePath

• Attacker can try to guess filenames,– Guessable directory names will be found.

Page 18: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Fix

No security through obscurity

Never rely on just the URL request for authentication.

E.g. Use cookies to control access.

Page 19: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A8: CSRF

• Cross-Site Request Forgery (CSRF)

• As discussed earlier.

• Defend against by using unique token in the hidden field of important forms.

Page 20: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A9: Using Components with Known Vulnerabilities

• If a new security patch comes out has it been applied? –A patch might require you to bring

down the site and so lose money. –Or it might even break your website.

• Is it worth applying the patch?

Page 21: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

A10: Invalidated Redirects and Forwards

• If attackers can forward a user to another page then they can use it for:

– Phishing (e.g. a fake log in page)– Ad Fraud.– Launch exploits on browser.

• Not a major threat (IMHO).

Page 22: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Web Security

• To secure a website you need to know how it works: – How clients request resources.– How clients are authenticated.– How HTTP and webservers work.

• Errors are often down to bad app logic

• Always sanitize everything.

Page 23: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow Attacks

Page 24: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow Attacks

• A simplified, high-level view of buffer overflow attacks.– x86 architecture– overflows on the stack

• Exploiting buffer overflows using Metasploit

Page 25: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Introduction

• In languages like C, you have to tell the compiler how to manage the memory.

– This is hard.

• If you get it wrong, then an attacker can usually exploit this bug to make your application run arbitrary code.

• Countless worms, attacks against SQL servers, Web Servers, iPhone Jailbreak, SSH servers, …

Page 26: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

USS Yorktown

US Navy Aegis missile cruiser

Page 27: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

USS Yorktown

US Navy Aegis missile cruiser

Dead in the water for 2 and a half hours due to a buffer overflow.

Page 28: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

USS Yorktown

“Because of politics, some things are being forced on us that without political pressure we might not do, …

Ron Redman, deputy technical director Aegis

US Navy Aegis missile cruiser

Dead in the water for 2 and a half hours due to a buffer overflow.

Page 29: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

USS Yorktown

“Because of politics, some things are being forced on us that without political pressure we might not do, like Windows NT. If it were up to me I probably would not have used Windows NT in this particular application.”

Ron Redman, deputy technical director Aegis

US Navy Aegis missile cruiser

Dead in the water for 2 and a half hours due to a buffer overflow.

Page 30: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 Architecture

Text

Data

Stack

Free Memory

Page 31: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 ArchitectureThe program code

Text

Data

Stack

Free Memory

Page 32: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 ArchitectureThe program code

Static variables, Strings, etc

Text

Data

Stack

Free Memory

Page 33: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 ArchitectureThe program code

Static variables, Strings, etc

Data in use

Text

Data

Stack

Free Memory

Page 34: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 ArchitectureThe program code

Static variables, Strings, etc

Data in use

Registers e.g. The Accumulator

Instruction point Stack point

Text

Data

Stack

Free MemoryESP

EIP

EAX

Page 35: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The x86 ArchitectureThe program code

Static variables, Strings, etc

Data is use

Registers e.g. The Accumulator

Instruction point Stack point

Text

Data

Stack

Free MemoryESP

EIP

EAX

Page 36: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Screen shot, IDA

Page 37: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The Stack

The stack part of the memory is mostly “Last In, First Out”.

We can only write and read to the top of the stack.

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B0EIP: 7797F9CD EAX:

Page 38: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

The Stack

You write to the stack with push

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B0EIP: 7797F9CD EAX:

Page 39: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

123456

The Stack

You write to the stack with push

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B1EIP: 7797F9CE EAX:

Page 40: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

678245123456

The Stack

You write to the stack with push

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B1EIP: 7797F9CF EAX:

Page 41: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

678245123456

The Stack

You write to the stack with push

You read and remove an item from the stack with pop

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B1EIP: 7797F9CF EAX:

Page 42: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

123456

The Stack

You write to the stack with push

You read and remove an item from the stack with pop

…PUSH 12345PUSH 678245POP EAX….

Data

Stack

Free MemoryESP: 0018F9B1EIP: 7797F9CF EAX: 678245

Page 43: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function calls

void main () { function (1,2);

}

Page 44: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function calls

void main () { function (1,2);

}

• Arguments 1 & 2 are passed on the stack.

• The CALL instruction runs a function

Page 45: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function calls

void main () { function (1,2);

}

• Arguments 1 & 2 are passed on the stack.

• The CALL instruction runs a function

PUSH <2>

PUSH <1>

CALL <function>

Page 46: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function Calls PUSH <arg2>PUSH <arg1>CALL <function>

Stack

Page 47: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function Calls PUSH <arg2>PUSH <arg1>CALL <function>

StackArg2

Page 48: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function Calls PUSH <arg2>PUSH <arg1>CALL <function>

StackArg2Arg1

Page 49: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function Calls PUSH <arg2>PUSH <arg1>CALL <function>

CALL writes the instruction point (EIP) onto the stack and then sets the EIP to to equal the code for the function.

StackArg2Arg1Old EIP

Page 50: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Function Calls PUSH <arg2>PUSH <arg1>CALL <function>

CALL writes the instruction point (EIP) onto the stack and then sets the EIP to to equal the code for the function.

Later a return instruction restores the old EIP and the program continues

StackArg2Arg1Old EIP

Page 51: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Screen shot, IDA

Page 52: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflows

• The instruction pointer controls which code executes,

Page 53: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflows

• The instruction pointer controls which code executes,

• The instruction pointer is stored on the stack,

Page 54: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflows

• The instruction pointer controls which code executes,

• The instruction pointer is stored on the stack,

• I can write to the stack …

Page 55: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflows

• The instruction pointer controls which code executes,

• The instruction pointer is stored on the stack,

• I can write to the stack …

Page 56: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

Stack

Page 57: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

Stack

1. Function called with “Hello World”

Page 58: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldOld EIP

1. Function called with “Hello World”

2. Arg and EIP written to stack

Page 59: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldOld EIP

1. Function called with “Hello World”

2. Arg and EIP written to stack

3. Function runs

Page 60: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldOld EIP<------16------>

1. Function called with “Hello World”

2. Arg and EIP written to stack

3. Function runs

4. Buffer allocated

Page 61: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffers

1. Functions called with “Hello World”

2. Arg and EIP written to stack

3. Function runs

4. Buffer allocated

5. String copied

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldOld EIPHello World

Page 62: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes? …

function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

Stack

Page 63: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes

1. Runs as before

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldX XXXXXXXXX

Old EIP

Page 64: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes

1. Runs as before

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldX XXXXXXXXX

Old EIP<------16------>

Page 65: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes

1. Runs as before

2. But the string flows over the end of the buffer

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldX XXXXXXXXX

Old EIP<------16------>

Page 66: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes

1. Runs as before

2. But the string flows over the end of the buffer

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldX XXXXXXXXX

XXXXIPHello WorldXX

Page 67: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Buffer Overflow

If user input is more than 16 bytes

1. Runs as before

2. But the string flows over the end of the buffer

3. EIP corrupted, segmentation fault

…function (user input);…

function (char *str) { char buffer[16]; strcpy(str,buffer);}

StackHello WorldX XXXXXXXXX

XXXXIPHello WorldXX

Page 68: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Once more, with malice

1. Runs as before

Stack

Page 69: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Once more, with malice

1. Runs as before

2. Attacker sends a very long message, ending with the address of some code that gives him a shell.

– The attackers code could also be part of the message

StackHello WorldX X7797F9

Page 70: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Once more, with malice

1. Runs as before

2. Attack send a very long message, ending with the address of some code that gives him a shell.

– The attackers code could also be part of the message

StackHello WorldX X7797F9

Old EIP

Page 71: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Once more, with malice

1. Runs as before

2. Attack send a very long message, ending with the address of some code that gives him a shell.

– The attackers code could also be part of the message

3. The attackers value is copied over the old EIP

StackHello WorldX X7797F9

7797F9Hello WorldXX

Page 72: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Once more, with malice

1. Runs as before

2. Attack send a very long message, ending with the address of some code that gives him a shell.

– The attackers code could also be part of the message

3. The attackers value is copied over the old EIP

4. When the function returns the attacks code is run

StackHello WorldX X7797F9

7797F9Hello WorldXX

Page 73: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

• Metasploit website• Metasploit attack demo

Page 74: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Over Writing Other Values

Attacking the instruction pointer (EIP) is the most powerful technique. However, any memory value can be attacked:

• Over write arguments on the stack – e.g. change the parameters to a chmod call

• Overflows on the heap– e.g. rewrite a password in memory

Page 75: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Defenses

• Stack canaries: – values placed on the stack, which are later tested.– if the stack is over written then the value test will

fail.• Randomisation

– Layout of the memory is randomised.– This makes it very hard for the attack to find the

memory to overwrite or code to jump to.

For more information see the Secure Programming Module

Page 76: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Recommend Paper:

• “Smashing the Stack for Fun and Profit”Elias Levy (Aleph One)

A simple introduction to buffer overflows from the mid 90s.

Standard defences now stop the attacks in this paper, but it gives an excellent introduction.

Page 77: The OWASP Top 10 and Buffer Overflow Attacks Tom Chothia Computer Security, Lecture 14

Conclusion

Buffer overflows are the result of poor memory management in languages like C– even the best programmers sometimes make

mistakes.

Buffer overflow attacks exploit these to over write memory values.

This often lets an attack execute arbitrary code.