A forum for reverse engineering, OS internals and malware analysis 

Forum for discussion about kernel-mode development.
 #27599  by halouworld
 Sat Jan 09, 2016 2:46 pm
I need intercept loadding PE files,such as EXE,DLL,SYS.

after readed a few docs,finally found two solutions:


First solution:
1. for EXEs, use PsSetCreateProcessNotifyRoutine , check EXE in my callbak function. if need block , call ZwTerminateProcess. (the problesm is I'm really not sure wether it is stable to call ZwTerminateProcess in callback routine)

2. for DLLs and SYSs,use PsSetLoadImageNotifyRoutine , if need block , copy <xor eax,eax; ret 0xC> to DllMain or <mov eax,status_access_denied , ret > to DriverEntry. I use MDL to make DLLMAIN writeable , but I found once I patched the DLLMain , the Patch always work , even unload my driver ,unless reboot the system. I tried to use NtProtectVirtualMemory to make DLLMAIN writeable, but sometimes it will cause system hungs or BSOD. I found reason on MSDN : in ImageLoad callback, can't perform operatings on usermode memory.

Second solution:
use minifilter , check file in IRP_MJ_ACQUIRE_FOR_SECTION_SYNCHRONIZATION. I think it's the best solution,because of Good compatibility and stable.
But I also got a problem :

Some common file operations also fire this irp, such as GetFileVersionInfo ,and extracticon , and all these operations open file with PAGE_EXECUTE flag .

I had think about using RtlWalkFrameChain to check whether the call comes from NTDLL!LdrLoadDll.
But I want to known is there any better ideals to determine whether it's An Actually PE Execute Operations?

Do you have any better solutions or suggestions ? :cry:
 #27601  by Vrtule
 Sat Jan 09, 2016 8:01 pm
First solution:
1. for EXEs, use PsSetCreateProcessNotifyRoutine , check EXE in my callbak function. if need block , call ZwTerminateProcess. (the problesm is I'm really not sure wether it is stable to call ZwTerminateProcess in callback routine)
If you are not interested in pre-Vista SP1 systems, you may use PsSetCreateProcessNotifyRoutineEx. This routine allows to block process creation (which is probably equivalent to your EXE blocking). It is a documented way how to do so.
2. for DLLs and SYSs,use PsSetLoadImageNotifyRoutine , if need block , copy <xor eax,eax; ret 0xC> to DllMain or <mov eax,status_access_denied , ret > to DriverEntry. I use MDL to make DLLMAIN writeable , but I found once I patched the DLLMain , the Patch always work , even unload my driver ,unless reboot the system. I tried to use NtProtectVirtualMemory to make DLLMAIN writeable, but sometimes it will cause system hungs or BSOD. I found reason on MSDN : in ImageLoad callback, can't perform operatings on usermode memory.
This is kinda dangerous because copy on write might be in action. And if the DLL belongs to KnownDlls, things may probably get even worse.