關于 webshell
由于 webshell 是通過 http 協議進行訪問,被遠程控制的服務器或者遠程主機不易發現,非常隱蔽。簡單的說來,webshell 就是后端程序編寫的木馬后門,通常是一條后端編程語言語句或者一個文件,通常是通過一句話執行命令行進行上傳下載文件、查看數據庫、執行任意程序命令等。
基于特征的檢測方式
該方式的優點是實現原理簡單,容易實現,準確率高;缺點就是新型的 webshell 無法檢測,需要不斷地更新規則庫進行完善。由于 webshell 是通過 http 協議進行訪問,檢測 webshell 常見有兩種工作方式:
· (WAF)通過 web 應用防火墻進行實時的特征及規則檢測
· 通過特征及規則對目錄下文件進行掃描檢測
基于 AST 語法樹的檢測方式
該方式主要使用抽象語法樹進行解析,遇到危險操作進行統計分析告警。優點在于動態解析,能發現具體代碼如何工作, 準確性高,缺點是該方式的實現方式復雜,局限大,如果需要對不同的后端代碼程序進行檢測,需要實現不同的語言的語法樹解析,難度呈現指數級。
Yara 規則快速匹配工具
Yara[fn:1]是一款旨在幫助惡意軟件研究人員識別和分類惡意軟件樣本的開源工具(由 virustotal 的軟件工程師 Victor M. Alvarezk 開發),使用 Yara 可以基于文本或二進制模式創建惡意軟件家族描述信息,當然也可以是其他匹配信息。
支持多平臺,可以運行在 windows、linux、mac OS X,并通過命令行界面或擴展的 Python 腳本使用。
Yara 使用
使用 Yara 進行規則匹配時需要兩樣東西:規則文件和目標文件,目標文件可以是文件、文件夾或進程。Yara-rules規則[fn:2],開源社區維護了一個很好的規則庫。該庫包含惡意軟件等規則,這里我們直接使用webshells_index.yar[fn:3]及 webshells 目錄下的規則即可。yara 參數如下:
YARA 3.11.0, the pattern matching swiss army knife.
Usage: yara [OPTION]... [NAMESPACE:]RULES_FILE... FILE | DIR | PID
Mandatory arguments to long options are mandatory for short options too.
--atom-quality-table=FILE path to a file with the atom quality table
-C, --compiled-rules load compiled rules
-c, --count print only number of matches
-d, --define=VAR=VALUE define external variable
--fail-on-warnings fail on warnings
-f, --fast-scan fast matching mode
-h, --help show this help and exit
-i, --identifier=IDENTIFIER print only rules named IDENTIFIER
-l, --max-rules=NUMBER abort scanning after matching a NUMBER of rules
--max-strings-per-rule=NUMBER set maximum number of strings per rule (default=10000)
-x, --module-data=MODULE=FILE pass FILE's content as extra data to MODULE
-n, --negate print only not satisfied rules (negate)
-w, --no-warnings disable warnings
-m, --print-meta print metadata
-D, --print-module-data print module data
-e, --print-namespace print rules' namespace
-S, --print-stats print rules' statistics
-s, --print-strings print matching strings
-L, --print-string-length print length of matched strings
-g, --print-tags print tags
-r, --recursive recursively search directories
-k, --stack-size=SLOTS set maximum stack size (default=16384)
-t, --tag=TAG print only rules tagged as TAG
-p, --threads=NUMBER use the specified NUMBER of threads to scan a directory
-a, --timeout=SECONDS abort scanning after the given number of SECONDS
-v, --version show version information
遞歸遍歷檢測
~# yara -r ./webshell_index.yar ~/

檢測webshell
統計樣本數量
統計查看有多少個樣本。 例如:
find ~/webshell/ -type f | wc -l

統計樣本
統計檢測到多少個樣本,由于不同規則檢測到重復的樣本,通過 uniq 去重。 例如:
~# yara -r ~/rules/webshells_index.yar ~/webshell/ | awk -F' ' '{print $2}'|sort|uniq|wc -l902

統計樣本去重
webshell 樣本
webshell的樣本在 github [fn:4]上找到的開源樣本。主要用來測試 yara 規則及與商業或開源的其他 webshell 檢測工具做對比。

檢測結果
在 1613 個樣本中,檢測識別出 902 個樣本。
樣本總數 檢測結果 檢測工具
1613 902 yara
隨機抽樣檢測(該結果有一些問題,實際檢測出來了,但由于命令行去重處理在 linux 下不能很好識別中文與空格的文件及文件夾名導致的,僅作參考)。
樣本總數 檢測結果 檢測工具
316 110 yara
注意:經過隨機抽樣檢測,在不同的情況下結果有差異。實際情況還是要根據 yara 的規則庫完善情況而定。
- https://github.com/VirusTotal/yar
- https://github.com/Yara-Rules/rules
- https://github.com/Yara-Rules/rules/blob/master/webshells%5Findex.yar
- https://github.com/7ym0n/webshell
微信公眾號
hacktribe