Silver
October 10th, 2005, 08:55
Hi guys,
A question and a thought... I've been playing with global hooks recently, leading me to my question. A global hookproc must be coded in a DLL which is then loaded into every process' address space. The normal DLL rules apply, so even though it's your DLL being loaded the data segments are unique to each application. One way around this is to create a DLL with a shared segment - that way, all the DLL instances and your "parent" process (the one that installed the hook) can communicate with each other and retrieve data.
All well and good, except where it comes to debugging. First of all, you can't directly debug a hookproc. My VS6 won't break on any breakpoints set in the hookproc (which is somewhat understandable), so I have to debug via PostMessage(). Second, if you did break to debug in the hookproc you're going to immediately screw the Windows messagepump, which will stall until your debug is finished and the hookproc returns. Third, there are always going to be multiple instances of the hookproc, all of which have access to the shared segment. So if data in the shared segment changes, you have no way to detect which instance did the changing. You can implement a mutex/csection or some other protective mechanism, but then you still have to somehow register which instance of the DLL (attached to a specific process) made the change. To clarify that a bit - you, as the coder, are not in control of where, when and how a new instance of the DLL is loaded. It's like thread synchronization, but with a moving target.
So the question: what's the best way to debug a hookproc and get data into it from the main application (my app) that loaded the hook?
The data question is a chicken-and-egg problem. Once you load a hook you have no way to get data into it other than via the shared segment, but you have to load the hook to have somewhere to send the data. You can't load the hook then call an exported func from the DLL because that's still the instance attached to your process. You can only include basic type data in the shared segment.
I've read up on this a bit and I can't see a way round it. All anyone seems to do with hooks is to Postmessage something to another app and that's it. I'm trying to do something more complex.
The thought: this could be an interesting way to make reversing more difficult. Adding some critical code into a hook would cause problems. The reverser would see multiple instances of the DLL, the data in the segment changing, but no way to know which DLL made the change and under what conditions. And of course breaking into the hookproc brings the whole thing crashing down...
Perhaps someone can see a large flaw in my thinking
A question and a thought... I've been playing with global hooks recently, leading me to my question. A global hookproc must be coded in a DLL which is then loaded into every process' address space. The normal DLL rules apply, so even though it's your DLL being loaded the data segments are unique to each application. One way around this is to create a DLL with a shared segment - that way, all the DLL instances and your "parent" process (the one that installed the hook) can communicate with each other and retrieve data.
All well and good, except where it comes to debugging. First of all, you can't directly debug a hookproc. My VS6 won't break on any breakpoints set in the hookproc (which is somewhat understandable), so I have to debug via PostMessage(). Second, if you did break to debug in the hookproc you're going to immediately screw the Windows messagepump, which will stall until your debug is finished and the hookproc returns. Third, there are always going to be multiple instances of the hookproc, all of which have access to the shared segment. So if data in the shared segment changes, you have no way to detect which instance did the changing. You can implement a mutex/csection or some other protective mechanism, but then you still have to somehow register which instance of the DLL (attached to a specific process) made the change. To clarify that a bit - you, as the coder, are not in control of where, when and how a new instance of the DLL is loaded. It's like thread synchronization, but with a moving target.
So the question: what's the best way to debug a hookproc and get data into it from the main application (my app) that loaded the hook?
The data question is a chicken-and-egg problem. Once you load a hook you have no way to get data into it other than via the shared segment, but you have to load the hook to have somewhere to send the data. You can't load the hook then call an exported func from the DLL because that's still the instance attached to your process. You can only include basic type data in the shared segment.
I've read up on this a bit and I can't see a way round it. All anyone seems to do with hooks is to Postmessage something to another app and that's it. I'm trying to do something more complex.
The thought: this could be an interesting way to make reversing more difficult. Adding some critical code into a hook would cause problems. The reverser would see multiple instances of the DLL, the data in the segment changing, but no way to know which DLL made the change and under what conditions. And of course breaking into the hookproc brings the whole thing crashing down...
Perhaps someone can see a large flaw in my thinking
