【问题标题】:Add Friend Classes to Exclusion List of Obfuscation将好友类添加到混淆排除列表
【发布时间】:2017-07-03 00:34:24
【问题描述】:

我正在使用 Dotfuscator CE 和 Visual Studio 2015 Update 3 来混淆我的 .Net 程序集。 我们知道公共类型和成员默认情况下不会被混淆。 我很想知道我们如何在排除列表中添加朋友类,以免混淆?

这是我用来混淆我的 DLL 的配置文件。

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE dotfuscator SYSTEM "http://www.preemptive.com/dotfuscator/dtd/dotfuscator_v2.3.dtd">
<dotfuscator version="2.3">
  <propertylist>
    <property name="SourceDirectory" value="This Path Will Be Replaced By Visual Studio" />
    <property name="SourceFile" value="This Filename Will Be Replaced By Visual Studio" />
  </propertylist>
  <global>
    <option>quiet</option>
  </global>
  <input>
    <asmlist>
      <inputassembly refid="e4ca1ab5-26cb-4ab7-9621-87063f75a38f">
        <option>honoroas</option>
        <option>stripoa</option>
        <option>library</option>
        <option>transformxaml</option>
        <file dir="${SourceDirectory}" name="${SourceFile}" />
      </inputassembly>
    </asmlist>
  </input>
  <output>
    <file dir="${SourceDirectory}" />
  </output>
  <renaming>
    <option>xmlserialization</option>
    <mapping>
      <mapoutput overwrite="true">
        <file dir="${SourceDirectory}\Dotfuscated" name="Map.xml" />
      </mapoutput>
    </mapping>
    <referencerulelist>
      <referencerule rulekey="{6655B10A-FD58-462d-8D4F-5B1316DFF0FF}" />
      <referencerule rulekey="{7D9C8B02-2383-420f-8740-A9760394C2C1}" />
      <referencerule rulekey="{229FD6F8-5BCC-427b-8F72-A7A413ECDF1A}" />
      <referencerule rulekey="{2B7E7C8C-A39A-4db8-9DFC-6AFD38509061}" />
      <referencerule rulekey="{494EA3BA-B947-44B5-BEE8-A11CC85AAF9B}" />
      <referencerule rulekey="{89769974-93E9-4e71-8D92-BE70E855ACFC}" />
      <referencerule rulekey="{4D81E604-A545-4631-8B6D-C3735F793F80}" />
    </referencerulelist>
  </renaming>
  <sos mergeruntime="true">
    <option>version:v4</option>
    <option>sendanalytics</option>
    <option>dontsendtamper</option>
  </sos>
  <smartobfuscation>
    <smartobfuscationreport verbosity="all" overwrite="false" />
  </smartobfuscation>
</dotfuscator>

实际上,我有一个带有 Friend 访问说明符的 Model 类。我通过 PostAsJsonAsync 方法发布它的对象,例如

Dim result As HttpResponseMessage = client.PostAsJsonAsync(APIEndPoints.LOGIN, _LoginModel).Result

这里是朋友类:

Friend Class LoginModel

    Public AccessKey As String

    Public Password As String
End Class

接收请求和模型的API方法:

[HttpPost]
        [Route("authenticate")]
        public async Task<JsonResult> Authenticate([FromBody] LoginViewModel lvm)
// Here lvm.Accesskey is null

当 API 接收到请求和 LoginModel 时,其字段为空。如果我将我的 LoginModel 公开,那么它就可以工作。 注意:这只会在我混淆我的 DLL 时发生,否则该实现也适用于 Friend 类。

另请注意:朋友类在 VB.Net 中很常见。当在程序集中访问时,它们就像公共类一样工作,但在程序集之外它们是私有的。

【问题讨论】:

    标签: vb.net obfuscation dotfuscator


    【解决方案1】:

    根据您的说明,听起来您不仅要排除 Friend 类型的名称,还要排除这些类型中的 Public 字段的名称。无论上下文如何,我都将您的原始问题解释为想要排除任何标记为 Friend 的内容。

    这里很重要的一点是,就 Dotfuscator 的规则而言,排除一个类型并不会自动排除其成员

    以下是排除顶级朋友类型以及这些类型的公共和朋友字段的排除规则集:

    <excludelist>
      <type name=".*" regex="true" speclist="+notpublic">
        <comment>Exclude top-level types that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "private" in IL).</comment>
        <field name=".*" speclist="+public" regex="true">
          <comment>Exclude public fields of types the parent rule matches</comment>
        </field>
      </type>
    </excludelist>
    

    您也可以只排除您知道重命名时会造成麻烦的类型和成员,而不是使用基于可访问性的规则排除大量名称。下面是一个示例,假设 LoginModel 在程序集 YourAssembly 和命名空间 YourNamespace.Here 中定义:

    <excludelist>
      <type name="YourAssembly.YourNamespace.Here.LoginModel">
        <field name="AccessKey" signature="string" />
        <field name="Password" signature="string" />
      </type>
    </excludelist>
    

    (我注意到您对多个输入程序集使用相同的配置,但此规则仍然是安全的,因为如果输入程序集不包含指定的类型,则该规则将被忽略。)

    作为参考,the Professional Edition documentation on Exclusion Rules(和该页面的子主题)可能有用 - 社区版和专业版共享相同的配置文件格式,这两个版本都支持的功能。

    披露:我在 Dotfuscator 团队工作,负责 PreEmptive Solutions。

    【讨论】:

      【解决方案2】:

      如果您尝试排除输入程序集的 Friend 类型和成员,因为您的程序集具有 Friend Assembly,请注意 Dotfuscator 将自动排除此类代码元素重命名(Dotfuscator CE 提供的唯一一种混淆)和将发出以下警告:

      警告:NameOfYourInputAssembly 有非输入的朋友程序集并且处于库模式;内部成员不会被重命名或修剪。考虑添加朋友程序集作为增加混淆的输入。

      (此处的术语“内部”是 VB 的“朋友”关键字的 C# 等效项)。

      正如警告所暗示的,您可以改为将 Friend 程序集作为 Dotfuscator 的另一个输入。 如果这样做,Dotfuscator 可以重命名 Friend 类型和成员,并更新 Friend Assembly 以通过新名称引用这些类型和成员。


      如果您仍想排除朋友类型和成员,您可以使用以下重命名排除规则集来执行此操作,添加为配置文件中 &lt;renaming&gt; 标记的子项:

      <excludelist>
        <type name=".*" regex="true" speclist="+notpublic">
          <comment>Exclude types that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "private" in IL).</comment>
        </type>
        <type name=".*" regex="true" speclist="+nestedassembly">
          <comment>Exclude nested types that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "private" in IL).</comment>
        </type>
        <type name=".*" regex="true" excludetype="false">
          <comment>Select, but do not exclude, all types.</comment>
          <method name=".*" speclist="+assembly" regex="true">
            <comment>Exclude methods that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "assembly" in IL).</comment>
          </method>
          <field name=".*" speclist="+assembly" regex="true">
            <comment>Exclude fields that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "assembly" in IL).</comment>
          </field>
          <propertymember name=".*" speclist="+assembly" regex="true">
            <comment>Exclude properties that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "assembly" in IL).</comment>
          </propertymember>
          <eventmember name=".*" speclist="+assembly" regex="true">
            <comment>Exclude events that are only accessible to the assembly ("Friend" in VB, "internal" in C#, or "assembly" in IL).</comment>
          </eventmember>
        </type>
      </excludelist>
      

      编辑:我在此答案的上一版本中错过了嵌套类型。

      披露:我在 Dotfuscator 团队工作,负责 PreEmptive Solutions。

      【讨论】:

      • 感谢@Joe 提供详细信息,但解决方案无效。我正在更新我的问题以提供更多详细信息。简而言之,您的答案中提到的排除规则并未排除 Friend Classes 被混淆。
      • 当我在 Idasm 中看到混淆的 DLL 时,它表明 Friend 类的公共数据成员已被混淆为名称 a 和 b。
      • @AzazulHaq 根据您的说明,我已经发布了单独的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多