ConfuserEx icon indicating copy to clipboard operation
ConfuserEx copied to clipboard

Disable type name rename when marked with SerializableAttribute

Open fjch1997 opened this issue 9 years ago • 6 comments
trafficstars

#58

This way serialized data is fully compatible with unobfuscated version

fjch1997 avatar May 15 '16 03:05 fjch1997

Thank you very much for this patch. However, it applies to the type only. The properties of the type will still be renamed.

We shall add the following line into the void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, PropertyDef property) method in the same code file.

else if (property.DeclaringType.IsSerializable) {
    service.SetCanRename(property, false);
}

wmjordan avatar Nov 06 '16 12:11 wmjordan

SerializableAttribute doesn't care about properties, it cares about fields only.

My understanding is SerializableAttribute is only for .NET Framework's rule on (de)serialization, which will not call constructors and will only serialize fileds (private and public). This is also the behavior of BinaryFormatter and SoapFormatter. All classes that come with the framework and marked as Serializable follows this rule. Many third party serializer, including Newtonsoft.Json, can also be set to follow this rule.

If the author of the class want it to be Json serialized or something, there is no need to use SerializableAttribute. Instead, apply DataMemberAttribute or the serializer specific ones.

fjch1997 avatar Nov 09 '16 18:11 fjch1997

Thank you for the explanation. Currently I am working with Dapper, which maps property names to database fields. We do need some mechanisms to prevent properties from being renamed. The DataMemberAttribute might not be suitable for this purpose.

wmjordan avatar Nov 10 '16 01:11 wmjordan

Probably better disabling renaming for the entire class using Confuser's attribute. This is not worth the effort.

fjch1997 avatar Nov 20 '16 22:11 fjch1997

@fjch1997 Using "Confuser's attribute" would have the project referencing Confuser, which is not the way to go I am afraid.

Did you mean ObfuscatorAttribute actually? I agree using ObfuscatorAttribute instead will be a better approach. The problem is that the support for ObfuscatorAttribute of Confuser is too limited at this moment.

wmjordan avatar Nov 22 '16 02:11 wmjordan

It's been a while since I had to use Confuser so I'm forgetting all the details. But yes, I mean ObfuscatorAttribute. Confuser doesn't have its own attributes. Actually you can set parameters to ObfuscatorAttribute to instruct ConfuserEx to only disable rename for a class and everything. I know this is not properly documented but it is supported. So overall it's quite powerful. I can't tell you how to use it since I don't even remember myself. But you can take a look at the samples and source code.

fjch1997 avatar Nov 29 '16 14:11 fjch1997