Bảo mật Web Portal & Firewall nftables trên Yocto
Ghi chú kỹ thuật về lỗi lộ port backend phía sau Nginx trên thiết bị Embedded Linux (ví dụ Raspberry Pi chạy Yocto) và cách khóa chặt bằng loopback binding + nftables.
Symptom (Hiện tượng)
Web portal trên board dùng Nginx (port 80) có auth_basic làm reverse proxy tới backend Flask (ví dụ port 5000). Tuy nhiên, backend lại bind 0.0.0.0:5000 (không có auth, bật CORS *), khiến mọi máy trong LAN hoặc client kết nối SoftAP có thể gọi trực tiếp http://<pi-ip>:5000 để vượt qua màn hình đăng nhập, thậm chí dính tấn công CSRF từ trình duyệt cùng mạng.
Root Cause (Nguyên nhân)
- Lớp xác thực chỉ đặt ở Nginx nhưng tiến trình backend lại lắng nghe trên tất cả network interface (
0.0.0.0). - Hệ điều hành tối giản trên Embedded Linux thường không bật sẵn firewall (
iptables/nftables).
Cách xử lý 1: Bind backend về 127.0.0.1 (Khuyến nghị)
Vì Nginx đã proxy cục bộ tới localhost, chỉ cần giới hạn backend lắng nghe trên loopback và tắt CORS * (do frontend và API dùng chung origin qua Nginx):
app.run(debug=False, host="127.0.0.1", port=5000)
Cách này đóng ngay lỗ hổng mà không cần chỉnh sửa OS image. Đồng thời kiểm tra các dịch vụ nội bộ khác (ví dụ Redis mặc định bind 127.0.0.1 và protected-mode yes).
Cách xử lý 2: Thêm lớp Firewall nftables trong Yocto (Defense-in-Depth)
1. Kiểm tra Kernel config & module trên board
zcat /proc/config.gz | grep -E "NF_TABLES|NFT_"
ls /lib/modules/$(uname -r)/kernel/net/netfilter/ | grep -E "nf_tables|nfnetlink"
Các option tối thiểu cần có: CONFIG_NF_TABLES (=y hoặc =m), CONFIG_NF_TABLES_INET. Nếu CONFIG_NFT_REJECT không bật, dùng action drop thay vì reject.
2. Cấu hình Yocto nhẹ cho nftables
nftables có sẵn trong meta-networking. Mặc định PACKAGECONFIG kéo theo python; có thể lược bớt trong local.conf hoặc distro config:
PACKAGECONFIG:pn-nftables = "readline json"
3. Recipe mẫu (recipes-security/pi-firewall/pi-firewall.bb)
SUMMARY = "Minimal nftables firewall rules for Embedded Linux"
LICENSE = "CLOSED"
SRC_URI = "file://pi-firewall.nft file://pi-firewall.service"
inherit systemd
SYSTEMD_SERVICE:${PN} = "pi-firewall.service"
RDEPENDS:${PN} = "nftables kernel-module-nf-tables kernel-module-nfnetlink"
do_install() {
install -Dm 0644 ${WORKDIR}/pi-firewall.nft ${D}${sysconfdir}/pi-firewall.nft
install -Dm 0644 ${WORKDIR}/pi-firewall.service ${D}${systemd_system_unitdir}/pi-firewall.service
}
File ruleset pi-firewall.nft (chặn port nội bộ từ ngoài, giữ nguyên lưu lượng hợp lệ khác):
flush ruleset
table inet pi_filter {
chain input {
type filter hook input priority 0; policy accept;
iif "lo" accept
tcp dport { 5000, 6379 } drop
}
}
File unit pi-firewall.service:
[Unit]
Description=Embedded Linux nftables firewall
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/nft -f /etc/pi-firewall.nft
ExecStop=/usr/sbin/nft flush ruleset
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Lưu ý: Kiểm tra nft list ruleset trước khi dùng flush ruleset để tránh xóa nhầm rule NAT/masquerade hoặc rule iptables-nft khác trên hệ thống.