Reloaded-Project / Reloaded.Hooks

Advanced native function hooks for x86, x64. Welcome to the next level!
GNU Lesser General Public License v3.0
213 stars 33 forks source link

Looking for example of how to hook to a call in System.Net #11

Closed AGenius closed 2 years ago

AGenius commented 2 years ago

I have a need to hook the System.net.WebRequest.DefaultWebProxy

We use a Proxy internally and an application we use appears to be using this call to determine if a proxy is in use on the PC's

Obviously we need the Proxy in place but the application will not allow it to work if it detects

It appears to use WebRequest.DefaultWebProxy and if it returns anything other then null it will fail

Is there someone who can provide the example of the setup for this hook intercept?

Thanks in advance.

Sewer56 commented 2 years ago

Not a direct example as I've never really tried to use the library to hook .NET methods themselves but here's an idea:

public unsafe void* GetFunctionPointer(Type type, string name)
{
    var method = type.GetMethod(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).MethodHandle;
    RuntimeHelpers.PrepareMethod(method);
    return (void*) method.GetFunctionPointer();
}

Grab the method using reflection first and then force the method (the getter can be treated as a method IIRC) to be JIT'ted using RuntimeHelpers.PrepareMethod();; then you can grab a pointer to the function you intend to hook.

Since I've no experience hooking in a managed environment, I cannot guarantee this will work; but this is at least an idea of what you could do.

Alternatively; try to figure out the Windows API calls being used under the hood for the code you plan to hook (if there's any), and hook those calls instead.

daemitus commented 2 years ago

Use Harmony to hook .NET methods. its much easier.