yck1509 / ConfuserEx

An open-source, free protector for .NET applications
http://yck1509.github.io/ConfuserEx/
Other
3.57k stars 1.63k forks source link

Cannot exclude classes from being obfuscated #203

Open bishtawi opened 9 years ago

bishtawi commented 9 years ago

Hi, I am trying to disable obfuscation on a few select classes. I am using Newtonsoft.JSON to serialize my models and ConfuserEx is preventing me from producing valid JSON.

Things I have tried:

[assembly: Obfuscation(Exclude = false, Feature = "namespace 'MyProgram.API.Models':-rename")]
[assembly: Obfuscation(Exclude = true, Feature = "namespace 'MyProgram.API.Models':-rename")]
[Obfuscation(Exclude = true, Feature = "rename")]

EDIT: Also tried

[Obfuscation(Exclude = true)]

and it did not work

bishtawi commented 9 years ago

Just tried adding a rule pattern to the .crproj file:

<rule pattern="namespace('MyProgram.API.Models')" inherit="false" />

Does not work.

bishtawi commented 9 years ago

It looks like attempting to disable obfuscation across a specific namespace (MyProgram.API.Models) disables obfuscation everywhere. When I add the above rule pattern to the .crproj file or add the above obfuscation assembly attribute, all protections are disabled across the entire MyProgram namespace.

And for whatever reason, disabling obfuscation on a specific class does not work.

bishtawi commented 9 years ago

Actually, looks like adding the rule pattern to the .crproj file does work. Its only adding the obfuscation attributes that does not work.

yck1509 commented 9 years ago

Hi, The correct way to use declarative attributes is:

[assembly: Obfuscation(Exclude = false, Feature = "rename")]
[assembly: Obfuscation(Exclude = false, Feature = "namespace 'MyProgram.API.Models':-rename")]

I found no problem with it with following command line:

ConfuserEx.CLI -o C:\App\Confused\ C:\App\Test.exe

Did you use the CLI version to protect it?

chendurkumar commented 9 years ago

Hi,

How to EXCLUDE just one Namespace from rename module?
For example : [assembly: Obfuscation(Exclude = true, Feature = "namespace 'ABC.XYZ':-rename")]

Looks it does not work. I have a objectmodels for Json parsing in a namespace and this throwing error. Also I am using .crproj file for obfuscating using CLI.

Waiting for your reply.

yck1509 commented 9 years ago

Hello, It seems there is a mistake in namespace matching. Please try the latest build at CI Server.

ghabre commented 9 years ago

Hello, Thanks for all the great work On the above, is there a way to exclude/cancel obfuscation of a specific class for ALL features? One class is giving me various issues when protected, it does some Serialization inside i am using the GUI to run the obfuscations want to protect the entire app executable except for that class. would [Obfuscation(Exclude = true)] work if i add it on top of the class? Perhaps i need a wildcard for the Features? Currently tried it and tried various other things as well, including all the above, and using latest build from CI Server.. no luck.. If there is a way much appreciated, otherwise i'll try next to refactor the code and move that class in addition to its many dependencies into a separate Dll Thanks

agray commented 8 years ago

If use the build 240 from here

My Post Build event command line is:

if /I "$(ConfigurationName)" == "Release" $(ProjectDir)..\..\..\Tools\ConfuserEx_LatestBuild\Confuser.CLI $(ProjectName).crproj

My crproj file is:

    <?xml version="1.0" encoding="utf-8"?>
      <project baseDir="." outputDir="Confused" xmlns="http://confuser.codeplex.com">
        <rule preset="none" pattern="true">
            <protection id="anti debug" />
            <protection id="anti dump" />
            <protection id="anti ildasm" />
            <protection id="anti tamper" />
            <protection id="constants" />
            <protection id="ctrl flow" />
            <protection id="ref proxy" />
            <protection id="rename" />
            <!--<protection id="invalid metadata" />-->
            <!--<protection id="resources" />--> 

            <!--resources -> CS0009: Metadata file 'MyDll.dll' could not be opened - Invalid public key.-->
        </rule>
        <module path="MyDll.dll" />
    </project>

If I add an obsfuscation attribute to my json deserialiser class like so:

[assembly: Obfuscation(Exclude = false, Feature = "namespace 'MyDll.SomeNamespace':-rename")]

Compilation fails as the Post Build event command line exits with -1.

My output window shows:

[INFO] ConfuserEx v0.6.0-66-gd531eaf Copyright (C) Ki 2014
1>   [INFO] Running on Microsoft Windows NT 6.2.9200.0, .NET Framework v4.0.30319.42000, 64 bits
1>  [DEBUG] Discovering plugins...
1>   [INFO] Discovered 10 protections, 1 packers.
1>  [DEBUG] Resolving component dependency...
1>   [INFO] Loading input modules...
1>   [INFO] Loading 'MyDll.dll'...
1>  [ERROR] Unknown error occurred.
1>  Exception: System.Exception: Error when parsing pattern namespace 'MyDll.SomeNamespace' in ObfuscationAttribute. Owner=MyDll, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null ---> Confuser.Core.Project.InvalidPatternException: Unexpected token 'MyDll.SomeNamespace' at position 10. Expected '('.
1>     at Confuser.Core.Project.PatternParser.UnexpectedToken(PatternToken token, Char expect) in c:\projects\confuserex\Confuser.Core\Project\PatternParser.cs:line 93
1>     at Confuser.Core.Project.PatternParser.ParseExpression(Boolean readBinOp) in c:\projects\confuserex\Confuser.Core\Project\PatternParser.cs:line 114
1>     at Confuser.Core.Project.PatternParser.Parse(String pattern) in c:\projects\confuserex\Confuser.Core\Project\PatternParser.cs:line 60
1>     at Confuser.Core.ObfAttrMarker.AddRule(ObfuscationAttributeInfo attr, List`1 infos) in c:\projects\confuserex\Confuser.Core\ObfAttrMarker.cs:line 344
1>     --- End of inner exception stack trace ---
1>     at Confuser.Core.ObfAttrMarker.AddRule(ObfuscationAttributeInfo attr, List`1 infos) in c:\projects\confuserex\Confuser.Core\ObfAttrMarker.cs:line 347
1>     at Confuser.Core.ObfAttrMarker.MarkModule(ProjectModule projModule, ModuleDefMD module, Dictionary`2 rules, Boolean isMain) in c:\projects\confuserex\Confuser.Core\ObfAttrMarker.cs:line 416
1>     at Confuser.Core.ObfAttrMarker.MarkProject(ConfuserProject proj, ConfuserContext context) in c:\projects\confuserex\Confuser.Core\ObfAttrMarker.cs:line 292
1>     at Confuser.Core.ConfuserEngine.RunInternal(ConfuserParameters parameters, CancellationToken token) in c:\projects\confuserex\Confuser.Core\ConfuserEngine.cs:line 134

Later....

Because I am using a crproj I even commenting out the Obsfuscation attributes completely and tried adding the following to my crproj file:

<rule preset="none" pattern="namespace('Saucery3.DataSources') and match-type-name('PlatformTestData')">
      <protection id="rename" action="remove" />
</rule>

This resulted in runtime errors of:

----> Newtonsoft.Json.JsonSerializationException : A member with the name '' already exists on 'MyDll.SomeNamespace.SomeClass'. Use the JsonPropertyAttribute to specify another name.

Please advise

joeatsalot commented 7 years ago

It seems to work if I change namespace 'myassembly.something.mynamespace': to namespace('myassembly.something.mynamespace'): ... ie. adding brackets around the namespace string.

(As mentioned in #510)