71
Hijacking Arbitrary .NET Application Control Flow Topher Timzen

Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Hijacking Arbitrary .NET Application Control Flow

Topher Timzen

Page 2: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Security Researcher, Intel

Security Trainer TopherTimzen.com

@TTimzen

#whoami

Page 3: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Overview

.NET?

Runtime Attacks

Modify Control Flow

Machine Code Editing

Managed Heap

Page 4: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Tools Released

Use .NET to attack

Using Objects on the Heap

Why are we Here?

Page 5: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

CLR Attacks

Controlling the Common Language Runtime

Accessing raw objects on Managed Heap

Manipulate AppDomains

• Controlling all Loaded Code

• Controlling Just-In-Time Compilation

Page 6: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Attack With ASM

Manipulate Resources

Attack methods at ASM level

Alter application control flow

Page 7: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Runtime .NET Process

CLR (2.0/4.0) & AppDomains

Assemblies (.EXE and .DLL(s))

Objects

Properties

Fields

Instance Methods

Classes

Methods

Logic

Page 8: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Frost &

Gray Storm

The Tools

Page 9: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Frost

Page 10: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Frost Payload delivery system

C++ .NET CLR Bootstrapper

Creates or injects 4.0 runtime

Capability to pivot into 2.0 runtime

Contains raw payload

2 Rounds

GrayFrostCpp

GrayFrostCSharp

• C# Payload

Page 11: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 1

.NET Process

Page 12: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 1

Mscoree

GrayFrostCpp

Page 13: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 1 GrayFrostCpp

Page 14: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 1

GrayFrostCSharp

GrayFrostCpp

Page 15: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 2

.NET Process

Page 16: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 2

.NET Process

GrayFrostCSharp

Page 17: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 2

.NET Process

payload void main()

GrayFrostCSharp

Page 18: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Round 2

.NET Process

Payload

Page 19: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

.NET Process

Pivoting Between runtimes

Page 20: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Mscoree

GrayFrostCpp

Pivoting Between runtimes

Page 21: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCpp

Pivoting Between runtimes

Page 22: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCSharp

GrayFrostCpp

Pivoting Between runtimes

Page 23: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCSharp

GrayFrostCpp

Pivoting Between runtimes

Page 24: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCpp

Pivoting Between runtimes

Page 25: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCSharp

GrayFrostCpp

Pivoting Between runtimes

Page 26: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

GrayFrostCSharp

GrayFrostCpp

Pivoting Between runtimes

Page 27: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Storm

Page 28: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Storm Reconnaissance and In-memory attack

payload

Features

Attacking the .NET JIT

Attacking .NET at the ASM level

ASM and Metasploit payloads

Utilize objects on the Managed Heap

Page 29: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Gray Storm Usage

Page 30: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Controlling the JIT

Method Tables contain address of JIT stub for a class’s methods.

During JIT the Method Table is referenced

We can control the address

Lives after Garbage Collection

Page 31: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Controlling the JIT

Page 32: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Controlling the JIT

Page 33: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Control Flow Attacks .NET uses far and relative calls

0xE8; Call [imm]

0xFF 0x15; Call dword segmentRegister[imm]

relCall = dstAddress - (currentLocation+ lenOfCall)

Page 34: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

ASM Payloads Address of a method known through

Reflection

Overwrite method logic with new ASM

Steal stack parameters

Change events

Page 35: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

ASM Payloads

Change return TRUE to return FALSE

Password validation

Key & Licensing validation

SQL Sanitization

Destroy security Mechanisms

Overwrite logic

Update Mechanisms

Page 36: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

ASM Payloads

Page 37: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

ASM Payloads

Metasploit

Hand Rolled

Portable Environment Block (PEB) changes

Page 38: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Portable Environment Block

http://www.tophertimzen.com/blog/shellcodeDotNetPEB/

Page 39: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Object Hunting in Memory

Page 40: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Managed Heap

Storage point for .NET Objects

New reference objects added to heap

Garbage Collector removes dead objects

Page 41: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Managed Heap

Storage point for .NET Objects

New reference objects added to heap

Garbage Collector removes dead objects

Let’s manipulate it!

Page 42: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Object Hunting in Memory

Objects are IntPtrs

Point to Object Instance on Managed Heap

All instantiated objects of the same class share the same Method Table

Reflection Object Hunting

Win

Page 43: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 44: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 45: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Construct an Object

Use Reflection to invoke a constructor

Can instantiate any object

If a constructor takes other objects, nullify them

https://gist.github.com/tophertimzen/010b19fdbde77f251414

Page 46: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

IntPtr = 024e9fe8

024e9fe8 (Object)

00000005

00000001

00000000

IntPtr = 5

STACK

024e9fe8 (Object) L H

https://gist.github.com/tophertimzen/812aa20dbe23cb42756d

Find location of Managed Heap

Page 47: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

IntPtr = 024e9fe8

024e9fe8 (Object)

00000005

00000001

00000000

IntPtr = 5

STACK

Managed Heap 024e9fe8 (Object) L H

https://gist.github.com/tophertimzen/812aa20dbe23cb42756d

Find location of Managed Heap

Page 48: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

IntPtr = 024e9fe8

024e9fe8 (Object)

00000005

00000001

00000000

IntPtr = 5

STACK

024e9fe8 (Object) L H

https://gist.github.com/tophertimzen/812aa20dbe23cb42756d

Find location of Managed Heap

Page 49: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

IntPtr = 024e9fe8

024e9fe8 (Object)

00000005

00000001

00000000

STACK

L H

https://gist.github.com/tophertimzen/812aa20dbe23cb42756d

Find location of Managed Heap

Page 50: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 51: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Signature instantiated type

Object Instances contain a Method Table pointer to their corresponding type.

(x86)

Bytes 0-3 are the Method Table (MT)

Bytes 4-7 in MT is Instance Size

0:009> dd 024e9fe8 024e9fe8 00774828 0000038c 00000001 00000000

Page 52: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Signature instantiated type

Object Instances contain a Method Table pointer to their corresponding type.

(x64)

Bytes 0-7 are the Method Table (MT)

Bytes 8-11 in MT is Instance Size

0:008> dd 00000000024e9fe8 00000000`0286b8e0 ea774828 000007fe

Page 53: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 54: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Scan Managed Heap

Scan down incrementing by size of object

Scan linearly up to top of heap

Compare object’s Method Table to the reference

If they match, get IntPtr address of object

Page 55: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 56: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Convert object ptr -> raw obj

STACK

Refer (System.IntPtr)

pointer(024ea00c ) pointer(024ea00c )

L H

https://gist.github.com/tophertimzen/1da2b0aab6245ed1c27b

Page 57: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Convert object ptr -> raw obj

STACK

Refer (System.IntPtr)

pointer(024ea00c )

pointer(024ea00c ) L H

https://gist.github.com/tophertimzen/1da2b0aab6245ed1c27b

Page 58: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Convert object ptr -> raw obj

Refer (GrayStorm.testClass)

pointer(024ea00c )

STACK

L H

https://gist.github.com/tophertimzen/1da2b0aab6245ed1c27b

Page 59: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Finding Objects at Runtime

i. Construct an object and find location of Managed Heap

ii. Signature instantiated type

iii. Scan Managed Heap for object pointers

iv. Convert object pointers to raw objects

v. ????

vi. PROFIT

Page 60: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

????

Page 61: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

PROFIT

Page 62: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Superpowers and Things?

Change Keys

Change Fields / Properties

Call Methods

With arguments!

Page 63: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Automation

Page 64: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Automation

GrayFrost can be used with automated payloads

Page 65: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Constructing Attack Chains

Page 66: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

How to construct attack chains

Gray Wolf / IL Decompiler Find Methods, Fields & Properties of

interest Locate meaningful objects Discover high level control flow

Gray Storm “Debugging” functionality Breakpoint at constructors or methods

from Method Pointers Use with WinDbg

Utilize DLL Hijacking!

Page 67: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Hybrid .NET/ASM Attacks

Hybrid C#/ASM code in .NET

Encrypting .NET payloads and unwinding

Encrypting ASM Payloads

Page 68: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Payload System

C# is easy

Can use Gray Frost in any application

Low and High level gap is easy

Page 69: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

.NET Hacking Space

Small

Few tools

Mostly hacking WoW

Lots of PowerShell

Previous DEF CON talks

DEF CON 18 & 19 - Jon McCoy

Page 70: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Conclusion

Arbitrary .NET applications can be injected and changed

New .NET attack possibilities

New tools that support automation

Get Gray Frost and Storm

github.com/graykernel

Page 71: Hijacking Arbitrary .NET Application Control Flow CON 23/DEF CON 23... · 2020. 5. 16. · CLR Attacks . Controlling the Common Language Runtime . Accessing raw objects on Managed

Questions? Contact Me

@TTimzen

https://www.tophertimzen.com

Get Gray Frost and Storm

github.com/graykernel

White Papers

Hijacking Arbitrary .NET Application Control Flow

Acquiring .NET Objects from the Managed Heap