Reputation: 139
I'm trying to obfuscate an .exe file obtained after compiling my .NET 6 project with the "Produce a single file" option, the problem is that no obfuscator works on it, I wanted to know if anyone knows why?
Thanks in advance for your answer
Upvotes: 5
Views: 8459
Reputation: 1286
When you publish a .NET application as a single file, the actual code is located in the DLL file. The EXE file is just a launcher.
Returning to the obfuscation, the idea is to obfuscate the assembly right after it is placed to the intermediate directory by a compiler. E.g. if you use ArmDot you just write:
<Target Name="Protect" AfterTargets="AfterCompile" BeforeTargets="BeforePublish">
<ItemGroup>
<Assemblies Include="$(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)" />
</ItemGroup>
<ArmDot.Engine.MSBuildTasks.ObfuscateTask
Inputs="@(Assemblies)"
ReferencePaths="@(_ResolveAssemblyReferenceResolvedFiles->'%(RootDir)%(Directory)')"
SkipAlreadyObfuscatedAssemblies="true"
/>
</Target>
The same approach can be used with any obfuscator. The key thing is to use the following path: $(ProjectDir)$(IntermediateOutputPath)$(TargetFileName).
After that, the obfuscated assembly (DLL) is published.
Upvotes: 1
Reputation: 406
You have to obfuscate main application dll, which is located in the "obj\Release\net6.0-windows\win-x64" folder and copy obfuscated dll to the path.
Here is a working example using Obfuscar. These lines are located in the .csproj file.
<Target Name="Obfuscation" AfterTargets="AfterCompile" Condition="'$(PublishProtocol)'!=''">
<Exec Command=""$(Obfuscar)" obfuscar.xml" />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(PublishProtocol)'!=''">
<Exec Command="COPY "C:\Users\Application\obj\Release\net6.0-windows\win-x64\Obfuscated\Application.dll" "C:\Users\Application\obj\Release\net6.0-windows\win-x64\Application.dll"" />
</Target>
After that, when you publish single file exe, your application code inside the archive will be obfuscated.
Upvotes: 3