PracticalMalwareAnalysis-Labs07 WriteUp

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

Lab 7-1

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

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

1. How does this program ensure that it continues running (achieves persistence) when the computer is restarted?

Malservice というWindows サービスを作成して、自身をサービス・ファイルとして登録する。Malservice はシステム起動時に実行されるように設定されている。

.text:00401082 FF 15 34 40 40 00       call    ds:GetCurrentProcess
.text:00401088 8D 44 24 1C             lea     eax, [esp+404h+Filename]
.text:0040108C 68 E8 03 00 00          push    3E8h            ; nSize
.text:00401091 50                      push    eax             ; lpFilename
.text:00401092 6A 00                   push    0               ; hModule
.text:00401094 FF 15 18 40 40 00       call    ds:GetModuleFileNameA ; get the path of the current process
.text:0040109A 6A 00                   push    0               ; lpPassword
.text:0040109C 6A 00                   push    0               ; lpServiceStartName
.text:0040109E 6A 00                   push    0               ; lpDependencies
.text:004010A0 6A 00                   push    0               ; lpdwTagId
.text:004010A2 8D 4C 24 2C             lea     ecx, [esp+414h+Filename] ; store current process to ECX
.text:004010A6 6A 00                   push    0               ; lpLoadOrderGroup
.text:004010A8 51                      push    ecx             ; lpBinaryPathName full path for the current process
.text:004010A9 6A 00                   push    0               ; dwErrorControl
.text:004010AB 6A 02                   push    2               ; dwStartType auto start
.text:004010AD 6A 10                   push    10h             ; dwServiceType
.text:004010AF 6A 02                   push    2               ; dwDesiredAccess
.text:004010B1 68 3C 50 40 00          push    offset DisplayName ; "Malservice"
.text:004010B6 68 3C 50 40 00          push    offset DisplayName ; "Malservice"
.text:004010BB 56                      push    esi             ; hSCManager
.text:004010BC FF 15 00 40 40 00       call    ds:CreateServiceA


2. Why does this program use a mutex?

このマルウェアは上述したWindows サービス Malservice を作成する前にHGL345というミューテックスが存在するか確認する。HGL345が存在する場合は処理を終了する。HGL345が存在しない場合はミューテックスの作成後、先述したWindows サービス Malservice を作成する。マルウェアのスレッドが複数存在する場合、複数のスレッドが同時にリソースにアクセスすることが無いようにミューテックスを利用して常にひとつのスレッドのみがリソースにアクセスするように制御している。

.text:00401046 68 48 50 40 00          push    offset Name     ; "HGL345"
.text:0040104B 6A 00                   push    0               ; bInheritHandle
.text:0040104D 68 01 00 1F 00          push    1F0001h         ; dwDesiredAccess
.text:00401052 FF 15 28 40 40 00       call    ds:OpenMutexA
.text:00401058 85 C0                   test    eax, eax
.text:0040105A 74 08                   jz      short loc_401064
---
.text:00401064 56                      push    esi
.text:00401065 68 48 50 40 00          push    offset Name     ; "HGL345"
.text:0040106A 6A 00                   push    0               ; bInitialOwner
.text:0040106C 6A 00                   push    0               ; lpMutexAttributes
.text:0040106E FF 15 20 40 40 00       call    ds:CreateMutexA


3. What is a good host-based signature to use for detecting this program?

  • ミューテックス名 HGL345
  • Windows サービス名 Malservice


4. What is a good network-based signature for detecting this malware?

  • URL http://www.malwareanalysisbook[.]com
  • ユーザーエージェント Internet Explorer 8.0
.text:00401150 56                      push    esi
.text:00401151 57                      push    edi
.text:00401152 6A 00                   push    0               ; dwFlags
.text:00401154 6A 00                   push    0               ; lpszProxyBypass
.text:00401156 6A 00                   push    0               ; lpszProxy
.text:00401158 6A 01                   push    1               ; dwAccessType
.text:0040115A 68 74 50 40 00          push    offset szAgent  ; "Internet Explorer 8.0"
.text:0040115F FF 15 C4 40 40 00       call    ds:InternetOpenA
.text:00401165 8B 3D C0 40 40 00       mov     edi, ds:InternetOpenUrlA
.text:0040116B 8B F0                   mov     esi, eax
---
.text:0040116D 6A 00                   push    0
.text:0040116F 68 00 00 00 80          push    80000000h       ; dwFlags
.text:00401174 6A 00                   push    0               ; dwHeadersLength
.text:00401176 6A 00                   push    0               ; lpszHeaders
.text:00401178 68 50 50 40 00          push    offset szUrl    ; "http://www.malwareanalysisbook.com"
.text:0040117D 56                      push    esi             ; hInternet
.text:0040117E FF D7                   call    edi ; InternetOpenUrlA
.text:00401180 EB EB                   jmp     short loc_40116D


5. What is the purpose of this program?

永続化のためにMalservice というWindows サービスを作成して、自身をサービス・ファイルとして登録する。Malservice はシステム起動時に実行されるように設定されている。また排他制御のためHGL345というミューテックスを作成する。ミューテックスとサービスの作成後、C2 URL http://www.malwareanalysisbook.com と20回通信を行う。この際ユーザーエージェントにInternet Explorer 8.0 を用いる。

.text:0040111A 57                      push    edi
.text:0040111B 8B 3D 30 40 40 00       mov     edi, ds:CreateThread
.text:00401121 BE 14 00 00 00          mov     esi, 20
---
.text:00401126 6A 00                   push    0
.text:00401128 6A 00                   push    0               ; dwCreationFlags
.text:0040112A 6A 00                   push    0               ; lpParameter
.text:0040112C 68 50 11 40 00          push    offset ConnectC2URL ; lpStartAddress
.text:00401131 6A 00                   push    0               ; dwStackSize
.text:00401133 6A 00                   push    0               ; lpThreadAttributes
.text:00401135 FF D7                   call    edi ; CreateThread
.text:00401137 4E                      dec     esi
.text:00401138 75 EC                   jnz     short loc_401126


6. When will this program finish executing?

  • すでにマルウェアのプロセスがシステム上に存在している場合、新規のマルウェア・プロセスは即座に終了する
  • WaitForSingleObjectの戻り値が非ゼロだった場合、長期 (49日間)のスリープに入る
  • C2 URL との通信が20回を超えた場合、長期 (49日間)のスリープに入る

Lab 7-2

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

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

1. How does this program achieve persistence?

CFF ExplorerやIDAでファイルを確認したところ、永続化に関係ありそうなインポート関数は見つからなかった。ファイルを実行して動的解析をしてみたが、永続化は認められなかった。

2. What is the purpose of this program?

このマルウェアは最初にCOMオブジェクトの作成を試みる。

.text:0040100F 8D 44 24 00             lea     eax, [esp+24h+ppv]
.text:00401013 50                      push    eax             ; ppv
.text:00401014 68 68 20 40 00          push    offset riid     ; riid IWebBrowser2
.text:00401019 6A 04                   push    4               ; dwClsContext
.text:0040101B 6A 00                   push    0               ; pUnkOuter
.text:0040101D 68 58 20 40 00          push    offset rclsid   ; rclsid Internet Explorer
.text:00401022 FF 15 4C 20 40 00       call    ds:CoCreateInstance
.text:00401028 8B 44 24 00             mov     eax, [esp+24h+ppv]
.text:0040102C 85 C0                   test    eax, eax
.text:0040102E 74 4F                   jz      short loc_40107F

IIDはD30C1661-CDAF-11D0-8A3E-00C04FC9E26E (IWebBrowser2)

.rdata:00402068 ; IID riid
.rdata:00402068 riid            dd 0D30C1661h           ; Data1
.rdata:00402068                                         ; DATA XREF: main_401000+14↑o
.rdata:00402068                 dw 0CDAFh               ; Data2
.rdata:00402068                 dw 11D0h                ; Data3
.rdata:00402068                 db 8Ah, 3Eh, 0, 0C0h, 4Fh, 0C9h, 0E2h, 6Eh; Data4

CLSIDは0002DF01-0000-0000-C000-000000000046 (Internet Explorer)

.rdata:00402058 ; IID rclsid
.rdata:00402058 rclsid          dd 2DF01h               ; Data1
.rdata:00402058                                         ; DATA XREF: main_401000+1D↑o
.rdata:00402058                 dw 0                    ; Data2
.rdata:00402058                 dw 0                    ; Data3
.rdata:00402058                 db 0C0h, 6 dup(0), 46h  ; Data4

上記より、このマルウェアはIWebBrowser2インターフェイスを作成してInternet Explorer経由でNavigate関数を利用するものと推測できる。COMオブジェクトの作成が完了すると、マルウェアはInternet ExplorerでURL http://www.malwareanalysisbook[.]com/ad.htmlをブラウズする。

.text:00401030 8D 4C 24 04             lea     ecx, [esp+24h+pvarg]
.text:00401034 56                      push    esi
.text:00401035 51                      push    ecx             ; pvarg
.text:00401036 FF 15 38 20 40 00       call    ds:VariantInit
.text:0040103C 68 10 30 40 00          push    offset psz      ; "http://www.malwareanalysisbook.com/ad.h"...
.text:00401041 66 C7 44 24 1C 03 00    mov     [esp+2Ch+var_10], 3
.text:00401048 C7 44 24 24 01 00 00 00 mov     [esp+2Ch+var_8], 1
.text:00401050 FF 15 3C 20 40 00       call    ds:SysAllocString

まとめると、このマルウェアはIWebBrowser2のNavigate関数を実行してInternet ExplorerでURL http://www.malwareanalysisbook[.]com/ad.htmlをブラウズする。

3. When will this program finish executing?

Navigate関数を実行してInternet ExplorerでURL http://www.malwareanalysisbook[.]com/ad.htmlをブラウズすると、マルウェアは処理を終了する。

Lab 7-3

解析対象のファイルは以下の通り。※自分の解析環境ではファイルを実行しても期待通りの挙動を見せなかったため、解析内容は静的解析に基づく

ファイル名ファイルの種類MD5ハッシュ値
Lab07-03.exe32ビット EXEBD62DAB79881BC6EC0F6BE4EEF1075BC
Lab07-03.dll32ビット DLL290934C61DE9176AD682FFDD65F0A669

1. How does this program achieve persistence to ensure that it continues running when the computer is restarted?

静的解析の範囲では永続化の手段は判明しなかった。

2. What are two good host-based signatures for this malware?

Lab07-03.exe

  • ファイル名: C:\windows\system32\kerne132.dll (kernel32.dllではない。アルファベットのlではなく数字の1)
.text:004017EA 68 4C 30 40 00          push    offset NewFileName ; "C:\\windows\\system32\\kerne132.dll"
.text:004017EF 68 7C 30 40 00          push    offset ExistingFileName ; "Lab07-03.dll"
.text:004017F4 FF 15 24 20 40 00       call    ds:CopyFileA

Lab07-03.dll

  • ミューテックス名: SADFHUHF
.text:10001048 68 38 60 02 10          push    offset Name     ; "SADFHUHF"
.text:1000104D F3 AB                   rep stosd
.text:1000104F 66 AB                   stosw
.text:10001051 6A 00                   push    0               ; bInheritHandle
.text:10001053 68 01 00 1F 00          push    1F0001h         ; dwDesiredAccess
.text:10001058 AA                      stosb
.text:10001059 FF 15 0C 20 00 10       call    ds:OpenMutexA
---
.text:10001067 68 38 60 02 10          push    offset Name     ; "SADFHUHF"
.text:1000106C 50                      push    eax             ; bInitialOwner
.text:1000106D 50                      push    eax             ; lpMutexAttributes
.text:1000106E FF 15 08 20 00 10       call    ds:CreateMutexA


3. What is the purpose of this program?

Lab07-03.exe

  • CreateFileMappingAとMapViewOfFileを用いてC:\Windows\System32\Kernel32.dllを編集する
  • Lab07-03.dllC:\windows\system32\kerne132.dllにコピーする
  • Cドライブ内のディレクトリから.exe拡張子を持つファイルを検索して内容を改ざんする
.text:00401806                         loc_401806:
.text:00401806 68 44 30 40 00          push    offset aC       ; "C:\\*"
.text:0040180B E8 D0 F9 FF FF          call    EnumDirectoryNWriteToEXE_4011E0
.text:00401810 83 C4 08                add     esp, 8

Lab07-03.dll

  • C2サーバー 127.26.152.13とTCP通信を行う。使用するポート番号は80。127.26.152.13と通信する際はまず最初にhelloというデータをC2サーバーに送る。
  • C2サーバーからはsleepexecqというコマンドのいずれかを受け取る
  • sleepコマンドを受け取った場合、マルウェアは60秒間スリープした後、再度C2サーバーにhelloパケットを送ってレスポンスを待つ
  • execコマンドを受け取った場合、マルウェアは新規のプロセスを作成する
  • qコマンドを受け取った場合、マルウェアはC2サーバーとの通信を終了する
  • C2サーバーからのレスポンスに上記のいずれのコマンドも含まれていなかった場合、マルウェアは60秒間スリープした後、再度C2サーバーにhelloパケットを送ってレスポンスを待つ
  • このマルウェアはSADFHUHFというミューテックスを作成して排他制御を行う

sleep コマンド:

.text:1000113C 8D 8C 24 08 02 00 00    lea     ecx, [esp+1208h+buf]
.text:10001143 6A 05                   push    5               ; MaxCount
.text:10001145 51                      push    ecx             ; Str2
.text:10001146 68 18 60 02 10          push    offset Str1     ; "sleep"
.text:1000114B FF D5                   call    ebp ; strncmp
.text:1000114D 83 C4 0C                add     esp, 0Ch
.text:10001150 85 C0                   test    eax, eax
.text:10001152 75 0D                   jnz     short loc_10001161
---
.text:10001154 68 00 00 06 00          push    60000h          ; dwMilliseconds
.text:10001159 FF 15 00 20 00 10       call    ds:Sleep
.text:1000115F EB 88                   jmp     short loc_100010E9

exec コマンド:

.text:10001161 8D 94 24 08 02 00 00    lea     edx, [esp+1208h+buf]
.text:10001168 6A 04                   push    4               ; MaxCount
.text:1000116A 52                      push    edx             ; Str2
.text:1000116B 68 10 60 02 10          push    offset aExec    ; "exec"
.text:10001170 FF D5                   call    ebp ; strncmp
.text:10001172 83 C4 0C                add     esp, 0Ch
.text:10001175 85 C0                   test    eax, eax
.text:10001177 75 3D                   jnz     short loc_100011B6
---
.text:10001179 B9 11 00 00 00          mov     ecx, 11h
.text:1000117E 8D 7C 24 34             lea     edi, [esp+1208h+StartupInfo]
.text:10001182 F3 AB                   rep stosd
.text:10001184 8D 44 24 24             lea     eax, [esp+1208h+ProcessInformation]
.text:10001188 8D 4C 24 34             lea     ecx, [esp+1208h+StartupInfo]
.text:1000118C 50                      push    eax             ; lpProcessInformation
.text:1000118D 51                      push    ecx             ; lpStartupInfo
.text:1000118E 6A 00                   push    0               ; lpCurrentDirectory
.text:10001190 6A 00                   push    0               ; lpEnvironment
.text:10001192 68 00 00 00 08          push    8000000h        ; dwCreationFlags
.text:10001197 6A 01                   push    1               ; bInheritHandles
.text:10001199 6A 00                   push    0               ; lpThreadAttributes
.text:1000119B 8D 94 24 29 02 00 00    lea     edx, [esp+1224h+CommandLine]
.text:100011A2 6A 00                   push    0               ; lpProcessAttributes
.text:100011A4 52                      push    edx             ; lpCommandLine
.text:100011A5 6A 00                   push    0               ; lpApplicationName
.text:100011A7 C7 44 24 5C 44 00 00 00 mov     [esp+1230h+StartupInfo.cb], 44h
.text:100011AF FF D3                   call    ebx ; CreateProcessA
.text:100011B1 E9 33 FF FF FF          jmp     loc_100010E9

q コマンド:

.text:100011B6                         loc_100011B6:
.text:100011B6 80 BC 24 08 02 00 00 71 cmp     [esp+1208h+buf], 'q'
.text:100011BE 74 10                   jz      short loc_100011D0
---
.text:100011D0 8B 44 24 10             mov     eax, [esp+1208h+hObject]
.text:100011D4 50                      push    eax             ; hObject
.text:100011D5 FF 15 10 20 00 10       call    ds:CloseHandle
---
.text:100011DB                         loc_100011DB:           ; s
.text:100011DB 56                      push    esi
.text:100011DC FF 15 4C 20 00 10       call    ds:closesocket
---
.text:100011E2                         loc_100011E2:
.text:100011E2 FF 15 50 20 00 10       call    ds:WSACleanup

まとめると、Lab07-03.exeLab07-03.dllを実行するためのローダーで、Lab07-03.dllをロードする以外にLab07-03.dllC:\windows\system32\kerne132.dllにコピーしたり、Cドライブ内のディレクトリから.exe拡張子を持つファイルを検索して内容を改ざんするというシステムに対して深刻なダメージを与える行為をする。
Lab07-03.dllはC2サーバー 127.26.152.13 とポート番号80を介してTCP通信を行い、sleepexecqというコマンドを受け取って実行する。

4. How could you remove this malware once it is installed?

動的解析を行えなかったためマルウェアの挙動の全容は明らかにならなかったものの、静的解析よりC:\Windows\System32\Kernel32.dllの改ざんの兆候が見受けられた。もしC:\Windows\System32\Kernel32.dllが改ざんされた場合、システムに対して深刻な影響を与える。またCドライブ内のEXEファイルを軒並み改ざんするようなコードが見受けられた。もっとも確実な復旧方法はシステムのクリーンインストールと思われる。

模範解答

Lab 7-1
1. This program creates the service MalService to ensure that it runs every time the computer is restarted.

2. The program uses a mutex to ensure that only one copy of the program is running at a time.

3. We could search for a mutex named HGL345 and for the service MalService.

4. The malware uses the user-agent Internet Explorer 8.0 and communicates with www.malwareanalysisbook[.]com.

5. This program waits until midnight on January 1, 2100, and then sends many requests to http://www.malwareanalysisbook[.]com/, presumably to conduct a distributed denial-of-service (DDOS) attack against the site.

6. This program will never finish. It waits on a timer until the year 2100, and then creates 20 threads, each of which runs in an infinite loop.

Lab 7-2
1. This program does not achieve persistence. It runs once and then exits.

2. The program displays and advertisement web page to the user.

3. The program finishes executing after displaying the advertisement.

Lab 7-3
1. This program achieves persistence by writing a DLL to C:\Windows\System32 and modifying every .exe file on the system to import that DLL.

2. The program is hard-coded to use the filename kerne132.dll, which makes a good signature. (Note the use of the number 1 instead of the letter l.) The program uses a hard-coded mutex named SADFHUHF.

3. The purpose of this program is to create a difficult-to-remove backdoor that connects to a remote host. The backdoor has two commands: one to execute a command and one to sleep.

4. This program is very hard to remove because it infects every .exe file on the system. It's probably best in this case to restore from backups. If restoring from backups is particularly difficult, you could leave the malicious kerne132.dll file and modify it to remove the malicious content. Alternatively, you could copy kernel32.dll and name it kerne132.dll, or write a program to undo all changes to the PE files.

答え合わせ

Lab 7-1

問2にて「マルウェアのスレッドが複数存在する場合、複数のスレッドが同時にリソースにアクセスすることが無いようにミューテックスを利用して常にひとつのスレッドのみがリソースにアクセスするように制御している。」と解答したが、より明快な解答は、「システムに複数のマルウェア・プロセスが存在しないようにミューテックスを用いて1つのマルウェア・プロセスのみが実行されるようにする」だった。

問5にてURL http://www.malwareanalysisbook[.]com と20回通信を行うと解答したが、これは正確ではない。以下のループではカウンタが20に設定してあり、ループ内でCreateThreadを呼び出してURLと通信を行うスレッドを作成する。

.text:0040111A 57                      push    edi
.text:0040111B 8B 3D 30 40 40 00       mov     edi, ds:CreateThread
.text:00401121 BE 14 00 00 00          mov     esi, 20
---
.text:00401126                         loc_401126:             ; lpThreadId
.text:00401126 6A 00                   push    0
.text:00401128 6A 00                   push    0               ; dwCreationFlags
.text:0040112A 6A 00                   push    0               ; lpParameter
.text:0040112C 68 50 11 40 00          push    offset ConnectC2URL ; lpStartAddress
.text:00401131 6A 00                   push    0               ; dwStackSize
.text:00401133 6A 00                   push    0               ; lpThreadAttributes
.text:00401135 FF D7                   call    edi ; CreateThread
.text:00401137 4E                      dec     esi
.text:00401138 75 EC                   jnz     short loc_401126

以下はCreateThreadに渡される関数である。

.text:0040116D                         loc_40116D:             ; dwContext
.text:0040116D 6A 00                   push    0
.text:0040116F 68 00 00 00 80          push    80000000h       ; dwFlags
.text:00401174 6A 00                   push    0               ; dwHeadersLength
.text:00401176 6A 00                   push    0               ; lpszHeaders
.text:00401178 68 50 50 40 00          push    offset szUrl    ; "http://www.malwareanalysisbook.com"
.text:0040117D 56                      push    esi             ; hInternet
.text:0040117E FF D7                   call    edi ; InternetOpenUrlA
.text:00401180 EB EB                   jmp     short loc_40116D

上記の関数はURL http://www.malwareanalysisbook[.]comと通信を行う。末尾のジャンプ先のアドレスに注目してみると、処理が関数の冒頭に戻るのが分かる。これは無限ループと同義。つまり正確にはURL http://www.malwareanalysisbook[.]com と20回通信を行うのではなく、http://www.malwareanalysisbook[.]comと無限に通信を行うスレッドを20個作成する。

問6の解答も誤っていた。以下のコードの分析がきちんと出来ていなかった。

.text:004010C4 8D 44 24 14             lea     eax, [esp+404h+FileTime]
.text:004010C8 89 54 24 04             mov     dword ptr [esp+404h+SystemTime.wYear], edx
.text:004010CC 8D 4C 24 04             lea     ecx, [esp+404h+SystemTime]
.text:004010D0 89 54 24 08             mov     dword ptr [esp+404h+SystemTime.wDayOfWeek], edx
.text:004010D4 50                      push    eax             ; lpFileTime
.text:004010D5 89 54 24 10             mov     dword ptr [esp+408h+SystemTime.wHour], edx
.text:004010D9 51                      push    ecx             ; lpSystemTime
.text:004010DA 89 54 24 18             mov     dword ptr [esp+40Ch+SystemTime.wSecond], edx
.text:004010DE 66 C7 44 24 0C 34 08    mov     [esp+40Ch+SystemTime.wYear], 2100
.text:004010E5 FF 15 14 40 40 00       call    ds:SystemTimeToFileTime
.text:004010EB 6A 00                   push    0               ; lpTimerName
.text:004010ED 6A 00                   push    0               ; bManualReset
.text:004010EF 6A 00                   push    0               ; lpTimerAttributes
.text:004010F1 FF 15 10 40 40 00       call    ds:CreateWaitableTimerA
.text:004010F7 6A 00                   push    0               ; fResume
.text:004010F9 6A 00                   push    0               ; lpArgToCompletionRoutine
.text:004010FB 6A 00                   push    0               ; pfnCompletionRoutine
.text:004010FD 8D 54 24 20             lea     edx, [esp+410h+FileTime]
.text:00401101 8B F0                   mov     esi, eax
.text:00401103 6A 00                   push    0               ; lPeriod
.text:00401105 52                      push    edx             ; lpDueTime
.text:00401106 56                      push    esi             ; hTimer
.text:00401107 FF 15 1C 40 40 00       call    ds:SetWaitableTimer
.text:0040110D 6A FF                   push    0FFFFFFFFh      ; dwMilliseconds
.text:0040110F 56                      push    esi             ; hHandle
.text:00401110 FF 15 2C 40 40 00       call    ds:WaitForSingleObject

SystemTimeToFileTimeで2100年1月1日という日付を設定し、SetWaitableTimerでタイマーをセットし、2100年1月1日を迎えるまでWaitForSingleObjectで処理を休止し、2100年1月1日になったら先述したhttp://www.malwareanalysisbook[.]comと無限に通信を行うスレッドを20個作成する。http://www.malwareanalysisbook[.]comはC2 URLではなくDDOS攻撃の標的URLである。

Lab 7-3

Lab 7-3は全体的に解析が不十分だった。まずLab07-03.exeは実行の際にWARNING_THIS_WILL_DESTROY_YOUR_MACHINEという引数を必要とするのだが、自分はこれをスルーしていた。

Lab07-03.exeは処理の冒頭で引数の数が (プログラム名を含めて) 2つか確認する。

.text:00401440 8B 44 24 04             mov     eax, [esp+argc]
.text:00401444 83 EC 44                sub     esp, 44h
.text:00401447 83 F8 02                cmp     eax, 2
.text:0040144A 53                      push    ebx
.text:0040144B 55                      push    ebp
.text:0040144C 56                      push    esi
.text:0040144D 57                      push    edi
.text:0040144E 0F 85 BF 03 00 00       jnz     loc_401813

引数の数が2つだった場合、受け取った引数をEAXに、WARNING_THIS_WILL_DESTROY_YOUR_MACHINEという文字列をESIに格納する。

.text:00401454 8B 44 24 5C             mov     eax, [esp+54h+argv]
.text:00401458 BE B0 30 40 00          mov     esi, offset aWarningThisWil ; "WARNING_THIS_WILL_DESTROY_YOUR_MACHINE"
.text:0040145D 8B 40 04                mov     eax, [eax+4]

その後、引数 (EAX) をDLに、WARNING_THIS_WILL_DESTROY_YOUR_MACHINEという文字列 (ESI) をBLに格納して、それぞれの値が同一かどうか1バイトずつ確認していく。

.text:00401460                         loc_401460:
.text:00401460 8A 10                   mov     dl, [eax]
.text:00401462 8A 1E                   mov     bl, [esi]
.text:00401464 8A CA                   mov     cl, dl
.text:00401466 3A D3                   cmp     dl, bl
.text:00401468 75 1E                   jnz     short loc_401488

正しい引数が渡されなかった場合、Lab07-03.exeは処理を終了する。ちなみに自分の解析環境では正しい引数を渡して実行しても期待通りの挙動はしなかった。

問3にて「execコマンドを受け取った場合、マルウェアは新規のプロセスを作成する」と解答したが、より正確には「execコマンドとともに渡されるプロセス名を新規プロセスとして実行する」だった。
以下はexecコマンドを受け取った際に実行される関数。

.text:10001179 B9 11 00 00 00          mov     ecx, 11h
.text:1000117E 8D 7C 24 34             lea     edi, [esp+1208h+StartupInfo]
.text:10001182 F3 AB                   rep stosd
.text:10001184 8D 44 24 24             lea     eax, [esp+1208h+ProcessInformation]
.text:10001188 8D 4C 24 34             lea     ecx, [esp+1208h+StartupInfo]
.text:1000118C 50                      push    eax             ; lpProcessInformation
.text:1000118D 51                      push    ecx             ; lpStartupInfo
.text:1000118E 6A 00                   push    0               ; lpCurrentDirectory
.text:10001190 6A 00                   push    0               ; lpEnvironment
.text:10001192 68 00 00 00 08          push    8000000h        ; dwCreationFlags
.text:10001197 6A 01                   push    1               ; bInheritHandles
.text:10001199 6A 00                   push    0               ; lpThreadAttributes
.text:1000119B 8D 94 24 29 02 00 00    lea     edx, [esp+1224h+CommandLine]
.text:100011A2 6A 00                   push    0               ; lpProcessAttributes
.text:100011A4 52                      push    edx             ; lpCommandLine
.text:100011A5 6A 00                   push    0               ; lpApplicationName
.text:100011A7 C7 44 24 5C 44 00 00 00 mov     [esp+1230h+StartupInfo.cb], 44h
.text:100011AF FF D3                   call    ebx ; CreateProcessA

ドキュメントによるとCreateProcessAのlpApplicationNameの値がNULL (0)だった場合、lpCommandLineで指定されたプロセス名を新規プロセスとして実行する。しかしIDAでコードを遡ってもlpCommandLineに渡されるプロセス名を確認できない。これはプロセス名をC2サーバーのレスポンス・データを格納している受信バッファから読み取るためである。

以下はサブルーチン 0x10001010のスタック・フレーム。

.text:10001010                         ExecC2command_10001010 proc near
.text:10001010
.text:10001010                         hObject= dword ptr -11F8h
.text:10001010                         name= sockaddr ptr -11F4h
.text:10001010                         ProcessInformation= _PROCESS_INFORMATION ptr -11E4h
.text:10001010                         StartupInfo= _STARTUPINFOA ptr -11D4h
.text:10001010                         WSAData= WSAData ptr -1190h
.text:10001010                         buf= byte ptr -1000h
.text:10001010                         var_FFF= byte ptr -0FFFh
.text:10001010                         CommandLine= byte ptr -0FFBh
.text:10001010                         arg_4= dword ptr  8

受信バッファ (buf) は0x1000に確保される。コマンドライン用の変数 (CommandLine)は受信バッファより5バイト下の0x0FFBに確保される (0x1000 - 0x0FFB = 5)。 つまりCommandLine変数は受信バッファの先頭から (0から数えて) 5バイト目に位置していることになる。C2サーバーからexecコマンドを受け取った場合、受信バッファの先頭4バイトにはexecという文字列が格納され、6バイト目以降にCreateProcessAによって実行されるプロセス名が含まれていることになる。

アドレス
0x1000e
0x0fffx
0x0ffee
0x0ffdc
0x0ffc
0x0ffb[first byte of process name]

問3や問4で「Lab07-03.exeはC:\Windows\System32\Kernel32.dllを改ざんする」と解答したが、これは誤りだった。実際には Cドライブ内の.exeファイルの中に含まれるkernel32.dllという文字列をkerne132.dllに書き換えるための処理だった。

サブルーチン0x4011E0はCドライブ内の.exeファイルを検索して、ファイルの中身を書き換える。

.text:00401806                         loc_401806:
.text:00401806 68 44 30 40 00          push    offset aC       ; "C:\\*"
.text:0040180B E8 D0 F9 FF FF          call    EnumDirectoryNWriteToEXE_4011E0

サブルーチン0x4011E0の中ではサブルーチン 0x4010A0が呼び出される。

.text:00401403 55                      push    ebp             ; lpFileName
.text:00401404 E8 97 FC FF FF          call    Replace_kernel32_to_kerne132_4010A0

サブルーチン 0x4010A0には以下のコードが含まれている。

.text:0040116E 68 20 30 40 00          push    offset Str2     ; "kernel32.dll"
.text:00401173 53                      push    ebx             ; Str1
.text:00401174 FF 15 64 20 40 00       call    ds:_stricmp
.text:0040117A 83 C4 08                add     esp, 8
.text:0040117D 85 C0                   test    eax, eax
.text:0040117F 75 26                   jnz     short loc_4011A7
---
.text:00401181 8B FB                   mov     edi, ebx
.text:00401183 83 C9 FF                or      ecx, 0FFFFFFFFh
.text:00401186 F2 AE                   repne scasb             ; equivalent to strlen; hence this instruction is checking the length of Str1 which is expected to be "kernel32.dll"
.text:00401188 F7 D1                   not     ecx
.text:0040118A 8B C1                   mov     eax, ecx
.text:0040118C BE 10 30 40 00          mov     esi, offset dword_403010 ; copying "kerne132.dll" to ESI
.text:00401191 8B FB                   mov     edi, ebx        ; copying "kernel32.dll" to EDI
.text:00401193 C1 E9 02                shr     ecx, 2
.text:00401196 F3 A5                   rep movsd               ; equivalent to memcpy; hence this instruction replaces the string "kernel32.dll" to "kerne132.dll"
.text:00401198 8B C8                   mov     ecx, eax
.text:0040119A 83 E1 03                and     ecx, 3
.text:0040119D F3 A4                   rep movsb
.text:0040119F 8B 74 24 10             mov     esi, [esp+1Ch+var_C]
.text:004011A3 8B 7C 24 20             mov     edi, [esp+1Ch+lpFileName]

模範解答によると repne scasb 命令はstrlenと同義で、rep movsd 命令はmemcpyと同義とのこと。よって上記のコードはkernel32.dllという文字列をkerne132.dllという文字列に置き換える。そして、この文字列置換のコードはCドライブ内の.exeファイルを書き換える関数の中で呼び出されている。つまり、Cドライブ内の.exeファイルの中からkernel32.dllという文字列を探し出し、kerne132.dllという文字列に置き換えるものと推測できる。

Lab07-03.exeの解析よりLab07-03.dllのコピーがC:\windows\system32\kerne132.dllに作成されることが判明している。これら一連の処理は.exeファイルの中に含まれるkernel32.dllのインポート関数を書き換えて悪意のあるkerne132.dllのインポート関数を参照させることが目的である。改ざんが成功するとCドライブ内に存在するEXEファイルは悪意のあるkerne132.dllを参照するようになるので、これがマルウェアの永続化につながる。(模範解答では静的解析のみで、これら一連の挙動を明らかにしてみせたが、自分はそこまで及ばなかった。)

問3にて「Lab07-03.exeLab07-03.dllを実行するためのローダー」と解答したが、これは正確とは言えないかもしれない。Lab07-03.exeの主目的は、EXEファイルのインポート関数の書き換えとLab07-03.dllC:\windows\system32\kerne132.dllにコピーすることで、明示的にLab07-03.dllを読み込むようなコードは見受けられなかった。ただし、Lab07-03.exe自身も正規のkernel32.dllから関数をインポートしているので、インポート関数の書き換えが完了すると、Lab07-03.exeLab07-03.dllをロードするようになる。その意味ではLab07-03.exeLab07-03.dllを実行するためのローダーと言えるかもしれない。

Leave a Reply

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