RouterOS防火墙基础配置指南

发布于2026-05-02 21:01 阅读18次 本文详细介绍MikroTik RouterOS防火墙的基础配置方法,包括Filter规则编写、连接状态匹配、常用防护策略等内容,帮助网络管理员快速搭建安全可靠的网络防护体系。
# RouterOS防火墙基础配置指南
RouterOS作为MikroTik路由器的核心操作系统,其内置的防火墙功能强大且灵活,是网络安全防护的重要基石。本文将系统介绍RouterOS防火墙的基础配置方法,帮助网络管理员快速上手。
## 一、防火墙基础概念
RouterOS防火墙基于Netfilter框架,主要包含三个表(table):**filter**、**nat**和**mangle**。其中filter表用于数据包过滤,是最常用的安全防护表。filter表下又分为三条链(chain):
- **Input链**:处理发往路由器本身的数据包
- **Forward链**:处理经过路由器转发的数据包
- **Output链**:处理从路由器本身发出的数据包
理解这三条链的作用范围,是编写正确防火墙规则的前提。
## 二、基础防护规则配置
### 2.1 建立安全输入规则
首先,我们需要保护路由器自身安全。以下是一组推荐的Input链规则:
```routeros
/ip firewall filter add chain=input action=accept connection-state=established,related comment="允许已建立连接"
/ip firewall filter add chain=input action=accept protocol=icmp comment="允许ICMP"
/ip firewall filter add chain=input action=accept in-interface-list=LAN comment="允许内网访问"
/ip firewall filter add chain=input action=drop comment="丢弃其他所有输入"
```
这套规则的核心思路是:**默认拒绝,按需放行**。先放行已有连接和ICMP协议,再允许内网访问路由器,最后丢弃其余所有流量。
### 2.2 转发链保护
Forward链负责管控经过路由器的流量,同样需要合理配置:
```routeros
/ip firewall filter add chain=forward action=accept connection-state=established,related comment="转发已建立连接"
/ip firewall filter add chain=forward action=accept in-interface-list=LAN out-interface-list=WAN comment="内网上网"
/ip firewall filter add chain=forward action=drop connection-state=invalid comment="丢弃无效连接"
/ip firewall filter add chain=forward action=drop in-interface-list=WAN comment="阻止外网主动访问内网"
```
## 三、连接状态匹配详解
RouterOS防火墙支持四种连接状态(connection-state),正确理解和使用它们至关重要:
| 状态 | 含义 | 典型用途 |
|------|------|----------|
| **established** | 已建立的连接 | 放行双向数据流 |
| **related** | 相关连接(如FTP数据通道) | 放行关联连接 |
| **new** | 新连接请求 | 限制新建连接速率 |
| **invalid** | 无效连接 | 丢弃异常数据包 |
在实际配置中,建议始终将established和related的放行规则放在最前面,这样已建立的连接不会被后续规则误拦截,同时显著提升处理效率。
## 四、防端口扫描与暴力破解
RouterOS提供了额外的防护层(Protection),可以有效抵御常见的网络攻击:
```routeros
/ip firewall connection tracking set tcp-syn-sent-timeout=5s tcp-syn-received-timeout=5s
/ip firewall filter add chain=input action=drop protocol=tcp psd=21,3s,3,1 comment="防端口扫描"
/ip firewall filter add chain=input action=drop src-address-list=blacklist protocol=tcp dst-port=22 comment="封禁SSH暴力破解IP"
/ip firewall filter add chain=input action=add-src-to-address-list connection-state=new protocol=tcp dst-port=22 address-list=blacklist address-list-timeout=1d comment="记录SSH尝试IP"
```
## 五、配置注意事项
1. **规则顺序很重要**:防火墙规则从上到下逐条匹配,首条匹配即生效,务必注意排列顺序
2. **不要锁死自己**:配置完成后务必确认仍能通过Winbox或SSH访问路由器,建议在配置前设置定时回滚脚本
3. **定期审查规则**:随着网络环境变化,防火墙规则也需要定期检查和优化
4. **善用注释**:为每条规则添加清晰的comment,便于后期维护
## 六、总结
RouterOS防火墙虽然功能强大,但核心逻辑并不复杂——默认拒绝、按需放行、合理利用连接状态。掌握本文介绍的基础配置后,已经能够构建起一道可靠的网络安全防线。后续可以进一步学习Address-List动态管理、Layer7协议过滤等进阶技术,不断提升防护能力。
网络安全是一个持续演进的过程,没有一劳永逸的配置方案。保持学习、定期更新,才能让RouterOS防火墙发挥最大的防护效果。