如何注入 .NET 代码来更改函数的返回值

发布于 2024-07-17 00:42:57 字数 415 浏览 7 评论 0原文

我目前正在研究如何编写可以注入另一个 .NET 进程并覆盖函数返回值的 .NET 代码。 我的想法是,我的代码将更改方法调用的返回值,如下所示:

Public Function DoSomething() As String

Return "Value"

End Function

我的代码将拦截对 DoSomething() 的调用并返回我自己的自定义字符串,而不是“Value”。

我猜想我需要使用 .NET Profiling API 来完成此类事情。 有没有人有使用 Profiling API 的经验,如果有的话,您认为这种代码注入是否可能?

我不打算将其用于任何恶意行为,我只是有兴趣在 .NET 中尝试一些非常低级的东西,看看是否可以完成。

干杯。 贾斯。

I am currently researching how to write .NET code that can be injected into another .NET process and override function return values. The idea is that my code would alter the return values of a method call, like this:

Public Function DoSomething() As String

Return "Value"

End Function

My code would intercept calls to DoSomething() and return my own custom string, rather then "Value".

I'm guessing that I would need to look o using the .NET Profiling API to do this type of thing. Does anyone have experience of using the Profiling API, and if so, do you think that this code injection is possible?

I don't intend to use this for anything malicious, I'm just interested in trying something very low-level in .NET to see if it can be done.

Cheers.
Jas.

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

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

发布评论

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

评论(5

薆情海 2024-07-24 00:42:57

您可以使用 TypeMock 隔离器 来完成此操作。

我没有 TypeMock,但根据 他们的一些示例,我认为你可以用这样的东西来做到这一点:(首先是C#,然后我会尝试翻译成VB)

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
    DoSomething();
    recorder.Return("My Own Custom String");
}

Using recorder As RecordExpectations = RecorderManager.StartRecording()
    DoSomething
    recorder.Return("My Own Custom String")
End Using

看起来TypeMock对于开源项目是免费的,否则它需要花钱。 但他们已经为您完成了 Profiling API 编码,因此您可以担心解决问题,而不是编写繁琐的 C++。

You can do this using TypeMock Isolator.

I don't have TypeMock, but based on some of their examples, I think you could do it with something like this: (C# first, then I'll try to translate to VB)

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
    DoSomething();
    recorder.Return("My Own Custom String");
}

Using recorder As RecordExpectations = RecorderManager.StartRecording()
    DoSomething
    recorder.Return("My Own Custom String")
End Using

Looks like TypeMock is free for open-source projects, otherwise it costs money. But they've already done the Profiling API coding for you, so you can worry about solving your problem, not about writing hairy C++.

¢好甜 2024-07-24 00:42:57

Detours 是 Win32 的实现方法。 我从未见过 Detours.NET,但也许他们正在做的事情会给你一个关于如何实现你想要做的事情的提示。

Detours is how you do it for Win32. I've never seen Detours.NET, but maybe something they are doing will give you a hint on how to implement what you are trying to do.

蝶…霜飞 2024-07-24 00:42:57

您可能会调查消息接收器......
这是一个高级领域,但基本上,它确实做了您想要做的事情,拦截从一个函数到另一个函数的调用,并在拦截期间执行您自己的代码...消息接收器可以链接在一起,并且可以写为发送消息接收器,在发起呼叫时进行处理,也可以写为接收接收器,在另一端进行处理

这是一个 更详细的解释

You might investigate message sinks...
This is an advanced area, but basically, it does exactly what you are trying to do, intercept a call frm one function to another, and during the interception, execute code of your own... The message sinks can be linked together, and can be written as send message sinks, to be processed as the call is initiated, or alternatively as receive sinks, to be processed on the other end

Here's a more detailed explanation

乜一 2024-07-24 00:42:57

PostSharp 可以做到这一点,但不是在运行时,它会在最初编写代码后修改代码。

PostSharp can do this, but not at runtime, it modifies the code after it was originally written.

倒数 2024-07-24 00:42:57

Typemock Isolator 附带的 CThru 项目可以为您提供此功能。
该项目是一个开源项目,但它使用 Typemock Isolator。
它将允许您注册回调方法并在运行时更改其行为。
它不限于运行单元测试。
需要注意的一件事是,一旦使用它,您的性能就会受到影响。

The CThru project that comes with Typemock Isolator can give you this functionallity.
The project is an open source but it uses Typemock Isolator.
It will let you register your callback method and change its behavior at runtime.
It is not limited to running with unit tests.
One thing to note is you'll get a performance hit once you'll use it.

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