Code: Select all
library HookProcessCreation;
{$IMAGEBASE $57000000}
uses Windows, SysUtils, madCodeHook, JWaWinBase, JwaWinNT, JwaWinType, Native;
var NtCreateProcessNextHook: function(ProcessHandle: PHANDLE; DesiredAccess: ACCESS_MASK; ObjectAttributes: POBJECT_ATTRIBUTES; InheritFromProcessHandle: HANDLE; InheritHandles: ByteBool; SectionHandle: HANDLE; DebugPort: HANDLE; ExceptionPort: HANDLE): NTSTATUS; stdcall;
var NtCreateProcessExNextHook: function(ProcessHandle: PHANDLE; DesiredAccess: ACCESS_MASK; ObjectAttributes: POBJECT_ATTRIBUTES; InheritFromProcessHandle: HANDLE; InheritHandles: ByteBool; SectionHandle: HANDLE; DebugPort: HANDLE; ExceptionPort: HANDLE; SafeFlags : DWORD): NTSTATUS; stdcall;
function NtCreateProcessCallbackProc(ProcessHandle: PHANDLE; DesiredAccess: ACCESS_MASK; ObjectAttributes: POBJECT_ATTRIBUTES; InheritFromProcessHandle: HANDLE; InheritHandles: ByteBool; SectionHandle: HANDLE; DebugPort: HANDLE; ExceptionPort: HANDLE): NTSTATUS; stdcall;
begin
Result := NtCreateProcessNextHook(ProcessHandle, DesiredAccess, ObjectAttributes, InheritFromProcessHandle, InheritHandles, SectionHandle, DebugPort, ExceptionPort);
MessageBox(0,'Hooked','Hooked',MB_OK);
end;
function NtCreateProcessExCallbackProc(ProcessHandle: PHANDLE; DesiredAccess: ACCESS_MASK; ObjectAttributes: POBJECT_ATTRIBUTES; InheritFromProcessHandle: HANDLE; InheritHandles: ByteBool; SectionHandle: HANDLE; DebugPort: HANDLE; ExceptionPort: HANDLE; SafeFlags : DWORD): NTSTATUS; stdcall;
begin
Result := NtCreateProcessExNextHook(ProcessHandle, DesiredAccess, ObjectAttributes, InheritFromProcessHandle, InheritHandles, SectionHandle, DebugPort, ExceptionPortm, SafeFlags);
MessageBox(0,'Hooked','Hooked',MB_OK);
end;
begin
HookAPI('ntdll.dll', 'NtCreateProcess', @NtCreateProcessCallbackProc, @NtCreateProcessNextHook);
HookAPI('ntdll.dll', 'NtCreateProcessEx', @NtCreateProcessExCallbackProc, @NtCreateProcessExNextHook);
end.
The other problem is that I cannot get image name from ObjectAttributes parameter. The name us UNICODE_STRING. I though that in my callback the parameter is already initialized but it crashes if I access it. I wrote a little driver a while ago which read and wrote to Registry and I know unicode strings but I just can't make this work. I also saw that Object attributes is an optional parameter, so I guess it can be empty. Is there a way at all to get the path and exe name of the process to be created.
And one more thing. Is there a way to inject the hook from a kernel mode driver. Probably not becouse madCodeHook is user mode library, but madshi uses a driver for his internal purposes. Just curious.
Again sorry for bringing this up again. I like to solve my own problems but this proved to be a steel wall for me.