Skip to content

Lesson 8 of 12 · 22m

Managed Code: When the Malware Is .NET

mscoree.dll imports, de4dot cleanup, dnSpyEx breakpoints on Assembly.Load, and why the CLR contains nothing.

Open a RedLine stealer or an AgentTesla build in your native PE tools and the import table is one line: mscoree.dll and _CorExeMain. Entropy normal, strings thin, capa quiet. Nothing is wrong with the file — it's .NET, and every instinct you built on native code is now a liability. A large slice of commodity Windows malware is C#: the CLR loads it, JITs it, and runs it with the same user rights as any native binary. The managed runtime is not a sandbox; it's just a different file format.

The wrong mental model: "managed means harmless" / "managed means readable"

Both halves are wrong. Harmless — the CLR happily P/Invokes into VirtualAlloc, WriteProcessMemory, and CreateRemoteThread; RedLine does credential theft entirely in C#. Readable — the obfuscators (ConfuserEx, its forks, custom name-manglers) turn method names into ሴ.የ፡የ() and encrypt every string, so dnSpy opens to fluent garbage. Managed code is different work, not less work.

Tells

DIE reports .NET or library mscoree. In pefile, IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR (data directory index 14) is nonzero. User strings live in the #US stream, not .text — which is why strings output looks anemic while the binary is doing plenty.

The toolchain

  • dnSpyEx — the maintained continuation of dnSpy (the original repo was archived). Debugger and editor; this is the workhorse.
  • ILSpy — clean, fast decompiler for static reading.
  • de4dot — strips ConfuserEx-family obfuscation: restores names, decrypts strings, removes junk code. Run it first: de4dot sample.exe and open sample-cleaned.exe instead.

What a .NET loader looks like

The shape is always the same three lines:

byte[] raw = Decrypt(Properties.Resources.payload, key);  // embedded resource + XOR/AES
var asm = Assembly.Load(raw);                             // load from memory, never touches disk
asm.EntryPoint.Invoke(null, new object[]{ args });        // or GetMethod("Run").Invoke(...)

Hunt for Assembly.Load, .Invoke(, and GetMethod in dnSpy's analyzer panel. Follow the byte[] backward: it usually comes from a .resources entry (dnSpy lists them in the tree), decrypted by a small XOR or AES routine in Main or the static constructor. Stealers keep their config — C2 URL, bot ID, target list — in a settings class, sometimes plaintext, often AES with Rfc2898DeriveBytes (PBKDF2) deriving the key from a hardcoded password you can now read.

Catching the payload in memory

Set a breakpoint on Assembly.Load or on the Invoke call, run the loader in the lab VM under dnSpyEx, and when it breaks, the payload sits in a byte[] local — right-click → save to file. Then triage that dump exactly like a pe-sieve output: is it another .NET assembly (keep going) or native bytes headed for an injected process? ConfuserEx's anti-debug will occasionally kill dnSpy mid-run; when that happens, go static with ILSpy and pull the resources out by hand — Resources.ResourceManager.GetObject names tell you which blob to extract.

Interop: where .NET stops pretending

[DllImport("kernel32.dll")] declarations are visible in the decompiler — VirtualAllocEx, OpenProcess, WNetAddConnection2. The string names survive even heavy obfuscation because P/Invoke needs them resolvable, so your strings-based YARA instincts still apply to the interop layer. Watch for Marshal.GetDelegateForFunctionPointer too: it's the managed-to-shellcode bridge, and finding it means the process is about to run unmanaged code the CLR can't protect you from.

Field notes

  • mscoree.dll + _CorExeMain as the only imports is the fastest .NET tell there is — one line and you switch toolchains.
  • de4dot first, dnSpy second. Opening an obfuscated build raw is how "thin strings" turns into a wasted hour.
  • Assembly.Load + Invoke is the native equivalent of VirtualAlloc + jump-to-OEP — same trapping idea, different runtime.
  • The CLR contains nothing. Run .NET samples under the same lab rules as native malware.
  • Dumped managed assemblies are PEs — file, diec, and your hash discipline all still apply.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.