So in the end APC injection cannot be done reliably, you need "luck" to find an alertable thread, and you have no means of performing an injection "now". So it's not suitable as a general purpose technique.
A forum for reverse engineering, OS internals and malware analysis
Trying to inject an APC into an already running process is far from reliableSpeaking soley about APCs - Not necessarily, granted this depends on which context you are speaking about be it a thread you created in the target process or a preexisting thread in the process which you do not "own". If you spawn a remote dummy thread in a suspended state and queue an APC to it your code will execute as soon as the thread is resumed. I've described this a bit on the forum already in another post.
Brock wrote:Speaking soley about APCs - Not necessarily, granted this depends on which context you are speaking about be it a thread you created in the target process or a preexisting thread in the process which you do not "own". If you spawn a remote dummy thread in a suspended state and queue an APC to it your code will execute as soon as the thread is resumed. I've described this a bit on the forum already in another post.I wasn't referring to the general limitations APCs have. I was referring to the fact that, due to the way APCs are actually implemented in Windows, applications can easily defend themselves against them. All usermode APCs are dispatched through KiUserApcDispatcher in their target process. Just hook that function and you gain full control over what APCs are executed inside your process.
APC injections into processes you spawn yourself are very reliable indeed. That being said: Trying to inject an APC into an already running process is far from reliable. Especially given the fact how easy a process can defend itself from such kind of attacks.But since we both aren't native speakers we most likely simply talked at cross-purposes. Anyways, to clarify what I meant to say in the first place: APC injection into an already running process is very unreliable not only because of the waiting state requirements APCs have, but also due to the fact that a process can gain complete control of the APC dispatching mechanism for all threads that belong to the process simply by hooking KiUserApcDispatcher in his own address space. So even an unprivileged application can protect itself from APCs issued by privileged applications and even kernel mode drivers.
newgre wrote:Well, couldn't you simply suspend all threads of that process, write the original KiUserAPCDispatcher routine, perform APC injection, restore the APC routine and resume all threads?Sure, you can. But if you already can do all those nasty things, why do you bother with APCs at all? :) You could simply redirect the code flow by overwriting code you know is going to be executed at one point or another.