*本文原創作者:Green_m,本文屬FreeBuf原創獎勵計畫,未經許可禁止轉載
0×01 sysmon介紹
sysmon是由Windows Sysinternals出品的一款Sysinternals系列中的工具。它以系統服務和設備驅動程序的方法安裝在系統上,並保持常駐性。sysmon用來監視和記錄系統活動,並記錄到windows事件日誌,可以提供有關行程創建,網絡連結和檔案創建時間更改的詳細資訊。
通過收集使用Windows事件集合或SIEM代理生成的事件,然後分析它們,您可以識別惡意或异常活動,並瞭解入侵者和惡意軟件在您的網絡上如何操作。
0×02 sysmon特點
用完整的命令列記錄下子行程和父行程的創建行為。
使用sha1(默認),MD5,SHA256或IMPHASH記錄行程鏡像文件的hash值。可以同時使用多個hash,包括行程創建過程中的行程GUID。
每個事件中包含session的GUID。
1.記錄驅動程序或者加載的DLL鏡像的簽名及其hash。
2.記錄磁片和卷的原始數據的讀取請求。
3.記錄網絡連接,包括每個連接的源行程,IP地址,埠號,主機名和埠名(可選)
4.如果更改註冊表則自動重新加載配寘。
5.具有規則過濾,以便動態包括或排除某些事件。
6.在加載行程的初期生成事件,能記錄在複雜的核心模式運行的惡意軟件。
0×03安裝
Install: Sysmon.exe -i <configfile>
[-h <[sha1|md5|sha256|imphash|*],...>] [-n [<process,...>]]
[-l (<process,...>)]
Configure: Sysmon.exe -c <configfile>
[--|[-h <[sha1|md5|sha256|imphash|*],...>] [-n [<process,...>]]
[-l [<process,...>]]]
Uninstall: Sysmon.exe -u
-c更新或顯示配寘
-h指定hash記錄的算灋
-i安裝,可用xml檔案來更新設定檔
-l記錄加載模塊,可指定行程
-m安裝事件清單
-n記錄網絡連結
-r檢測證書是否撤銷
-u卸載服務和驅動
一鍵安裝:
sysmon -accepteula –i -n
指定設定檔(安裝時請用-i)
sysmon -c xxx.xml
注:安裝需要管理員許可權並重啓,windows 7或者以上,服務器系統windows server 2012及以上。
0×04設定檔
在實際環境中,往往生產環境和使用需求不盡相同,而記錄所有的日誌又顯得繁瑣和龐大,這時候自定義規則顯得尤為重要。
sysmon提供了xml格式的設定檔來讓用戶自定義過濾規則,設定檔的東西比較多,把個人總結的一些東西貼出來方便大家交流。
一個xml設定檔的示例(xml大小寫敏感)
<Sysmon schemaversion="3.20">
<!-- Capture all hashes -->
<HashAlgorithms>*</HashAlgorithms>
<EventFiltering>
<!-- Log all drivers except if the signature -->
<!-- contains Microsoft or Windows -->
<DriverLoad onmatch="exclude">
<Signature condition="contains">microsoft</Signature>
<Signature condition="contains">windows</Signature>
</DriverLoad>
<!-- Do not log process termination -->
<ProcessTerminate onmatch="include" />
<!-- Log network connection if the destination port equal 443 -->
<!-- or 80, and process isn't InternetExplorer -->
<NetworkConnect onmatch="include">
<DestinationPort>443</DestinationPort>
<DestinationPort>80</DestinationPort>
</NetworkConnect>
<NetworkConnect onmatch="exclude">
<Image condition="end with">iexplore.exe</Image>
</NetworkConnect>
</EventFiltering>
</Sysmon>
可選擇的事件篩檢程式有
ProcessCreate 行程創建
FileCreateTime行程創建時間
NetworkConnect網絡連結
ProcessTermina行程結束
DriverLoad 驅動加載
ImageLoad 鏡像加載
CreateRemoteTh遠程線程創建
RawAccessRead 驅動器讀取
ProcessAccess 行程訪問
FileCreate 檔案創建
RegistryEvent 註冊表事件
FileCreateStre檔案流創建
篩檢程式事件的選項:
ProcessCreate
UtcTime, ProcessGuid, ProcessId, Image, CommandLine, CurrentDirectory, User, LogonGuid, LogonId, TerminalSessionId, IntegrityLevel, Hashes, ParentProcessGuid, ParentProcessId, ParentImage, ParentCommandLine
檔案創建時間
UtcTime, ProcessGuid, ProcessId, Image, TargetFilename, CreationUtcTime, PreviousCreationUtcTime
UtcTime, ProcessGuid, ProcessId, Image, User, Protocol, Initiated, SourceIsIpv6, SourceIp, SourceHostname, SourcePort, SourcePortName, DestinationIsIpv6, DestinationIp, DestinationHostname, DestinationPort, DestinationPortName
行程終止
UtcTime, ProcessGuid, ProcessId, Image
司機
UtcTime, ImageLoaded, Hashes, Signed, Signature
影像加載
UtcTime, ProcessGuid, ProcessId, Image, ImageLoaded, Hashes, Signed, Signature
UtcTime, SourceProcessGuid, SourceProcessId, SourceImage, TargetProcessGuid, TargetProcessId, TargetImage, NewThreadId, StartAddress, StartModule, StartFunction
拉瓦塞雷德
UtcTime, ProcessGuid, ProcessId, Image, Device
過程存取
UtcTime, SourceProcessGUID, SourceProcessId, SourceThreadId, SourceImage, TargetProcessGUID, TargetProcessId, TargetImage, GrantedAccess, CallTrace
UtcTime, ProcessGuid, ProcessId, Image, TargetFilename, CreationUtcTime
注册事件
UtcTime, ProcessGuid, ProcessId, Image, EventType, TargetObject
UtcTime, ProcessGuid, ProcessId, Image, TargetFilename, CreationUtcTime, Hash
注:更多篩檢程式的詳細說明請參攷https://technet.microsoft.com/en-us/sysinternals/sysmon。
onmatch選項只能設定為include或exclude。
condition根據不同的需求可設定為如下值:
下述規則將記錄所有創建tmp格式或exe格式檔案的行為:
<FileCreate onmatch="include">
<TargetFilename condition="end with">.tmp</TargetFilename>
<TargetFilename condition="end with">.exe</TargetFilename>
</FileCreate>
下述規則將記錄加載沒有簽名的鏡像行為:
<ImageLoad onmatch="exclude"> <Signed condition="is">true</Signed> </ImageLoad>
<NetworkConnect onmatch="exclude">
<Image condition="end with">chrome.exe</Image>
</NetworkConnect>
<NetworkConnect onmatch="include">
<DestinationPort condition="is">80</DestinationPort>
<DestinationPort condition="is">443</DestinationPort>
</NetworkConnect>
貼一個我PC機器上的xml設定檔,在實際生產中請根據實際情況調整
<Sysmon schemaversion="3.20">
<!-- Capture all hashes -->
<HashAlgorithms>*</HashAlgorithms>
<EventFiltering>
<!-- Log all drivers except if the signature -->
<!-- contains Microsoft or Windows -->
<DriverLoad onmatch="exclude">
<Signature condition="contains">Microsoft</Signature>
<Signature condition="contains">Windows</Signature>
</DriverLoad>
<ProcessTerminate onmatch="include" >
<Image condition="end with">MsMpEng.exe</Image>
</ProcessTerminate>
<!-- Log network connection if the destination port equal 443 -->
<!-- or 80, and process isn't InternetExplorer -->
<!--NetworkConnect onmatch="include">
<DestinationPort>443</DestinationPort>
<DestinationPort>80</DestinationPort >
</NetworkConnect -->
<FileCreateTime onmatch="exclude" >
<Image condition="end with">chrome.exe</Image>
</FileCreateTime>
<ImageLoad onmatch="include">
<Signed condition="is">false</Signed>
</ImageLoad>
<!-- Log access rights for lsass.exe or winlogon.exe is not PROCESS_QUERY_INFORMATION -->
<ProcessAccess onmatch="exclude">
<GrantedAccess condition="is">0x1400</GrantedAccess>
</ProcessAccess>
<ProcessAccess onmatch="include">
<TargetImage condition="end with">lsass.exe</TargetImage>
<TargetImage condition="end with">winlogon.exe</TargetImage>
</ProcessAccess>
<NetworkConnect onmatch="exclude">
<Image condition="end with">chrome.exe</Image>
<SourcePort condition="is">137</SourcePort>
<SourcePortName condition="is">llmnr</SourcePortName>
<DestinationPortName condition="is">llmnr</DestinationPortName>
</NetworkConnect>
<CreateRemoteThread onmatch="include">
<TargetImage condition="end with">explorer.exe</TargetImage>
<TargetImage condition="end with">svchost.exe</TargetImage>
<TargetImage condition="end with">winlogon.exe</TargetImage>
<SourceImage condition="end with">powershell.exe</SourceImage>
</CreateRemoteThread>
</EventFiltering>
</Sysmon>
<Sysmon schemaversion=“3.20”>
<!-- Capture all hashes -->
<HashAlgorithms>*</HashAlgorithms>
<EventFiltering>
<!-- Log all drivers except if the signature -->
<!-- contains Microsoft or Windows -->
<DriverLoad onmatch=“exclude”>
<Signature condition=“contains”>Microsoft</Signature>
<Signature condition=“contains”>Windows</Signature>
</DriverLoad>
<ProcessTerminate onmatch=“include”>
<Image condition=“end with”>MsMpEng.exe</Image>
</ProcessTerminate>
<!-- Log network connection if the destination port equal 443 -->
<!-- or 80,and process isn't InternetExplorer -->
<!--NetworkConnect onmatch=“include”>
<DestinationPort>443</DestinationPort>
<DestinationPort>80</DestinationPort >
</NetworkConnect -->
<FileCreateTime onmatch=“exclude”>
<Image condition=“end with”>chrome.exe</Image>
</FileCreateTime>
<ImageLoad onmatch=“include”>
<Signed condition=“is”>false</Signed>
</ImageLoad>
<!-- Log access rights for lsass.exe or winlogon.exe is not PROCESS_QUERY_INFORMATION -->
<ProcessAccess onmatch=“exclude”>
<GrantedAccess condition=“is”>0x1400</GrantedAccess>
</ProcessAccess>
<ProcessAccess onmatch=“include”>
<TargetImage condition=“end with”>lsass.exe</TargetImage>
<TargetImage condition=“end with”>winlogon.exe</TargetImage>
</ProcessAccess>
<NetworkConnect onmatch=“exclude”>
<Image condition=“end with”>chrome.exe</Image>
<SourcePort condition=“is”>137</SourcePort>
<SourcePortName condition=“is”>llmnr</SourcePortName>
<DestinationPortName condition=“is”>llmnr</DestinationPortName>
</NetworkConnect>
<CreateRemoteThread onmatch=“include”>
<TargetImage condition=“end with”>explorer.exe</TargetImage>
<TargetImage condition=“end with”>svchost.exe</TargetImage>
<TargetImage condition=“end with”>winlogon.exe</TargetImage>
<SourceImage condition=“end with”>powershell.exe</SourceImage>
</CreateRemoteThread>
</EventFiltering>
</Sysmon>
注:有時候explorer.exe或者svchost會發出http請求,不用驚慌,仔細看下連結,可能只是訪問Akamai或者Microsoft(剛剛測試的時候發現svchost連到新加坡111.221.29.254:443,查了一下才發現是微軟新加坡分公司)。如果實在不放心,就在防火牆裏配寘規則把explorer的所有流量封锁,萬無一失。
0×05測試示例
記錄dump hash
筆者之所以看中了這個軟件,因為最近更新可以記錄訪問行程的資訊,通過事件CreateRemoteThread和ProcessAccess可以記錄特殊操作,如dump hash和線程注入等。下麵用mimikatz來演示。
當我運行了mimikatz抓hash之後,默認在”Applications and Services Logs/Microsoft/Windows/Sysmon/Operational”裏能找到日誌記錄。
如下圖:
記錄下來mimikatz訪問了行程lsass.exe,屬於行程訪問事件。那麼從哪裡能看出來是進行了dump hash操作呢,關鍵的一點就是GrantedAccess的值為0x143A,這個值表示什麼呢,我們查閱msdn可知:
0x1000 PROCESS_QUERY_LIMITED_INFORMATION 受限制的进程查询信息
0x0400 PROCESS_QUERY_INFORMATION 进程查询权限,包括token,退出代码,优先级,改权限自动继承PROCESS_QUERY_LIMITED_INFORMATION
0x0002 PROCESS_CREATE_THREAD 创建线程权限
0x0008 PROCESS_VM_OPERATION 需要对进程的地址空间执行操作,常用于VirtualProtectEx和WriteProcessMemory。
0x0010 PROCESS_VM_READ 读取进程中的内存
0x0020 PROCESS_VM_WRITE 使用WriteProcessMemory写进程内存,看见WriteProcessMemory,大概应该知道会有些什么操作了。
將上面的許可權按比特异或即得0x143A,及表示mimikatz對lsass擁有上述存取權限,包括寫行程記憶體和讀行程記憶體,這樣就能獲取到用戶口令。而一般的行程訪問只需要0×1400,也就是只有行程査詢許可權,這裡mimikatz明顯有惡意行為。
而普通的行程對於lsass.exe的訪問如下圖:
這裡記錄下來的是windows defender對lsass.exe的訪問,這裡的GrantedAccess是屬於普通的標準行程存取權限,calltrace也有詳細的記錄,屬於正常的行程訪問。
更多關於行程訪問的許可權可參攷
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880
惡意網絡連接和操作
用meterpreter回連,可以看到連接的詳細資訊,包括ip,埠,pid等,如圖
執行migrate操作也會記錄在CreateRemoteThread裏,如圖:
這裡就能很明顯的看出來test.exe把惡意程式碼注入到explorer.exe,借explorer這個軀殼來隱藏自己。而整個過程中我的windows defender一個泡都沒冒。
而filetime篩檢程式會將系統上所有檔案的覆蓋和創建時間修改時間等都記錄下來:
很多惡意程式留後門和dll劫持都會有替換檔案和修改時間的操作(這裡不是惡意行為,只是用來作一個演示)。
在本地加載了任何無簽名的exe和dll等可執行文件:
在面對dll注入,各種白加黑,利用dll劫持等操作的惡意軟件都會因為這一項被記錄下來(有簽名的惡意軟件極少)
0×06日誌記錄
現時的惡意軟件為了對抗檢測很多都有日誌删除功能,為了保存好系統日誌,可以將日誌定期保存在本地或遠程服務器,日誌的默認保存在%SystemRoot%\System32\Winevt\Logs\Microsoft-Windows-Sysmon% 4Operational.evtx,可在事件檢視器的日誌内容設定保存在遠程服務器,或通過其他工具或腳本保存。
總結
雖然現時有很多監視和分析軟體行為的工具,如process explorer,process monitor,pstools,還有各種沙箱分析軟體,反彙編調試工具,以及各大殺毒軟體,但sysmon最為一個羽量級的監控軟體,有它的亮眼的地方,結合其他工具能讓監控系統變得更容易和更效率。如有興趣進一步交流討論,歡迎訪問筆者微博或部落格。
*本文原創作者:Green_m,本文屬FreeBuf原創獎勵計畫,未經許可禁止轉載