虚机抓取Hyper-V宿主的镜像流量+开启 Hyper-V 网卡的混杂模式

1.将交换机流量镜像到Hyper-V宿主的一块网卡(eth4)
2.在Hyper-V宿主上新建虚拟交换机(Network_Mirror),选择外部网络,扩展属性中启用“Microsoft NDIS捕获”,设置如下图所示:
虚机抓取Hyper-V宿主的镜像流量+开启 Hyper-V 网卡的混杂模式
虚机抓取Hyper-V宿主的镜像流量+开启 Hyper-V 网卡的混杂模式
3.在已创建好的虚机上,选择使用“Network_Mirror”虚拟交换机,然后在高级功能中,镜像模式选择“目标”,即该虚机用来接收镜像流量
虚机抓取Hyper-V宿主的镜像流量+开启 Hyper-V 网卡的混杂模式
4.选择流量来源,在Hyper-V宿主上运行PowerShell命令:

$VMSwitchPortFeature = Get-VMSystemSwitchExtensionPortFeature -FeatureName 'Ethernet Switch Port Security Settings' #选择扩展功能 - 交换机端口安全设置“类”
$VMSwitchPortFeature.SettingData.MonitorMode = 2 #MonitorMode参数说明(0 = 无, 1 = 目标, 2 = 来源)
Add-VMSwitchExtensionPortFeature -ExternalPort -SwitchName 'Network_Mirror' -VMSwitchExtensionFeature $VMSwitchPortFeature #将该功能加入到该虚拟交换机
注:All traffic hitting the hyper-v Host is also hitting the external port of the virtual switch. However for the NIC on the VM to get these packets, we need to now “mirror” this Switch port (on the Virtual switch) to let him know that he has to forward the packets onto the “DESTINATION” we had configured earlier.

5.在虚机(CentOS)上运行命令监控镜像流量:

tcpdump -i eth1 -w /tmp/a.txt

运行tail -f tmp/a.txt 可以看到捕获到的镜像流量
附:
虚机抓取Hyper-V宿主的镜像流量+开启 Hyper-V 网卡的混杂模式
(Hyper-V) 开启 Hyper-V 网卡的混杂模式
PowerShell 中执行:

Get-VM -Name 虚拟机名称 | Get-VMNetworkAdapter | Where-Object { $_.MacAddress -eq "无连接符的MAC地址" } | Set-VMNetworkAdapter -MacAddressSpoofing On

Hyper-V 中“混杂模式”的概念是 MAC Spoofing(MAC 地址欺骗)。在正常的 L2 网络中,一个网卡设备只会接收目标 MAC 地址是自己的数据包,混杂模式允许监听整个网络的数据包。基于这个模式,可以很好地实现虚拟网卡网络桥(Virtual Adapter Bridging)。混杂模式在 VMWare / VirtualBox / Hyper-V 中默认关闭(打开会是网络不安全),但是在特定模式下需要手动打开,比如笔者最近遇到的情况:在 Hyper-V 虚拟机中使用 SoftEtherV×N 桥接本地网卡,如果不为虚拟机开启混杂模式,则 V×N 客户端无法获取 IP 地址以及网络通信。

参考:
https://blogs.technet.microsoft.com/networking/2015/10/16/setting-up-port-mirroring-to-capture-mirrored-traffic-on-a-hyper-v-virtual-machine/
https://dotblogs.com.tw/terrychuang/2015/01/19/148176
https://iedon.com/2019/05/03/725.html

评论区 0 已关闭评论