第96篇:BGP 前缀限制与安全防护

关键词

BGP、前缀限制、Prefix Limit、BGP 安全、路由泛洪、max-prefix


一、BGP 面临的安全问题

BGP 是互联网的基础协议,但天然存在安全风险:

攻击方式 说明 后果
路由泛洪 对端发送大量 BGP 路由 内存耗尽,路由器崩溃
路由劫持 恶意 AS 宣告不属于自己的前缀 流量被劫持
路由泄露 错误的路由传播 流量绕行,业务中断

二、前缀限制(max-prefix)

2.1 基本配置

max-prefix 限制从指定 BGP 邻居可以学到的最多路由数量:

bgp 100
 peer 10.0.12.2 as-number 200
 peer 10.0.12.2 maximum-prefix 1000       # 最多接受 1000 条

2.2 超出限制后的处理

# 超过限制时断开连接
peer 10.0.12.2 maximum-prefix 1000 warning-only   # 只告警,不断开

# 超过限制后断开连接并设置重启时间
peer 10.0.12.2 maximum-prefix 1000 restart 60     # 断连 60 秒后重连

2.3 配置示例

# eBGP 对等体(运营商)——建议设置限制
bgp 65001
 peer 10.0.12.1 as-number 100
 peer 10.0.12.1 maximum-prefix 100000             # 互联网全表 ≈ 90 万
 peer 10.0.12.1 maximum-prefix 50000 restart 120  # 设置重启间隔

# iBGP 对等体(内部)——通常不限制

2.4 查看前缀统计

# 查看从邻居收到的路由数
display bgp peer 10.0.12.2 verbose

  Peer: 10.0.12.2, AS: 200
  BGP version 4, remote router ID 2.2.2.2
  Maximum prefix limit: 1000
  Prefix count: 850                ← 当前收到 850 条
  Threshold: 75%                    ← 告警阈值(默认 75%)

三、BGP 路由过滤

3.1 入方向过滤

在接收路由前过滤,防止不需要的路由进入 BGP 表:

# 使用 IP 前缀列表
ip ip-prefix ALLOW_PREFIX permit 172.16.0.0 16

route-policy FILTER_IN permit node 10
 if-match ip-prefix ALLOW_PREFIX

bgp 100
 peer 10.0.12.2 route-policy FILTER_IN import

3.2 出方向过滤

控制向外通告的路由:

# 只通告内部网段
ip ip-prefix INTERNAL permit 10.0.0.0 8

route-policy FILTER_OUT permit node 10
 if-match ip-prefix INTERNAL

bgp 100
 peer 10.0.12.2 route-policy FILTER_OUT export

3.3 基于 AS_PATH 过滤

# 拒绝来自指定 AS 的路由
ip as-path-filter 1 deny ^100_         # AS 100 始发的路由
ip as-path-filter 1 permit .*          # 其他路由放行

route-policy AS_FILTER permit node 10
 if-match as-path-filter 1

bgp 100
 peer 10.0.12.2 route-policy AS_FILTER import

四、BGP 安全加固清单

4.1 基础安全配置

# 1. 前缀限制
bgp 100
 peer 10.0.12.2 maximum-prefix 10000

# 2. MD5 认证
peer 10.0.12.2 password cipher Huawei@123

# 3. TTL Security(eBGP)
peer 10.0.12.2 ttl-security hops 1

# 4. GTSM(全局)
peer 10.0.12.2 ttl-security hops 1

4.2 高级安全策略

# 5. 禁止接收默认路由(除非需要)
route-policy DENY_DEFAULT deny node 10
 if-match ip-prefix DEFAULT

route-policy DENY_DEFAULT permit node 20

ip ip-prefix DEFAULT permit 0.0.0.0 0

# 6. 禁止接收私有地址路由
ip ip-prefix PRIVATE deny 10.0.0.0 8
ip ip-prefix PRIVATE deny 172.16.0.0 12
ip ip-prefix PRIVATE deny 192.168.0.0 16
ip ip-prefix PRIVATE permit 0.0.0.0 0 less-equal 32

# 7. RPKI 验证(如支持)
bgp 100
 rpki-server tcp 192.0.2.1 port 323

五、BGP 路由劫持案例

5.1 典型劫持场景

攻击者 AS 700 宣告:
  172.16.0.0/16(实际属于 AS 100)

结果:
  互联网流量错误地流向 AS 700
  AS 100 的用户无法收到流量

防御措施

# 1. 前缀列表过滤(白名单)
ip ip-prefix CUSTOMER permit 172.16.0.0 16

route-policy FROM_CUSTOMER permit node 10
 if-match ip-prefix CUSTOMER

# 2. AS_PATH 验证
ip as-path-filter 1 permit ^100_       # 只接受来自 AS 100 始发的路由

5.2 IRR(Internet Routing Registry)

# 查询路由注册信息
whois -h whois.radb.net 172.16.0.0

route: 172.16.0.0/16
origin: AS100
source: RADB

六、案例:运营商 BGP 安全策略

# 上游运营商的安全策略示例
bgp 100
 # 前缀限制
 peer 10.0.12.2 maximum-prefix 500000 restart 30

 # MD5 认证
 peer 10.0.12.2 password cipher Secure@BGP

 # TTL 安全
 peer 10.0.12.2 ttl-security hops 1

 # 路由过滤
 peer 10.0.12.2 route-policy FROM_CUSTOMER import
 peer 10.0.12.2 route-policy TO_CUSTOMER export

# 客户侧
bgp 65001
 peer 10.0.12.1 maximum-prefix 100000 warning-only
 peer 10.0.12.1 password cipher BGP@Secure

七、总结

安全措施 作用 推荐级别
max-prefix 防止路由泛洪 ⭐⭐⭐⭐⭐
路由过滤 只允许特定路由 ⭐⭐⭐⭐⭐
MD5 认证 防止伪造 BGP 报文 ⭐⭐⭐⭐
TTL Security 防止远程攻击 ⭐⭐⭐⭐
AS_PATH 过滤 防止路由劫持 ⭐⭐⭐⭐
IRR/RPKI 验证路由合法性 ⭐⭐⭐

八、思考

  1. maximum-prefix 命令的作用是什么?超过限制会发生什么?
  2. 如何基于 AS_PATH 过滤来自指定 AS 的路由?
  3. BGP MD5 认证和 TTL Security 分别防止什么类型的攻击?
  4. 什么是 BGP 路由劫持?如何防御?
  5. 在企业与运营商对接的场景中,建议配置哪些 BGP 安全措施?

下篇预告:第97篇《BGP 团体属性在运营商网络中的应用》——深入 Community 在 ISP 网络中的实战用法。