Code Injection vs. Calling remote DLL function

Started by
6 comments, last by phear- 13 years, 7 months ago
For my app (Window Detective) i am using code injection to obtain information from other processes.
I am currently using two different methods:
1) DLL injection using hooks - SetWindowsHookEx(WH_CALLWNDPROC, ...). I use this to monitor messages sent to windows. Functions are defined in the DLL and called by the remote process (via Window's hook mechanism).
2) Code injection using CreateRemoteThread. I use this to extract information from within the remote process. I write a function which i then inject into the process.

I was wondering if i could use the first method for both cases. That is, can i call a function from my DLL that is loaded into a remote process? It would be a whole lot easier since i don't have to deal with all the caveats mentioned here.
[Window Detective] - Windows UI spy utility for programmers
Advertisement
I could do what is done here. That is, the function that is injected using CreateRemoteThread gets the module handle of my DLL (assuming it is already loaded, by the hook method), get the proc address of a function in the DLL, then call that function.
Therefore the function is generic enough to call any DLL function that is passed to it - as long as that function does not take any parameters.

I don't suppose there is a better way.
[Window Detective] - Windows UI spy utility for programmers
Can't you just define a structure with a function pointer, use CreateRemoteThread and use a pointer to that structure as the context param, then in your thread proc, cast to pointer to structure and then you have your function pointer?

Alternatively, if you wanted to be able to call a variety of functions, then pass the module handle in the structure instead of a function pointer. This is kind of what you said, except instead of the remote thread "getting the module handle" you're giving it to the remote thread at launch.
I believe what you described is what i said in my second post (which is further described in the link). I don't think either one of us described the methods very well, perhaps a code snippet would help explain things...

The limitation of that method is that all remote functions need to have the same signature. I think they can have parameters (contrary to what i said earlier) so i will need to pass a struct containing the data the function needs (similar to a struct i would pass to CreateRemoteThread).
The advantage is that by defining the function in the DLL, i do not need to pass every function address, string and any other data to the remote function.

I will think about it and see if i can get that working.
[Window Detective] - Windows UI spy utility for programmers
Jeffrey Richter's book Advanced Windows has a chapter in which he discusses all of this stuff. It's been a while so let me google ... yeah, according to the bibliography of this Dr. Dobb's article Chapter 18 of Advanced Windows is called "Breaking Through Process Boundary Walls"; this is the chapter I'm talking about.

I'm not sure Advanced Windows is still up to date, however. It apparently has a 4th edition now titled Programming Applications for Microsoft Windows but I don't know if the stuff about crossing process boundary walls is still included; I had the old book.
Quote:Original post by jwezorek
Jeffrey Richter's book Advanced Windows has a chapter in which he discusses all of this stuff. It's been a while so let me google ... yeah, according to the bibliography of this Dr. Dobb's article Chapter 18 of Advanced Windows is called "Breaking Through Process Boundary Walls"; this is the chapter I'm talking about.

I'm not sure Advanced Windows is still up to date, however. It apparently has a 4th edition now titled Programming Applications for Microsoft Windows but I don't know if the stuff about crossing process boundary walls is still included; I had the old book.


BTW, the newest version of that book is called "Windows via C/C++". It's basically a complete re-write of the original, which is why the naming scheme was changed. Amazing book though, must read.
fyi, i have got it working now. I basically pass a function name (char*) to the injected function, which in turn gets the module handle and function address then calls it. I also found a way to pass data to that function, albeit just a single struct parameter.
[Window Detective] - Windows UI spy utility for programmers
I don't think i'm quite understanding your dilemma because I don't see why you cant call your function directly from your code... Why do you need to get the module address and then get a pointer to the function?

When you write a function hook, its solely supposed to be used for redirecting the execution of the remote process. Sending execution from the hook function to another function in your DLL should be as simple as calling it... This is for "static" injection though, where your DLL is added to the program before it is run. "Dynamic" injection is when the remote program gets injected while it is running, but even then I think it still is possible (though i've only worked with static injection before).
__________________________________________
Eugene Alfonso
GTP | Twitter | SFML | OS-Dev

This topic is closed to new replies.

Advertisement