Skip to content

Constants Protection

Martin Karing edited this page Jan 9, 2021 · 4 revisions

ID: constants
Preset: Normal
Availability: ConfuserEx ConfuserEx 2

This protection encodes and compresses constants (numbers, strings, and initializers) in the code.

Parameters

  • mode: This parameter define the way ConfuserEx encode the constants. Supported values are:

    • normal: ConfuserEx would use static algorithms with random parameters to encode the constants. (default)
    • dynamic: ConfuserEx would use dynamically generated algorithms to encode the constants.
    • x86: ConfuserEx would use dynamically generated native x86 expressions to encode the constants. This mode will disable the "AnyCPU" mode of an assembly.
      (Produces unverifiable modules)
  • decoderCount: This parameter is an integer value defining how many constant decoder ConfuserEx would generate. Default is 5.
    Since each decoder has slight differences, more decoders would make manual decoding of constants by attackers more annoying, but the result file size would increase.

  • elements: This parameter defines what type of constants would be encoded. Possible values are a combination of:

    • S: String constants (excluding primitive constants)
    • N: Numeric constants (excluding primitive constants)
    • P: Primitive constants (empty strings and commonly used numbers, e.g. 0, -1, 1, 2, etc.). Only works in combination with S or N.
    • I: Array initializer (Those using RuntimeHelpers.InitializeArray)

    The value is case-insensitive. For example, a value of SI indicates non-empty strings and initializers should be encoded. Default is SI.

  • cfg: This parameter is a boolean value whether decoding of constants are based on a control flow dependent state variable.

    • true: The values are decoded using the control flow dependent variable
    • false: The values are decoded using random static values (default)

    Enabling it would greatly enhance the strength of protection but the runtime performance may have a impact.

  • compressor: Only in ConfuserEx 2 ConfuserEx 2
    Select the compression algorithm that is used to compact the data block containing the protected constants. Supported values are:

    • None: No compression is being applied
    • Deflate: The default deflate algorithm is applied, decompression is handled by the native implementation of the .NET Framework.
    • Lzma: LZMA compression is being applied. Decompression is handled by the runtime embedded into the assembly. (default)
    • Lz4: Lz4 compression is being applied. Decompression is handled by the runtime embedded into the assembly.

    ConfuserEx is always using the LZMA compression.

  • compress: Only in ConfuserEx 2 ConfuserEx 2
    Set if any compression is applied. Supported values are:

    • Auto: The datablock is being compressed. In case the compressed data block including it's header is smaller than the decompressed data, the compressed data is used. Otherwise the uncompressed data is used. (default)
    • Force: The compressed data block is used in any case.
    • Off: No compression is being applied.

    ConfuserEx is always forcing the compression.

Example

For ConfuserEx ConfuserEx:

<protection id="constants">
  <argument name="mode" value="x86" />
  <argument name="decoderCount" value="10" />
  <argument name="elements" value="SNI" />
  <argument name="cfg" value="true" />
</protection>

For ConfuserEx 2 ConfuserEx 2:

<protection id="constants">
  <argument name="mode" value="x86" />
  <argument name="decoderCount" value="10" />
  <argument name="elements" value="SNI" />
  <argument name="cfg" value="true" />
  <argument name="compressor" value="Lz4" />
  <argument name="compress" value="Force" />
</protection>

Remarks

Protecting the constants may have a significant impact on the performance of the protected assembly. It's usually a good idea to activate this protection selectively. There is no point in protecting the string constants in the view-model of a WPF application, where the strings are just the names of the property they are located in.