How to install and use obfuscar?

Viewed 22325

I just finished my C# project(WPF), but now I meet problems on using "obfuscar" (another applicaion would be welcome, if easier to use).

Of course I looked already on internet for it, but didn't find a documentation "for newbies"...

I also tried to use BasicExempleExe provided with obfuscar from github, then some questions remaining :

  • I see no install file for obfuscar, so already for 1st step I think I missed something?
  • I see in Example directory a file obfuscar.xml. I understood what is that file for, but I don't see at what level it is called.

Can please some guy be of any help?It is not such a big project I am doing now, but I would like to learn how to do that, and since yesterday I could not understand anything, that's why I finally decided to post here.

7 Answers

I have just created an extended configuration file for Obfuscar and would like to share it.

Full list of available parameters here.

<?xml version='1.0'?>
<Obfuscator>
  <Var name="InPath" value="." />
  <Var name="OutPath" value=".\Obfuscator_Output" />
  <Var name="KeepPublicApi" value="false" />
  <Var name="HidePrivateApi" value="true" />
  <Var name="RenameProperties" value="true" />
  <Var name="RenameEvents" value="true" />
  <Var name="RenameFields" value="true" />
  <Var name="UseUnicodeNames" value="true" />
  <Var name="HideStrings" value="true" />
  <Var name="OptimizeMethods" value="true" />
  <Var name="SuppressIldasm" value="true" />
  <Module file="$(InPath)\MyApplication.exe" />
</Obfuscator>

I set this up to run on the Post Build event in Visual Studio.

  1. Add Obfuscar NuGet Package to your solution.
  2. Add obfuscar.xml file to your project and change Copy to Output Directory: Always. See the post on this thread by vinsa for an XML sample. I had to include the full path to my project in "InPath" variable. Then the obfucasted folder was under bin/[debug / release].

  3. In the Visual Studio post build events enter: "$(Obfuscar)" obfuscar.xml

In order to use it on a .Net Core 2.1 console app, you can do this:

1 - Install Obfuscar by executing the following command in a console window:

dotnet tool install --global Obfuscar.GlobalTool --version 2.2.18

(you can check the last version here: https://www.nuget.org/packages/Obfuscar.GlobalTool)

2 - Add an XML file to the project you want to obfuscate called obfuscar.xml with the following content (from @vinsa answer):

<?xml version='1.0'?>
<Obfuscator>
<Var name="InPath" value=".\bin\Release\netcoreapp2.1\linux-arm\publish" />
<Var name="OutPath" value="$(InPath)\ReadyForDeployment" />
<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="true" />
<Var name="RenameProperties" value="true" />
<Var name="RenameEvents" value="true" />
<Var name="RenameFields" value="true" />
<Var name="UseUnicodeNames" value="true" />
<Var name="HideStrings" value="true" />
<Var name="OptimizeMethods" value="true" />
<Var name="SuppressIldasm" value="true" />
<Module file="$(InPath)\YourProject.dll" />
</Obfuscator>

3 - Finally, right click on your project, then Properties, Build Events, and in the Post-build event text box add this line:

if $(ConfigurationName) == Release obfuscar.console .\obfuscar.xml

From PowerShell as Administator:

PS> Install-Package Obfuscar

This places the Obfuscar.Console.exe executable in the directory:

C:\Program Files\PackageManagement\NuGet\Packages\Obfuscar.2.2.9\tools

Change the Version number in the directory name accordingly.

Had to use these settings for a .NET MVC web app (not core). I was getting default document errors without the changes to Public and Private API variables. Assumes running from NuGet created folder within "packages" at root of solution.

<?xml version='1.0'?>
<Obfuscator>
  <Var name="InPath" value="..\..\..\MyWebApp.WebApp\bin\" />
  <Var name="OutPath" value="..\..\..\MyWebApp.WebApp\bin\obfuscar" />
  <Var name="KeepPublicApi" value="true" /><!--Keep true for web app.-->
  <Var name="HidePrivateApi" value="false" /><!--Keep false for web app.-->
  <Var name="RenameProperties" value="true" />
  <Var name="RenameEvents" value="true" />
  <Var name="RenameFields" value="true" />
  <Var name="UseUnicodeNames" value="true" />
  <Var name="HideStrings" value="true" />
  <Var name="OptimizeMethods" value="true" />
  <Var name="SuppressIldasm" value="true" />
  <Module file="$(InPath)\MyWebApp.DataAccess.dll" />
  <Module file="$(InPath)\MyWebApp.Helpers.dll" />
  <Module file="$(InPath)\MyWebApp.Models.dll" />
  <Module file="$(InPath)\MyWebApp.WebApp.dll" />
</Obfuscator>

Autmate Obfuscar in Publish procedure

(Tested in .net 5)

Install Obfuscar Global Tool as .Net Tool global or local in your solution/project root:

dotnet new tool-manifest
dotnet tool install --local Obfuscar.GlobalTool

Place obfuscar.xml at your project root. Then add folowing in project file (.csproj)

  <Target Name="Obfuscation" AfterTargets="Build" Condition=" '$(Configuration)' == 'Release'">
    <Exec WorkingDirectory="$(TargetDir)" Command="obfuscar.console $(ProjectDir)obfuscar.xml" />
    <ItemGroup>
      <DistFiles Include="$(OutDir)Obfuscated\**\*.dll; $(OutDir)Obfuscated\**\*.exe" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.RecursiveDir)%(DistFiles.Filename)%(DistFiles.Extension)</RelativePath>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

or use ComputeFilesToPublish target (not tested well).

obfuscar.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Obfuscator>
    <Var name="InPath" value="." />
    <Var name="OutPath" value="$(InPath)\Obfuscated" />
    <AssemblySearchPath path="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\5.0.5" />
    <Module file="$(InPath)\dependencyToObfuscate.dll" />
    <Module file="$(InPath)\mylibraryToObfuscate.dll" />
</Obfuscator>

Refer to more about obfuscar configuration file (obfuscar.xml).

Note: this solution does not work for dotnet 5 single file output publish.

Hope be helpful. Thanks to any better Solution for this perpose. I tested this in asp.net core 5 project on windows.

Related