4.6.x까지 잘 쓰던 confuserEx가 net5 지원을 안 해서 알아봄
1. 빌드하고
2. obfuscar 돌리고
3. publish 전 난독화 파일로 교체하는 형태임
obfuscar global 설치
# dotnet tool install --global Obfuscar.GlobalTool --version 2.2.31
VS 2019 설정
Project\Properties\PublishProfiles\FolderProfile.pubxml:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>..\publish\</PublishDir> <!--Optional for custom publish folder-->
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net5.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>False</PublishReadyToRun>
</PropertyGroup>
</Project>
obfuscar.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Obfuscator>
<Var name="InPath" value=".\obj\Release\net5.0-windows\win-x64" />
<Var name="OutPath" value=".\obj\Release\net5.0-windows\win-x64\Obfuscated" />
<!--To solves "Unable to resolve dependency: PresentationFramework" for WPF project.-->
<AssemblySearchPath path="C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\5.0.8" />
<!--To solves something what your project depdends like "Unable to resolve dependency: ...".-->
<AssemblySearchPath path=".\bin\Release\net5.0-windows" />
<Module file="$(InPath)\YourProjectOutput.dll" />
</Obfuscator>
Project.csproj:
<Project Sdk="Microsoft.NET.Sdk">
...
<!--Append these at the end of your .csproj file.-->
<Target Name="Obfuscation" AfterTargets="AfterCompile" Condition="'$(PublishProtocol)'!=''">
<Message Text="Obfuscating" Importance="high" /><!--Optional to log a message.-->
<Exec Command="obfuscar.console.exe obfuscar.xml" /><!--Assuming you have installed Obfuscar.GlobalTool(https://www.nuget.org/packages/Obfuscar.GlobalTool). -->
<Exec Command="COPY $(ProjectDir)$(IntermediateOutputPath)Obfuscated\$(TargetFileName) $(ProjectDir)$(IntermediateOutputPath)$(TargetFileName)" />
</Target>
</Project>
CLI 로 하려면
publish 할 때 --no-build 옵션이 중요함
# obfuscar.console.exe obfuscar.xml
# dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true --self-contained false --no-build
참고
publish 할 때, dll 파일 어디선가 사용 중 이면 오류 뜨니 주의하자
(ILSPY로 결과물 확인하다가 오류 떠서 괜히 시간 버림 ㅠㅠ)
링크
https://www.nuget.org/packages/Obfuscar.GlobalTool
https://github.com/obfuscar/obfuscar
https://github.com/obfuscar/obfuscar/issues/314)