Windows 内置 Netsh / ETW 抓包与 DNS 解析
Packet Capture
开始抓包
以下过程涉及的 cmd.exe / powershell.exe 窗口,在没有抓包完成前请勿关闭!!
适用系统:Windows 7 SP1 / Windows Server 2008 R2 (含)以上
使用方法:
- 启动一个经过 UAC Elevated 的 Windows Terminal (e.g. cmd.exe / powershell.exe)
netsh trace start capture=yes report=disabled
开始,如果report=enabled
则还会额外输出系统的各类软硬件及系统诊断配置信息并打包为cab格式。netsh trace stop
停止。
默认输出路径:%Temp%\NetTraces
下,可以额外增加参数修改:tracefile=C:\tmp\pktcap.etl maxsize=512mb
适用系统:Windows 10 1809+ / Windows Server 2019 (含)以上
使用方法:
- 启动一个经过 UAC Elevated 的 Windows Terminal (e.g. cmd.exe / powershell.exe)
pktmon filter
增加适当过滤器,pktmon start
开启抓包,pktmon stop
停止。
默认输出路径:C:\Windows\System32\PktMon.etl
此方法抓包后得到的 etl 是不携带 trace 数据的,亦不包含进程信息,使用 MMA 分析没有意义,也无法看到有用的数据包和解析,且在转换格式后分析时携带了大量重复且无用的数据。如需携带 trace 数据,自行添加 --trace
参数并写入对应的 Trace Provider。
- 抓取停止后使用
pktmon unload
卸载系统驱动。
抓包后的分析
Netsh 抓包的只能使用 GitHub 的 etl2pcapng 转换!
- 微软已经弃用且不再提供下载的 Microsoft Message Analyzer ,优点:数据全面,包括了 PID / TID 等信息,缺点:格式不通用,需要独立分析工具,下载困难
- Wireshark,优点:通用格式,缺点:进程元数据丢失,仅有数据包和PID
MMA 操作:Session — New Viewer — Analysis Grid — Process View
格式转换:https://github.com/microsoft/etl2pcapng,运行参数参见 Readme。使用 pktmon 转换会导致无法读取到数据包。
Win10 系统使用 pktmon
抓包的可以直接转换:
转换过程:
Wireshark 分析:
列举 TraceProvider,使用 Powershell: Get-NetEventProvider -ShowInstalled | Select-Object Name,Guid | sort Name
,添加 Microsoft-Windows-WinHttp 作为 Provider 之后,使用 Powershell Invoke-WebRequest 请求 HTTP 网站,依然没有关联到对应的数据包和 PID。
关于 DNS 解析的问题
- 如遇应急场景,需要先确认DNSCache服务所在的 svchost 进程,并尝试清空 DNS 缓存。
- 创建一个 Windows DNS Client 的 ETW 追踪器,ETW 追踪时进一步配合系统的进程创建审计可以获得 PID 与进程名+可执行文件名的对应关系。
(1) 创建 “进程创建”审计 — 组策略
(2) 创建 ETW Session — 计算机管理
名字随意。
选择追踪数据即可。
添加追踪提供者,选择 MS-Windows-DNS-Client。
添加后选择 Keywords(Any),点击 Edit,勾选所有项,或者选择 Manual,值为 0xc0001ffff0000100
,确认即可。
创建完成后右键菜单启动追踪即可。
在对应的 Collector 位置找到 ETL 文件后使用 MMA 分析即可。
测试 DNS 抓取请求完整:
对应 DNSCache 服务所在 svchost 进程可以观察到 DNS 缓存的查询记录:
经过测试,确实存在 DNS 请求 在 ETW Trace 中 “漏报” ,使用 WSL 1 中的 kdig 进行测试,配合 Go 语言的自有 Resolver 进行测试,无法抓取,经验证,原因为使用系统自有 C Resolver 解析,不依赖操作系统解析的应用程序不受 ETW 追踪 MS-Windows-DNS-Client 影响。抓包情况下会发现出现 PID=4 (System 伪进程) 的 DNS 解析,亦为同理。
如需进一步追踪,可参考知识库中其他文章,使用 ALPCLogger 追踪系统内核的 ALPC/RPC 调用。
Reference
- https://learn.microsoft.com/en-us/message-analyzer/installing-and-upgrading-message-analyzer
- https://learn.microsoft.com/en-us/message-analyzer/microsoft-message-analyzer-operating-guide
- http://woshub.com/network-sniffer-packet-monitor-pktmon/
- https://blog.polarbill.com/2018/04/09/MMA-might-save-the-day.html
- https://github.com/microsoft/etl2pcapng
- https://www.networkcomputing.com/networking/capturing-packets-natively-microsoft-windows
- https://techcommunity.microsoft.com/t5/core-infrastructure-and-security/introduction-to-network-trace-analysis-using-microsoft-message/ba-p/257467
- https://subhasisray.medium.com/network-trace-in-production-windows-netsh-trace-analyzer-7f28746ffa57
- https://blog.didierstevens.com/2020/01/28/etl2pcapng-support-for-process-ids/
- https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/jj129382(v=ws.11)
- https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd878517(v=ws.10)?redirectedfrom=MSDN
- https://web.archive.org/web/20190420141924/http://download.microsoft.com/download/2/8/3/283DE38A-5164-49DB-9883-9D1CC432174D/MessageAnalyzer64.msi
- https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/pktmon-start
- https://github.com/jdu2600/Windows10EtwEvents
- http://irs.nsfocus.com/#/wiki-detail?id=165715600592
- http://irs.nsfocus.com/#/wiki-detail?id=165459057849
- https://github.com/asgarciap/etw-dns
- https://stackoverflow.com/questions/31891814/trace-dns-query-answer-with-event-tracing-for-windows
- https://jameshfisher.com/2017/08/03/golang-dns-lookup/
- https://pkg.go.dev/net#hdr-Name_Resolution
- 对于 Go 来说,解析方式可用环境变量控制,参考上方 Go 包文档即可。