Slide 1

Slide 1 text

Reverse Engineering and Attacking .NET Applications Topher Timzen

Slide 2

Slide 2 text

Topher Timzen Security Researcher, Intel @TTimzen TopherTimzen.com #whoami

Slide 3

Slide 3 text

Overview Intro to the .NET Reverse Engineering .NET Attacking .NET Runtime Defense Strategies Next Steps

Slide 4

Slide 4 text

Intro to .NET

Slide 5

Slide 5 text

What is .NET Common Language Runtime (CLR) • Manages execution of .NET programs. • Responsible for Just-in-time compilation, Memory management, type safety, garbage collection, etc. • CLR 2.0 and CLR 4.0 are both widely used. • Open Sourced (kind-of) http://referencesource.microsoft.com/ Common Intermediate Language (CIL) • Independent set of instructions converted to native machine language through implicit complication. • Memory is allocated for the assembly per JIT. • Marked as RWX

Slide 6

Slide 6 text

C# v. CIL v. ASM

Slide 7

Slide 7 text

C# v. CIL v. ASM

Slide 8

Slide 8 text

C# v. CIL v. ASM

Slide 9

Slide 9 text

Runtime .NET Process CLR (2.0/4.0) & AppDomains Assemblies (.EXE and .DLL(s)) Objects Properties Fields Instance Methods Classes Methods Logic

Slide 10

Slide 10 text

Reverse Engineering .NET

Slide 11

Slide 11 text

Tools of the Trade SOS.DLL & WinDbg DnSpy De4dot Runtime Reflection

Slide 12

Slide 12 text

WinDbg & SOS The best one liner in history (for .NET) Loads Son of Strike (SOS.DLL) !for_each_module .if(($sicmp( "@#ModuleName" , "mscorwks") = 0) ) {.loadby sos mscorwks} .elsif ($sicmp( "@#ModuleName" , "clr") = 0) {.loadby sos clr}

Slide 13

Slide 13 text

JIT Transformation of IL to native ASM Only JITs when a method is called! • Objects Garbage Collected unless pinned – Everything in .NET is System.Object (Remember Java?) • We can use this to our advantage later.

Slide 14

Slide 14 text

JIT WinDbg helps us view JIT patterns 1) !dumpdomain 2) !dumpmodule –mt 3) !dumpmt –md PreJIT is native .NET None = not yet JIT JIT = JIT’d

Slide 15

Slide 15 text

JIT WinDbg helps us view JIT patterns !dumpdomain

Slide 16

Slide 16 text

JIT WinDbg helps us view JIT patterns !dumpmodule –mt

Slide 17

Slide 17 text

JIT WinDbg helps us view JIT patterns !dumpmt –md

Slide 18

Slide 18 text

Hooking JIT Mscorjit/Clrjit.dll Can hook jit.dll::compileMethod VTable entry Useful to determine code flow / event tracing https://github.com/UbbeLoL/SJITHook

Slide 19

Slide 19 text

Managed Heap Storage point for .NET Objects New reference objects added to heap Garbage Collector removes dead objects http://www.tophertimzen.com/blog/dotNetHeapObjects/

Slide 20

Slide 20 text

Runtime Objects All objects are pointers to a location on the Managed Heap. !dumpheap !dumpheap –mt !dumpobj

Slide 21

Slide 21 text

System.Reflection View metadata of AppDomains & Assemblies Contains method body, IL, Method signature, classes, type information, etc. • Ability to view all metadata! Essentially every .NET RE tool uses reflection as primary means of obtaining metadata

Slide 22

Slide 22 text

Reflection https://github.com/tophertimzen/reflectionDemo

Slide 23

Slide 23 text

DnSpy https://github.com/0xd4d/dnSpy Branded as an “.NET assembly editor, decompiler, and debugger” Great for IL Editing – On-Disk Attacking

Slide 24

Slide 24 text

What .NET Packers Do Obfuscated .NET is still object-oriented and contains all needed metadata. Static DEobfuscators work well with .NET as .NET obfuscators/packers cannot really do a whole lot. • Really just refactor code to be ugly.

Slide 25

Slide 25 text

How to Get Around Them System.Reflection.Assebmly.Load is used to decompress/decrypt the assembly before running it. • Breakpoint at entry -> dump code

Slide 26

Slide 26 text

How to Get Around Them .cctor is used to init itself (static constructor) and is called for each Type. Unpacking can occur at .cctor as it is called before static members are referenced or before the first instance object is created. Program::Main() executes Program::.cctor() beforehand. Breakpoint at ::.cctor() -> dump code

Slide 27

Slide 27 text

de4dot De4dot does this for us. Contains 21 deobfuscator modules. de4dot program.exe -o program_deobfuscated.exe Not rolled into DnSpy. Gray Wolf has this native.

Slide 28

Slide 28 text

Attacking .NET Applications in Memory

Slide 29

Slide 29 text

Types of Attacks CLR to abuse raw objects on managed heap Manipulate AppDomains to control loaded code and Just-In-Time Compilation Attack with ASM due to RWX JIT • Alter control flow Post-exploitation Techniques or Local Binaries

Slide 30

Slide 30 text

Tools https://github.com/GrayKernel Gray Frost & Gray Storm .NET CLR Bootstrapper and Memory Attack Platform

Slide 31

Slide 31 text

Gray Frost 2 round payload delivery system C++ .NET CLR Bootstrapper Creates or injects 4.0 runtime Pivots into 2.0 runtime if needed Contains raw payload for round 2

Slide 32

Slide 32 text

Gray Storm Reconnaissance and In-memory attack payload Features Attacking the .NET JIT Attacking .NET at the ASM level Utilize objects on the Managed Heap

Slide 33

Slide 33 text

Method Table Hijack Method Tables contain address of JIT stub for a class’s methods. During JIT the Method Table is referenced We can control the address

Slide 34

Slide 34 text

ASM Payloads Address of a method known through Reflection Overwrite method logic with new ASM

Slide 35

Slide 35 text

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 http://www.tophertimzen.com/blog/dotNetHeapObjects/

Slide 36

Slide 36 text

CLRMD Microsoft.Diagnostics.Runtime.CLRMD Makes object hunting a lot easier but increases overhead. • Practical for game hacking, attacks that can touch disk, etc. Object Control = Power

Slide 37

Slide 37 text

Automation

Slide 38

Slide 38 text

Automation GrayFrost can be used with automated payloads

Slide 39

Slide 39 text

Application Weaknesses

Slide 40

Slide 40 text

Key Weaknesses in .NET Due to runtime reflection you cannot store any secrets • All code is viewable! Update systems are easy to manipulate • Ensure app is using secure update mechanisms • See MitM KeePass Licensing Mechanisms Poor Communications Security

Slide 41

Slide 41 text

Defense Techniques

Slide 42

Slide 42 text

Reversing Just add this to EULA. Hackers listen. and read EULA. DESCRIPTION OF OTHER RIGHTS AND LIMITATIONS. Limitations on Reverse Engineering, Decompilation, Disassembly and change (add,delete or modify) the resources in the compiled the assembly. You may not reverse engineer, decompile, or disassemble the SOFTWARE PRODUCT, except and only to the extent that such activity is expressly permitted by applicable law notwithstanding this limitation.

Slide 43

Slide 43 text

EMET Attack Surface Reduction (ASR) prevents DLL Injection • Mostly dead technique with modern mitigations. – Requires local access.

Slide 44

Slide 44 text

Defensive Code Write good Defensive .NET Applications • Depends what you want to protect. Don’t expose secrets If you want to pretend to hide code use a C++ DLL and import it

Slide 45

Slide 45 text

Conclusion

Slide 46

Slide 46 text

More .NET at ToorCamp! Jon McCoy Friday June 10th @ 16:00 Workshop • Hacking .NET/C# Applications: Hands on Black Arts • Today @ 1700

Slide 47

Slide 47 text

Questions? Contact Me @TTimzen https://www.tophertimzen.com Get Gray Frost and Storm github.com/graykernel