# 0x08. Advanced topic - Injection techniques
[toc]
## Why?
經過了前面幾個系列的介紹,我們可以發現有很多種方法來分析惡意程式
當然惡意程式軍火商也不是吃素的,都知道前面列舉的這些方法
所以這些神人們開發出能夠隱藏自己真正本體不被找到的方法 - Injection
利用這樣的方法,惡意程式能夠將自己注入到其他正常process裡面進而增加被抓到的難度
<br>
下面會介紹一些這些神仙打架的方法
<br>
## DLL Injection
相當典型的做法,先來看一下流程圖

http://blog.opensecurityresearch.com/2013/01/windows-dll-injection-basics.html
<br>
首先利用VirtualAlloc申請目標process的memory,寫入惡意DLL路徑
再調用CreateRemoteThread()搭配LoadLibrary()來開啟這個被寫入的惡意DLL路徑
缺點:惡意程式DLL必須存在Disk,容易被抓到
常用API:
```
VirtualAlloc()
WriteProcessMemory()
GetModuleHandle()
GetProcAddress()
CreateRemoteThread()
```
<br>
## PE Injection
跟DLL Injection相當類似,但是PE Injection是直接注入整個code而不是透過LoadLibrary()DLL路徑
跟DLL Injection相比,好處是不需要有個DLL存在Disk,可以只注入shellcode
常用API:
```
VirtualAlloc()
WriteProcessMemory()
CreateRemoteThread()
```
<br>
## Process Hollowing
也是相當經典的手法,選定目標process之後(svchost.exe這類),利用CreateProcess()執行一個正常process並且設定flag為CREATE_SUSPENDED (不會開始執行,只是加載起來)
之後利用ZwUnmapViewOfSection()清空目標process virtual memory
> MSDN: The ZwUnmapViewOfSection routine unmaps a view of a section from the virtual address space of a subject process.
之後惡意程式利用VirtualAlloc() + WriteProcessMemory()申請並注入惡意程式本體到各個Sections
最後利用SetThreadContext() + ResumeThread() 將Entry Point指向新寫入的惡意程式並執行
<br>
**分析重點**
在 CreateProcess()這個函數,creation flags會等於 CREATE_SUSPENDED
```
BOOL creationResult;
creationResult = CreateProcess(
NULL, // No module name (use command line)
cmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, // creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startupInfo, // Pointer to STARTUPINFO structure
&processInformation); // Pointer to PROCESS_INFORMATION structure
```
常用API:
```
CreateProcess(..., CREATE_SUSPENDED, ...)
ZwUnmapViewOfSection()
VirtualAlloc()
WriteProcessMemory()
SetThreadContext()
ResumeThread()
```
<br>
## Process Doppleganging
Doppleganger:幽靈
比較新穎的手法,這真的是神仙打架,是在[2017黑帽大會](https://www.blackhat.com/docs/eu-17/materials/eu-17-Liberman-Lost-In-Transaction-Process-Doppelganging.pdf)提出來的
利用 [NTFS Transaction rollback](https://www.ntfs.com/transaction.htm) 的特性來掩蓋蹤跡
方法跟Process Hollowing 類似,但是更神一點
利用了Transaction的特性,對一系列惡意操作進行包裝
包含了下列步驟:
覆寫target process,注入成惡意執行檔
載入惡意執行檔到memory
RollBack <- 此時Disk覆寫失敗,但是memory還是惡意檔案,也就成了Doppleganger
執行memory
最後再用**RollBack**還原這一系列操作
建議直接看code比較容易理解
https://gist.github.com/hfiref0x/a9911a0b70b473281c9da5daea9a177f
<br>
可以看到幾個關鍵的API
```
NtCreateTransaction()
CreateFileTransacted()
WriteFile()
NtCreateSection()
NtRollbackTransaction()
NtCreateProcess()
NtCreateThread()
```
結論:這真的太神啦
<br>
## Hooking
一直以來Hooking也是相當常被使用的技術,包括Sandbox檢測API使用來判斷惡意行為
惡意程式也會使用Hooking來監控/紀錄/變更受害者的電腦的行為,使用情況相當廣泛
<br>
Hooking 的精髓在於更改原有執行function的執行流程
當某個事件(API Call/Key stroke/mouse)被觸發,會先執行惡意程式想執行的東西再來執行原本該執行的function
下面介紹幾個常見的Hooking方法
---
**SetWindowsHookExA**
先來看一個經典API的介紹:SetWindowsHookEx(),利用這個function可以達成像是Keylogger的功能
>Installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.
```
HHOOK SetWindowsHookExA(
int idHook, <- 事件類型 ex:滑鼠/鍵盤/...
HOOKPROC lpfn, <- address of hook procedure
HINSTANCE hmod, <- handle to the module contains the hook procedure(DLL Handle)
DWORD dwThreadId <- Thread ID,如果為 0 就代表All thread
);
```
---
**Inline Hook**
透過改寫API函數來實現,必須等DLL載入後才能利用
```
function_A:
push ebp <- 被改寫成 jmp hook ---> hook:
... <---------------------- do bad thing
do something | ....
... --- ret ;(back to function_A)
return
```
---
**IAT Hook**
經典的Rootkit Hook方法
Import Address Table(IAT)中裝了API函數的指標,透過修改IAT內的指標值,能夠改寫程式執行流程
原本IAT
```
Address | Value | API Name
0000 | aaaaaaaa | VirtualAlloc()
0004 | bbbbbbbb | CreateProcess()
0008 | cccccccc | IsDebuggerPresent()
000c | dddddddd | ExitProcess()
```
改成這樣
```
Address | Value | API Name
0000 | aaaaaaaa | VirtualAlloc()
0004 | FFFFFFFF | CreateProcess() <- Pointer被改變指向惡意程式
0008 | cccccccc | IsDebuggerPresent()
000c | dddddddd | ExitProcess()
```
只要執行CreateProcess(),程式會直接改走惡意程式所控制的地方(FFFFFFFF)
<br>
## More Resources
本篇只介紹了一部分的Injection方式,有興趣的話可以參考下面的文章
https://i.blackhat.com/USA-19/Thursday/us-19-Kotler-Process-Injection-Techniques-Gotta-Catch-Them-All-wp.pdf
https://www.deepinstinct.com/2019/09/15/malware-evasion-techniques-part-1-process-injection-and-manipulation/
[-0xbc](https://hackmd.io/@0xbc000)
###### tags: `Malware Analysis` `Reverse Engineering` `tutorials`