Practical Malware Analysis (by Michael Sikorski and Andrew Honig) Lab21のWriteUp。
まずは自分の解答を載せて、最後に模範解答を載せる。不正解の解答も戒めとしてそのまま載せる事とする。
ラボはこちらからダウンロード可能。
ちなみに、これが最終のラボになる。
Lab 21-1
ファイル名 | ファイルの種類 | MD5ハッシュ値 |
Lab21-01.exe | 64ビット EXE | C8403FB05244E23A7931C766409B5E22 |
※ このファイルはLab09-02.exeに手を加えて、64ビット・プログラムとしてコンパイルしたものである。
1. What happens when you run this program without any parameters?
特に何も起きない。
2. Depending on your version of IDA Pro, main may not be recognized automatically. How can you identify the call to the main function?
自分の使用しているIDAはmain (0x1400010C0) を認識できたので割愛。
3. What is being stored on the stack in the instructions from 0x0140001150 to 0140001161?
ocl.exe
というファイル名が格納されている。

4. How can you get this program to run its payload without changing the filename of the executable?
ファイルをデバッガ等で開いて、アドレス0x014000120Cのjz命令をjnz命令に書き換えて保存する。

以下はx64dbgでのアセンブリ命令の書き換え手順。
Lab21-01.exeをx64dbgにロードして、アドレス 0x014000120Cへ飛び、ダブルクリックする。すると以下のウインドウがポップアップする。

je命令をjne命令に書き換えて、OKをクリックする。


FileメニューからPatch fileを選択して、Patch Fileボタンをクリックし、パッチしたファイルを保存する。

FakeNet-NGを起動して、パッチしたファイル Lab21-01-patched.exeを実行したところ、Lab21-01-patched.exeがコマンドプロプントのプロセス cmd.exe
を起動して、www.practicalmalwareanalysis.com
と通信している様子が確認できた。



5. Which two strings are being compared by the call to strncmp at 0x0140001205?
実行しているファイル名がjzm.exe
と一致するか確認している。


6. Does the function at 0x01400013C8 take any parameters?
ソケット・ディスクリプタを引数として受け取る。ソケットは0x14000124Fにて作成されている。


7. How many arguments are passed to the call to CreateProcess at 0x0140001093? How do you know?
18個の引数が渡される。詳細は下記を参照。CreateProcessAのドキュメントはこちら。

レジスタ | パラメーター | 引数の値 |
RCX | lpApplicationName | NULL |
RDX | CommandLine | cmdという文字列 |
R8 | lpProcessAttributes | NULL |
R9 | lpThreadAttributes | NULL |
RSP | bInheritHandles | 1 |
RSP | dwCreationFlags | 0 |
RSP | lpEnvironment | NULL |
RSP | lpCurrentDirectory | NULL |
RSP | lpStartupInfo | STARTUPINFO構造体へのポインタ |
RSP | StartupInfo.cb | 104 |
RSP | StartupInfo.dwFlags | 0x100 |
RSP | StartupInfo.hStdInput | ソケット・ディスクリプタへのハンドル |
RSP | StartupInfo.hStdOutput | ソケット・ディスクリプタへのハンドル |
RSP | StartupInfo.hStdError | ソケット・ディスクリプタへのハンドル |
RSP | lpProcessInformation | PROCESS_INFORMATION構造体へのポインタ |
RSP | ProcessInformation.hProcess | cmd.exeへのハンドル |
RSP | ProcessInformation.hThread | cmd.exeのプライマリ・スレッドへのハンドル |
RSP | ProcessInformation.dwProcessId | cmd.exeのプロセスID |
Lab 21-2
ファイル名 | ファイルの種類 | MD5ハッシュ値 |
Lab21-02.exe | 32ビット EXE | 986C463587FF681A30D7C75256745E99 |
※ このファイルはLab12-01.exeに変更が加えられたものである。
1. What is interesting about the malware's resource sections?
3つのWindows PEファイルが、X64
、X64DLL
、X86
というリソースにそれぞれ埋め込まれている。
2. Is this malware compiled for x64 or x86?
Lab21-02.exe自体は32ビット・プログラムなので、x86を対象としている。ただし、Windowsは後方互換性があるので、64ビットOSでも32ビット・プログラムは動作する。
よってLab21-02.exeはx64とx86のどちらでも動作する。
3. How does the malware determine the type of environment in which it is running?
IsWow64Processを呼び出して、自身が64ビットOSで実行されているか確認する。ちなみにIsWow64Processのアドレスは、プログラムの実行時にGetModuleHandleAとGetProcAddressによって動的に解決される。そのためインポート・テーブルにIsWow64Processは記載されていない。

4. What does this malware do differently in an x64 environment versus an x86 environment?
64ビットOSで実行されている場合はリソースセクションからX64DLL
とX64
というリソースを抽出して、それぞれLab21-02x.dll
、Lab21-02x.exe
としてシステム・ディレクトリに保存する。

64ビットOS上でLab21-02.exeを実行したところ、先述したLab21-02x.dll
およびLab21-02x.exe
がC:\Windows\SysWOW64
に作成された。

32ビットOSで実行されている場合はリソースセクションからX86
というリソースを抽出して、Lab21-02.dll
としてシステム・ディレクトリに保存する。

動的解析したところ、先述したLab21-02.dll
がC:\Windows\SysWOW64
に作成された。(手元に32ビットOSが無かったので、IsWow64Processのcall直後のジャンプ命令を書き換えることで、32ビットOS時の振る舞いをするようマルウェアを誘導した。)

5. Which files does the malware drop when running on an x86 machine? Where would you find the file or files?
32ビットOSで実行されている場合はリソースセクションからX86
というリソースを抽出して、Lab21-02.dll
としてシステム・ディレクトリに保存する。問4の回答も併せて参照。
6. Which files does the malware drop when running on an x64 machine? Where would you find the file or files?
64ビットOSで実行されている場合はリソースセクションからX64DLL
とX64
というリソースを抽出して、それぞれLab21-02x.dll
、Lab21-02x.exe
としてシステム・ディレクトリに保存する。問4の回答も併せて参照。
7. What type of process does the malware launch when run on an x64 system?
64ビットOSでLab21-02.exeを実行すると、コマンドプロンプトのプロセスcmd.exe
が起動する。さらにcmd.exe
はLab21-02x.exe
というプロセスを生成する。

ShellExecuteAによって以下のコマンドが実行される。
cmd.exe /c C:\windows\system32\Lab21-02x.exe
上記のコマンドにより、コマンドプロンプトからLab21-02x.exe
が起動される。

8. What does the malware do?
Lab21-02.exeは、まず自身が64ビットOSで実行されているか否か確認する。
64ビットOSで実行されている場合
64ビットOSで実行されている場合はリソースセクションからX64DLL
とX64
というリソースを抽出して、それぞれLab21-02x.dll
、Lab21-02x.exe
としてシステム・ディレクトリに保存する。
Lab21-02x.exe
とLab21-02x.dll
は、それぞれ64ビットのEXEとDLLである。
2つのファイルの抽出が完了すると、cmd.exe /c C:\windows\system32\Lab21-02x.exe
というコマンドを実行して、コマンドプロンプトから先述したLab21-02x.exe
というファイルを実行する。
Lab21-02x.exe
はエクスプローラーのプロセスexplorer.exe
にLab21-02x.dll
をインジェクトする。Lab21-02x.dll
がインジェクトされると、Press OK to reboot
というメッセージボックスがポップアップする。


32ビットOSで実行されている場合
32ビットOSで実行されている場合はリソースセクションからX86
というリソースを抽出して、Lab21-02.dll
としてシステム・ディレクトリに保存する。Lab21-02.dll
は32ビットのDLLである。
Lab21-02.dll
の抽出が完了すると、Lab21-02.exeはエクスプローラーのプロセスexplorer.exe
にLab21-02.dll
をインジェクトする。Lab21-02.dll
がインジェクトされるとPress OK to reboot
というメッセージボックスがポップアップする。

まとめると、Lab21-02.exeの目的は悪意のあるDLLをドロップしてエクスプローラーのプロセスにインジェクトすることである。
32ビットOSの場合は、Lab21-02.exeが直接 悪意のあるDLLのインジェクションを行うが、64ビットOSの場合はLab21-02.exeからドロップされたLab21-02x.exe
がDLLのインジェクションを行う。
模範解答
Lab 21-1
1. When you run the program without any parameters, it exits immediately.
2. The main function is located at 0x01400010C0. You can spot the call to main by looking for a function call that accepts an integer and two pointers as parameters.
3. The string ocle.exe is stored on the stack.
4. To have this program run its payload without changing the filename of the executable, you can patch the jump instruction at 0x0140001213 so that its a NOP instead.
5. The name of the executable is being compared against the string jzm.exe by the call to strncmp at 0x0140001205.
6. The function at 0x01400013C8 takes one parameter, which contains the socket created to the remote host.
7. The call to CreateProcess takes 10 parameters. We can't tell from the IDA Pro listing because we can't distinguish between things being stored on the stack and things being used in a function call, but the function is documented in MSDN as always taking 10 parameters.
Lab 21-2
1. The malware contains the resource section X64, X64DLL, and X86. Each of the resources contains an embedded PE file.
2. Lab21-02.exe is compiled for a 32-bit system. This is shown in the PE header's Characteristics field, where the IMAGE_FILE32BIT_MACHINE flag is set.
3. The malware attempts to resolve and call IsWow64Process to determine if it is running on an x64 system.
4. On an x86 machine, the malware drops the X86 resource to disk and injects it into explorer.exe. On an x64 machine, the malware drops two files from the X64 and X64DLL resource sections to disk and launches the executable as a 64-bit process.
5. On an x86 system, the malware drops Lab21-02.dll into the Windows system directory, which will typically be C:\Windows\System32\.
6. On an x64 system, the malware drops Lab21-02x.dll and Lab21-02x.exe into the Windows system directory, but because this is a 32-bit process running in WOW64, the directory is C:\Windows\WOW64\.
7. On an x64 system, the malware launches Lab21-02x.exe which is a 64-bit process. You can see this in the PE header, where the Characteristics field has the IMAGE_FILE_64BIT_MACHINE flag set.
8. On both x64 and x86 systems, the malware performs DLL injection into explorer.exe. On an x64 system, it drops and runs a 64-bit binary to inject a 64-bit DLL into the 64-bit running explorer.exe. On an x86 system, it injects a 32-bit DLL into the 32-bit running explorer.exe.
答え合わせ
Lab 21-1
模範解答によると、CreateProcessAが受け取る引数の数は10個。(Microsoftのドキュメントでそのように規定されているため。)
StartupInfo構造体やProcessInformation構造体のメンバへの値の代入は引数としてカウントされない。( 自分はStartupInfo構造体の5つのメンバとProcessInformation構造体の3つのメンバへの値の代入もカウントしたため、答えが10個ではなく18個となった。)