net malware threat: internals and reversing … con 27/def con 27 presentations/defcon-27... ·...

99
1 .NET MALWARE THREAT: INTERNALS AND REVERSING DEF CON USA 2019 DEF CON USA 2019 by Alexandre Borges ALEXANDRE BORGES MALWARE AND SECURITY RESEARCHER

Upload: others

Post on 07-Sep-2019

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

1

.NET MALWARE THREAT: INTERNALS AND

REVERSING

DEF CON USA 2019

DEF CON USA 2019

by Alexandre Borges

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

Page 2: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

DEF CON USA 20192

9 Malware and Security Researcher. 9 Speaker at DEF CON USA 20189 Speaker at DEF CON China 20199 Speaker at CONFidence Conference

2019 (Poland)9 Speaker at HITB 2019 Amsterdam9 Speaker at BSIDES

2019/2018/2017/20169 Speaker at H2HC 2016/20159 Speaker at BHACK 20189 Consultant, Instructor and Speaker on

Malware Analysis, Memory Analysis, Digital Forensics and Rootkits.

9 Reviewer member of the The Journal of Digital Forensics, Security and Law.

9 Referee on Digital Investigation: The International Journal of Digital Forensics & Incident Response

Agenda:

� Introduction� Managed executable structures� CLR and Assembly Loader details� .NET internals metadata� Modules, assemblies and manifest� .NET program structures� Malicious code through MSIL� .NET debugging� Few GC and synchronous aspects� Conclusion

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

Page 3: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 20193

9 Last talks in conferences:

9 CONFidence Conference 2019:9 https://confidence-conference.org/2019/bio.html#id=374869 slides:

http://www.blackstormsecurity.com/CONFIDENCE_2019_ALEXANDRE.pdf

9 DEF CON China 2019:9 https://www.defcon.org/html/dc-china-1/dc-cn-1-speakers.html#Borges 9 slides:

http://www.blackstormsecurity.com/docs/DEFCON_CHINA_ALEXANDRE.pdf

9 HITB Amsterdam 2019: https://conference.hitb.org/hitbsecconf2019ams/speakers/alexandre-borges/

9 slides: http://www.blackstormsecurity.com/docs/HITB_AMS_2019.pdf

9 DEF CON USA 2018: 9 https://www.defcon.org/html/defcon-26/dc-26-speakers.html#Borges 9 slides: http://www.blackstormsecurity.com/docs/DEFCON2018.pdf

9Malwoverview Tool: https://github.com/alexandreborges/malwoverview

Page 4: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

INTRODUCTION

DEF CON USA 20194

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

Page 5: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 20195

9 Motivations to this talk about .NET reversing and internals:

9 Most of the time, professionals are interested in unpacking embedded resources from a .NET sample.

9 In another moment, the concern is dumping the unpacked binary from memory.

9 Sometimes, we have looked for any unpacking routine to dynamically unpack the encrypted content.

9 All of these actions are correct and recommended.

9 However....

9 Many people don’t understand .NET metadata components.

9 Most people based their analysis on the decompiled code, but never on IL.

9 Malware’s authors have manipulated the IL to attack the system and even the .NET runtime.

Page 6: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 6

9 There are many available methods to infect a system using .NET malware. Most of the time, a .NET code decrypts and loads a native code (or injects a code into a target process).

9 However, there are few approaches that use indirect techniques:

9 An e-mail comes from the Internet and a first dropper is downloaded. 9 This dropper fetches a encrypted payload, which contains a native payload

and a managed code.9 The payload 1 executes and injects a DLL into a remote chosen process. 9 This DLL loads (and sometime decrypts) the malicious managed code. 9 The malicious managed code drops the payload 2 (real and advanced).9 The true infection starts.

dropper(unmanaged)

payload 1(unmanaged)

vector(managed)

inject a DLL in a remote process

payload 2

Infection

Page 7: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 20197

9 It is not necessary to comment about how to inject a code because the steps are the same ever-sequence:

9 CreateToolhelp32Snapshot( ) ÆModule32First( ) ÆModule32Next( ) Æcomparison (wcscmp( ))

9 VirtualAllocEx( ) ÆWriteProcessMemory( ) Æ CreateRemoteThread( ) ÆWaitForSingleObject Æ VirtualFreeEx( ).

9 Find the offset of injected DLL from the base module (any testing module).

9 Use this offset to invoke functions from any injected remote process through GetProcessAddress( ) + CreateRemoteThread( ).

9 Thi injected DLL can load the next stage and, eventually, decrypt it.

9 Obviously, the .NET managed code can be loaded from any process or, even worse, from an natived injected code (DLL).

9 After loading it, it is easy to execute it. Our simple case above.

Page 8: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 20198

9 We should remember that a typical native application can also load a .NET runtime and execute a managed code:

9 CLRCreateInstance( ): provides the ICLRMetaHost interface.

9 ICLRMetaHost::GetRunTime( ): gets the ICLRRuntimeInfo.

9 ICLRRuntimeInfo::GetInterface( ): Loads the CLR into the current process and returns runtime interface pointers.

9 ICLRRuntimeHost::ExecuteApplication( ): specifies the application to be activated in a new domain.

9 ICLRRuntimeHost::Start( ): starts the the runtime.

9 ICLRRuntimeHost::ExecuteInDefaultAppDomain( ): invokes a method in the .NET managed assembly (this steps does not work for all .NET assembly’s method). Thus, in this case, starts the managed assembly. -

9 Finally, the real infection starts. -

Page 9: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 20199

9 We know that .NET malware samples can be very complicated to analyze when packed and obfuscated. -

9 The .NET framework is composed by:

9 CLR (Common Language Runtime), which is the .NET engine. 9 Libraries (System.IO, System.Reflection, System.Collections, ...).

9 Basically:

9 source code is written in C#, F#, VB.NET and Powershell.9 compiled to CLI (Common Language Infrastruture Code).9 executed by the CLR.

9 Tools used to reverse and analyze .NET malware threats are completely different than ones used to reverse native language:

9 dnSpy (excellent)9 ILSpy (excellent)9 RedGate .NET Reflector9 De4dot (deobfuscator)9 Microsoft Visual Studio

9 WinDbg (including SOS.dll extension)9 DotPeek9 IDA Pro9 Microsoft ILASM/ILDASM (Intermediate

Language Assembly/Disassembler)

Page 10: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201910

9 Other interesting tools to analyze and understand .NET runtime are available:

9 MemoScope.Net: https://github.com/fremag/MemoScope.Net9 Shed -- a .NET runtime inspector: https://github.com/enkomio/shed9 SuperDump, for automated crash dump analysis:

https://github.com/Dynatrace/superdump9 DumpMiner: https://github.com/dudikeleti/DumpMiner9 MemAnalyzer: https://github.com/Alois-xx/MemAnalyzer9 Sharplab: https://sharplab.io/9 ObjectLayoutInspector to analyze internal structures of the CLR types at

runtime (https://github.com/SergeyTeplyakov/ObjectLayoutInspector)

9 Tools are excellent to help us, but most .NET malware threats have deployed the same tricks from native code to make our job harder: packers, obfuscation and anti-reversing techniques.

9 .NET Reactor9 Salamander .NET Obfuscator9 Dotfuscator9 Smart Assembly9 CryptoObfuscator for .NET

9 Agile9 ArmDot9 babelfor.NET9 Eazfuscator.NET9 Spice.Net9 Skater.NET9 VM Protect 3.40

Page 11: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201911

9 There are many obfuscators, which perform:

9 Control flow obfuscation and dead/junk code insertion. 9 Renaming: methods signatures, fields, methods implementation, namespaces,

metadata and external references. 9 Re-encoding: changing printable to unprintable characters9 Simple encryption of methods and strings. 9 Cross reference obfuscation.

9 Yes, I know... I’ve already talked about de-obfuscation in DEF CON China 2019. -

9 Most time, the real and encoded malicious code (payload) is downloaded and decrypted/loaded into the memory for execution:

9 System.Reflection.Assembly.Load( )9 System.Reflection.Assembly.LoadFile()9 System.Reflection.MethodInfo.Invoke( )

9 As we already know, Load( )/LoadFile( ) function are usually followed by:

9 GetType ( ) Æ GetMethod( ) Æ Invoke( ) (this is a typical Reflection approach)

Page 12: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201912

9 Another possible approach would be:

9 GetAssemblyName( ) + GetType( ) + GetMethod( ) + Invoke( )

9 Some “encrypted content” is loaded from as a resource, so it is usual finding the following sequence:

9 FindResource( ) + SizeOfResource( ) + LoadResource( ) + LockResource( )9 Resources.ResourceManager.GetObject( )

9 Additionally, we’ve seen techniques using embedded references such as DLLs as resources through a sequence of calls using:

9 AssemblyLoader.Attach( ) + AssemblyLoader.ResolveAssembly( ).

9 As you’ve guessed, AssemblyLoader.ResolveAssembly( ) is used to resolving assemblies that are not available at the exact time of calling other methods, which are external references to the binary itself.

9 Similar to resources mentioned previously, these “external references” (DLL required by the binary) must be first “decompressed” at most of the time.

Page 13: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201913

Page 14: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201914

Page 15: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 15

MemberRef Table(check slide 27)

Page 16: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201916

Manifest

Page 17: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201917

9 As every single malware code, this one is using Reflection to retrieve information in runtime. In this case also calls the GetExecutingAssembly( ) method to get the Assembly object, which represents the current assembly.

Page 18: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201918

9 Therefore, we can extract these resources (DLLs, for example) by using either dnSpy or ILSpy , decrypt and load them again into the managed code.

9 Of course, in this case, we’ll be able to see all “hidden” references, finally. -

9 To load the “decrypted” resources into the managed code, we can use ILSpy + Reflexil plugin (http://reflexil.net/).

9 Finally, it is necessary to remove the “old” references to the embedded resources (performed by AssemblyLoader.Attach( )) from the initializer (or removing the whole initializer) because, at this time, they are “decrypted”.

9 By the way, Reflexil is able to handle different obfuscators such as Babel NET, CodeFort, Skater NET, SmartAssembly, Spices Net, Xenocode, Eazfuscator NET, Goliath NET, ILProtector, MaxtoCode, MPRESS, Rummage, CodeVeil,CodeWall, CryptoObfuscator, DeepSea, Dotfuscator, dotNET Reactor, CliSecure and so on.

9 Another interesting point is that calling Win32 APIs through P/Invoke (Platform Invoke) is always possible.

9 At end, gaining knowledge in .NET internals and metadata can be interesting.

Page 19: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201919

9 Most time, there are module/type initializers (similar to TLS in native code) executing before classes and entry point methods.

9 .NET protectors hardly change the entry point and, usually, the trick is in the initializer.

9 .cctor( ) method is a static class constructor:

9 called before the Main( ) method (usually set as entry point), for example.

9 when the module has a .cctor (<Module>::.cctor( )), so it is run before executing any other class initializers or even an entry point.

9 It is common finding unpackers, decrypters and hooks in the .cctor( ) method.

9 Hijacking the ICorJitCompiler::compileMethod( ) is an interesting and useful way to take the control of the JIT engine because this method is used to create a native code, so we find managed and native code together. -

9 In this case: .cctor( ) Æ hooking compileMethod( ) Æ hiding/encryting user code.

Page 20: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

.NET details

DEF CON USA 201920

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

Page 21: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201921

9 Malware authors have written malware threats in ILAsm and using constructions that are not compliance to CLS (Common Language Specification).

9 In this case, the malware is valid to the runtime engine, though it is not always able to communicate to other applications.

9 Metadata works as descriptors for each structure component of the application: classes, attributes, members, and so on.

9 Remember that a .NET application is composed by:

9 managed executable files, which each one contains metadata9 managed code (optionally)

9 .NET Assembly: managed .NET application (modules) + class libraries + resources files (more information later)

9 CLR runtime environment: loaders + JIT compiler.

9 .NET source code Æ .NET compiler Æmodule (IL + metadata) Æ CLR ( loaders + JIT compiler) Æ native instruction Æ Execution Engine

Page 22: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201922

9 Managed module is composed by:

9 PE header: If the module contains only IL code, so most of information of header is ignored. However, if the module also contains native code, so things are different. -

9 CLR header: contains the version of the CLR, token of Main( ) (natural entry poiint), resources and so on.

9 Metadata: describe types and members. Additionally, it helps the GC to track the life time of objects. -

9 IL (Intermediate Language) code: the managed code.

Managed Modules

Resource Files

Compiler (C#, VB, F#) + Linker

Managed Modules

Resource Files

Manifest

.NET Assembly(.exe or .dll)

Page 23: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201923

PE Header

Native Code / Data

CLR Header

CLR Data(ILcode, metadata,

managed resources)

DOS Header

PE Header

Data Directories(size and location of CLR header)

Section Headers

.text(includes MSIL and metadata)

.idata

.data

Remaining sections

Page 24: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

DEF CON USA 201924

9 CLR Header is represented by a structure named IMAGE_COR20_HEADER (defined in CorHdr.h).

9 The CLR header + data holds information such as:

9 Metadata information (size and RVA).

9 Token of the entry point of the image file (EntryPointTokenField).

9 Array of v-table fixup Table : used when a managed method is called from an unmanaged code. Each entry of a v-table holds a token of the target method.

9 Resource information (size and RVA)

9 Flags indicating:

9 32 bits only. 9 strong assembly9 entrypoint of a native code.

9 Managed resources in contained into .text section (and not .rsrc section).

Page 25: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201925

9 Metadata is composed by tables such as:

9 Definition tables: ModuleDef, TypeDef, MethodDef, FieldDef, ParamDef, PropertyDef and EventDef

9 Reference tables: AssemblyRef, ModuleRef, TypeRef and MemberRef.

9 Manifest tables: AssemblyDef, FileDef, ManifestResourceDef and ExportedTypesDef.

9 Most malicious .NET malware samples have:

9 Used code manipulation (encryption/decryption) in .ctor( )/.cctor( )/Finalize( )

9 Called unmanaged functions from DLLs using P/Invoke.

9 Used COM components (very usual).

9 Even a trivial .NET malware, the managed code can be small. - (ILDasm.exe ÆView Æ Statistics)

Page 26: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201926

9 ILDasm Æ View ÆMetaInfo Æ Show! menu:

Page 27: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201927

9 ILDasm.exe Æ View Æ Statistics

Page 28: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 28

9 Metadata describes all declared or referenced data in a module such as classes, members, attributes, properties and relationships.

9 Metadata is organized as a relational database using cross-references and making possible to find what class each method comes from.

9 Metadata are represented by named streams, which are classified as metadata heaps and metadata tables.

slot 1: Class A -- methods at slot 1

slot 2: Class B -- methods at slot 3

slot 3: Class C -- methods at slot 5

slot 4: Class D -- methods at slot 6

slot 5: Class E -- methods at slot 8

slot 1: Method 1 - Classe A

slot 2: Method 2 - Classe A

slot 3: Method 1 - Classe B

slot 4: Method 2 - Classe B

slot 5: Method 1 - Classe C

slot 6: Method 1 - Classe D

slot 7: Method 2 - Classe D

slot 8: Method 1 - Classe E

Page 29: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201929

9 Metadata heaps:

9 GUID heap: contains objects of size equal to 16 bytes.9 String heap: contains strings. 9 Blog heap: contains arbitrary binary objects aligned on 4-byte boundary.

9 There can be 6 named streams:

9 #GUID: contains global unique identifiers. 9 #Strings: contains names of classes, methods, and so on. 9 #US: contains user defined strings. 9 #~: contains compressed metadata stream. 9 #-: contains uncompressed metadata stream. 9 Blob: contains metadata from binary objects.

9 An important note: compressed and uncompressed named streams are mutually exclusive.

9 Metadata heaps:

9 The schema defines the metadata tables by usings a descriptor. 9 There are more than 40 metadata tables.

Page 30: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201930

9 Tokens have 4 bytes, which the first byte determines the metadata table and the three remaining bytes are the RID.

9 RID (record identifiers) are used as row indexes in metadata tables.9 Tokens determines which metadata tables are being referred.

9 Unfortunately, tokens don’t cover all tables (auxiliary tables, which are hardcoded).

0(0x0): Module1(0x1): TypeRef2(0x2): TypeDef3(0x3): FieldPtr4(0x4): Field5(0x5): MethodPtr6(0x6): Method7(0x7): ParamPtr8(0x8): Param9(0x9): InterfaceImpl10(0xa): MemberRef11(0xb): Constant12(0xc): CustomAttribute13(0xd): FieldMarshal14(0xe): DeclSecurity15(0xf): ClassLayout

16(0x10): FieldLayout17(0x11): StandAloneSig18(0x12): EventMap19(0x13): EventPtr20(0x14): Event21(0x15): PropertyMap22(0x16): PropertyPtr23(0x17): Property24(0x18): MethodSemantics25(0x19): MethodImpl26(0x1a): ModuleRef27(0x1b): TypeSpec28(0x1c): ImplMap29(0x1d): FieldRVA30(0x1e): ENCLog31(0x1f): ENCMap

32(0x20): Assembly33(0x21): AssemblyProcessor34(0x22): AssemblyOS35(0x23): AssemblyRef36(0x24): AssemblyRefProcessor37(0x25): AssemblyRefOS38(0x26): File39(0x27): ExportedType40(0x28): ManifestResource41(0x29): NestedClass42(0x2a): GenericParam43(0x2b): MethodSpec44(0x2c): GenericParamConstraint

Page 31: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201931

9 ILDasm.exe Æ View Æ Statistics

Page 32: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201932

9 IDAasm Æ View ÆMetaInfo Æ RawHeap9 ILDasm Æ View ÆMetaInfo Æ Show!

Page 33: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201933

9 The JIT compiler stores the native instructions in the memory, which it is discarded after the application terminates. Eventually, we can perform a memory dump to examine this code. -

9 Check the installed .NET version:

9 Subdirectories under C:\Windows\Microsoft.NET9 clrver.exe9 clrver.exe -all

9 Programming directly in IL (Intermediate Language) can be interesting because:

9 IL is stack based, so we don’t find any instruction related to register manipulation. -

9 It is too easy to check, reverse it (ILDasm.exe) and make “customizations”.9 Ngen.exe can be used to compile IL instructions to native code.9 Of course, CLR checks some details (CLR version, CPU type, Windows

version, and so on) before executing it.9 Eventually, malware threats have attacked the .NET runtime to subvert the

system. -

Page 34: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201934

9 In .NET applications:

9 .NET Assembly:

9 managed .NET application (modules) + class libraries + resources files 9 In malware samples, we usually find that resources are encrypted

binaries and DLLs. -9 contains information such as version, publisher, culture and exported

files. 9 Remember that the application can download assembly files from a URL

(codeBase element). 9 .NET malware have used multi-file assemblies, partitioning types over

different files. Unfortunately, it is only possible to create multfile assembly in the command line. -

9 Few malware authors have create .NET malware containing different types: such as C# and VB in the same assembly.

9 The manifest also holds metadata tables containing the names of files that are part of the assembly: AssemblyDef, FileDef, ManifestResourceDef and ExportedTypesDef.

Page 35: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201935

9 Assemblies can be classified as:

9 private: it is specific of an application and deployed at same directory. 9 shared: it is shared and used by other applications.

9 All memory requested by the assembly is really “managed” by CLR, which “interfaces” this request to the operating system.

9 Therefore, the CLR is able to control the access to different objects in the memory between the application domains (like processes), which are representing the code of assemblies.

9 Of course, there are permited methods to cross-access requests between application domains through proxy objects by marshalling objects either by value or by reference.

9 As we’ve mentioned previously, manifest is a kind of file to “wrap up everything”, describing modules, resources and other details of the assembly.

Page 36: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201936

9 Compile multi-file .NET malware is pretty easy:

9 csc.exe /t:module hooking.cs9 csc.exe /t:module injection.cs9 csc.exe /out:malwarelib.dll /t:library /addmodule:hooking.netmodule

/addmodule:injection.netmodule Defcon.cs

9 In this case, we have a multi-file assembly:

9 includes a managed module named hooking.netmodule and injection.netmodule. The output file is a DLL named malwarelib.dll

9 a manifest file wrapping everything.

9 This compiling command add the hooking.mod file to the FileDef manifest metadata table and the its exported types to the ExportedTypeDef manifest metadata table.

9 To check: ILDasm Æ View ÆMetaInfo Æ Show! and look for the FileDef and ExportedTypeDef tables.

Page 37: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 37

Native modules referred by the assembly. The module name is in the ModuleRef.

External assemblies that referred by the assembly

(AssemblyRef table).

Manifest

Used when a strong assembly is specified.

Page 38: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201938

Assembly name

Custom attributes used by the compiler (or tools) and defined in the CustomAttribute metadata table (0x0C).

CALG_SHA1

Page 39: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 39

Managed Resources (ManifestResource metadata table)

other attributes

Globally unique identifier.

Page 40: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201940

9 Of course, we could have a “big malware module” to use in projects:

9 al.exe /out: BigMalwareLib.dll /t:library hooking. netmodule injection.netmodule

9 csc.exe /t:module /r:BigMalwareLib.dll Defcon.cs9 al /out:Defcon.exe /t:exe /main:Defcon.Main Defcon.netmodule

9 In this case, the __EntryPoint() global function will contain the Defcon::Main( ) function call (check the IL code to confirm it).

9 Finally, using csc.exe /resource makes simple to add resources (generated by resgen.exe , for example). It updates the the ManifestResourceDef table.

9 It is not necessary to mention that malware’s authors usually don’t write strong assemblies, which as signed with the private/public key pair from the publisher. Unless that this key pair has been stolen... -

9 csc.exe /out:TestProgram.exe /t:exe Program.cs9 sn.exe -k AlexandreBorges.snk9 sn.exe -p AlexandreBorges.snk AlexandreBorges.PublicKey sha2569 Sn.exe -tp AlexandreBorges.PublicKey9 csc.exe /out:TestProgram.exe /t:exe /keyfile:AlexandreBorges.snk Program.cs

Page 41: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 41

Public key generated by:

sn.exe -p AlexandreBorges.snk AlexandreBorges.PublicKey sha256

Page 42: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201942

9 Once the system is compromised through a native malware and we have access to the system as administrator, so it is possible to copy our .NET assembly to the Global Assembly Cache (GAC). The Registry is not changed.

9 Once a malicious .NET assembly (first stage, as a resource library) is copied to GAC, so it can be accessed by other assemblies.

9 Thus, other malicious .NET malware samples (second stage) can access methods and types from the first stage.

9 Only strong assemblies (signed) can be copied to the GAC (located at C:\Windows\Microsoft.NET\assembly) by using GACUtil.exe /i command.

9 Futhermore, including /r option integrates the assembly with the Windows install engine.

9 Unfortunately, the GACUtil.exe is not available in home-user systems, but it is possible to use the MSI to install the malware threat into the GAC. -

9 At end, it is still feasible to using delay signing, which is a partial signing only using the public key. Therefore, private key is not used (and there isn’t real protection).

Page 43: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201943

9 The delay signing allows that the malicious assembly to be installed into the GAC and, worse, other assemblies can make reference to it. -

9 csc.exe /out:malware.dll /t:exe Program.cs

9 sn.exe -k AlexandreBorges.snk9 sn.exe -p AlexandreBorges.snk AlexandreBorges.PublicKey sha2569 sn.exe -tp AlexandreBorges.PublicKey

9 csc.exe /out:malware.dll /t:exe /keyfile:AlexandreBorges.PublicKey /delaysign Program.cs

9 sn.exe -Vr malware.dll (CLR trust in the assembly without using the hash).

9 It is not so hard to perform a supply-chain attack because, when a file is specified as reference in the csc.exe compiler using /reference switch, it looks at:

9 the working directory9 csc.exe directory

9 directory specified by the /lib switch9 directory specified by the LIB environment variable.

Page 44: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201944

9 Several malware samples have been modified or written directly in ILAsm to bypass common tools.

9 While ILAsm is not complicated, maybe it is still recommended to remember few directives and instructions. -

9 .assembly DefCon { }: identifies the current assembly as being DefCon. 9 .assembly extern <assemblyname>: determines the external managed assembly

used by the program. For example, .assembly extern <mscorlib> 9 .module malware.dll: identifies the current module. 9 .namespace Conference: identities the namespace, but it does not represent a

metadata. 9 .class public auto ansi Hacker entends [mscorlib]System.Object. Its keywords;

9 .class: identifies the current class (Hacker)9 public: specifies the visibility. For example, it could be “private”.9 auto: determines the class layout style. It could be “explicit” and

“sequencial”. 9 ansi: string encode while communicating to unmaged code. Other values are

autochar and unicode. 9 extends: determines its base class

Page 45: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201945

9 Other flags for .class directive are:

9 private: used with private classes, which are not visible outside the current assembly.

9 sealed: the current class can’t be derived from this class. 9 abstract: the current class can’t be instantiated (it holds abstract methods). 9 explicit: the loader preserve the order of fields in the memory. 9 sequential: the loader preserves the order of the instance fields as specified

in the class. 9 nested family: the class is visible from the descendants of the current class

only. 9 nested assembly: the class is visible only from the current assembly.9 nested famandassem: the class is visible from the descendants of the current

class, but residing in the same assembly only.9 windowsruntime: the class is a Windows runtime type.

9 .class public enum Exam: declares a class enumeration named “Exam”.

9 .ctor( ): instance constructor, which is related to instance fields. 9 .cctor( ): class constructor (known as type initializer), which is related to static

fields.

Page 46: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201946

9 During malware analysis, take care: there can be a global .cctor directive, which it is related to global fields.

9 call: call a method. Its possible keywords:

9 return type: void, int32, and so on.9 vararg: variable number of arguments

9 calli: directive used to call methods indirectly by taking arguments + function pointer.

9 ldc.i4.09 ldc.i4.19 ldc.i4.29 ldftn void DefCon::Test(int32, int32, int32)9 calli void(int32, int32, int32)

9 (method reference):

9 call instance void DefCon::Exam(int32, int32, int32)9 call instance [.module malware.dll]::Hooking(int32, int32, native int)

Page 47: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201947

9 .field: specifies a variable of any type declared directly in the class (or struct). Its main keywords can be:

9 public / assembly / family (accessed by any decending class) / private9 static (shared by all instances of the referred class).

9 .method: specifies the method declaration. Its main keywords (flags) can be:

9 public / static: similar meaning as especified in “field” explanation above. -9 cil managed: it means this method is represented in managed code.9 newslot: creates a new slot in the virtual table of the class to prevent that a

existing method (same name and signature) to be overriden in a derived class. 9 native unmanaged: it means this method is represented in a native code.9 abstract: of course, no implementation is provided. 9 final: as known, the method can’t be overridden. 9 virtual: method can be “redefined” in derived classes.9 strict: this method can only be overridden whether it is accessible from the

class that is overriding it. Of course, the method must be virtual. 9 noinline: it is not allowed to replace calls to this method by an inline version.9 pinvokeimpl: declares an unmanaged method from a managed code (it’s is

also known as P/Invoke mechanism).

Page 48: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201948

9 .method public hidebysig static pinvokeimpl("user32.dll" winapi) int32 FindWindow(string,string) cil managed preservesig

9 preservesig: return of method must be preserved. 9 FindWindows(string,string): function invoked from the “user32.dll” and that

returns a int32 value.

9 .class public DefCon implements InterfaceA,InterfaceB {.method void virtual int32 IfB_Speaker(string) {

.override InterfaceB::Speaker... }

9 .class public DefConChina extends DefCon {.method public specialname void .ctor( ) {

ldarg.0call instance void DefCon::.ctor( )ret }

callvirt instance void DefCon::IfB_Speaker( )

Page 49: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201949

9 .entrypoint: identifies the method as the entry point of the assembly. 9 .maxstack: defines the maximum stack depth used by the function code

9 .locals int: defines the local variable of the current method and the “init” keyword is initializing the variable with “zero” (for example, a integer variable).

9 .data <var_1>: defines a data segment named “var_1”.

9 stloc <var>: retrieves the value returned by the call and stores into the “var” variable.

9 ldarg.0: Load argument 0 onto the stack.

9 ldloc <var>: copies the value of “var” onto the stack. Variants, after optmization and run, such as ldloc.0, ldloc.1, ldloc2 and ldloc3 (representing the first local variables) are possible.

9 ldstr: loads the reference to a string onto stack.

9 ldsflda: loads the reference of a static field onto the stack.9 ldsfld: loads the value of a static field onto the stack. 9 ldc.i4 8: loads the constant value 8 onto the stack.

Page 50: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 50

9 br Borges: its unconditional jump similar to “jmp” in native assembly. In this case, jumping to “Borges” label.

9 brtrue DefCon: takes an item from stack and, if it is zero, so jumps to “Alex” branch. Similar to jz instruction.

9 brfalse Alex: takes an item from stack and, if it is one, so jumps to “Alex” branch. Similar to jnz instruction.

9 .this: it is a reference to the current class (not instance of the class like C++).9 .base: it is a reference to the parent of the current class. 9 .typedef: creates a alias to a type. 9 .try / catch: the same meaning of traditional C language.

Page 51: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201951

auto: loader defines the “best lay out” in the memory”

nested and sealed class!

specialname flag helps the loader to understand this is a special function (constructor)

Remember that a field is a variable of any type that is declared directly in a class or struct.

Page 52: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201952

instance constructor

load argument 0 onto the stack.

reserving 8 slots for arguments.

push 0 onto stack as int32.

loads a string reference onto stack.

replaces the value of a field with a value from stack.

Using a custom attribute statement to set the value of CompilerGeneratedAttribute .

Page 53: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201953

Invoking an unmanaged methods.

Declares a private class.

Defines an initially runtime zeroed local variable (type class) of the current method.

Page 54: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201954

It calls a virtual method, which can be overriden the the derived class.

It loads -1 onto the stack.

Calls a static method named GetCurrentProcess( ) from Process class (within namespace System.Diagnostics) and returning an instance of Process class. -

Duplicate the value of the top of the stack.

legal instructions to way out of a “try block.”

Page 55: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201955

It seems that someone is interested in our typed information. -

Page 56: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201956

family: can be accessed by any class descending from the current one.

ldfld: loads the instance field onto the stack.ldsfld: loads the static field onto the stack.

Event declaration. We should remember that all events must have a subscribing method (.addon ) and a unsubscribing method (.removeon), at least. -

Cleaning up

Page 57: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 57

Declaring three local class variables in three different slots: 0, 1 and 2. We should remember that, eventually, slots of same type can be reused. However, it is another talk... -

Page 58: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 58

Finally, when the publisher calls the Invoke method of the aggregate delegate, so the event is raised. -

Delegates are references representing “type-safe” function pointers. Thus, Combine( ) adds callback function pointers to an aggregate, which is used to implement the event.

Generic Delegate! Compares the second and third arguments and, if it’s equal, replace the first argument (!!0&).

Page 59: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 59

turn off compile optimization and not allow put this function as inline.

calling several instance contructors

calling the virtual method

Page 60: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 60

pushes a null references onto the stack

converts to int64 and pushes it onto the stack

converts to int32 and throws an exception when overflow

declares and initializes the local variable

Function used to decrypt strings

loads the local variable onto stack.

Page 61: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201961

9 As mentioned previously, a managed assembly can be compiled into a native image and installed by using Ngen.exe, but the managed assembly is still necessary.

9 DLL loaded from the Global Assembly Cache can and need to be monitored to detect strange behavior. Tools to log the DLL loading such as Fuslogvw.exe (Assembly Bind Log Viewer) and common applications such as Process Monitor can help us.

9 Of course, .NET malware threats can try to compromise the .NET runtime class libraries and JIT, which would cause a deep infection in the system and demand a detailed investigation because:

9 changing the runtime library (at IL code) can be lethal to many applications.9 it is feasible to change (hooking)/replace a runtime library.

9 changing the runtime library allows the threat to “break” some strict rules.9 Changing JIT cause same problems, but it is harder.

9 Remember about basics: 9 copy DLL from GAC Æ dnSpy/Reflector + Reflexil Æ ildasm Æ change Æ ilasmÆ NgenÆ copy back to GAC (malware dropper can accomplish this task) -

Page 62: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201962

9 Of course, nothing is so simple:

9 If the malware’s target is a DLL from .NET runtime, so it is digitally signed and it would be necessary to have the private key to sign it. Unfortunately, we don’t have.

9 Another option would be to generate a new pair of keys and re-sign all the DLL Framework. Unfortunately, it is so much work.

9 Copying a modified runtime DLL over the existing one can be difficult or almost impossible because other programs can be using it. Thus, we should stop programs and services to accomplish this task.

9 Eventually, it is necessary to reboot the machine (urgh!) to perform this copy from a script.

9 Using the new and modified DLL can be tricky: uninstall the existing native library (ngen uninstall <dll>) and remove it from its respective directory under NativeImages_<version> directory.

9 There are other many tricks such as dropping an assembly into C:\Windows\System32 or Syswow64)\Tasks\Tasks.dll (hint from Casey Smith)

Page 63: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201963

9 An alternative would be change the Registry. In this case, the GAC continue being associated to the original (and untouched) assembly, while its associated native image is changed to the modified version.

9 In this case:

9 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\NativeImagesIndex\v2.0.50727_64\IL key holds information (name + signature key) about the original assembly.

9 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\NativeImagesIndex\v2.0.50727_64\NI key would hold information (name + MVID) about the modified native image.

9 Using the MVID from NI key makes simple to locate the native image.

9 Thus, we can either override the native image with a modified version or change the MVID entry to point to another native image.

9 GAC (old .NET assemblies) / GAC_32 (IL and x86) / GAC_64 (IL and x64) / GAC_MSIL (IL code) directories are under C:\Windows\Assembly directory.

Page 64: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201964

9 Most of the time, .NET malwares attacking the .NET libraries try either to remove some check or introduce hooking points at entry or exit of a method. In this case, System.Reflection is commonly used.

9 Additionally, there are cases of .NET malware threats attacking applications and the service management offered by System.ServiceProcess.ServiceBase class and their associated method such as OnStart( ), OnStop( ), Run( ), ServiceMain( ) and so on.

9 Modifying a target code for changing the execution flow demands inserting references (.assembly extern directive) to signed libraries (version + public key) to be able to access member and call methods.

9 Of course, we should remember to increase the stack (.maxstack).

9 At end, we have multiple types of attacks from a malicious managed code by establishing a C2, intercepting communication with trusted websites, opening shells, gathering system information, launching native second stage droppers, intercepting filesystem communication, stealing information and so on.

Page 65: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201965

9 WinDbg is always an excellent tool to understand a .NET malware in a better way or even getting a basic understanding, at least. -

9 Install SOSEX extension:

9 Download it from http://www.stevestechspot.com/downloads/sosex_64.zipor http://www.stevestechspot.com/downloads/sosex_32.zip

9 Unpack it and copy to your WinDbg installation directory. For example: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64|x86

9 Attach the WinDbg to either a running application (the .NET malware) or a saved dump.

9 Remember that the CLR process is composed by:

9 System Domain9 Shared Domain9 Default Domain

9 code running at this domains can’t access resources from another application domain.

Page 66: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 66

0x25B05A: Entry Point from dumpbin /headers malware1.exe

� Remember:

9 Malware execution9 Win loaders find the PE’s entry point9 Jump to mscoree.dll9 Call to CorExeMain9 Return to assembly’s entry point.

Disassembling CorExeMain( ) from the start.

We could have used before this point: sxe ld mscorwks.dll ; g

Page 67: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201967

Listing domains of the CLR process.

As commented previously.

Page 68: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201968

Used assemblies tell us a bit about the application.

Page 69: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201969

Checks managed exception in each thread.

switch to the thread 5

managed threads:0, 2 , 5, 10 and 14

Page 70: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201970

Check the managed stack trace for this thread.

switch to the thread 0

Page 71: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 71

Get a list of managed threads. Of course, we could used the -special

option to get additional information.

Checks the unmanaged stack trace for this thread.

COM Threading Model:9 STA: Single Thread Apartment 9 MTA: Multi Thread Apartment

Threat state:

9 (0x0) Newly initialized thread.

9 (0x020) It can enter a Join.

9 (0x200) background thread.

Page 72: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201972

Check if the instruction pointer address belongs to the JIT code and find the Method Descriptor -

Disassembling the code

Page 73: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201973

Checks the managed stack

Display information about the MethodDesc structure

Dump the MethodDesc structure at this address.

Method definition.

Remember: Metadata token is composed by a Table Reference (1 byte) and a Table Index (3 byte).

Page 74: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 74

Displays information about the EEClass structure associated with a type

Page 75: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 75

dumps the object content, but in this case it is a value type. -

Class

Page 76: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 76

Dump information about the Method Table

Dump information about the Method Table and display a list of all methods.

Code is PreJIT compiled

Type definition

EEClass data structure is similar to the method table, but it stores fields that are used less frequently.

Page 77: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 77

Dump information about a specific module

Dump information about the assembly (as shown previously)

Data accessed and/or updated less frequently

Data accessed and/or updated very frequently

Data used to help COM operations

Page 78: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 78

Displays the MethodTable structure and EEClass structure

Page 79: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201979

Bad intentions? -

Page 80: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 80

PreJIT: pre-compiled codeJIT: compiled CodeNONE: the code hasn’t been compiled by the JIT.

Page 81: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 81

Shows information about the EEClass structure

Set up a breakpoint on a code that is not JIT yet. -

Displays the MethodTable structure and EEClass structure of test.Client.Verbunden method.

Displays the MethodDesc structure information

Page 82: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201982

Dumps out arrays

Performs stack walking and display managed objects from current thread

Page 83: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 83

Value Type: 1Reference: 0

Method Table of the field

Page 84: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 84

As a general overview, during allocation requests:

9 If the maximum expected memory for the Gen0 is exceeded, collect non-rooted objects and promote rooted objects to Gen 1.

9 The same approach is valid when collecting objects from Gen 1 and Gen 2.

9 If Gen 2 is exceeded, so GC adds a new segment to Gen 2.

9 Objects in Gen 0 and 1 are short-lived.

Reference chain to the object -

from stack...

from handle tables...

Page 85: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201985

9 The Finalization Queue contains objects with finalizers (Finalize( )).

9 When an object in Finalization Queue becomes rootless, so the GC put it into the f-reachable queue, which are considered garbage (but alive).

Page 86: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201986

Excessive or long-time pinned handles can cause CLR heap fragmentation.

Page 87: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 87

Dumps the process to later analysis

Look for the string in the managed heap.

It shows information about locks

Make easier to find deadlocked threads

Displays information about a type or variable

It could seems unbelievable, but some malware samples don’t work because deadlocks -

If there is some deadlock, so use the DumpObj command to find additional information about the thread. -

CCW: COM Callable WrapperRCW: Runtime Callable Wrapper, which intercepts, manage the object’s lifetime and the transition between managed code and native code.

Page 88: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 88

9 Remember that an event works as synchronization object. -

9 When an event happens (going from non-signaled state to signaled state), the waiting thread (WaitForSingleObject( )) starts its execution.

9 Auto reset: If the event is signaled, so allows the thread being release and it is automatically reset to non-signaled state.

9 Manual reset: the event remains in signaled state until being intentionally reset.

9 Other synchonization techniques could be Semaphores, ReaderWriterLock, Mutex and so on...

It shows specific-object handle information

Page 89: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 89

Additionally, it is always recommended to investigate the current stack, looking for some interesting string-

Few hints about our malware...-

Page 90: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 90

Get objects (and their respective metadata) stored in the heap. To a short output, use !DumpHeap -stat

Dumps the heap, but limit the output to the specified type name.

Class !

Page 91: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 91

Displays information about the method table

Page 92: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 92

Boxing turns a value type into an object reference (reference type)

Unboxing turns a object reference into a value type

!DumpIL displays the IL instructions of a method

Page 93: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 93

!DumpHeap -strings is always excellent to find valuable strings. -

If you can’t recognise these strings, they are related to banks. -

Page 94: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201994

9 Other possible WinDbg breakpoints that could be used to gather further information:

9 How to log API calls:

9 bp mscorwks!MethodTable::MapMethodDeclToMethodImpl9 bp clr!MethodTable::MapMethodDeclToMethodImpl

9 How to get possible strings:

9 !bpmd mscorlib.dll System.String.CreateStringFromEncoding9 !bpmd mscorlib.dll System.String.Intern9 !bpmd mscorlib.dll System.Text.StringBuilder.ToString9 bp mscorwks!GlobalStringLiteralMap::GetStringLiteral9 bp clr!StringLiteralMap::GetstringLiteral

9 How to examine loaded assemblies:

9 bp mscorwks!CLRMapViewOfFileEx9 bp clr!AssemblyNative::LoadFromBuffer

Page 95: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 201995

Surprise... is it malicious? -

� https://github.com/alexandreborges/malwoverview

Page 96: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 96

Page 97: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

DEF CON USA 2019 97

Page 98: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

DEF CON USA 2019 98

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

� Acknowledgments to:

9DEF CON staff, who have been always very kind with me.

9You, who have reserved some time attend my talk.

9Security is like a drunk: while walking back-and-forth, he always proceeds halfway through the remaining distance, but he never gets there. -

9Remember: the best of this life are people. -

Page 99: NET MALWARE THREAT: INTERNALS AND REVERSING … CON 27/DEF CON 27 presentations/DEFCON-27... · ALEXANDRE BORGES – MALWARE AND SECURITY RESEARCHER DEF CON USA 2019 5 9Motivationsto

DEF CON USA 201999

9 Malware and Security Researcher. 9 Speaker at DEF CON USA 20189 Speaker at DEF CON China 20199 Speaker at CONFidence Conference

2019 (Poland)9 Speaker at HITB 2019 Amsterdam9 Speaker at BSIDES

2019/2018/2017/20169 Speaker at H2HC 2016/20159 Speaker at BHACK 20189 Consultant, Instructor and Speaker on

Malware Analysis, Memory Analysis, Digital Forensics and Rootkits.

9 Reviewer member of the The Journal of Digital Forensics, Security and Law.

9 Referee on Digital Investigation: The International Journal of Digital Forensics & Incident Response

THANK YOU FOR ATTENDING MY TALK. -

¾ Twitter:

@ale_sp_brazil@blackstormsecbr

¾ Website: http://www.blackstormsecurity.com

¾ LinkedIn: http://www.linkedin.com/in/aleborges

¾ E-mail: [email protected]

ALE

XAN

DRE

BO

RGES

–M

ALW

ARE

AN

D S

ECU

RITY

RES

EARC

HER

Blackstorm Security: we offer one of best training courses around the world. -