28
1 Jonathan Afek, 2/8/07, BlackHat USA Dangling Pointer Dangling Pointer

Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

1

Jonathan Afek, 2/8/07, BlackHat USA

Dangling PointerDangling Pointer

Page 2: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

2

What is a Dangling Pointer?

Code Injection

Object Overriding

Demonstrations

Remediation

Summary

Q&A

Table of Contents

Page 3: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

33

What is a Dangling Pointer?

Invalid Pointer:

Dangerous

Easy to Exploit

Common

PointerPointer PointerPointer PointerPointer

ObjectObject ObjectObjectDeleted Object

Deleted Object

DanglingPointer

DanglingPointer

New DataNew DataObjectObject

Page 4: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

44

What is a Dangling Pointer? – An Example

Results:

Crash

Page 5: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

5

What is a Dangling Pointer? – An Example

Debugger View

Page 6: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

6

What is a Dangling Pointer?

Code Injection

Object Overriding

Demonstrations

Remediation

Summary

Q&A

Where are We

Page 7: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

7

Code Injection – The Layout of an Object

Class_A:

class Class_A{

int member_of_A;public:virtual long vfunc_A1();virtual long vfunc_A2();static void sfunc_A();void funcA();

};

class Class_A{

int member_of_A;public:virtual long vfunc_A1();virtual long vfunc_A2();static void sfunc_A();void funcA();

};

vfunc_A1 Codevfunc_A1 CodeClass_A VFTableClass_A VFTableInstance_A memoryInstance_A memory

vfunc_A1 addressvfunc_A1 address

vfunc_A2 addressvfunc_A2 address

VFTABLE PointerVFTABLE Pointer

member_of_Amember_of_A Assembly codeAssembly code

vfunc_A2 Codevfunc_A2 Code

Assembly codeAssembly code{

...this.vfunc_A2();...

}

{...this.vfunc_A2();...

}

……MOVE EAX, [ECX]MOVE EAX, [ECX]

CALL [EAX + 4]CALL [EAX + 4]

……

Page 8: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

88

Code Injection – The Double Reference Exploit

Exploit Overview:– Free the Object– Override the Object – covered later– Execute a Virtual Function

Page 9: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

9

Original ObjectOriginal ObjectFreed SpaceFreed Space

9

Code Injection – The Double Reference Exploit

Injecting Code– Free the Object– Shellcode– Call/Jmp ECX– Finding a “VFTABLE”– Interpreted as Code

VFTABLEVFTABLE

VFTABLE + 4VFTABLE + 4

VFTABLE + 8VFTABLE + 8

VFTABLE + CVFTABLE + C

VFTABLE + 10VFTABLE + 10

VFTABLE PointerVFTABLE Pointer

SHELLCODESHELLCODE

CALL/JMP ECXCALL/JMP ECX

ECX – Original Object

ECX – Original Object

PointerPointer

Continue– Automation

Page 10: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

10

We can now override the second VFTABLE!!!

10

Code Injection – Double Inheritance

Multiple InheritanceClass_A::vfunc_A1Class_A::vfunc_A1

Inherited::Class_A VFTable

Inherited::Class_A VFTableObject’s memoryObject’s memory

vfunc_A1 addressvfunc_A1 address

vfunc_A2 addressvfunc_A2 addressA VFTABLE PointerA VFTABLE Pointer

member_of_Amember_of_A

Assembly codeAssembly code

Inherited::vfunc_A2Inherited::vfunc_A2

Assembly codeAssembly code

Class_B::vfunc_B1Class_B::vfunc_B1Inherited::Class_B

VFTable Inherited::Class_B

VFTable

vfunc_B1 addressvfunc_B1 address

vfunc_B2 addressvfunc_B2 address

B VFTABLE PointerB VFTABLE Pointer

member1_of_Bmember1_of_B

Assembly codeAssembly code

Inherited::vfunc_B2Inherited::vfunc_B2

Assembly codeAssembly code

Member2_of_BMember2_of_B

Class A

Class B

class Inherited: public Class_A, public Class_B{public:

virtual int vfunc_A2();virtual int vfunc_B2();

};

class Inherited: public Class_A, public Class_B{public:

virtual int vfunc_A2();virtual int vfunc_B2();

};

Page 11: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

11

What is a Dangling Pointer?

Code Injection

Object Overriding

Demonstrations

Remediation

Summary

Q&A

Where are We

Page 12: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

1212

Object Overriding

Allocation Implementation– Numerous heaps

• Two Default heaps• Different API • C-Runtime functions

– Malloc– Free– New– Delete– Etc.

Page 13: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

13

A De-Allocated BufferA De-Allocated Buffer

Next Buffer PointerNext Buffer Pointer

Another De-Allocated Buffer

Another De-Allocated Buffer

13

Object Overriding

Allocation implementation details– Lookaside List

• A list for each size (8-1024) (8) and for each heap• First Allocation Priority• Merges

NULLNULL

Lookaside list base pointer

Lookaside list base pointer

40 Bytes

40 Bytes

40 Bytes

40 Bytes

Page 14: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

1414

Object Overriding

And Finally – Overriding– Search for Allocations

• Static Analysis– Method: Disassembly– Restriction: Static Size– Validation: Controllable Content– Usage: Causing the Allocation

• Dynamic analysis– Method: API Breakpoints– Restriction: Static/Dynamic Size– Validation: Controllable Content

Page 15: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

15

New BufferNew BufferSHELLCODE

Rest of SHELLCODE

SHELLCODE

Rest of SHELLCODE

VFTABLE + 8

VFTABLEVFTABLE

Pointer

Object Overriding – The VFTABLE Exploit

Exploitation:– Empty the Lookaside List– Allocate a Buffer– Insert Content– Free the Buffer

VFTABLE PointerVFTABLE Pointer

CALL/JMP EAXCALL/JMP EAX

Original ObjectOriginal Object

Continue:– Free the Object– Execute a VFunc

NULL

Page 16: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

16

The De-Allocated Buffer

The De-Allocated Buffer

A Function Pointer

A Function Pointer

……

The De-Allocated Object

The De-Allocated Object

A VFTABLE Pointer

A VFTABLE Pointer

……

The Shellcode Buffer

The Shellcode Buffer

NULLNULL

ShellcodeShellcode

16

Object Overriding – The Lookaside Exploit

Empty the Lookaside

Allocate Two Buffers

Insert Shellcode

Free One Buffer

Free The Other

Free The Object

Execute the Destructor

GAME OVER!!!

Page 17: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

1717

Object Overriding – The Lookaside Exploit

Executing NULL – NO Problem

Page 18: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

18

Summary

Summary– Double Reference

• Controllable First DWORD• Static Address

– VFTABLE Exploit• Controllable Allocations• No First DWORD• Static Address

– Lookaside Exploit• Controllable Allocations• No First DWORD• No Static Address• Destructor Execution

Page 19: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

19

What is a Dangling Pointer?

Code Injection

Object Overriding

Demonstrations

Remediation

Summary

Q&A

Where are We

Page 20: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

20

Demonstrations – Configuration Item

Allocating the Object

De-Allocation the Object

Page 21: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

21

Demonstrations – Configuration Item

Allocating User Data

Page 22: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

22

Demonstrations – Configuration Item

Executing a VFunc

Page 23: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

23

Demonstrations – Configuration Item

Putting it Together– De-Allocate– Re-Allocate– Execute

Page 24: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

24

Demonstrations – Remote Exploit

Another Exploit on IIS, but this time – a remote one

Page 25: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

25

What is a Dangling Pointer

Code Injection

Object Overriding

Demonstrations

Remediation?

Summary

Q&A

Where are We

Page 26: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

26

Remediation

Known Protection Mechanisms– NX Bit– ASLR

VFTABLE Sanitation

Safe Programming

Page 27: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

27

Summary

Technical Background– Memory Allocations– Objects Implementation

Exploits– Double Reference Exploit– VFTABLE Exploit– Lookaside Exploit

Demonstrations– Configuration Item– Remote Exploit

Dangling Pointer– Only Object Oriented Objects

Page 28: Dangling Pointer - Black Hat | Home · 2015-05-28 · Pointer. Pointer. CALL/JMP EAX. CALL/JMP EAX. Original Object. Original Object z. Continue: – Free the Object – Execute a

28

Questions

Ask Away…