The question below is for educational purposes only and the discussed featured are not meant to alter registered DLLs or develop a malware but for learning and experiencing.
Recently I've been exploring few methods to load my own custom DLLs instead of an application's original DLLs. One of the methods that came up was the
.local
method.After experiencing with this method a little bit and after I removed the
KnownDlls
entry from the registry I managed to replace some system DLLs with my patched DLLs successfully.These are the DLLs:
However, the DLLs are IN the local folder:
However, there are still some DLLs that insist loading from the
system32
directory, although they are present in the local folder.Is there any way I can force the DLL's to load from the local folder instead of the system32 folder?
This is not an answer so much as a rambling, unsourced, brain dump.
It does serve to explain why I am not surprised at your result. This boils down, for me, to the crucial difference between CreateProcess and LoadLibrary, and how Win32 processes work. Normally, when using LoadLibrary, you are using it from within the process you want the dll to be loaded into. As such, it can take advantage of a whole bunch of in-process context information about activation contexts, dll search paths etc. including knowledge of things like the app.local flag. All these values are specific to the current process and it is not the job of any other process (or even the Kernel) to track stuff like this. But, if we look at CreateProcess we can see some problems. When it is initially called, it is called in the context of the launching, not destination, process, so it knows nothing of the destination processes activation context. In fact, the destination process does not exist yet. The CreateProcess implementation needs to create a NT process, and execute some code in it asap to perform the process load as it doesn't make any sense to instantiate all that per process context stuff in the current process. But, to do that, there needs to be at least some code in the destination process: The kernel code responsible for parsing the EXE files header, extracting the headers and building the activation contexts that will be used to load the remaining dlls. This means that, unfortunately for you, kernel32.dll and some dependencies need to be mapped into a process long before that process is capable of building a dll search context, noticing the app.local flag etc. |
|||
You should look at how the Windows loader works. This is OS version
dependent, but some of those DLLs load before your program and the
loader always looks for them on a path provided by the system. Look at
the sequence by starting your program with WinDbg.
http://stackoverflow.com/questions/38042757/loading-custom-dlls-instead-of-original-dlls |
LoadLibrary
andLoadLibraryEx
.ntdll.dll
andkernel32.dll
are what provideLoadLibrary
(Ex), so by chicken-and-egg analysis you can see that they aren't loaded byLoadLibrary
(Ex), and therefore are not affected by DLL redirection. In fact, I think you'll find thatntdll
andkernel32
aren't loaded into a new process at all, they are in the initial module table. – Ben Voigt Jun 26 at 21:59