你的浏览器无法正常显示内容,请更换或升级浏览器!

RouterOS 防火墙规则配置实战:从零构建安全策略

tenfei
tenfei
发布于2026-06-02 12:04 阅读16次
RouterOS 防火墙规则配置实战:从零构建安全策略
本文通过实际案例讲解RouterOS防火墙配置的核心要点,涵盖input/forward链的区别、连接状态跟踪、常见坑点(误屏蔽自身IP、规则顺序错误)、以及防DDoS的raw表用法,帮助初学者避开90%的踩雷。
# RouterOS 防火墙规则配置实战:从零构建安全策略 ## 为什么要重视 RouterOS 防火墙 很多初学者在拿到 MikroTik RouterOS 设备后,第一件事是配上网,第二件事就是忘了防火墙。RouterOS 默认配置下,防火墙规则非常「开放」,尤其是 input 链,如果不手动加固,路由器自身暴露在局域网甚至公网中,风险极高。 ## 核心概念:三大链(Chain) RouterOS 防火墙基于 Linux netfilter,有三个核心链: - **input**: destiny 是路由器自身的流量(如访问 WinBox、SSH、WWW) - **forward**:穿越路由器的流量(LAN → WAN,或 LAN → LAN) - **output**:路由器自身发起的流量(较少用到) ## 实战案例:安全加固 input 链 ```routeros # 允许已建立和相关的连接 /ip firewall filter add chain=input connection-state=established,related action=accept comment="允许已建立连接" # 允许 ICMP(ping),但限制速率防洪水 /ip firewall filter add chain=input protocol=icmp action=accept limit=50,50 comment="ICMP限速" # 允许来自信任IP的WinBox访问 /ip firewall filter add chain=input protocol=tcp dst-port=8291 src-address=192.168.88.0/24 action=accept comment="允许内网WinBox" # 丢弃其余input流量 /ip firewall filter add chain=input action=drop comment="丢弃其他input" ``` ⚠️ **坑点1**:规则是有顺序的!RouterOS 从上到下匹配,一旦匹配就执行 action。如果你把 `drop` 规则放在最前面,你会立刻把自己踢出路由器。 ⚠️ **坑点2**:`connection-state=invalid` 的包一定要 drop,否则容易被用于绕过防火墙。 ## forward 链:保护内网 ```routeros # 允许内网访问外网 /ip firewall filter add chain=forward src-address=192.168.88.0/24 out-interface=ether1 action=accept # 禁止外网主动访问内网(NAT后的保护) /ip firewall filter add chain=forward connection-nat-state=!dstnat connection-state=new in-interface=ether1 action=drop ``` ## 使用 address-list 管理黑名单 ```routeros # 创建黑名单 /ip firewall address-list add list=blacklist address=10.10.10.5 comment="恶意扫描IP" # 在filter中引用 /ip firewall filter add chain=input src-address-list=blacklist action=drop ``` ## 性能优化:raw 表 当流量很大时,firewall filter 会消耗大量 CPU。可以在 `/ip firewall raw` 中提前丢弃无效流量: ```routeros /ip firewall raw add chain=prerouting protocol=tcp tcp-flags=rst action=drop ``` ## 总结 RouterOS 防火墙配置的核心原则:**先允许必要流量,最后丢弃其余**。务必在修改规则前通过 `safe mode`(WinBox 左上角安全模式按钮)操作,防止自己被锁在外面。掌握 `connection-state` 和 `address-list` 是进阶的必经之路。

2

0

文章点评
Copyright © from 2021 by namoer.com
458815@qq.com QQ:458815
蜀ICP备2022020274号-2