Hooking techniques¶
links: MAI TOC - Hooking - Index
Introduction¶
Hooking is a technique used in malware to intercept and modify the execution flow of software. It allows an attacker to manipulate data, steal information, and take control of processes. While often associated with malicious activities, hooking also has legitimate applications in software development and security tools.
Why Malware Uses Hooking¶
- Control and Manipulation: Hooking allows attackers to intercept and modify function calls within a victim process, enabling control over the process.
- Data Theft: Attackers can insert malicious code into system and library calls to steal or manipulate data passed through these calls.
- Man-in-the-Browser Attacks: Hooking can be used to steal credentials by intercepting API calls like
HTTPSendRequest()
, or modify web forms to gather sensitive information. - Eavesdropping: Attackers can monitor network traffic by hooking before encryption or after decryption.
- Rootkit Techniques: Hooking can hide files, processes, and registry keys by manipulating API calls that list these items.
- For example, by hooking an API that lists directory contents, attackers can filter out specific files from the return values, effectively hiding them from the user.
Legitimate Applications of Hooking¶
- Security Tools: Some security tools use hooking to monitor and protect systems.
- Operating Systems: Windows OS, for example, uses hooking for various system functions.
Path of a System Call¶
- Process Initiation: A user-mode process, such as
notepad.exe
, makes a system call likeWriteFile()
. - Kernel32.dll: The call is directed to the
Kernel32.dll
which handles Windows API functions. - IAT (Import Address Table): The call is linked via the IAT, an array of addresses pointing to functions in DLLs.
- Execution in Kernel: The call then proceeds to the Windows kernel where the actual system-level operation is performed.
Path of a Library Call¶
- Process Initiation: A user-mode process, such as
notepad.exe
, calls a library function likeCryptProtectData()
. - crypt32.dll: The call is directed to the
crypt32.dll
which handles cryptographic functions. - IAT: Similar to system calls, the call is linked via the IAT.
- Execution: The library function is executed as per the linked address in the DLL.
Hooking: Idea and Placement¶
- Concept: Hooking involves inserting code into the execution path of a legitimate program to divert execution to malicious code. Hooking and code injections are related, as the malicious code is typically injected into the victim process.
- Placement: Hooks can be placed in various processes depending on the attacker's goal.
- Web Browsers: To steal credentials from web forms.
- Password Managers: To compromise saved passwords.
- Network APIs: To control network traffic.
- File APIs: To hide files or directories.
Hooking Techniques¶
- IAT Hooks (User Mode): Modify function pointers in the IAT to point to malicious code.
- EAT Hooks (User Mode): Modify the Export Address Table (EAT) to redirect function calls.
- Inline Hooks (User Mode): Insert a jump instruction in the original code to redirect execution to malicious code.
- SSDT Hooks (Kernel Mode): Modify the System Service Descriptor Table (SSDT) in the kernel to redirect system calls.
- IDT Hooks (Kernel Mode): Modify the Interrupt Descriptor Table (IDT) to intercept hardware and software interrupts.
- Driver IRP (Kernel Mode): Intercept I/O request packets (IRPs) in kernel mode drivers.
IAT Hooking in Detail¶
- Concept: IAT hooking involves modifying entries in the Import Address Table so that function pointers point to malicious code instead of the original function.
- Implementation: This is achieved by altering the addresses in the IAT to point to the attacker's code, which then executes instead of the intended function.
- Ease of Use: IAT hooking is relatively simple to implement, requiring only modifications to the IAT entries.
Example of Inline Hooking¶
- Zeus Malware: Uses inline hooking to intercept function calls and redirect them to malicious code, demonstrating a practical implementation of this technique.
Detection of Hooks¶
- Live Forensics: Use a debugger to inspect memory and code of running processes.
- Memory Forensics: Use tools like Volatility with the
apihooks
plugin to detect hooks. - IAT Hook Detection:
- Heuristics: Iterate through IAT entries in the DLLs of the process being analyzed.
- Criteria: If an entry points outside the DLL currently being inspected, a hook is likely present.
- Inline Hook Detection:
- Heuristics: Locate functions in DLLs and analyze the function code.
- Criteria: Check for non-legitimate execution transfers (e.g., JMP, CALL) to code outside the function body.
False Positives¶
- Not every hook is malicious. To reduce false positives, it is important to check where a hook points to and whether it is pointing to legitimate or malicious code. For example, hooks pointing to code injection or DLL injection regions are likely malicious.
HollowsHunter¶
See HollowsHunter
- Overview: HollowsHunter is a versatile tool used to find code injections and hooks.
- Functionality:
- Inline Hooks: Can identify inline hooks using commands like
hollows_hunter32.exe /hooks
. - IAT Hooks: Can also identify IAT hooks using commands like
hollows_hunter32.exe /iat
. - Output: The tool generates detailed reports, producing a folder per suspicious process with analysis results.
- Details: Reports include information such as the name of the hooked API, the relative virtual address of the hook, and the module to which the hook points.
- Inline Hooks: Can identify inline hooks using commands like
links: MAI TOC - Hooking - Index