RouterOS防火墙实战配置指南

发布于2026-05-08 12:14 阅读54次 本文详细介绍MikroTik RouterOS防火墙的配置方法,包括默认安全策略、端口扫描防护、DDoS防护、NAT配置等内容,帮助网络管理员构建安全的网络环境。
# RouterOS防火墙实战配置指南
RouterOS作为MikroTik路由器的核心操作系统,其防火墙功能强大且灵活。本文将详细介绍如何配置RouterOS防火墙,构建安全的网络环境。
## 一、防火墙基础概念
RouterOS防火墙基于Linux内核的Netfilter框架,采用状态检测机制,能够识别和跟踪连接状态。主要包含以下几个核心组件:
- **Filter链**:用于过滤数据包,是最常用的防火墙规则
- **NAT链**:实现网络地址转换功能
- **Mangle链**:用于数据包标记和修改
- **Raw链**:用于配置例外,绕过连接跟踪
## 二、默认安全策略配置
在开始配置前,建议先设置默认策略:
```
/ip firewall filter
add chain=input connection-state=established,related action=accept comment="接受已建立连接"
add chain=input connection-state=invalid action=drop comment="丢弃无效连接"
add chain=input protocol=icmp action=accept comment="允许ICMP"
add chain=input in-interface=ether1 action=drop comment="拒绝外部访问"
```
## 三、端口扫描防护
为防止恶意扫描,可以添加检测规则:
```
/ip firewall filter
add chain=input protocol=tcp psd=21,3s,3,1 action=drop comment="检测端口扫描"
add chain=input protocol=tcp tcp-flags=syn,rst action=drop comment="TCP标志异常"
add chain=input protocol=tcp connection-limit=limit=10,32 action=drop comment="连接数限制"
```
## 四、DDoS防护策略
针对DDoS攻击,RouterOS提供了多种防护手段:
### 1. 地址列表过滤
```
/ip firewall address-list
add list=blacklist address=192.168.1.100 comment="恶意IP"
/ip firewall filter
add chain=input src-address-list=blacklist action=drop comment="黑名单过滤"
```
### 2. 连接速率限制
```
/ip firewall filter
add chain=input protocol=tcp dst-port=80 connection-rate=100-100000 action=drop comment="HTTP速率限制"
```
## 五、NAT配置实例
### 端口映射
```
/ip firewall nat
add chain=dstnat protocol=tcp dst-port=8080 in-interface=ether1 action=dst-nat to-addresses=192.168.88.10 to-ports=80 comment="Web服务映射"
```
### 源NAT(上网共享)
```
/ip firewall nat
add chain=srcnat out-interface=ether1 action=masquerade comment="外网共享"
```
## 六、日志与监控
启用防火墙日志便于问题排查:
```
/ip firewall filter
add chain=input action=log log-prefix="FW_INPUT: " comment="记录输入链"
/system logging
add topics=firewall action=memory
```
## 总结
RouterOS防火墙配置需要遵循"默认拒绝,按需开放"的安全原则。通过合理配置Filter链、NAT链以及各种防护策略,可以有效抵御网络攻击,保障网络安全稳定运行。建议定期审查防火墙规则,及时更新安全策略。
配置完成后,务必使用`/export`命令备份配置,以便故障恢复时快速还原。