7
2

【CyberDefenders】Obfuscated【Walkthrough】

Last updated at Posted at 2023-12-16

これは ZOZO Advent Calendar 2023 カレンダーVol.6の17日目の記事です。昨日の投稿は@kitsune_ykさんでした。

はじめに

本記事は CyberDefenders(以下リンク参考)の「Obfuscated」にチャレンジした際のWalkthroughになります

※本チャレンジについてはRed側のペネトレというよりはBlue側の分析力を問われるものになります。
※今回のチャレンジはマルウェア解析メインでしたね。

チャレンジ開始前

問題について

以下の画像の「Download challenge」に問題ファイルのリンクがあります。
アーカイブファイルで圧縮されているので仮想環境で解凍してください。
※ホストで解凍しないでください。本物のマルウェアが入ってたりします。

この解凍したファイルを解析して左側の「Questions」に解答します。
スクリーンショット 2023-12-01 154944.png

環境

この CyberDefenders を解く際には仮想環境でマルウェア解析やメモリフォレンジックを行う環境を用意する必要があります。
今回は以下のような環境を用意しました。

基本的な解析はToolが豊富に揃ってるREMuxで行い、どうしても動的解析でデバッグを簡易に行いたい場合にはWindowsOSを利用しました。

Q1

What is the sha256 hash of the doc file?
以下のコマンドで確認出来ます。

sha256sum 49b367ac261a722a7c2bbbc328c32545

Q2

Multiple streams contain macros in this document. Provide the number of lowest one.
oledump.pyを使って確認します。

remnux@remnux:~/Downloads$ oledump.py 49b367ac261a722a7c2bbbc328c32545 -i
  1:       114             '\x01CompObj'
  2:       284             '\x05DocumentSummaryInformation'
  3:       392             '\x05SummaryInformation'
  4:      8017             '1Table'
  5:      4096             'Data'
  6:       483             'Macros/PROJECT'
  7:        65             'Macros/PROJECTwm'
  8: M    7117   5667+1450 'Macros/VBA/Module1'
  9: m    1104     947+157 'Macros/VBA/ThisDocument'
 10:      3467             'Macros/VBA/_VBA_PROJECT'
 11:      2964             'Macros/VBA/__SRP_0'
 12:       195             'Macros/VBA/__SRP_1'
 13:      2717             'Macros/VBA/__SRP_2'
 14:       290             'Macros/VBA/__SRP_3'
 15:       565             'Macros/VBA/dir'
 16:        76             'ObjectPool/_1541577328/\x01CompObj'
 17: O   20301             'ObjectPool/_1541577328/\x01Ole10Native'
 18:      5000             'ObjectPool/_1541577328/\x03EPRINT'
 19:         6             'ObjectPool/_1541577328/\x03ObjInfo'
 20:    133755             'WordDocument'
remnux@remnux:~/Downloads$ 

Macroが含まれている場合は上記のようにm,Mが付与されます。
最低値を答えれば解答となります。

Q3

What is the decryption key of the obfuscated code?
とりあえずOLEファイルについてはolevbaを使って置けばある程度解析出来ます。
回します。

remnux@remnux:~/Downloads$ olevba 49b367ac261a722a7c2bbbc328c32545 
XLMMacroDeobfuscator: pywin32 is not installed (only is required if you want to use MS Excel)
olevba 0.60.1 on Python 3.8.10 - http://decalage.info/python/oletools
===============================================================================
FILE: 49b367ac261a722a7c2bbbc328c32545
Type: OLE
-------------------------------------------------------------------------------
VBA MACRO ThisDocument.cls 
in file: 49b367ac261a722a7c2bbbc328c32545 - OLE stream: 'Macros/VBA/ThisDocument'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
(empty macro)
-------------------------------------------------------------------------------
VBA MACRO Module1.bas 
in file: 49b367ac261a722a7c2bbbc328c32545 - OLE stream: 'Macros/VBA/Module1'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

...省略

OBKHLrC3vEDjVL = B8qen2T433Ds1bW & "\" & "maintools.js"
Open (OBKHLrC3vEDjVL) For Binary As #K764B5Ph46Vh
Put #K764B5Ph46Vh, 1, Wk4o3X7x1134j
Close #K764B5Ph46Vh
Erase Wk4o3X7x1134j
Set R66BpJMgxXBo2h = CreateObject("WScript.Shell")
R66BpJMgxXBo2h.Run """" + OBKHLrC3vEDjVL + """" + " EzZETcSXyKAdF_e5I2i1"
ActiveDocument.Save
Exit Sub
MnOWqnnpKXfRO:
Close #K764B5Ph46Vh
ActiveDocument.Save
End Sub








+----------+--------------------+---------------------------------------------+
|Type      |Keyword             |Description                                  |
+----------+--------------------+---------------------------------------------+
|AutoExec  |AutoOpen            |Runs when the Word document is opened        |
|AutoExec  |AutoClose           |Runs when the Word document is closed        |
|Suspicious|Environ             |May read system environment variables        |
|Suspicious|Open                |May open a file                              |
|Suspicious|Put                 |May write to a file (if combined with Open)  |
|Suspicious|Binary              |May read or write a binary file (if combined |
|          |                    |with Open)                                   |
|Suspicious|Kill                |May delete a file                            |
|Suspicious|Shell               |May run an executable file or a system       |
|          |                    |command                                      |
|Suspicious|WScript.Shell       |May run an executable file or a system       |
|          |                    |command                                      |
|Suspicious|Run                 |May run an executable file or a system       |
|          |                    |command                                      |
|Suspicious|CreateObject        |May create an OLE object                     |
|Suspicious|Windows             |May enumerate application windows (if        |
|          |                    |combined with Shell.Application object)      |
|Suspicious|Xor                 |May attempt to obfuscate specific strings    |
|          |                    |(use option --deobf to deobfuscate)          |
|Suspicious|Base64 Strings      |Base64-encoded strings were detected, may be |
|          |                    |used to obfuscate strings (option --decode to|
|          |                    |see all)                                     |
|IOC       |maintools.js        |Executable file name                         |
+----------+--------------------+---------------------------------------------+

WScript.Shellを実行してmaintools.jsを動かしてるのが何となく分かると思います。
後はQ3の入力文字列欄と比較してdecryption keyぽいのを選べば良いです。

Q4

What is the name of the dropped file?
先ほどのolevbaの結果にIOCで出ています。

Q5

This script uses what language?
ファイルの拡張子から予測値を答えます。

Q6

What is the name of the variable that is assigned the command-line arguments?
実際にmaintools.jsを見ないと何とも言えないです。
なので公開されている脅威情報から件のJSファイルを探します。
以下のサイトから落としてきましょう。

6-1.png
下層にDownloads項目があります。
6-2.png
ログイン状態であればダウンロード可能です。
6-3.png
末尾193b3-21のファイルが例のJSです。
中身を確認します。
6-4.png
コマンドライン引数の入る変数は何かは分かると思う。一応見やすいように以下のサイトで整形します。

6-5.png
スクリプトの実行順序も分かりやすくなりました。
これで解答できますね。

Q7

How many command-line arguments does this script expect?
このJSに渡していたものはQ3で解答したkeyのみです。

Q8

What instruction is executed if this script encounters an error?
整形したコードでエラーハンドリングの該当箇所が見えます。
8.png

Q9

What function returns the next stage of code (i.e. the first round of obfuscated code)?
最初に実行されている関数をみればよい。
9-1.png
中身を確認する。
9-2.png
reternは何がされているかは見ればわかります。

Q10

The function LXv5 is an important function, what variable is assigned a key string value in determining what this function does?
関数の中を確認します。
10.png
この文字列は、Base64では!!!
その前に変数を解答します。

Q11

What encoding scheme is this function responsible for decoding?
前の問題が分かれば自ずと導くことが可能です。

Q12

In the function CpPT, the first two for loops are responsible for what important part of this function?
何にも分からないのでこういうものはChatGPTに聞きます。
12-0.png
RC4らしいのでアルゴリズムを検索する。

そうすると似たようなコーディングをしているGistを見つけます。
12-1.png

あたりっぽいのでこのRC4の中でそれっぽいアルゴリズムを解答すればよい。

Q13

The function CpPT requires two arguments, where does the value of the first argument come from?
13.png
見れば一発で分かります。

Q14

For the function CpPT, what does the first argument represent?
Q13が何だったのか、Q3をもう一度思い出せばよい。

Q15

What encryption algorithm does the function CpPT implement in this script?
Q12が分かれば分かるはずです。

Q16

What function is responsible for executing the deobfuscated code?
エラーハンドリングされる前のコードを見れば分かります。
よくJSなどスクリプトマルウェアで第2のPayloadを落としてきて実行するときに使う関数です。

Q17

What Windows Script Host program can be used to execute this script in command-line mode?
一般的なコマンドを書けばよい。Windowsでスクリプトを回すときはよく使うコマンドです。

Q18

What is the name of the first function defined in the deobfuscated code?
実物のスクリプトに以下のようにWScript.Echoを追加し、ES3cを出力してみます。
18-1.png
実行結果は以下です。
18-2.png
スクリプトが出力されるので、最初の行を確認すればよいです。
18-3.png

最後に

スクリプトを用いたマルウェア解析でよくある解析てんこ盛りと言ったチャレンジでした。
Deobfuscationは楽しいですね!?いいチャレンジになりました。

ちなみにこのチャレンジは以下のTurla APT groupの攻撃チェインの途中までの流れを問うたものになってます。
興味があればレポートもご覧ください。

明日は@yamajoooonさんです。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2