Code Injection & Hooking
Code Injection & Hooking
Hooking
Virtual Memory
• Address Space Expansion
• Memory Isolation
• Page File for Efficiency
Virtual Memory
• Virtual memory is segregated into process memory (process space or user space) and
kernel memory (kernel space or system space). The size of the virtual memory address
space depends on the hardware platform. For example, on a 32-bit architecture, by
default, the total virtual address space (for both process and kernel memory) is a
maximum of 4 GB. The lower half (lower 2 GB), ranging from 0x00000000 through
0x7FFFFFFF, is reserved for user processes (process memory or user space), and the
upper half of the address (upper 2 GB), ranging from 0x80000000 through 0xFFFFFFFF,
is reserved for kernel memory (kernel space).
Process Memory Components (User Space)
Process Memory Components (User
Space)
Process Memory Components (User
Space)
Kernel Memory Contents (Kernel Space)
Kernel Memory Contents (Kernel Space)
User Mode And Kernel Mode
1.User and Kernel Space Distinction:
• Virtual memory is divided into user-space and kernel space.
User-space contains user-mode code, while kernel space
houses the operating system's kernel and device drivers. This
separation ensures restricted access for user-mode
applications, preventing direct interaction with hardware.
2.Execution Modes:
• User mode and kernel mode represent different privilege
levels. User mode restricts access, while kernel mode allows
high-privileged execution. Kernel components, like
ntoskrnl.exe, can interact with both user and kernel space.
Third-party drivers, when signed, can execute in kernel mode.
User Mode And Kernel Mode
• The APC injection technique is similar to remote DLL injection, but instead of using
CreateRemoteThread(), a malware makes use of Asynchronous Procedure Calls (APCs)
to force the thread of a target process to load the malicious DLL.
• An APC is a function that executes asynchronously in the context of a particular thread.
Each thread contains a queue of APCs that will be executed when the target thread
enters an alertable state. As per Microsoft documentation (read online), a thread
enters an alertable state if it calls one of the following functions:
SleepEx(),
SignalObjectAndWait()
MsgWaitForMultipleObjectsEx()
WaitForMultipleObjectsEx()
WaitForSingleObjectEx()
• For detail steps read section 3.2
DLL Injection Using SetWindowsHookEx()
• The SetWindowsHookEx() API can also be used to load a DLL into a target process address
space and execute malicious code. To do that, a malware first loads the malicious DLL into
its own address space. It then installs a hook procedure (a function exported by the
malicious DLL) for a particular event (such as a keyboard or mouse event), and it
associates the event with the thread of the target process (or all of the threads in the
current desktop). The idea is that when a particular event is triggered, for which the hook is
installed, the thread of the target process will invoke the hook procedure. To invoke a hook
procedure defined in the DLL, it must load the DLL (containing the hook procedure) into the
address space of the target process.
• In other words, an attacker creates a DLL containing an export function. The export
function containing the malicious code is set as the hook procedure for a particular event.
The hook procedure is associated with a thread of the target process, and when the event
is triggered, the attacker's DLL is loaded into the address space of the target process, and
the hook procedure is invoked by the target process's thread, thereby executing malicious
code. The malware can set the hook for any type of event, as long as that event is likely to
occur. The point here is that the DLL is loaded into the address space of the target process,
and performs malicious actions.
• For detail steps read section 3.3
DLL Injection Using The Application Compatibility
Shim
• The Microsoft Windows application compatibility infrastructure/framework (application
shim) is a feature that allows programs created for older versions of the operating
system (such as Windows XP) to work with modern versions of the operating system
(such as Windows 7 or Windows 10). This is achieved through application compatibility
fixes (shims). The shims are provided by Microsoft to the developers so that they can
apply fixes to their programs without rewriting the code. When a shim is applied to a
program, and when the shimmed program is executed, the shim engine redirects the
API call made by the shimmed program to shim code; this is done by replacing the
pointer in the IAT with the address of the shim code.
• In other words, it hooks the Windows API to redirect calls to the shim code instead of
calling the API directly in the DLL. As a result of API redirection, the shim code can
modify the parameters passed to the API, redirect the API, or modify the response from
the Windows operating system.
• For detail steps read section 3.3
DLL Injection Using The Application Compatibility
Shim
Creating A Shim
Creating a shim for an application can be broken down into four
steps:
• Choosing the application to shim.
• Creating the shim database for the application.
• Saving the database (.sdb file).
• Installing the database.
How Attackers Use Shims
• The following table outlines some of the interesting shims and their descriptions:
Remote Executable/Shellcode Injection
• In this technique, the malicious code is injected into the target process memory
directly, without dropping the component on the disk. The malicious code can be a
shellcode or an executable whose import address table is configured for the target
process. The injected malicious code is forced to execute by creating a remote thread
via CreateRemoteThread(), and the start of the thread is made to point to the
code/function within the injected block of code. The advantage of this method is that
the malware process does not have to drop the malicious DLL on the disk; it can extract
the code to inject from the resource section of the binary, or get it over the network
and perform code injection directly.
• For detail steps read section 3.5
Hollow Process Injection (Process Hollowing)