In the past few years, malware (and some pen-test tools like Metasploit's meterpreter payload) have begun to use reflective DLL injection (PDF) to load a DLL into the memory of a process. The benefit is that the file is never written to disk and is difficult to detect. Many examples I've seen are based on Joachim Bauch's work.
However, at DEF CON 20 Andrew King demonstrated that he was able to detect DLL's injected using reflective DLL injection. His presentation was called "Detecting Reflective Injection". Unfortunately, he has not released the source code (which he certainly is under no obligation to do).
UPDATE: Apparently I missed it, but Andrew did open-source this work a couple years ago: https://github.com/aking1012/dc20
In addition, a tool called "Antimeter" can detect the meterpreter engine when loaded using reflective dll injection. Again, closed source.
I understand that Andrew King's tool and Antimeter are both written in Python and use pydbg/pydasm in order to enumerate the memory of running executables.
Does anyone have some general source code (in Python, C, Delphi, or otherwise) that they are willing to share that demonstrates how to detect reflective DLL injection? There are memory forensic tools that can analyze a memory dump and find this, but I'm looking to execute an application on a running system (like antimeter does) and find processes with reflectively injected DLL's.
If you are interested in understanding how reflective DLL injection works, there is some open-source code written in Delphi that shows how to do this.
UPDATE: I tested and I can reflectively inject DLL's without admin rights (and as a regular user), but of course as a USER I can only inject into processes running at the same integrity level (and in my session)...but that still covers applications like the Office suite, Internet Explorer, etc.
And finally, Reflective DLL Injection uses custom function that can easily avoid detection. However at DEF CON 20 Andrew King demonstrated that he was able to detect DLL's injected using reflective DLL injection. His presentation was called "Detecting Reflective Injection".
Reflective DLL injection is a library injection technique in which the concept of reflective programming is employed to perform the loading of a library from memory into a host process. As such the library is responsible for loading itself by implementing a minimal Portable Executable (PE) file loader.
Adversaries may reflectively load code into a process in order to conceal the execution of malicious payloads. Reflective loading involves allocating then executing payloads directly within the memory of the process, vice creating a thread or process backed by a file path on disk.
A crucial aspect of reflectively loading a DLL is to have executable memory available for the DLL code. This can be accomplished by taking existing memory and changing its protection flags or by allocating new executable memory.
What about hooking the VirtualProtect API. Because DLLs that load itself will certainly set execute on its memory code range. This is because (as you mentioned) they use User access rights so they have to use the process userspace API.
NTSYSAPI NTSTATUS NTAPI ZwProtectVirtualMemory( IN HANDLE ProcessHandle, IN PVOID * BaseAddress, IN SIZE_T * NumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG OldAccessProtection );
If you hook that at the very beginning of your program, you can filter out suspicious protection calls (the one that enable code execution). I would then scan for PE header or such in front of the requested pages to know that its a loadable module... note: i think this is not called for regular DLLs as LoadLibrary handles this inside the Kernel space. right? TODO: verify
Normally the PE header is located 0x1000 (4096) bytes or one page in front of the first executable code. So a VERY basic approach can be to scan for the "MZ" tag:
char* pe = ((char*)BaseAddress) - 0x1000; if ((NewAccessProtection == PAGE_EXECUTE || ... ) & pe[0] == 'M' && pe[0] == 'Z') { // do checks here }
If you need further info on API hooking just ask or read tons of articles on the net. Another hooking candidate is: FlushInstructionCache(...). But I think only Blizzard is using this for warden anti cheat modules as theres no reason on x86 architecture to call this.
... just a thought,
will
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With