Hi. I use KeInsertQueueApc to inject user mode shellcode. How can I wait until the user mode shellcode execution finished.
Code: Select all
procedure ApcProc(hEvent: THandle); stdcall;
begin
// execute shellcode & trigger event when done
SetEvent(hEvent);
end;
procedure ThreadProc(lpParam: Pointer); stdcall;
begin
ExitThread(0);
end;
procedure WaitQueuedApc();
var
hThread: THandle;
hEvent: THandle;
dwThreadId: DWORD;
const
MAX_WAIT = 5000; // 5 seconds
TYPE_FLAGS = (MB_ICONINFORMATION or MB_TOPMOST);
begin
hEvent := CreateEvent(nil, False, False, '__evtApc');
if hEvent <> 0 then
begin
hThread := CreateThread(nil,
0,
@ThreadProc,
nil,
CREATE_SUSPENDED,
dwThreadId);
if hThread <> 0 then
begin
if QueueUserApc(@ApcProc,
hThread,
hEvent) then
begin
ResumeThread(hThread);
if WaitForSingleObject(hEvent,
MAX_WAIT) = WAIT_OBJECT_0 then
MessageBox(0,
'Apc shellcode completed',
'success',
TYPE_FLAGS);
end;
CloseHandle(hThread);
end;
CloseHandle(hEvent);
end;
end;