# Code Virtualization

The Common Intermediate Language (CIL) is a set of platform-independent instructions generated by the language-specific compiler (C#, VB.NET...) from the source code. The CIL is platform-independent and can be executed on any Common Language Infrastructure supported environments such as the .NET runtime or Mono.

The way how the CIL and other metadata is stored must follow a specific standard (ECMA-335). This way tools like .NET Reflector or ILSpy can read the CIL instructions and translate the code back to its source language (C#, VB.NET...).

Code Virtualization converts the CIL code into a set of randomized instructions which are interpreted at runtime by our virtual machine. As there is no standardized procedure to interpret the new instruction set correctly, the original CIL instructions can't be reconstructed. Consequently, the virtualized code can't be translated back to its source language.

# Enable virtualization

First enable Code Virtualization in the .NET Reactor protection settings. The attribute then selects the methods which should be virtualized; the attribute does not enable Code Virtualization by itself.

Decorate the corresponding methods with the following attribute:

[System.Reflection.ObfuscationAttribute(Feature = "Virtualization", Exclude = false)] 

# Integrity-linked virtualization

.NET Reactor can combine code virtualization and Anti Tampering inside selected methods. The Anti Tampering code is inserted into the method before the complete method body is virtualized. This makes the integrity check part of the protected functionality instead of leaving it as an easily identifiable separate call.

Attribute value Friendly name Integrity-check frequency
Virtualization Standard Virtualization No additional embedded check
VirtualizationOneTimeIntegrity One-Time Integrity Virtualization At most once during normal application execution
VirtualizationRepeatedIntegrity Repeated Integrity Virtualization Every call to the marked method

The descriptive attribute values state the behavior directly and are recommended for new code.

# One-Time Integrity Virtualization

[System.Reflection.Obfuscation(
    Feature = "VirtualizationOneTimeIntegrity",
    Exclude = false)]
internal void InitializeCriticalState()
{
    // Important initialization logic
}

The Anti Tampering code is inserted into InitializeCriticalState, and the resulting combined method is virtualized. The embedded integrity check is guarded so that it runs at most once during the process lifetime. Calling the method again continues to execute its normal functionality without repeating the full file-integrity check.

Use this mode when an important method is called more than once but one integrity check is sufficient.

# Repeated Integrity Virtualization

[System.Reflection.Obfuscation(
    Feature = "VirtualizationRepeatedIntegrity",
    Exclude = false)]
internal void LoadProtectedConfiguration(Stream stream)
{
    // Important, non-performance-critical logic
}

Repeated Integrity Virtualization uses the same combined and virtualized method body, but the embedded Anti Tampering code runs every time the marked method is called. This creates a stronger recurring dependency between important functionality and the assembly-integrity check.

The repeated check reads and verifies the protected assembly and can be expensive. Do not use this mode on hot paths, tight loops, rendering methods, frequently called property accessors, or other performance-sensitive code.

# Choosing suitable methods

Good candidates are methods which are essential for useful application behavior but are called infrequently, for example:

  • loading protected configuration or encrypted application data;
  • initializing important business or licensing state;
  • opening a protected project or document;
  • performing an infrequent security-sensitive transition.

Embedding the check in essential functionality increases the work required to remove Anti Tampering. An attacker cannot simply delete an obvious standalone call without also dealing with the virtualized functional method. This significantly raises the cost of modification.

If a combined method cannot be virtualized, .NET Reactor restores its original method body and reports the virtualization limitation instead of leaving a partially injected method behind.

# Example Usage

# Before protection

[System.Reflection.Obfuscation(Feature = "Virtualization", Exclude = false)]
public void CreateFile(string filename, string content)
{
	string directory = Path.GetDirectoryName(filename) ;
	if (!Directory.Exists(directory))
	{
		Directory.CreateDirectory(directory);
	}
	StreamWriter streamWriter = new StreamWriter(File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
	streamWriter .Write(content);
	streamWriter.Flush();
	streamWriter.Close();
}

# After protection (decompiled with ILSpy)

Decompiled output after applying code virtualization
Decompiled output after applying code virtualization

The remaining stub code executes the virtual machine.

# Related topics

Anti Tampering · Declarative Protection · Advanced Rules Editor

Choosing protection settings