探究——C# .net 代码混淆/加壳

  • 探究——C# .net 代码混淆/加壳已关闭评论
  • 113 次浏览
  • A+
所属分类:.NET技术
摘要

  保密。  先查询一下常见的加壳工具:  那就先用Obfuscar试试水。方式一:nuget安装(推荐,这种方式可以针对性下载各.net版本对应工具包)


背景:

  保密。

过程:

  先查询一下常见的加壳工具:

  • DotFuscator,官方自带,据说免费版混淆程度不高
  • Virbox Protector,很好很优秀,但是收费
  • NET Reactor,可能会被识别为病毒
  • Obfuscar,开源,可以用dotnet tool或项目构建的方式进行使用

  那就先用Obfuscar试试水。

方式一:nuget安装(推荐,这种方式可以针对性下载各.net版本对应工具包)

  1、在项目中使用nuget安装obfuscar

探究——C# .net 代码混淆/加壳

   2、在项目根目录下找到packagesObfuscar.2.2.38tools,将Obfuscar.Console.exe拷到要加密文件的文件夹中

探究——C# .net 代码混淆/加壳

   3、新建一个Obfuscar.xml 文件放到相同目录下,内容如下:

探究——C# .net 代码混淆/加壳探究——C# .net 代码混淆/加壳

<?xml version='1.0'?> <Obfuscator>   <Var name="InPath" value="." />   <Var name="OutPath" value=".Obfuscar" />   <Var name="KeepPublicApi" value="true" />   <Var name="HidePrivateApi" value="true" />   <Var name="HideStrings" value="true" />   <Var name="UseUnicodeNames" value="true" />   <Var name="ReuseNames" value="true" />   <Var name="RenameFields" value="true" />   <Var name="RegenerateDebugInfo" value="true" />    <Module file="$(InPath)Logic.dll" />  </Obfuscator>

View Code

  下图中,Logic.dll是要加密的类库:

探究——C# .net 代码混淆/加壳

   4、用命令提示符cmd进入到目录下(可以在cmd里用cd指令跳转,也可以直接打开目标文件夹,然后在上方的文件路径那里直接替换成cmd后enter)

探究——C# .net 代码混淆/加壳

   5、执行>Obfuscar.Console.exe Obfuscar.xml

探究——C# .net 代码混淆/加壳

   6、在生成的Obfuscar文件夹中可以找到被加壳后的同名Logic.dll

探究——C# .net 代码混淆/加壳

  上述得到的Logic.dll即可被其他项目直接引用,加密后类似:

探究——C# .net 代码混淆/加壳

  方式二:dotnet tool(.net 6)

  1、新建一个Obfuscar.xml 文件放到类库所在目录,内容如下:

探究——C# .net 代码混淆/加壳探究——C# .net 代码混淆/加壳

<?xml version='1.0'?> <Obfuscator>   <Var name="InPath" value="." />   <Var name="OutPath" value=".Obfuscar" />   <Var name="KeepPublicApi" value="true" />   <Var name="HidePrivateApi" value="true" />   <Var name="HideStrings" value="false" />   <Var name="UseUnicodeNames" value="true" />   <Var name="ReuseNames" value="true" />   <Var name="RenameFields" value="true" />   <Var name="RegenerateDebugInfo" value="true" />   <Module file="$(InPath)PlanManager.dll" />   <Module file="$(InPath)MapManager.dll" />    <AssemblySearchPath path="C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App6.0.9" />   <AssemblySearchPath path="C:Program FilesdotnetsharedMicrosoft.NETCore.App6.0.9" /> </Obfuscator>

View Code

  其中,Module对应填入想要加壳的类库,可以添加多行,AssemblySearchPath根据自己.net的路径进行配置。

  2、在cmd中进入到上述目录中,执行命令:dotnet tool install --global Obfuscar.GlobalTool

  3、在cmd中执行命令:obfuscar.console Obfuscar.xml

  4、在上述目录中找到自动生成的Obfuscar文件夹,加壳后的类库就存放在里面,拷贝出来即可使用。

PS:.net6的带WebAPI的exe好像加壳失败,待测试。

方式三:项目构建

  1、在csproj 项目文件中添加安装Obfuscar的代码:

探究——C# .net 代码混淆/加壳探究——C# .net 代码混淆/加壳

  <ItemGroup>     <PackageReference Include="Obfuscar" Version="2.2.33">       <PrivateAssets>all</PrivateAssets>       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>     </PackageReference>   </ItemGroup>

View Code

  2、在项目中添加一个Obfuscar.xml 文件,内容跟方式一的类似,再在csproj 项目文件中设置更新

探究——C# .net 代码混淆/加壳探究——C# .net 代码混淆/加壳

  <ItemGroup>     <None Update="Obfuscar.xml">       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>     </None>   </ItemGroup>

View Code

  3、在csproj 项目文件中设置自动构建

探究——C# .net 代码混淆/加壳探究——C# .net 代码混淆/加壳

  <Target Name="ObfuscarTask" AfterTargets="AfterBuild">     <PropertyGroup>       <ObfuscateCommand>$(Obfuscar) "Obfuscar.xml"</ObfuscateCommand>     </PropertyGroup>     <Exec WorkingDirectory="$(OutputPath)" Command="$(ObfuscateCommand)" />   </Target>

View Code

 PS:这种方式还没测试过,待测试

结语:

  懒得写。