dangling pointer - owasp · code injection –the double reference exploit object allocated object...

23
1 Jonathan Afek, 1/8/07, BlackHat USA Dangling Pointer Dangling Pointer

Upload: others

Post on 07-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

1

Jonathan Afek, 1/8/07, BlackHat USA

Dangling PointerDangling Pointer

Page 2: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

� What is a Dangling Pointer?

� Code Injection

� Object Overwriting

Demonstration

Table of Contents

2

� Demonstration

� Remediation

� Summary

� Q&A

Page 3: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Pointer3

What is a Dangling Pointer?

Invalid Pointer:

� Real Life Example

� Dangerous

Exploitable

Pointer1 Pointer2Dangling

Pointer

3

Deleted

ObjectNew DataObject

3

� Exploitable

� Common

ObjectObject

Pointer3 = new Object();…delete Pointer3;…Pointer3->func();

Application Code:

Overwritethe object

Pointer

Page 4: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

What is a Dangling Pointer?

� Assembly

– Memory Layout

4

– Registers

– Assembly code

Page 5: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

� What is a Dangling Pointer?

� Code Injection

� Object Overwriting

Demonstration

Where are We

5

� Demonstration

� Remediation

� Summary

� Q&A

Page 6: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Code Injection – The Layout of an Object

� Class_A:

class Class_A

{

int member_of_A;

vfunc_A1 CodeClass_A VFTableInstance_A memory

vfunc_A1 address

vfunc_A2 address

VFTABLE Pointer

member_of_AAssembly code

vfunc_A3 address

6

};

int member_of_A;

public:

virtual long vfunc_A1();

virtual long vfunc_A2();

virtual long vfunc_A3();

static void sfunc_A();

void funcA();

};

{

...

this.vfunc_A3();

...

}

……MOV EAX, [ECX]

CALL [EAX + 8]

……

vfunc_A2 Code

Assembly code

vfunc_A3 address

vfunc_A3 Code

Assembly code

Page 7: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Code Injection – The Double Reference Exploit

Exploit Overview:

– Free the Object

– Overwrite the Object

– Execute a Virtual Function

77

Page 8: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Shellcode

Finding a “VFTable”

Code Injection – The Double Reference Exploit

� Object Allocated

� Object De-allocated

� Overwriting Object

� VFunc3 Executed

Application Code:

8

Original ObjectFreed Space

delete a;a->v_func3();a = new A();

Pointer

“VFTABLE”

8

Memory Junk

Memory Junk

Pointer

Memory Junk

Memory Junk

“VFTABLE” Pointer

CALL/JMP ECX

ECX – Original Object

Application Code:

……MOV EAX, [ECX]

CALL [EAX + 8]

……

EAX – “VFTABLE”

SHELLCODE

Page 9: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

� What is a Dangling Pointer?

� Code Injection

� Object Overwriting

Demonstration

Where are We

9

� Demonstration

� Remediation

� Summary

� Q&A

Page 10: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding

� Allocation Implementation

– C-Runtime heap

– C-Runtime functions

• Malloc

• Free

1010

• Free

• New

• Delete

• Etc.

Page 11: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding

� Allocation implementation details

– Lookaside List: Cache De-allcated Memory

• A list for each size (8-1024) (8)

• First Allocation Priority

11

Another De-Allocated

Buffer

NULL

40

Bytes

A De-Allocated Buffer

Next Buffer Pointer

40

Bytes

11

40 bytes

Lookaside list

base pointer

Page 12: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding

� Exploit Review

� Overwriting

– Search for Allocations

• DisassemblyPointer3Dangling

Pointer

1212

• Same Size

• Controllable Content

Deleted

ObjectNew DataObject

Pointer3 = new Object();…delete Pointer3;…Pointer3->func();

Application Code:

Overwritethe object

Pointer

Page 13: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding – The VFTABLE Exploit

� Empty the Lookaside List

� Allocate a Buffer

� Insert Content

……MOV EAX, [ECX]

� Free the Buffer

� Free the Object

� Execute a VFunc

13

Pointer

New Buffer

SHELLCODE

SHELLCODE

Rest of

SHELLCODE

Pointer

“VFTABLE”

“VFTABLE” Pointer

CALL/JMP EAX

Original Object

NULL

MOV EAX, [ECX]

CALL [EAX + 8]

……

ECX – Original Object EAX – “VFTABLE”

Page 14: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding – The Lookaside Exploit

� Empty the Lookaside

� Allocate Two Buffers

� Insert Shellcode

� Free One Buffer

� Free the Other

� Free the Object

� Trigger the Bug

1414

The De-Allocated

Buffer

Pointer

A Function

Pointer

The De-Allocated

Object

Pointer

A VFTABLE

Pointer

The Shellcode

Buffer

NULL

ShellcodeGAME OVER!!!GAME OVER!!!

Page 15: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Object Overriding – The Lookaside Exploit

� Executing NULL – NO Problem

1515

Page 16: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Summary

� Double Reference Exploit

– Controllable First DWORD

– Static Address

� VFTABLE Exploit

– Controllable Allocations

16

– Controllable Allocations

– No First DWORD

– Static Address

� Lookaside Exploit

– Controllable Allocations

– No First DWORD

– No Static Address

– Destructor Execution

Page 17: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

� What is a Dangling Pointer?

� Code Injection

� Object Overwriting

Demonstration

Where are We

17

� Demonstration

� Remediation

� Summary

� Q&A

Page 18: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Demonstration

� Putting it Together

– De-Allocate

– Inject

– Trigger

18

Page 19: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

� What is a Dangling Pointer

� Code Injection

� Object Overwriting

Demonstration

Where are We

19

� Demonstration

� Remediation

� Summary

� Q&A

Page 20: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Remediation

� Known Protection Mechanisms

– NX Bit

– ASLR

� VFTABLE Sanitation

20

� Safe Programming

Page 21: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Summary

� Technical Background

– Memory Allocations

– Objects Implementation

� Exploits

21

– Double Reference Exploit

– VFTABLE Exploit

– Lookaside Exploit

� Demonstration

– Microsoft IIS 5.1

� Dangling Pointer

– Only Object Oriented Objects

Page 22: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

More Information

� www.Watchfire.com

22

Page 23: Dangling Pointer - OWASP · Code Injection –The Double Reference Exploit Object Allocated Object De-allocated Overwriting Object VFunc3 Executed Application Code: 8 ... Demonstration

Questions

� Ask Away…

23