Firewall & NAT
Содержание
Архитектура Firewall в RouterOS 7
В RouterOS 7 файрвол переработан на базе nftables. Основные таблицы: filter, nat, mangle и raw. Порядок обработки пакетов соответствует стандартному Netfilter flow, но с оптимизациями FastTrack для established-соединений.
Цепочки: input, forward, output
| Цепочка | Назначение | Типичное применение |
|---|---|---|
| input | Пакеты, адресованные самому роутеру | Защита сервисов (SSH, Winbox, DNS) |
| forward | Пакеты, проходящие через роутер | Фильтрация между LAN и WAN |
| output | Пакеты, генерируемые роутером | Обычно не фильтруются |
Connection Tracking
Connection tracking (conntrack) — механизм отслеживания состояния соединений. Каждое соединение может находиться в одном из состояний:
- new — первый пакет нового соединения
- established — пакет принадлежит уже установленному соединению
- related — пакет связан с существующим соединением (FTP data, ICMP errors)
- invalid — пакет не принадлежит ни одному известному соединению
- untracked — пакет, помеченный в raw-таблице как notrack
FastTrack: В RouterOS 7 FastTrack ускоряет обработку established/related пакетов, пропуская их мимо файрвола. Это значительно повышает throughput, но такие пакеты не будут обработаны mangle и queue rules.
Базовый набор правил
# === INPUT CHAIN ===
/ip/firewall/filter
add chain=input connection-state=established,related action=accept comment="defconf: established,related"
add chain=input connection-state=invalid action=drop comment="defconf: drop invalid"
add chain=input protocol=icmp action=accept comment="defconf: ICMP"
add chain=input src-address=192.168.88.0/24 action=accept comment="allow LAN full access"
add chain=input protocol=tcp dst-port=8291 src-address-list=management action=accept comment="Winbox from management list"
add chain=input protocol=tcp dst-port=22 src-address-list=management action=accept comment="SSH from management list"
add chain=input action=drop comment="drop everything else"
# === FORWARD CHAIN ===
add chain=forward connection-state=established,related action=accept
add chain=forward connection-state=invalid action=drop
add chain=forward connection-state=new in-interface=bridge-lan out-interface=ether1 action=accept comment="LAN to WAN"
add chain=forward action=drop comment="drop all other"
# FastTrack для производительности
/ip/firewall/filter/add chain=forward connection-state=established,related action=fasttrack-connection place-before=0RouterOS CLI
NAT: srcnat и dstnat
Source NAT (маскарадинг)
# Маскарадинг для выхода LAN в интернет
/ip/firewall/nat/add chain=srcnat out-interface=ether1 action=masquerade
# Или src-nat с фиксированным IP (стабильнее для VPN)
/ip/firewall/nat/add chain=srcnat out-interface=ether1 action=src-nat to-addresses=203.0.113.1RouterOS CLI
Destination NAT (проброс портов)
# Проброс порта 8080 на внутренний сервер
/ip/firewall/nat/add chain=dstnat protocol=tcp dst-port=8080 in-interface=ether1 action=dst-nat to-addresses=192.168.88.100 to-ports=80
# Hairpin NAT (доступ к серверу из LAN по внешнему IP)
/ip/firewall/nat/add chain=srcnat src-address=192.168.88.0/24 dst-address=192.168.88.100 protocol=tcp dst-port=80 action=masqueradeRouterOS CLI
Address Lists
# Статические записи
/ip/firewall/address-list
add list=management address=10.0.0.0/24 comment="Office VPN"
add list=management address=203.0.113.50 comment="Admin home"
# Динамическое добавление — блокировка брутфорса SSH
/ip/firewall/filter
add chain=input protocol=tcp dst-port=22 src-address-list=ssh-blacklist action=drop
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=1d
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
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
add chain=input protocol=tcp dst-port=22 connection-state=new action=add-src-to-address-list address-list=ssh-stage1 address-list-timeout=1mRouterOS CLI
Raw table и защита от DDoS
Raw-таблица обрабатывается до conntrack и позволяет отбрасывать пакеты с минимальными затратами CPU.
# Блокировка bogon-сетей на входе
/ip/firewall/raw
add chain=prerouting in-interface=ether1 src-address=0.0.0.0/8 action=drop
add chain=prerouting in-interface=ether1 src-address=10.0.0.0/8 action=drop
add chain=prerouting in-interface=ether1 src-address=172.16.0.0/12 action=drop
add chain=prerouting in-interface=ether1 src-address=192.168.0.0/16 action=drop
# SYN flood protection
add chain=prerouting protocol=tcp tcp-flags=syn connection-state=new in-interface=ether1 limit=200,5:packet action=accept
add chain=prerouting protocol=tcp tcp-flags=syn connection-state=new in-interface=ether1 action=dropRouterOS CLI