背景概述
安全團隊近期捕獲到一個後門木馬樣本,是一個64位的Linux核心模組,根據程式碼中出現的關鍵詞將其命名為Rmgr木馬。該木馬病毒會連線惡意域名,隱藏本身的程式,並對終端植入後門,用於後續進行其他入侵行為。
經過研究人員分析,木馬包含如下幾個檔案:

執行流程
木馬的母體檔案只有 rmgr.ko 這一個檔案,透過 insmod 命令裝載到核心模組後開啟運作,逐步釋放木馬的其他元件部分。木馬本身作為一個核心模組,具有ring0的許可權,可以從核心層面實現隱藏程式、檔案、埠等操作,相較於ring3具有更強的隱藏能力。
樣本分析
rmgr.ko 分析
隱藏自身模組
模組隱藏對自身進行了隱藏處理

其程式碼效果等同於如下:
list_del(&THIS_MODULE->list); //實現 lsmod 的隱藏
kobject_del(&THIS_MODULE->mkobj.kobj); //實現 /sys/modules 的隱藏
隨後還透過底層替換 /proc/modules 檔案的show函式過濾了ati_remote3模組的顯示


隱藏程式載入模組
我們在使用諸如 cat /proc/PID/maps 這樣的指令檢視程式載入的模組時,底層會呼叫到 seq_path 函式,木馬則使用了 register_kretprobe 技術對 seq_path 實施了監聽,只要檢測到路徑為 /tmp/.tmp_ 開頭的模組則將其資訊隱藏,可實現 /proc/PID/maps 中木馬模組的隱藏


隱藏檔案
木馬透過hook的方式替換了 sys_getdents 系統呼叫,然後執行如下流程: fake_sys_getdents -> vfs_readdir -> fake_filldir


fake_filldir 函式中可以看到模組隱藏了/proc/[PID]、/tmp/.tmp_*、模組自身以及所釋放的數個檔案,這樣 ls 命令就再也看不到這些檔案了
只有在明確知道檔名的情況下才能看到被隱藏的檔案,增強了木馬的隱蔽性

隱藏埠


木馬還替換了 /proc/net/tcp 檔案的 seq_show 函式,隱藏 6825/6826/6821/6822 埠
核心與使用者態程式間通訊
建立 /proc/.dot3 檔案用於接受使用者態程式的指令

在 proc_create_data 的第四個引數中,/proc/.dot3 檔案的寫入和讀取操作分別由 dot_write 和 dot_open 控制

先看看 dot_write 的內容,透過switch case分支條件可以看出外部程式對 /proc/.dot3 寫入不同的內容可以實現不同的功能:

這裡是總結出來的功能列表:
• 8位元組長度指令
– 0xFF11 = 向木馬核心模組註冊寫入該命令的程式,該程式及子程式將會受到核心的保護
– 0x55AA = 更新木馬核心模組的檔案資訊
– 0x66BC = 控制C2伺服器的互動頻率
– 0x66BD = 向指定程式注入rmgr_inject.so檔案,替換部分C庫函式
– 0x66BE = 取消0x66BD指令
• 40位元組長度指令
– 0xA43F = 檢視 rmgr_inject.so 檔案的路徑
– 0xA45F = 更新 rmgr_inject.so 檔案的路徑
– 0xF33F = 更新 rmgr.elf 檔案的路徑
– 0xF34F = 檢視 rmgr.elf 檔案的路徑
• 其他非指令資料
– 直接存入核心緩衝區,其內容可以透過直接讀取 /proc/.dot3 檔案獲得
/proc/.dot3 的寫入功能分析完了,再看看讀取部分。讀取功能實際是由 dot_show 函式實現:

我們再看看 dot_show 的操作:

木馬在讀取的程式上做了限制,只允許 rmgr.elf 程式讀取,而所讀取到內容即是前面提到的向 /proc/.dot3 中寫入的非指令資料
啟動核心執行緒
完成前面的工作後,啟動 dot_thread 執行緒

然後釋放 rmgr_inject.so 和 rmgr.elf 檔案,並使用 /bin/chmod 命令賦予了 755 許可權


隨後以 [khelper] 為名稱啟動了 rmgr.elf 程式

rmgr_inject.so 分析
rmgr_inject.so 是一個動態連結庫,可以透過LD_PRELOAD環境變數及/etc/ld.so.preload等方式注入到普通程式中,達到替換C庫函式的目的

rmgr_inject.so 替換了 bash_add_history 函式,將使用者執行過的命令一一記錄,儲存到/proc/.dot3檔案中,可供將來發回C2服務端

同時監控 ss 指令,隱藏26660/26661/26662/26657/26658/26659這幾個埠
rmgr.elf 分析
rmgr.elf加了UPX殼,脫殼後的函式列表如下:

可以看出該二進位制檔案是由C++編寫的。經過分析後得知該檔案包含兩個類,rmgr_client類處理與C2伺服器的互動,sshd_client類處理與sshd後門的互動
利用核心模組隱藏程式
程式啟動後首先第一件事就是向 /proc/.dot 寫入 0xFF11 指令獲得保護,利用rmgr.ko 的ring0許可權隱藏自身程式以及子程式
與C2伺服器通訊
隨後與 hm2.yrnykx.com C2伺服器進行連線,等待指令

rmgr_client::handle_read_body 函式負責處理C2服務端發來的命令

這裡整理了一份可供C2服務端呼叫的功能指令,列表如下:
• 0x1000F = 從C2服務端下載更新木馬的所有元件
• 0x10008 = 未知功能
• 0x1000A = 建立一個新的sshd後門會話
• 0x1000B = 關閉指定的sshd後門會話
• 0x10004 = 讀取 /proc/.dot3 中儲存的內容,發往C2伺服器
• 0x10007 = 接收檔案
• 0x10011 = 啟動sshd後門
• 0x10012 = 停止sshd後門
• 0x10010 = 暫停sshd後門
其中比較值得注意的是,收到0x10011指令後,rmgr.elf會從自身釋放出 rmgr_sshd.elf 後門檔案並啟動,這是個被修改過的openssh服務,後面會有這個檔案的分析

建立好ssh後門後(監聽地址是127.0.0.1:26657),C2服務端便可使用 0x1000A 指令連線,此時C2伺服器和ssh後門的流量會透過sshd_client類進行處理和轉發

rmgr.elf 還存在下載替換 /usr/sbin/sshd、/usr/bin/ssh、/lib/modules/%s/kernel/drivers/input/misc/ati_remote3.ko、/etc/sysconfig/modules/ati_remote3.modules 檔案的行為,但一直沒等到這些行為的觸發

rmgr_sshd.elf 分析
rmgr_sshd.elf 由rmgr.elf釋放,本身是一個被修改過的openssh,用於提供ssh後門。該後門的配置檔案是硬編碼的,監聽地址為127.0.0.1:26657

ssh中硬編碼的私鑰檔案:

這個ssh後門的認證函式還被修改過了,不論輸入什麼密碼都會直接認證成功

由於ssh後門的監聽地址位於本地,因此從外部掃描是觀察不到這個後門存在的,並且只能透過rmgr.elf中的sshd_client物件進行流量轉發訪問。
IOC
rmgr_inject.so: MD5 a4013790035fc3021546c09e9cf85df1
rmgr.elf: MD5 5ecf30b7a6221af8f209a7b6681f91f9
rmgr_sshd.elf: MD5 7d859a22f38f0bcd55a46bc8b67c40df
rmgr.ko: MD5 82bf8e99391ec0cdc527f2fc94621899
hxxp://hm2.yrnykx.com
深信服安全產品解決方案
1.深信服安全感知管理平臺SIP、下一代防火牆AF、終端響應檢測平臺EDR使用者,建議及時升級最新版本,並接入安全雲腦,使用雲查服務以及時檢測防禦新威脅;

2.深信服安全產品整合深信服SAVE人工智慧檢測引擎,擁有強大的泛化能力,精準防禦未知病毒;
3.深信服推出安全運營服務,透過以“人機共智”的服務模式幫助使用者快速提高安全能力。針對此類威脅,安全運營服務提供安全裝置策略檢查、安全威脅檢查、相關漏洞檢查等服務,確保第一時間檢測風險以及更新策略,防範此類威脅。
作為國內前沿的網路安全廠商,深信服多年來持續投入勒索防護研究,內容涵蓋黑產洞察、病毒研究、情報追蹤、投放分析,並沉澱出完整覆蓋突破邊界、病毒投放、加密勒索、橫向傳播等勒索攻擊鏈的系統性解決方案,目前已協助1000+各行業使用者有效構建起了勒索病毒防護最佳實踐。