Obfuscation of Value Tuples

C# 7 and Visual Basic 15 introduced the notion of value tuples. The concept is similar to what you may find in System.Tuple but with two important distinctions:

  1. The new System.ValueTuple is a value type while the older System.Tuple is a reference type. The value type tends to be a more efficient data type when it comes to passing the values in and out of the method calls
  2. System.ValueTuple has a special compiler support and thus allows to define named fields. This is a big advantage comparing to System.Tuple which only has Item1, Item2, ... properties.

How does it affect the obfuscation? Let's take a look at C# example:

static (int sum, int count) Calc(IEnumerable<int> source)  
    var r = (sum: 0, count: 0);
    foreach (var value in source)
        r.sum += value;
    return r;

The given method Calc can be used like this:

static void Main()  
    var seq = new[] { 1, 2, 3 };
    var r = Calc(seq);
    Console.WriteLine("Sum: {0}", r.sum);
    Console.WriteLine("Count: {0}", r.count);

Please note that method Calc returns a value tuple with two named fields: sum and count. Do those names find their ways into resulting .NET assembly? The short answer is yes. They are specified by C# compiler behind the scenes with TupleElementNamesAttribute as shown below:

[return: TupleElementNamesAttribute(new[] { "sum", "count" })]
static System.ValueTuple<int, int> Calc(IEnumerable<int> source)

This is how it looks after compilation:

Leaked ValueTuple metadata

As you can see, the field names of value tuples basically become the part of .NET assembly metadata. And they will stay there disclosing the private implementation details to the outside world even when you build the assembly in Release configuration.

You can automatically strip the unneeded field names of value tuples by obfuscating the assembly with Eazfuscator.NET:

Obfuscate Visual Studio project with Eazfuscator.NET

Starting with version 5.6, Eazfuscator.NET fully understands the semantics implied by value tuples and takes care to automatically prune the unused value tuple metadata, including the aforementioned field names. Such transformation is not only a win in terms of security, but it also gives slight size and performance benefits.

Give Eazfuscator.NET a try

comments powered by Disqus