Chương 7
🛡️ Firewall & Bảo mật hệ thống mạng
Xây dựng bộ Firewall rules hoàn chỉnh, chống BruteForce, Port Scan, VPN và QoS cho doanh nghiệp.
⏱ 35 phút🛡️ Security🏢 Production-Ready
📚
7.1 Kiến trúc Firewall MikroTik
| Chain | Xử lý traffic | Sử dụng để |
|---|---|---|
| input | Gói tin ĐẾN router (router là đích) | Bảo vệ bản thân router |
| forward | Gói tin ĐI QUA router (router là gateway) | Lọc traffic LAN↔WAN, VLAN↔VLAN |
| output | Gói tin XUẤT PHÁT từ router | Hiếm dùng |
| Action | Mô tả | Khi nào dùng |
|---|---|---|
| accept | Cho phép đi qua | Traffic được phép |
| drop | Chặn, im lặng (không thông báo) | Block tấn công, stealth |
| reject | Chặn + gửi ICMP unreachable | Block traffic hợp lệ cần thông báo |
| log | Ghi log rồi tiếp tục | Debug, audit |
| add-src-to-address-list | Thêm IP vào danh sách | BruteForce detection, ban list |
🔒
7.2 INPUT Chain – Bảo vệ Router
⚠️ Trước khi áp dụng DROP ALL rule, hãy đảm bảo bạn đang kết nối qua MGMT VLAN (99). Nếu không sẽ bị mất kết nối!
# ══ INPUT CHAIN – Bảo vệ bản thân Router ══
# Thứ tự rules RẤT QUAN TRỌNG – rules trên match trước
# [1] Cho phép traffic đã thiết lập (established/related)
/ip firewall filter add \
chain=input connection-state=established,related \
action=accept comment="[IN-1] Allow established/related"
# [2] Drop gói tin invalid
/ip firewall filter add \
chain=input connection-state=invalid \
action=drop comment="[IN-2] Drop invalid"
# [3] Cho phép ICMP ping từ LAN (không từ WAN)
/ip firewall filter add \
chain=input protocol=icmp \
src-address=192.168.0.0/16 \
action=accept comment="[IN-3] Allow ICMP from LAN"
# [4] Cho phép Winbox CHỈ từ VLAN Management
/ip firewall filter add \
chain=input protocol=tcp dst-port=8291 \
src-address=192.168.99.0/24 \
action=accept comment="[IN-4] Allow Winbox MGMT only"
# [5] Cho phép SSH CHỈ từ VLAN Management (port đã đổi)
/ip firewall filter add \
chain=input protocol=tcp dst-port=2222 \
src-address=192.168.99.0/24 \
action=accept comment="[IN-5] Allow SSH MGMT only"
# [6] Cho phép DNS queries từ LAN (nếu router làm DNS relay)
/ip firewall filter add \
chain=input protocol=udp dst-port=53 \
src-address=192.168.0.0/16 \
action=accept comment="[IN-6] Allow DNS from LAN"
# [7] Cho phép DHCP requests
/ip firewall filter add \
chain=input protocol=udp dst-port=67,68 \
action=accept comment="[IN-7] Allow DHCP"
# [8] Cho phép RADIUS (nếu dùng User Manager trên router)
/ip firewall filter add \
chain=input protocol=udp dst-port=1812,1813 \
src-address=192.168.0.0/16 \
action=accept comment="[IN-8] Allow RADIUS from LAN"
# [9] DROP TẤT CẢ còn lại – LOG trước khi drop
/ip firewall filter add \
chain=input action=log log-prefix="DROP-INPUT: " \
comment="[IN-9] Log before drop"
/ip firewall filter add \
chain=input action=drop \
comment="[IN-10] !!! DROP ALL OTHER INPUT !!!" 🔀
7.3 FORWARD Chain – Kiểm soát Traffic
# ══ FORWARD CHAIN – Lọc traffic đi qua Router ══
# [1] Allow established/related
/ip firewall filter add \
chain=forward connection-state=established,related \
action=accept comment="[FW-1] Allow established forward"
# [2] Drop invalid
/ip firewall filter add \
chain=forward connection-state=invalid \
action=drop comment="[FW-2] Drop invalid forward"
# [3] IT-VLAN → Internet: tất cả (full access)
/ip firewall filter add \
chain=forward src-address=192.168.10.0/24 \
out-interface=ether1 action=accept \
comment="[FW-3] IT VLAN full Internet"
# [4] Business-VLAN → Internet: chỉ HTTP/HTTPS/DNS
/ip firewall filter add \
chain=forward src-address=192.168.20.0/24 \
out-interface=ether1 protocol=tcp dst-port=80,443 \
action=accept comment="[FW-4] Business HTTP/HTTPS"
/ip firewall filter add \
chain=forward src-address=192.168.20.0/24 \
out-interface=ether1 protocol=udp dst-port=53 \
action=accept comment="[FW-4b] Business DNS"
# [5] CHẶN Guest WiFi → LAN nội bộ (ISOLATION)
/ip firewall filter add \
chain=forward src-address=192.168.30.0/24 \
dst-address=192.168.0.0/16 \
action=drop comment="[FW-5] BLOCK Guest to Internal LAN"
# [6] Guest-VLAN → Internet: chỉ HTTP/HTTPS
/ip firewall filter add \
chain=forward src-address=192.168.30.0/24 \
out-interface=ether1 protocol=tcp dst-port=80,443 \
action=accept comment="[FW-6] Guest HTTP/HTTPS only"
# [7] MGMT-VLAN → Tất cả (full access cho quản trị)
/ip firewall filter add \
chain=forward src-address=192.168.99.0/24 \
action=accept comment="[FW-7] MGMT full access"
# [8] Drop all other forward
/ip firewall filter add \
chain=forward action=drop \
comment="[FW-8] DROP all other forward" 🚫
7.4 Chống BruteForce SSH
# ══ Chống BruteForce SSH ══
# Tự động ban IP cố đăng nhập SSH sai nhiều lần
/ip firewall filter add chain=input protocol=tcp dst-port=2222 \
src-address-list=ssh-blacklist action=drop \
comment="[SEC] Drop SSH blacklisted IPs"
/ip firewall filter add chain=input protocol=tcp dst-port=2222 \
connection-state=new src-address-list=ssh-stage3 \
action=add-src-to-address-list address-list=ssh-blacklist \
address-list-timeout=10d
/ip firewall filter add chain=input protocol=tcp dst-port=2222 \
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=2222 \
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=2222 \
connection-state=new action=add-src-to-address-list \
address-list=ssh-stage1 address-list-timeout=1m
# → Sau 3 lần thử SSH trong 1 phút = bị ban 10 ngày!🔍
7.5 Phát hiện Port Scanner
# ══ Phát hiện và chặn Port Scanner ══
/ip firewall filter add \
chain=input protocol=tcp psd=21,3s,3,1 \
action=add-src-to-address-list \
address-list=port-scanners \
address-list-timeout=2w \
comment="[SEC] Detect port scanners"
/ip firewall filter add \
chain=input src-address-list=port-scanners \
action=drop comment="[SEC] Drop port scanners"
# Kiểm tra address-list
/ip firewall address-list print where list=port-scanners
/ip firewall address-list print where list=ssh-blacklist💡 Thứ tự Firewall rules rất quan trọng. Rule đứng trước có độ ưu tiên cao hơn. LUÔN đặt accept established/related lên ĐẦU TIÊN.
🔐
7.6 System Hardening
# ══ System Hardening ══
# 1. Đổi port SSH
/ip service set ssh port=2222
# 2. Tắt dịch vụ không cần thiết
/ip service disable telnet
/ip service disable ftp
/ip service disable www ;# Tắt WebFig nếu không dùng
/ip service disable api
/ip service disable api-ssl
# 3. Giới hạn truy cập Winbox theo IP
/ip service set winbox address=192.168.99.0/24
# 4. Tắt MAC Telnet và MAC WinBox (chỉ cho MGMT interface)
/tool mac-server set allowed-interface-list=none
/tool mac-server mac-winbox set allowed-interface-list=none
# 5. Tắt Bandwidth Test server
/tool bandwidth-server set enabled=no
# 6. Tắt IP Neighbor Discovery (không broadcast thông tin router)
/ip neighbor discovery-settings set discover-interface-list=none
# 7. Bảo vệ DNS khỏi bị lạm dụng
/ip dns set allow-remote-requests=no🔗
7.7 VPN Client-to-Site (L2TP/IPsec)
# ══ L2TP/IPsec VPN – Client-to-Site ══
# Tạo L2TP Server
/interface l2tp-server server set \
enabled=yes \
use-ipsec=required \
ipsec-secret="VPN@IPsec2025!" \
default-profile=default-encryption \
authentication=mschap2
# Tạo VPN User
/ppp secret add name=vpnuser01 password="VpnPass@123" \
service=l2tp local-address=10.10.10.1 \
remote-address=10.10.10.101 comment="VPN Remote Worker"
# Firewall mở cổng VPN
/ip firewall filter add chain=input protocol=udp \
dst-port=500,1701,4500 action=accept \
comment="[VPN] Allow L2TP/IPsec ports"
/ip firewall filter add chain=input protocol=ipsec-esp \
action=accept comment="[VPN] Allow IPsec ESP"
# Kiểm tra VPN connections
/ppp active print
/interface l2tp-server print⚡
7.8 QoS – Bandwidth Management
# ══ QoS – Bandwidth Management ══
# Giới hạn Guest WiFi: Upload 5Mbps / Download 10Mbps
/queue simple add \
name="Limit-Guest-WiFi" \
target=192.168.30.0/24 \
max-limit=5M/10M \
burst-limit=8M/15M \
burst-threshold=4M/8M \
burst-time=10s/10s \
comment="Guest WiFi bandwidth limit"
# Giới hạn Business VLAN: Upload 20Mbps / Download 50Mbps
/queue simple add \
name="Limit-Business" \
target=192.168.20.0/24 \
max-limit=20M/50M \
comment="Business VLAN limit"
# Priority: IT > Business > Guest
/queue tree add name="WAN-Download" parent=ether1 max-limit=100M
/queue tree add name="IT-DL" parent=WAN-Download priority=1 max-limit=60M
/queue tree add name="Biz-DL" parent=WAN-Download priority=4 max-limit=30M
/queue tree add name="Guest-DL" parent=WAN-Download priority=8 max-limit=10M📊
7.9 Logging & SNMP Monitoring
# ══ Logging & Monitoring ══
# Gửi log ra Syslog server (192.168.99.11)
/system logging action add \
name=remote-syslog type=remote \
remote=192.168.99.11 remote-port=514 \
bsd-syslog=yes syslog-facility=daemon
/system logging add topics=firewall action=remote-syslog
/system logging add topics=dhcp action=remote-syslog
/system logging add topics=radius action=remote-syslog
/system logging add topics=account action=remote-syslog
# SNMP (cho Zabbix/PRTG/The Dude)
/snmp set enabled=yes \
contact="[email protected]" \
location="HCM-Office-Server-Room"
/snmp community add name=monitoring \
address=192.168.99.0/24 \
security=private read-access=yes write-access=no
# Xem log real-time
/log print follow-onlyℹ️ Kiểm tra firewall rules định kỳ: /ip firewall filter print stats – xem bytes/packets counter để biết rule nào đang active.
🏆
7.10 Checklist Bảo mật Doanh nghiệp
✅ Production Security Checklist
🔐 Authentication
- ☐ Đổi mật khẩu admin mặc định
- ☐ Tạo user quản trị riêng
- ☐ Disable user admin mặc định
- ☐ Đổi port SSH (8291 → port khác)
- ☐ Giới hạn Winbox/SSH theo IP MGMT
🛡️ Firewall
- ☐ INPUT chain rules đầy đủ
- ☐ FORWARD chain rules đầy đủ
- ☐ Guest VLAN isolated khỏi LAN
- ☐ BruteForce protection SSH
- ☐ Port scan detection
📡 Network
- ☐ VLAN phân vùng đúng
- ☐ WiFi WPA2-Enterprise (RADIUS)
- ☐ Management VLAN riêng biệt
- ☐ VPN cho remote workers
- ☐ QoS bandwidth control
📊 Monitoring
- ☐ Syslog gửi về server tập trung
- ☐ SNMP monitoring (Zabbix/PRTG)
- ☐ Backup config định kỳ
- ☐ RouterOS cập nhật firmware
- ☐ Review firewall logs hàng tuần