Red Hat Training
A Red Hat training course is available for Red Hat Enterprise Linux
17.14.11.6. 自定义过滤器示例
虽然上述 XML 中的其中一个规则包含客户机虚拟机的 IP 地址作为源或目标地址,但过滤流量可以正常工作。其原因是,而规则的评估在各个接口在内部进行,而规则将根据发送或将接收数据包,而不是其源或目标 IP 地址可以进行额外评估。
例 17.12. 网络接口描述的 XML 示例
测试客户机虚拟机的域 XML 中可能出现的网络接口描述 XML 片段类似如下:
[...] <interface type='bridge'> <source bridge='mybridge'/> <filterref filter='test-eth0'/> </interface> [...]
要更严格地控制 ICMP 流量,并且强制只能从客户机虚拟机发送 ICMP 回显请求,并且只有 ICMP 回显响应才能被客户机虚拟机接收,以上 ICMP 规则可以替换为以下两条规则:
<!- - enable outgoing ICMP echo requests- -> <rule action='accept' direction='out'> <icmp type='8'/> </rule>
<!- - enable incoming ICMP echo replies- -> <rule action='accept' direction='in'> <icmp type='0'/> </rule>
例 17.13. 第二个自定义过滤器示例
本例演示如何像上面示例所示构建类似的过滤器,但通过位于 guest 虚拟机中的 ftp 服务器扩展要求列表。这个过滤器的要求如下:
- 防止客户机虚拟机的接口来自 MAC、IP 和 ARP 欺骗
- 在客户机虚拟机接口中仅打开 TCP 端口 22 和 80
- 允许客户机虚拟机从接口发送 ping 流量,但不允许客户机虚拟机在接口上 ping
- 允许客户机虚拟机执行 DNS 查找(UDP 至端口 53)
- 启用 FTP 服务器(在活动模式中),以便它可以在客户机虚拟机中运行
额外的要求允许 FTP 服务器在客户机虚拟机中运行,允许允许端口 21 访问 FTP 控制流量,同时使客户机虚拟机能够建立源自客户机虚拟机的 TCP 端口 20 到 FTP 客户端(FTP 活跃模式)的传出 TCP 连接。本例中如何编写此过滤器和两个可能的解决方案有几种方法。
第一种解决方案使用 TCP 协议的 state 属性,为 Linux 主机物理机器的连接跟踪框架提供 hook。对于 guest 虚拟机初始化的 FTP 数据连接(FTP 活动模式) RELATED 状态,使用 RELATED 状态启用检测客户机虚拟机发起的 FTP 数据连接会导致(或与' 的关系)现有 FTP 控制连接,从而使它通过防火墙。然而,RELATED 状态仅对 FTP 数据路径传出 TCP 连接的完全第一个数据包有效。之后,状态是 ESTABLISHED,然后同样适用于传入和传出方向。所有这些都与源自客户机虚拟机的 TCP 端口 20 的 FTP 数据流量相关。然后,这会导致以下解决方案:
<filter name='test-eth0'>
<!- - This filter (eth0) references the clean traffic
filter to prevent MAC, IP, and ARP spoofing. By not providing an IP address parameter, libvirt will detect the IP address the guest virtual machine is using. - ->
<filterref filter='clean-traffic'/>
<!- - This rule enables TCP port 21 (FTP-control) to be reachable - ->
<rule action='accept' direction='in'>
<tcp dstportstart='21'/>
</rule>
<!- - This rule enables TCP port 20 for guest virtual machine-initiated FTP data connection related to an existing FTP control connection - ->
<rule action='accept' direction='out'>
<tcp srcportstart='20' state='RELATED,ESTABLISHED'/>
</rule>
<!- - This rule accepts all packets from a client on the FTP data connection - ->
<rule action='accept' direction='in'>
<tcp dstportstart='20' state='ESTABLISHED'/>
</rule>
<!- - This rule enables TCP port 22 (SSH) to be reachable - ->
<rule action='accept' direction='in'>
<tcp dstportstart='22'/>
</rule>
<!- -This rule enables TCP port 80 (HTTP) to be reachable - ->
<rule action='accept' direction='in'>
<tcp dstportstart='80'/>
</rule>
<!- - This rule enables general ICMP traffic to be initiated by the guest virtual machine, including ping traffic - ->
<rule action='accept' direction='out'>
<icmp/>
</rule>
<!- - This rule enables outgoing DNS lookups using UDP - ->
<rule action='accept' direction='out'>
<udp dstportstart='53'/>
</rule>
<!- - This rule drops all other traffic - ->
<rule action='drop' direction='inout'>
<all/>
</rule>
</filter>
在尝试使用 RELATED 状态的过滤器前,您必须确定已将适当的连接跟踪模块加载到主机物理计算机的内核。根据内核的版本,您必须在建立与客户机虚拟机的 FTP 连接前运行以下命令之一:
- modprobe nf_conntrack_ftp - 其中可用 OR
- modprobe ip_conntrack_ftp 如果上述版本不可用
如果 FTP 以外的协议与 RELATED 状态结合使用,则必须加载对应的模块。模块可用于协议:ftp、tftp、irc、sip、sctp 和 amanda。
第二个解决方案利用了超过之前解决方案的连接的状态标志。此解决方案利用了事实:当检测到流量非常第一个数据包时,连接的 NEW 状态会有效。因此,如果接受非常第一个流的数据包,流会变为连接,因此进入 ESTABLISHED 状态。因此,可以编写常规规则以便允许 ESTABLISHED 连接到达 guest 虚拟机或由客户机虚拟机发送。这是为由 NEW 状态标识的非常第一次数据包编写特定规则,并规定接受数据的端口。所有未明确接受的封包将被丢弃,因此不会到达 ESTABLISHED 状态。从该端口发送的所有后续数据包也会被丢弃。
<filter name='test-eth0'>
<!- - This filter references the clean traffic
filter to prevent MAC, IP and ARP spoofing. By not providing and IP address parameter, libvirt will detect the IP address the VM is using. - ->
<filterref filter='clean-traffic'/>
<!- - This rule allows the packets of all previously accepted connections to reach the guest virtual machine - ->
<rule action='accept' direction='in'>
<all state='ESTABLISHED'/>
</rule>
<!- - This rule allows the packets of all previously accepted and related connections be sent from the guest virtual machine - ->
<rule action='accept' direction='out'>
<all state='ESTABLISHED,RELATED'/>
</rule>
<!- - This rule enables traffic towards port 21 (FTP) and port 22 (SSH)- ->
<rule action='accept' direction='in'>
<tcp dstportstart='21' dstportend='22' state='NEW'/>
</rule>
<!- - This rule enables traffic towards port 80 (HTTP) - ->
<rule action='accept' direction='in'>
<tcp dstportstart='80' state='NEW'/>
</rule>
<!- - This rule enables general ICMP traffic to be initiated by the guest virtual machine, including ping traffic - ->
<rule action='accept' direction='out'>
<icmp state='NEW'/>
</rule>
<!- - This rule enables outgoing DNS lookups using UDP - ->
<rule action='accept' direction='out'>
<udp dstportstart='53' state='NEW'/>
</rule>
<!- - This rule drops all other traffic - ->
<rule action='drop' direction='inout'>
<all/>
</rule>
</filter>