Hooking Techniques:
- Using SetWindowsHookEx function which is designed to allow you to “hook” Windows messages for a given thread
- Inline\Detour hooking technique where you override the first assembly instruction of a function with a jump to your code and in the end of your code you jump back to the original function
Dll\Code Injection Techniques:
- Dll injection using KnownDlls section mechanism. Comment: The Section object where the KnownDlls are loaded at startup are Memory Mapped Files with names of the form: KnownDlls\{dll name}. On 64 bit operating system there is also KnownDlls32\.
- Standard way using CreateRemoteThread, VirtualAllocEx, WriteProcessMemory
- Using NtCreateThreadEx or RtlCreateUserThread instead of CreateRemoteThread. Better to bypass antivirus than using CreateRemoteThread. “RtlCreateUserThread appears to be a small wrapper for NtCreateThreadEx. The reason we would want to call RtlCreateUserThread is that the syscall option for NtCreateThreadEx could change between versions of Windows. Therefore, it will be more likely to work if we use RtlCreateUserThread. Mimikatz and Meterpreter use RtlCreateUserThread so it is safe to go with that option”. Those functions can\could help bypass zero session isolation.
- Using NtUnmapViewOfSection, patching, and then using NtMapViewOfSection.
- This malware used this technique: http://www.sentinelone.com/wp-content/themes/sentinelone/img/SentinelOne-Intelligence-Report.pdf
- Using GetThreadContext, change code and eip, and SetThreadContext
- Using QueueUserAPC which adds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread
- Reflective dll injection – injecting dll without LoadLibrary by copying it into memory and finding the addresses of the functions and jumping to them (tries to implement the code of LoadLibrary but loading from memory instead of loading from file. Sometimes doesn’t fully do all the steps that LoadLibrary does and thus some structures are weird looking and this allows to detect the injection for example by using volatility malfind extension)
- https://github.com/stephenfewer/ReflectiveDLLInjection
- The following link has a good-understandable explanation about reflective dll injection and also explanation of how to turn your dll into reflective dll.
The problem with what is stated there is that it requires to change the code of the dll. Maybe with a little change in the injection code, you can make it such that you can inject the code into the target process and the injected code will load your evil dll which you didnt change its code.
Here the whole library loading process is implemented and used instead of the windows loading functions http://securitycafe.ro/2015/02/26/upgrade-your-dll-to-reflective-dll/
- Hooking NT functions that are part of dll loading and then calling LoadLibrary but making sure using the hook that the library is loaded from memory and not from disk
- Powerloader technique – writing to shared sections with explorer and then making explorer execution execute malicious address writen using SetWindowLong by using SendNotifyMessage and finally the address written is of KiUserApcDispatcher which together with ROP chains executes the malicious code which was written to the shared sections. Note that the function KiUserApcDispatcher is related to QueueUserAPC from previously mentioned technique. One of the things to note here is that there is no remote thread injection here with CreateRemoteThread or QueueUserAPC or suspicios cross process call to SetThreadContext, and instead a function of a known process which is exposed using some api is used for the trigger – in this case explorer.exe SendNotifyMessage function.
- General technique of overriding remote process function which can be triggered using some api. For example ardbot malware overrides GetMessageW function of explorer.exe and triggers it to execute its code. An interesting thing about ardbot is that it uses NtWriteVirtualMemory instead of WriteProcessMemory which liked mentioned before can be used to evade av. Like before, one of the things to note here is that there is no remote thread injection here with CreateRemoteThread or QueueUserAPC or suspicios cross process call to SetThreadContext.
Execution\Loading Hijacking Techniques:
- Dll side-loading using Side-by-side assembly, WinSxS
- Dll Search order hijacking
Tools:
- Detours – “Software package for re-routing Win32 APIs underneath applications”
- EasyHook – “EasyHook starts where Microsoft Detours ends. This project supports extending (hooking) unmanaged code (APIs) with pure managed ones, from within a fully managed environment like C# using Windows 2000 SP4 and later, including Windows XP x64, Windows Vista x64 and Windows Server 2008 x64. Also 32- and 64-bit kernel mode hooking is supported as well as an unmanaged user-mode API which allows you to hook targets without requiring a NET Framework on the customers PC. An experimental stealth injection hides hooking from most of the current AV software. ”
- DllInjector – “dll injection tool that implements various methods”
- IAT hooking circumventing common detection mechanisms
- Frida – lets you inject snippets of JavaScript into native apps on Windows, Mac, Linux, iOS and Android.
- MiniHook – code implementing detour technique
Other Material: