Need help learning how to detour a functtion stored in a DLL

So I don't have access to the sourcecode of the dll itself however I do have the function names and the exported function names themselves.

what I'm wanting to do is something I've done in Lua before. Basically in lua I've detoured a function like this before https://pastebin.com/P1rPjkct

This basically keeps a copy of the original function, and then overides the global function to add in a if statement to determin if it should perform the original functionality or if it should do something differant instead.

I want to be able to do the same but with functions created in C++ aswell. I have a lengthy list of functions I'm wanting to target and the additional checks I want to put in place but I don't know how to really override the functions themself. I'd also want to be able to prevent any other unexpected 3rd party software trying to bypass the checks I've added to those functions.

So basically I want the checks I'm adding to apply to any usage of the function, be it my usage of the function or anybody else using the exported function in their project
So the DLL is loaded by processes that you don't control and you want to modify the behavior of functions in the DLL? Here's what you do: create a DLL with the same name that exports the same symbols. In the DllMain for the load library event, load the original DLL and obtain the pointers for the exported symbols by calling GetProcAddress(). Then in each of the exported functions add your logic before and after the call to the original DLL. For example:
1
2
3
4
5
int make_snafucated(void *foo){
    if (!some_check(foo))
        return 0;
    return global_state->call_make_snafucated(foo) + 5;
}

You will need to know in detail the parameters the functions take and their semantics.
Last edited on
in my situation I don't really have source code access to the dll source code or the exe thats loading it and my own dll. For context I'm working with a game called Garry's Mod.

The game has a file in it called lua_shared.dll which has a function that I wish to overide in a way where evem if somebody were to use get procaddres in some third dll, it would make it so that even if they try to use the function exported from lua_shared it would still use my overriden function instead
alternativly is there any way I could detect if that exported function was called from anywhere in the program?
if you know the function prototypes and the dll name and the basic info you can rebuild that dll with do-nothing functions and the program will happily do your version instead.
What this may break is an entirely different issue, but you can do it.

you can also hex-edit the dll. this is nontrivial but you can certainly hack any of the contained code to do something else. Some programs check integrity of modules and refuse to work, and you have to hack the check out as well in that case, or re-hash your new dll and inject the result back into their checking code, or other annoying things.

Many games, someone already did the dirty work and would have a how-to on a modding site. Maybe you can get what you need from someone else's efforts? Some games, the devs help the modders as well, if you have a forum or line of contact. Depends on the game... competition online games where modding == cheating, you can expect no end of problems trying to do this. Single player modder friendly games, you can get all the support and help in the world. And anything in between.

visual studio used to have a depends.exe that could tell you what dlls an exe uses, including nested dlls called by other dlls, all the way down to the system ones.
Last edited on
jonnin wrote:
visual studio used to have a depends.exe

3rd party updated dependency tool: Dependencies.
https://github.com/lucasg/Dependencies

x86 and x64 versions available, latest version 1.10 dated 29 December 2019.

Source code is available.
Topic archived. No new replies allowed.