示例:配置无状态防火墙过滤器以防止 TCP 和 ICMP 泛洪
此示例说明如何创建无状态防火墙过滤器来抵御 TCP 和 ICMP 拒绝服务攻击。
要求
配置无状态防火墙过滤器之前,不需要除设备初始化之外的特殊配置。
概述
在此示例中,我们将创建一个无状态防火墙过滤器,用于 protect-RE 监管 TCP 和 ICMP 数据包。它使用下面所述的监管器:
tcp-connection-policer— 此监管器将 TCP 流量限制为每秒 1,000,000 位 (bps),最大突发大小为 15,000 字节。超过任一限制的流量将被丢弃。icmp-policer— 此监管器将 ICMP 流量限制为 1,000,000 bps,最大突发大小为 15,000 字节。超过任一限制的流量将被丢弃。
指定限制时,带宽限制可以是 32,000 bps 到 32,000,000,000 bps,突发大小限制可以从 1,500 字节到 100,000,000 字节。指定限制时使用以下缩写:k (1,000)、m (1,000,000) 和 g (1,000,000,000)。
每个监管器都合并到过滤器项的操作中。此示例包含以下术语:
tcp-connection-term— 管制源地址为 192.168.0.0/24 或 10.0.0.0/24 的某些 TCP 数据包。这些地址在前缀列表中定义trusted-addresses。过滤的数据包包括
tcp-established数据tcp-established包 匹配条件是位字段匹配条件tcp-flags “(ack | rst)”的别名,表示已建立的 TCP 会话,但不是 TCP 连接的第一个数据包。icmp-term— 管制 ICMP 数据包。所有 ICMP 数据包都计入计数器中icmp-counter。
您可以使用命令insert在防火墙过滤器中移动术语。请参阅《Junos OS CLI 用户指南》中的 insert。
您可以将无状态防火墙应用于接口的输入端或输出端,或同时应用两者。要过滤通过设备传输的数据包,请将防火墙过滤器应用于任何非路由引擎接口。要过滤来自 路由引擎 的数据包或向 发送的数据包,请将防火墙过滤器应用于 环路 (lo0) 接口。
图 1 显示了示例网络。
由于此防火墙过滤器将路由引擎流量限制为 TCP 数据包,因此当此过滤器处于活动状态时,对第 4 层使用其他传输协议的路由协议无法成功建立会话。为了演示,此示例在设备 R1 和设备 R2 之间设置 OSPF。
#configuration1102__policy-firewall-tcp-icmp-st 部分介绍了设备 R2 上的步骤。
配置
过程
CLI 快速配置
要快速配置无状态防火墙过滤器,请将以下命令复制到文本文件中,删除所有换行符,然后将命令粘贴到 CLI 中。
设备 R1
set interfaces fe-1/2/0 unit 0 family inet address 10.0.0.1/30 set interfaces lo0 unit 0 family inet address 192.168.0.1/32 primary set interfaces lo0 unit 0 family inet address 172.16.0.1/32 set protocols bgp group ext type external set protocols bgp group ext export send-direct set protocols bgp group ext peer-as 200 set protocols bgp group ext neighbor 10.0.0.2 set protocols ospf area 0.0.0.0 interface fe-1/2/0.0 set protocols ospf area 0.0.0.0 interface lo0.0 passive set policy-options policy-statement send-direct term 1 from protocol direct set policy-options policy-statement send-direct term 1 then accept set routing-options router-id 192.168.0.1 set routing-options autonomous-system 100
设备 R2
set interfaces fe-1/2/0 unit 0 family inet address 10.0.0.2/30 set interfaces lo0 unit 0 family inet filter input protect-RE set interfaces lo0 unit 0 family inet address 192.168.0.2/32 primary set interfaces lo0 unit 0 family inet address 172.16.0.2/32 set protocols bgp group ext type external set protocols bgp group ext export send-direct set protocols bgp group ext neighbor 10.0.0.1 peer-as 100 set protocols ospf area 0.0.0.0 interface lo0.0 passive set protocols ospf area 0.0.0.0 interface fe-1/2/0.0 set policy-options prefix-list trusted-addresses 10.0.0.0/24 set policy-options prefix-list trusted-addresses 192.168.0.0/24 set policy-options policy-statement send-direct term 1 from protocol direct set policy-options policy-statement send-direct term 1 then accept set routing-options router-id 192.168.0.2 set routing-options autonomous-system 200 set firewall family inet filter protect-RE term tcp-connection-term from source-prefix-list trusted-addresses set firewall family inet filter protect-RE term tcp-connection-term from protocol tcp set firewall family inet filter protect-RE term tcp-connection-term from tcp-established set firewall family inet filter protect-RE term tcp-connection-term then policer tcp-connection-policer set firewall family inet filter protect-RE term tcp-connection-term then accept set firewall family inet filter protect-RE term icmp-term from source-prefix-list trusted-addresses set firewall family inet filter protect-RE term icmp-term from protocol icmp set firewall family inet filter protect-RE term icmp-term then policer icmp-policer set firewall family inet filter protect-RE term icmp-term then count icmp-counter set firewall family inet filter protect-RE term icmp-term then accept set firewall policer tcp-connection-policer filter-specific set firewall policer tcp-connection-policer if-exceeding bandwidth-limit 1m set firewall policer tcp-connection-policer if-exceeding burst-size-limit 15k set firewall policer tcp-connection-policer then discard set firewall policer icmp-policer filter-specific set firewall policer icmp-policer if-exceeding bandwidth-limit 1m set firewall policer icmp-policer if-exceeding burst-size-limit 15k set firewall policer icmp-policer then discard
分步程序
下面的示例要求您在各个配置层级中进行导航。有关导航 CLI 的信息,请参阅在 配置模式下使用 CLI 编辑器。
要配置无状态防火墙过滤器以丢弃:
配置设备接口。
[edit interfaces fe-1/2/0 unit 0 family inet ] user@R2# set address 10.0.0.2/30 [edit interfaces lo0 unit 0 family inet] user@R2# set address 192.168.0.2/32 primary user@R2# set address 172.16.0.2/32
配置 BGP 对等会话。
[edit protocols bgp group ext] user@R2# set type external user@R2# set export send-direct user@R2# set neighbor 10.0.0.1 peer-as 100
配置自治系统 (AS) 编号和路由器 ID。
[edit routing-options] user@R2# set autonomous-system 200 user@R2# set router-id 192.168.0.2
配置 OSPF。
[edit protocols ospf area 0.0.0.0] user@R2# set interface lo0.0 passive user@R2# set interface fe-1/2/0.0
定义可信地址列表。
[edit policy-options prefix-list trusted-addresses] user@R2# set 10.0.0.0/24 user@R2# set 192.168.0.0/24
配置策略以播发直接路由。
[edit policy-options policy-statement send-direct term 1] user@R2# set from protocol direct user@R2# set then accept
配置 TCP 监管器。
[edit firewall policer tcp-connection-policer] user@R2# set filter-specific user@R2# set if-exceeding bandwidth-limit 1m user@R2# set if-exceeding burst-size-limit 15k user@R2# set then discard
创建 ICMP 监管器。
[edit firewall policer icmp-policer] user@R2# set filter-specific user@R2# set if-exceeding bandwidth-limit 1m user@R2# set if-exceeding burst-size-limit 15k user@R2# set then discard
配置 TCP 过滤规则。
[edit firewall family inet filter protect-RE term tcp-connection-term] user@R2# set from source-prefix-list trusted-addresses user@R2# set from protocol tcp user@R2# set from tcp-established user@R2# set then policer tcp-connection-policer user@R2# set then accept
配置 ICMP 过滤器规则。
[edit firewall family inet filter protect-RE term icmp-term] user@R2# set from source-prefix-list trusted-addresses user@R2# set from protocol icmp user@R2# set then policer icmp-policer user@R2# set then count icmp-counter user@R2# set then accept
将过滤器应用于环路接口。
[edit interfaces lo0 unit 0] user@R2# set family inet filter input protect-RE
结果
从配置模式输入 show interfaces、 show protocols、 show routing-options show policy-options和 show firewall 命令,以确认您的配置。如果输出未显示预期的配置,请重复此示例中的说明以更正配置。
user@R2# show interfaces
fe-1/2/0 {
unit 0 {
family inet {
address 10.0.0.2/30;
}
}
}
lo0 {
unit 0 {
family inet {
filter {
input protect-RE;
}
address 192.168.0.2/32 {
primary;
}
address 172.16.0.2/32;
}
}
}
user@R2# show protocols
bgp {
group ext {
type external;
export send-direct;
neighbor 10.0.0.1 {
peer-as 100;
}
}
}
ospf {
area 0.0.0.0 {
interface lo0.0 {
passive;
}
interface fe-1/2/0.0;
}
}
user@R2# show policy-options
prefix-list trusted-addresses {
10.0.0.0/24;
192.168.0.0/24;
}
policy-statement send-direct {
term 1 {
from protocol direct;
then accept;
}
}
user@R2# show routing-options router-id 192.168.0.2; autonomous-system 200;
user@R2# show firewall
family inet {
filter protect-RE {
term tcp-connection-term {
from {
source-prefix-list {
trusted-addresses;
}
protocol tcp;
tcp-established;
}
then {
policer tcp-connection-policer;
accept;
}
}
term icmp-term {
from {
source-prefix-list {
trusted-addresses;
}
protocol icmp;
}
then {
policer icmp-policer;
count icmp-counter;
accept;
}
}
}
}
policer tcp-connection-policer {
filter-specific;
if-exceeding {
bandwidth-limit 1m;
burst-size-limit 15k;
}
then discard;
}
policer icmp-policer {
filter-specific;
if-exceeding {
bandwidth-limit 1m;
burst-size-limit 15k;
}
then discard;
}
}
如果完成设备配置,请从配置模式进入。commit
验证
确认配置工作正常。
要验证 TCP 监管器,可以使用数据包生成工具。此处未显示此任务。
- 显示有效的无状态防火墙过滤器
- 使用 telnet 验证 TCP 防火墙过滤器中的 TCP 建立条件
- 使用 telnet 验证 TCP 防火墙过滤器中的可信前缀条件
- 使用 OSPF 验证 TCP 防火墙过滤器
- 验证 ICMP 防火墙过滤器
显示有效的无状态防火墙过滤器
目的
验证防火墙过滤器的配置。
行动
在操作模式下,输入命令 show firewall 。
user@R2> show firewall Filter: protect-RE Counters: Name Bytes Packets icmp-counter 0 0 Policers: Name Bytes Packets icmp-policer 0 tcp-connection-policer 0
意义
输出显示对设备 R2 有效的过滤器、计数器和监管器。
使用 telnet 验证 TCP 防火墙过滤器中的 TCP 建立条件
目的
确保 telnet 流量按预期工作。
行动
验证设备是否只能与满足 from tcp-established 条件的主机建立 TCP 会话。
在设备 R2 中,确保已建立与设备 R1 的 BGP 会话。
user@R2> show bgp summary | match down Groups: 1 Peers: 1 Down peers: 0
从设备 R2,telnet 到设备 R1。
user@R2> telnet 192.168.0.1 Trying 192.168.0.1... Connected to R1.example.net. Escape character is '^]'. R1 (ttyp4) login:
从设备 R1,telnet 到设备 R2。
user@R1> telnet 192.168.0.2 Trying 192.168.0.2... telnet: connect to address 192.168.0.2: Operation timed out telnet: Unable to connect to remote host
在设备 R2 上,停用匹配
from tcp-established条件。[edit firewall family inet filter protect-RE term tcp-connection-term] user@R2# deactivate from tcp-established user@R2# commit
从设备 R1 再次尝试将 telnet 连接到设备 R2。
user@R1> telnet 192.168.0.1 Trying 192.168.0.2... Connected to R2.example.net. Escape character is '^]'. R2 (ttyp4) login:
意义
验证以下信息:
正如预期一样,BGP 会话已建立。
from tcp-established匹配条件预计不会阻止 BGP 会话建立。从设备 R2,您可以以 telnet 连接到设备 R1。设备 R1 未配置防火墙过滤器,因此这是预期行为。
您无法从设备 R1 以 telnet 连接到设备 R2。Telnet 使用 TCP 作为传输协议,因此此结果可能令人惊讶。Telnet 连接缺失的原因是匹配条件。
from tcp-established此匹配条件限制设备 R2 接受的 TCP 流量类型。停用此匹配条件后,telnet 会话成功。
使用 telnet 验证 TCP 防火墙过滤器中的可信前缀条件
目的
确保 telnet 流量按预期工作。
行动
验证设备是否只能与其中一个可信源地址匹配的 IP 地址上的主机建立 telnet 会话。例如,使用来自另一台主机的命令登录设备 telnet ,其中包含其中一个可信地址前缀。此外,还要验证是否阻止了具有不可信源地址的 telnet 会话。
从设备 R1,telnet 从不受信任的源地址连接到设备 R2。
user@R1> telnet 172.16.0.2 source 172.16.0.1 Trying 172.16.0.2... ^C
从设备 R2 中,将 172.16/16 添加到可信前缀列表中。
[edit policy-options prefix-list trusted-addresses] user@R2# set 172.16.0.0/16 user@R2# commit
从设备 R1 再次尝试将 telnet 连接到设备 R2。
user@R1> telnet 172.16.0.2 source 172.16.0.1 Trying 172.16.0.2... Connected to R2.example.net. Escape character is '^]'. R2 (ttyp4) login:
意义
验证以下信息:
您无法使用不受信任的源地址从设备 R1 telnet 到设备 R2。将 172.16/16 前缀添加到可信前缀列表中后,将接受来自源地址 172.16.0.1 的 telnet 请求。
OSPF 会话建立被阻止。OSPF 不使用 TCP 作为其传输协议。停用匹配条件后
from protocol tcp,不会阻止 OSPF 会话建立。
使用 OSPF 验证 TCP 防火墙过滤器
目的
确保 OSPF 流量按预期工作。
行动
验证设备是否无法建立 OSPF 连接。
从设备 R1 检查 OSPF 会话。
user@R1> show ospf neighbor Address Interface State ID Pri Dead 10.0.0.2 fe-1/2/0.0 Init 192.168.0.2 128 34
在设备 R2 中,检查 OSPF 会话。
user@R2> show ospf neighbor
从设备 R2 中,移除
from protocol tcp匹配条件。[edit firewall family inet filter protect-RE term tcp-connection-term] user@R2# deactivate from protocol user@R2# commit
从设备 R1 重新检查 OSPF 会话。
user@R1> show ospf neighbor Address Interface State ID Pri Dead 10.0.0.2 fe-1/2/0.0 Full 192.168.0.2 128 36
从设备 R2 中,重新检查 OSPF 会话。
user@R2> show ospf neighbor Address Interface State ID Pri Dead 10.0.0.1 fe-1/2/0.0 Full 192.168.0.1 128 39
意义
验证以下信息:
OSPF 会话建立被阻止。OSPF 不使用 TCP 作为其传输协议。匹配条件停用后
from protocol tcp,OSPF会话建立成功。
验证 ICMP 防火墙过滤器
目的
验证是否正在对 ICMP 数据包进行监管和计数。此外,请确保当请求来自不受信任的源地址时,将丢弃 ping 请求。
行动
撤消在之前的验证步骤中所做的配置更改。
重新激活 TCP 防火墙设置,并删除 172.16/16 可信源地址。
[edit firewall family inet filter protect-RE term tcp-connection-term] user@R2# activate from protocol user@R2# activate from tcp-established [edit policy-options prefix-list trusted-addresses] user@R2# delete 172.16.0.0/16 user@R2# commit
从设备 R1 对设备 R2 上的环路接口执行 ping 命令。
user@R1> ping 192.168.0.2 rapid count 600 size 2000 PING 192.168.0.2 (192.168.0.2): 2000 data bytes !!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!!!.!!!!!!!!.!!!!!!!!.!!!!!!!! --- 192.168.0.2 ping statistics --- 600 packets transmitted, 536 packets received, 10% packet loss pinground-trip min/avg/max/stddev = 2.976/3.405/42.380/2.293 ms
从设备 R2 检查防火墙统计信息。
user@R2> show firewall Filter: protect-RE Counters: Name Bytes Packets icmp-counter 1180804 1135 Policers: Name Bytes Packets icmp-policer 66 tcp-connection-policer 0
从设备 R1 上不受信任的源地址,向设备 R2 的环路接口发送 ping 请求。
user@R1> ping 172.16.0.2 source 172.16.0.1 PING 172.16.0.2 (172.16.0.2): 56 data bytes ^C --- 172.16.0.2 ping statistics --- 14 packets transmitted, 0 packets received, 100% packet loss
意义
验证以下信息:
ping 输出显示正在发生 10% 的数据包丢失。
ICMP 数据包计数器在递增,ICMP 监管器在递增。
设备 R2 不会向命令
ping 172.16.0.2 source 172.16.0.1发送 ICMP 响应。