Skip to content

Serializing with Newtonsoft breaks application #421

@hitgubmitt

Description

@hitgubmitt
  • ConfuserEx Version:Confuser.Core 1.6.0-alpha
  • Target Framework: .NET Framework v4.0.30319.42000 (.NetFramework v4.6.1)
  • Operating System: Windows 10 (Windows NT 6.2.9200.0)

Steps to Reproduce:

I obfustacted the code below with this crproj:

<project outputDir="outputDir" baseDir="baseDir" seed="seednumber" xmlns="http://confuser.codeplex.com">
  <rule pattern="true" inherit="false">
    <protection id="ctrl flow" />
    <protection id="rename" />
  </rule>
  <module path="ConfuserTests.exe" />
</project>

Obf.cs

using System.Reflection;
using Newtonsoft.Json;

namespace ConfuserTests
{
  [Obfuscation(Exclude = true)]
  public class Obf
  {
    [JsonProperty("a")] public string a;

    [JsonProperty("b")] public string b;

    [JsonProperty("c")] public string c;

    public Obf(string a, string b, string c){
      this.a = a;
      this.b = b;
      this.c = c;
    }
    
    public override string ToString(){
      return JsonConvert.SerializeObject(this);
    }
  }
}

Program.cs

    public static void Main(string[] args){

      try{
        Obf a = new Obf("a","b","c");
        string obfstring = a.ToString();
        Console.WriteLine(obfstring);
      }
      catch (Exception e){
        Console.WriteLine(e);
      }
      
      Console.ReadLine();
    }

This code triggers an error:

Newtonsoft.Json.JsonSerializationException: A member with the name '' already exists on 'ConfuserTests.Obf'. Use the JsonPropertyAttribute to specify another name.
   at Newtonsoft.Json.Serialization.JsonPropertyCollection.AddProperty(JsonProperty property)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateConstructorParameters(ConstructorInfo constructor, JsonPropertyCollection memberProperties)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
   at ConfuserTests.Obf.?????????????????????????????????????????() in C:\path\ConfuserTests\Obf.cs:line 25
   at ?????????????????????????????????????????.?????????????????????????????????????????(String[] ) in C:\path\ConfuserTests\Program.cs:line 11

As you can see, serialization breaks application. Even with [Obfuscation(Exclude = true)] attribute on the Obf class. What is the recommended way to achieve obfuscation when using json serialization via newtonsoft?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions