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

MikroTik RouterOS 防火墙规则配置实战指南

tenfei
tenfei
发布于2026-04-01 13:49 阅读4次
MikroTik RouterOS 防火墙规则配置实战指南
MikroTik RouterOS 是一款功能强大的路由器操作系统,其防火墙模块基于 netfilter 链式规则,支持精细化的流量控制与安全策略。本文将从防火墙基础概念出发,详细讲解 Filter、NAT、Mangle 三张表的使用方法,并结合实际场景演示如何配置入站保护、端口转发和流量标记,帮助网络管理员快速上手 RouterOS 防火墙配置。
# MikroTik RouterOS 防火墙规则配置实战指南 ## 前言 MikroTik RouterOS 是由拉脱维亚公司 MikroTik 开发的路由器操作系统,广泛应用于企业网络、ISP 以及家庭高级用户场景。其防火墙功能基于 Linux netfilter 框架,提供了强大而灵活的流量过滤与控制能力。 本文将系统介绍 RouterOS 防火墙的核心概念,并通过实际配置示例帮助读者快速掌握防火墙规则的编写与管理。 --- ## 一、RouterOS 防火墙基础概念 RouterOS 防火墙由三张核心表组成: | 表名 | 作用 | |---------|------------------------------| | Filter | 过滤数据包,决定允许或丢弃 | | NAT | 网络地址转换,端口映射 | | Mangle | 标记数据包,用于流量整形 | 每张表包含若干**链(Chain)**,数据包按链中规则顺序匹配,命中第一条匹配规则后执行对应动作。 ### 1.1 Filter 表的三条链 - **input**:目标为路由器本身的流量 - **forward**:穿越路由器的流量(客户端 → 互联网) - **output**:由路由器本身发出的流量 ### 1.2 常用动作(Action) - `accept`:允许通过 - `drop`:静默丢弃 - `reject`:拒绝并返回 ICMP 错误 - `log`:记录日志 - `jump`:跳转到自定义链 --- ## 二、通过 Winbox 访问防火墙配置 Winbox 是 MikroTik 官方提供的图形化管理工具,推荐使用最新版本。 1. 打开 Winbox,输入路由器 IP(默认 `192.168.88.1`) 2. 用户名 `admin`,默认无密码 3. 进入菜单:**IP → Firewall** 命令行方式(SSH 或 Terminal): ```bash /ip firewall filter print ``` --- ## 三、入站保护配置(保护路由器本身) 以下规则保护路由器的 `input` 链,防止外网直接访问管理端口: ```bash # 允许已建立的连接 /ip firewall filter add chain=input connection-state=established,related action=accept comment="Allow Established" # 丢弃无效连接 /ip firewall filter add chain=input connection-state=invalid action=drop comment="Drop Invalid" # 允许本地局域网访问路由器 /ip firewall filter add chain=input src-address=192.168.88.0/24 action=accept comment="Allow LAN" # 允许 ICMP(ping) /ip firewall filter add chain=input protocol=icmp action=accept comment="Allow ICMP" # 丢弃所有其他入站流量(放在最后) /ip firewall filter add chain=input action=drop comment="Drop All Others" ``` > ⚠️ 注意:最后一条 drop 规则必须放在所有 accept 规则之后,否则会导致合法流量被丢弃。 --- ## 四、端口转发配置(NAT) 将外网访问路由器 `8080` 端口的流量转发到内网服务器 `192.168.88.100:80`: ```bash /ip firewall nat add chain=dstnat protocol=tcp dst-port=8080 action=dst-nat to-addresses=192.168.88.100 to-ports=80 comment="Web Server Port Forward" ``` 同时需要在 Filter 的 forward 链中放行该流量: ```bash /ip firewall filter add chain=forward dst-address=192.168.88.100 dst-port=80 protocol=tcp action=accept comment="Allow Forward to Web Server" ``` --- ## 五、防止暴力破解 SSH 通过连接频率限制,防止外部对路由器 SSH(22端口)进行暴力破解: ```bash # 将短时间内多次连接的 IP 加入黑名单 /ip firewall filter add chain=input protocol=tcp dst-port=22 src-address-list=ssh_blacklist action=drop comment="Block SSH Brute Force" /ip firewall filter add chain=input protocol=tcp dst-port=22 connection-state=new src-address-list=ssh_stage3 action=add-src-to-address-list address-list=ssh_blacklist address-list-timeout=10d comment="Stage 3 to Blacklist" /ip firewall filter add chain=input protocol=tcp dst-port=22 connection-state=new src-address-list=ssh_stage2 action=add-src-to-address-list address-list=ssh_stage3 address-list-timeout=1m /ip firewall filter add chain=input protocol=tcp dst-port=22 connection-state=new src-address-list=ssh_stage1 action=add-src-to-address-list address-list=ssh_stage2 address-list-timeout=1m /ip firewall filter add chain=input protocol=tcp dst-port=22 connection-state=new action=add-src-to-address-list address-list=ssh_stage1 address-list-timeout=1m comment="Stage 1" ``` --- ## 六、Mangle 流量标记(QoS 基础) 为 VoIP 流量打标记,配合队列实现优先转发: ```bash /ip firewall mangle add chain=prerouting protocol=udp dst-port=5060,10000-20000 action=mark-connection new-connection-mark=voip_conn comment="Mark VoIP Connection" /ip firewall mangle add chain=prerouting connection-mark=voip_conn action=mark-packet new-packet-mark=voip_packet comment="Mark VoIP Packet" ``` 之后在 **Queues** 中为 `voip_packet` 设置高优先级即可。 --- ## 七、查看与调试 ```bash # 查看所有 filter 规则及命中计数 /ip firewall filter print stats # 实时查看防火墙日志 /log print follow where topics~"firewall" # 查看连接跟踪表 /ip firewall connection print ``` --- ## 八、最佳实践总结 1. **默认拒绝原则**:input 和 forward 链末尾始终加 `drop all` 2. **规则顺序很重要**:RouterOS 按顺序匹配,越具体的规则越靠前 3. **定期备份配置**:`/export file=backup` 导出配置文件 4. **使用注释**:每条规则加 `comment`,便于后期维护 5. **测试环境先验证**:生产环境修改防火墙前,务必在测试环境验证 --- ## 结语 RouterOS 防火墙功能强大,但规则配置需要严谨细致。本文覆盖了入站保护、端口转发、暴力破解防护和流量标记等常见场景,希望能帮助你建立起对 RouterOS 防火墙的系统认知。随着网络规模扩大,建议结合 Address List 和自定义链进一步优化规则结构,提升可维护性。

1

0

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