본문 바로가기
Infra2024년 12월 3일6분 읽기

iptables와 nftables 방화벽 설정 실전 가이드

YS
김영삼
조회 407

iptables vs nftables

iptables는 Linux의 전통적인 패킷 필터링 도구이고, nftables는 이를 대체하는 차세대 프레임워크입니다. nftables는 문법이 통일되고, 성능이 향상되었으며, iptables/ip6tables/arptables를 하나로 통합합니다. 최신 배포판(Debian 11+, RHEL 9+)은 nftables를 기본으로 채택하고 있습니다.

iptables 기본 설정

#!/bin/bash
# 기존 규칙 초기화
iptables -F
iptables -X
iptables -Z

# 기본 정책
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 루프백 허용
iptables -A INPUT -i lo -j ACCEPT

# 기존 연결 유지
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# SSH 브루트포스 방어
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW   -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW   -m recent --update --seconds 60 --hitcount 4 --name SSH -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# HTTP/HTTPS
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

# ICMP
iptables -A INPUT -p icmp --icmp-type echo-request -m limit   --limit 1/s --limit-burst 4 -j ACCEPT

# 로깅
iptables -A INPUT -m limit --limit 5/min   -j LOG --log-prefix "IPT_DROP: " --log-level 4

# 규칙 저장
iptables-save > /etc/iptables/rules.v4

nftables 동일 설정

# /etc/nftables.conf
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
    set ssh_bruteforce {
        type ipv4_addr
        flags dynamic,timeout
        timeout 60s
    }

    chain input {
        type filter hook input priority 0; policy drop;

        iif "lo" accept
        ct state established,related accept
        ct state invalid drop

        tcp dport 22 ct state new             add @ssh_bruteforce { ip saddr limit rate 3/minute } accept
        tcp dport 22 ct state new drop

        tcp dport { 80, 443 } accept

        icmp type echo-request limit rate 1/second burst 4 packets accept

        limit rate 5/minute log prefix "NFT_DROP: " level warn
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }

    chain output {
        type filter hook output priority 0; policy accept;
    }
}

iptables vs nftables 비교

항목iptablesnftables
문법명령어 기반선언적 구문
IPv4/IPv6별도 명령어inet으로 통합
성능선형 탐색집합(set) 기반 O(1)
원자적 적용미지원지원
커널 호환3.x+3.13+

실전 DDoS 방어 규칙

# nftables: SYN Flood 방어
table inet ddos_protection {
    chain input {
        type filter hook input priority -10; policy accept;

        tcp flags syn limit rate 25/second burst 50 packets accept
        tcp flags syn drop

        ct count over 100 drop

        tcp flags & (fin|syn|rst|psh|ack|urg) == 0 drop
        tcp flags & (fin|syn) == (fin|syn) drop
        tcp flags & (syn|rst) == (syn|rst) drop
    }
}
  • iptables에서 nftables로 마이그레이션 시 iptables-translate 명령어로 규칙을 변환할 수 있습니다
  • nftables의 set/map은 수천 개의 IP를 O(1)로 매칭하여 대규모 차단에 효율적입니다
  • nft list ruleset으로 현재 규칙을 확인하고 nft -f로 원자적으로 적용합니다
  • SSH 포트를 22가 아닌 다른 포트로 변경하는 것만으로도 무차별 공격의 90%를 줄일 수 있습니다
  • 방화벽 설정 변경 시 반드시 원격 접속이 끊기지 않도록 at 또는 cron으로 복구 스크립트를 예약하세요

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록