是否可以使用像 Reflector 这样的反汇编器查看成员变量的硬编码值?

发布于 2024-08-07 13:52:08 字数 258 浏览 1 评论 0原文

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

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

    public void Foo()
    {
        ...
    }
}

谢谢!

Given the example source code below, is it possible for someone to see the value of _secret using a disassembler? I didn't see a way to get at the value via Reflector, but I haven't used it very much. Assume the code is not obfuscated in any way.

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

    public void Foo()
    {
        ...
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-08-14 13:52:08

它在 Reflector 的构造函数中可见。

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

构造函数

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

转换为具有在方法 .ctorFoo 下的 Reflector 中可见的

。您还可以在 Foo::.ctor : void 中的 ildasm(随 Microsoft Visual Studio 一起提供)中查看此信息:

.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
);

It's visible in the constructor in Reflector.

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

translates to having constructor

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

which is visible in Reflector under Foo in method .ctor.

You can also see this information in ildasm (ships with Microsoft Visual Studio) in 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

Finally, if someone knows the name of your type and the name of your private field, you can obtain the value as such:

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

Of course, I can always see all of your private fields with

var fields = typeof(Foo).GetFields(
    BindingFlags.Instance | BindingFlags.NonPublic
);
潜移默化 2024-08-14 13:52:08

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

Yes, it is possible. The hard-coded value will be present in the IL and will be viewable via any .NET disassembler. Since this is a field, its initialization from the literal will be viewable in the constructor in Reflector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文