【问题标题】:Is it possible to see a member variable's hard-coded value using a disassembler like Reflector?是否可以使用像 Reflector 这样的反汇编程序来查看成员变量的硬编码值?
【发布时间】:2009-10-13 18:42:43
【问题描述】:

鉴于下面的示例源代码,是否有人可以使用反汇编程序查看_secret 的值?我没有看到通过 Reflector 获得价值的方法,但我并没有经常使用它。假设代码没有以任何方式混淆。

class Foo
{
    private string _secret = @"all your base are belong to us";

    public void Foo()
    {
        ...
    }
}

谢谢!

【问题讨论】:

    标签: .net reflector disassembly


    【解决方案1】:

    在 Reflector 的构造函数中可见。

    class Foo { private string _secret = @"all your base are belong to us"; }
    

    翻译为有构造函数

    public Foo() { this._secret = "all your base are belong to us"; }
    

    在 Foo 方法 .ctor 下的 Reflector 中可见。

    您还可以在ildasm(Microsoft Visual Studio 附带)中查看此信息Foo::.ctor : void:

    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed {
        // Code size       19 (0x13)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  ldstr      "all your base are belong to us"
        IL_0006:  stfld      string Playground.Foo::_secret
        IL_000b:  ldarg.0
        IL_000c:  call       instance void [mscorlib]System.Object::.ctor()
        IL_0011:  nop
        IL_0012:  ret
    } // end of method Foo::.ctor
    

    最后,如果有人知道你的类型名和你的私有字段名,你可以这样获取值:

    object o = typeof(Foo).GetField(
        "_secret",
        BindingFlags.Instance | BindingFlags.NonPublic
    ).GetValue(f);
    Console.WriteLine(o); // writes "all your base are belong to us" to the console
    

    当然,我总是可以看到你所有的私有字段

    var fields = typeof(Foo).GetFields(
        BindingFlags.Instance | BindingFlags.NonPublic
    );
    

    【讨论】:

    • 我以为它在里面,但我找不到。非常感谢!
    【解决方案2】:

    是的,这是可能的。硬编码的值将出现在 IL 中,并且可以通过任何 .NET 反汇编程序查看。由于这是一个字段,因此可以在 Reflector 的构造函数中查看其从字面量初始化的内容。

    【讨论】:

      猜你喜欢
      • 2011-05-09
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      相关资源
      最近更新 更多