Try   HackMD

CHTB2021-Alienware

Info

題目敘述

We discovered this tool in the E.T. toolkit which they used to encrypt and exfiltrate files from infected systems. Can you help us recover the files?

能力

  • 靜態分析exe檔
  • 動態分析提取dll檔
  • 靜態分析dll檔
  • 動態分析
  • 可獨立查詢windows API的使用手冊
  • 可動態修改calling table和參數

難易度 難易度:★★★☆☆

tags: CTF-WRITEUP

Write_Up

使用 IDA Pro競態分析

SHIFT + F12看到xuTaV.dll%s%sencryptFiles

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

追蹤字串位置在TlsCallback_0

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

TlsCallback_0做了幾件事:

  1. 載入Resource檔案

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 向系統申請一段空間並初始為0

  3. 逐Byte做xor解碼

  4. 將每一個解碼完Byte存進去剛剛申請的空間

  5. 寫檔並命名為xuTaV.dll

  6. 動態載入xuTaV.dll並使用其中encryptFiles的API

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

因此目標在取得xuTaV.dll並分析encryptFiles怎麼實作加密功能

動態分析找出 xuTaV.dll

方法一:從x64dbg的記憶體中提取

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

方法二:程式執行到一半到存放xuTaV.dll的位置拉出來就好 GetTempPathA會取得當下User目錄下Temp得位置

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

檔案會存在這個目錄下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

靜態分析xuTaV.dll

直接定位到encryptFiles的位置 encryptFiles做了幾件事:

  1. C:\\ User \\ [user_name] \\ Docs\\ . 路徑下的所有檔案逐一加密
  2. 加密後的檔案會另存新檔,並"再"加上附檔名.alien
  3. 透過sub_1800011C0函式進行檔案加密,完成後,會將原本的檔案刪除

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

sub_1800011C0做了幾件事:

  1. 利用CryptDeriveKey產生加密的金鑰
  2. 使用CryptEncrypt函式並引用上一步產生的金鑰進行加密

觀察微軟對這兩個API的說明

  • CryptDeriveKey
    • Important This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.
    • The CryptDeriveKey function generates cryptographic session keys derived from a base data value. This function guarantees that when the same cryptographic service provider (CSP) and algorithms are used, the keys generated from the same base data are identical. The base data can be a password or any other user data.
    • 加密和解密使用同一把金鑰,且當CSP相同,金鑰會相同
      • 這樣就不用找解密金鑰了~直接用現成的就好 :+1:
  • CryptEncrypt
    • Important This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases.
    • The CryptEncrypt function encrypts data. The algorithm used to encrypt the data is designated by the key held by the CSP module and is referenced by the hKey parameter.
      ​​​​​​​​BOOL CryptEncrypt(
      ​​​​​​​​HCRYPTKEY  hKey,
      ​​​​​​​​HCRYPTHASH hHash,
      ​​​​​​​​BOOL       Final,
      ​​​​​​​​DWORD      dwFlags,
      ​​​​​​​​BYTE       *pbData,
      ​​​​​​​​DWORD      *pdwDataLen,
      ​​​​​​​​DWORD      dwBufLen
      ​​​​​​​​  );
      
    • 觀察加密和解密(CryptDecrypt)的差別->差一個dwBufLen
      ​​​​​​​​BOOL CryptDecrypt(
      ​​​​​​​​HCRYPTKEY  hKey,
      ​​​​​​​​HCRYPTHASH hHash,
      ​​​​​​​​BOOL       Final,
      ​​​​​​​​DWORD      dwFlags,
      ​​​​​​​​BYTE       *pbData,
      ​​​​​​​​DWORD      *pdwDataLen
      ​​​​​​​​);
      

動態分析xuTaV.dll

透過動態分析驗證思緒:

  1. 建立 C:\ User \ [user_name] \ Docs\ 路徑
  2. 將 Confidential.pdf.alien 放在指定路徑下
  3. 動態分析將「 斷點 」下在 CryptEncrypt 附近
  4. 將多餘的參數刪掉
  5. 修改API呼叫表
  6. 將「斷點」下在「 寫檔完成之後」,再檢查指定資料夾下是否解密成功 :question:

以下為成式執行的流程,再動態分析中有3個重點

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

1.NOP不必要的參數

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2.修改 CryptEncrypt -> CryptDecrypt

修改後:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

3.設斷點在完成寫檔,避免原檔案被刪除

最後會取得「Confidential.pdf.alien.alien」檔案,將副檔名都刪掉打開看看是否會成功^^

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →