9

I want to obfuscate my assembly files (*.dll, *.exe) by Dotfuscator. my question is if I do this, can I still use classes and types that are in those assemblies by their original names (I mean names before obfuscation) and using System.Reflection methods to work with them?

If you need more detail,please tell me

Henrik
  • 23,186
  • 6
  • 42
  • 92
amirhosseinab
  • 1,030
  • 2
  • 16
  • 29

4 Answers4

9

Obfuscation are Reflection can cause you some problem. Even if you take the suggestion to use option to not obfuscate public method, some of the reflected code may call private method. The problem is that obfuscation will change name of some code that you may need to stay the same.

If you know or can locate the region that is susceptible to be used with reflection you can use

[global::System.Reflection.Obfuscation(Exclude=true, Feature="renaming")]

This will tell the obfuscator to keep the name.

Running obfuscation with reflection require more testing that's for sure but still possible...

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
3

Read for example here http://msdn.microsoft.com/en-us/library/ms227298(v=vs.80).aspx There is a "library mode" to disable obfuscation of public members. Otherwhise you probably won't be able to access the methods. There is an attribute to control obfuscation at type level: http://msdn.microsoft.com/en-us/library/ms227281(v=vs.80).aspx

xanatos
  • 109,618
  • 12
  • 197
  • 280
1

You can use System.Reflection on an obfuscated assembly, but since some of the point of obfuscation is to rename everything in the assembly into random and meaningless things, you can't do reflection on the same names and identifiers as you would in a non-obfuscated assembly. If you want to do reflection on an obfuscated assembly, you would need to do it in a way that aren't dependent on what types and members are named.

Asbjørn Ulsberg
  • 8,721
  • 3
  • 45
  • 61
  • 1
    Normally public members aren't obfuscated (or at least there is an option to not obfuscate public members) – xanatos Sep 15 '11 at 15:11
  • @asbjornu - Is there any method for reflection and avoid using members name? – amirhosseinab Sep 15 '11 at 15:40
  • A solution (or not) would be create a static class, that haves a System.Type dictionary and then when you need to use reflection, get the type from this dictionary. So dosen't matter which name the method/class became after the obsfucator, you still have a reference for that. Of course, if you have uncountable types in your assembly, you'll end up with a giant, without any design pattern static class, that could cause performace issues in your application and also unhandled exceptions. – Guilherme Branco Stracini Jul 03 '17 at 23:07
0

You can create your own private map to get new names from old ones. Mapper must write table of a sort to disk/db with following structure: Module(executable),Index,OriginalType,ObfuscatedType

Create "Mapper" console application that operates on two modes based on an argument: The application will receive as argument executable path

  1. Load Assembly
  2. GetTypes from loadedAssembly
  3. PreObfuscation deletes all entries and writes anew the indexes and OriginalType values . PostObfuscation updates ObfuscatedType by index. The post build events must be as follows:
    1. Mapper.exe "target.exe" "Pre"
    2. [Obfuscate]
    3. Mapper.exe "target.exe" "Post"

Now you need a function to getObfuscatedName from OriginalName and you're done.

Note that this solution will not work with pruning as the number of types will change and indexes will no longer match between

OriginalAssembly.GetTypes()

and

ObfuscatedAssembly.GetTypes()
Cogent
  • 404
  • 7
  • 16