# 0x09 Zero2Automated Custom Lab
[toc]
## 前言
本篇為[Zero2Auto](https://courses.zero2auto.com/)課程中的Lab,目的是要分析malware的功能
>Hi there,
>
>During an ongoing investigation, one of our IR team members managed to locate an unknown sample on an infected machine belonging to one of our clients. We cannot pass that sample onto you currently as we are still analyzing it to determine what data was exfilatrated. However, one of our backend analysts developed a YARA rule based on the malware packer, and we were able to locate a similar binary that seemed to be an earlier version of the sample we're dealing with. Would you be able to take a look at it? We're all hands on deck here, dealing with this situation, and so we are unable to take a look at it ourselves.
We're not too sure how much the binary has changed, though developing some automation tools might be a good idea, in case the threat actors behind it start utilizing something like Cutwail to push their samples.
I have uploaded the sample alongside this email.
>
>Thanks, and Good Luck!
<br>
## Basic Static Analysis
### PE Studio
將樣本放入PE Studio中,可以看到有幾樣東西值得注意
* High Entropy - Maybe Packed
* Resources

<br>
* Lack of other dll imports - Import dynamically

<br>
### Resource Hacker
很不幸的,在裡面並沒有看出什麼一眼能辨認出來的文字

<br>
### CFF Explorer
此樣本compiled with ASLR,關掉之後對動態分析會相對方便許多

<br>
### IDA
在string window中,可以看到有些“貌似”encode過的文字
我會猜測可能是程式執行中要做動態載入(Dynamic Import)的加密文字

<br>
下圖可以檢查我們的猜測,樣本提取一串奇怪的文字後呼叫`sub_401300`(被呼叫兩次,很可能是decode)
之後馬上呼叫`LoadLibraryA`,`GetProcAddress` 來獲得存在dll中function的位址
標準的Dynamic Import

<br>
### `sub_401400`
#### More Dynamic Import
在`sub_401400`之中,有相當多的function被載入

<br>
#### RC4 Crypto Algorithm
做完動態載入後,馬上接著是經典RC4解密的function

<br>
## Sandbox Analysis
可以看到,樣本接連啟動多個process,使用了process injection的手法

<br>
同時也可以看到會連上pastebin下載檔案

<br>
最後螢幕上會出現對話框

<br>
在沙箱分析中,可以觀察到有以下重點
* Process Injection
* Network Traffic
<br>
## Dynamic Analysis
分析重點
* Unpack
* Resource
* Process Injection
* Network Traffic
### x32dbg
先用ScyllaHide做Function Hook

<br>
### Dynamic Import
`sub_401300` 的確是做decode
可以看到其中有許多API,都是跟Resource相關
```
FindResourceA
LoadResourceA
SizeIfResource
LockResource
```

<br>
### Decode Function (`sub_401300`)
在執行前,`414940`的值是無法辨認的文字

<br>
執行完之後,可以看到decode成`FindResourceA`

<br>
### Resource Allocate
在動態載入完之後,可以看到馬上呼叫
* `FindResourceA`

<br>
* `LoadResource`

<br>
* `SizeOfResource`

<br>
* `LockResource`

<br>
* `VirtualAlloc`


<br>
### RC4 Decrypt
將斷點設在`40161B`,重複`F9`執行,可以看到`ECX`一直在增加
並且有個新的PE檔案經過解密出現

直接執行到`call 0x401000`的地方,新的PE檔案經過RC4 Decrypt完成,可以把memory區塊dump下來
但重點是樣本會用何種方法做Process Injection?
<br>
整理一下思路
到目前為止,我們發現樣本執行了Resource Allocation並且用RC4做解密
<br>
### `sub_401000`
IDA所分析的結果,可以看到有更多動態載入的function


<br>
### Process Injection
用了一樣的動態載入,可以發現樣本調用了`CreateProcessA`

<br>
`VirtualAlloc`

<br>
`WriteProcessMemory`

<br>
`SetThreadContext`

<br>
以及最後的`ResumeThread`

<br>
上述的API是Process Hollowing所會利用的API,可以達成下列效果
有個新的Child Process Create,樣本最後會結束Parent Process

<br>
## Second Stage Payload
還記得RC4解密出一個PE檔案嗎?
那是剛剛透過Process Hollowing塞入的第二階段payload
接下來要繼續分析
此樣本的分析重點會擺在剛剛都沒出現的Internet Download上面
<br>
### `sub_401000`
#### CRC32 API Hashing
很不巧的,樣本採用了CRC32 API Hashing來增加分析的難度
可以從下圖中觀察出來CRC32的計算方法

<br>
* 可以使用rahash2來尋找是否有特別感興趣的API
```
rahash2 -a crc32 -s "InternetOpenA"
0x00000000-0x0000000c crc32: da16a83d
rahash2 -a crc32 -s "InternetOpenURLA"
0x00000000-0x0000000f crc32: acac67a2
```
* 也可以透過動態分析解密出API
可以看到結果為抓出執行中Process的API
`CreateToolhelp32Snapshot`,`Process32First`,`Process32Next`
樣本會檢查是否有存在x32dbg/x64dbg/processhacker/wireshark等process
可以簡單patch做bypass

<br>
### IsDebuggerPresent
做完動態載入後,樣本呼叫`IsDebuggerPresent`,如果沒偵測到
程式會來到`call sub_401000`

<br>
### `sub_401d50`
Function `sub_401d50` 做了API Load,可以發現載入了Process Hollowing 使用的API

<br>
### Process Injection
`sub_401CA0`function 再度做了`CreateProcessA(SUSPEND)`
回到Process Hacker查看,這次目標變成svchost.exe
跟在any.run觀察到的結果一樣

<br>
### Recap
植入的新PE其實還是一樣的檔案,但這次重點回到剛剛尚未提到的CRC32判斷式

`0b925c42d`其實是`svchost.exe`的CRC32值
```
>>> hex(zlib.crc32('svchost.exe') % (1<<32))
'0xb925c42d'
```
由於只是判斷Process Name之CRC32值
我們可以直接把樣本重新命名為svchost.exe並執行
這次會執行右邊`loc_402085`

<br>
### Load WinINet API

<br>
### Decode C2 URL

<br>
### Download the file from the link
樣本調用了`InternetOpenA`,`InternetOpenURLA`

<br>
`HttpQueryInfoA`

<br>
下載儲存到user\AppData\Local\Temp\cruloader\

<br>
樣本會提取裡面的URL,下載之後用XOR 0x61解密

<br>
並再做一次Process Injection


<br>
WriteProcessMemory,再度寫入svchost.exe

<br>
最後執行Final Payload

<br>
## 結論
分析到這也就結束了,這個Sample利用了大量方法來增加分析難度,其中包括了
* Multi-Stage payload
* CRC32
* XOR Decryption
* API Dynamic loading
* Anti-Analysis
* Process Injection
<br>
[-0xbc](https://hackmd.io/@0xbc000)
###### tags: `Malware Analysis` `Reverse Engineering` `tutorials`