[Linux] 리눅스 방화벽 도구(3) – nftables

출처 : https://cryptsus.com/blog/setting-up-nftables-firewall.html
이 글은 RHEL 9 Getting started with nftables를 참고해서 작성하였습니다.
- ipset 및 iptables-nft 패키지는 Red Hat Enterprise Linux 9에서 더 이상 사용되지 않습니다. 여기에는 iptables, ip6tables, arptables 및 ebtables 유틸리티와 같은 nft 변형의 더 이상 사용되지 않습니다. 예를 들어, 이전 RHEL 버전에서 업그레이드했기 때문에 이러한 도구 중 하나를 사용하는 경우 Red Hat은 nftables 패키지에서 제공하는 nft 명령줄 도구로 마이그레이션할 것을 권장합니다.
- 다양한 방화벽 관련 서비스(firewalld, nftables 또는 iptables)가 서로 영향을 주지 않도록 하려면 RHEL 호스트에서 해당 서비스 중 하나만 실행하고 다른 서비스는 비활성화하세요.
다음은 방화벽 도구(firewalld, nftables, iptables) 중 하나를 사용해야 하는 시나리오에 대한 간략한 개요입니다.
- iptables:
Red Hat Enterprise Linux의 iptables 유틸리티는 레거시 백엔드 대신 nf_tables 커널 API를 사용합니다. nf_tables API는 iptables 명령을 사용하는 스크립트가 Red Hat Enterprise Linux에서 계속 작동할 수 있도록 이전 버전과의 호환성을 제공합니다. 새로운 방화벽 스크립트의 경우 Red Hat은 nftables를 사용할 것을 권장합니다. - Firewalld:
간단한 방화벽 사용 사례에는 Firewalld 유틸리티를 사용합니다. 이 유틸리티는 사용하기 쉽고 이러한 시나리오의 일반적인 사용 사례를 다룹니다. - nftables:
nftables 유틸리티를 사용하여 전체 네트워크와 같이 복잡하고 성능이 중요한 방화벽을 설정합니다.
1. 등록된 iptables와 ip6tables 정책을 nftables 정책으로 전환하기
- iptables와 ip6tables 정책을 파일로 저장합니다.
# iptables-save >/root/iptables.dump # ip6tables-save >/root/ip6tables.dump
- dump 파일을 nftables 정책 파일로 전환합니다.
iptables-restore-translate -f /root/iptables.dump > /etc/nftables/ruleset-migrated-from-iptables.nft ip6tables-restore-translate -f /root/ip6tables.dump > /etc/nftables/ruleset-migrated-from-ip6tables.nft
- nftables의 환경설정 파일(/etc/sysconfig/nftables.conf)에 등록합니다. 시스템 부팅 시 nftables 정책들이 자동으로 로드됩니다.
include "/etc/nftables/ruleset-migrated-from-iptables.nft" include "/etc/nftables/ruleset-migrated-from-ip6tables.nft"
- iptables 서비스를 중지키시고, nftables 서비스를 시작합니다.
# systemctl disable --now iptables # systemctl enable --now nftables
2. 신규 iptables와 ip6tables 정책을 대응하는 nftables 정책으로 표시하기
- nftables 패키지에 포함되어 있는 iptables-translate나 ip6tables-translate 명령어를 사용합니다.
# iptables-translate
# iptables-translate -A INPUT -p tcp -s 192.168.56.0/24 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp ip saddr 192.168.56.0/24 counter accept # iptables-translate -A INPUT -j CHECKSUM --checksum-fill nft # -A INPUT -j CHECKSUM --checksum-fill
3. 방화벽 정책 출력 명령어
Method | iptables | nftables | firewalld |
---|---|---|---|
Listing all rules | iptables-save | nft list ruleset | firewall-cmd –-list-all-zones |
Listing a filter table | iptables -L | nft list table ip filter | firewall-cmd –-zone=<zone-name> –-list-all |
Listing INPUT chain | iptables -L INPUT | nft list chain ip filter INPUT | |
Listing prerouting chain of nat table | iptables -t nat -L PREROUTING | nft list chain ip nat PREROUTING |
4. nftables 스크립트 파일 작성하기
- nftables 프레임워크 사용의 주요 이점은 스크립트 실행이 원자성(atomic)이라는 것입니다. 이는 시스템이 전체 스크립트를 적용하거나 오류가 발생하면 실행을 방지한다는 것을 의미합니다. 이는 방화벽이 항상 일관된 상태를 유지하도록 보장합니다.
- nftables 패키지를 설치하면 RHEL는 /etc/nftables/ 디렉토리에 *.nft 스크립트를 자동으로 생성합니다. 이러한 스크립트에는 다양한 목적으로 테이블과 빈 체인을 생성하는 명령이 포함되어 있습니다.
- 스크립트 포맷1: nft list ruleset 명령과 동일한 형식으로 rule set이 표시됩니다.
#!/usr/sbin/nft -f # Flush the rule set flush ruleset table inet example_table { chain example_chain { # Chain for incoming packets that drops all packets that # are not explicitly allowed by any rule in this chain type filter hook input priority 0; policy drop; # Accept connections to port 22 (ssh) tcp dport ssh accept } }
- 스크립트 포맷2: nft 명령과 동일한 구문을 사용합니다.
#!/usr/sbin/nft -f # Flush the rule set flush ruleset # Create a table add table inet example_table # Create a chain for incoming packets that drops all packets # that are not explicitly allowed by any rule in this chain add chain inet example_table example_chain { type filter hook input priority 0 ; policy drop ; } # Add a rule that accepts connections to port 22 (ssh) add rule inet example_table example_chain tcp dport ssh accept # Using variables in nftables script define INET_DEV = enp1s0 define DNS_SERVERS = { 192.0.2.1, 192.0.2.2 } # Add rules using variables add rule inet example_table example_chain iifname $INET_DEV tcp dport ssh accept add rule inet example_table example_chain ip daddr $DNS_SERVERS accept
5. nftables 스크립트 파일 실행하기
- nft 명령어를 사용하기
# nft -f /etc/nftables/<example_firewall_script>.nft
- nftables 스크립트 파일을 직접 실행하기: nftables 스크립트 파일은 “#!/usr/sbin/nft -f” shebang으로 시작해야 합니다.
# chown root /etc/nftables/<example_firewall_script>.nft # chmod u+x /etc/nftables/<example_firewall_script>.nft # /etc/nftables/<example_firewall_script>.nft
6. 부팅할 때 자동으로 nftables 정책을 로딩하기
- nftables 스크립트 파일들은 /etc/nftables/ 디렉터리에 저장되어 있어야 합니다.
- /etc/sysconfig/nftables.conf 파일을 편집해서 nftables 스크립트 파일들 포함시킵니다.
# vi /etc/sysconfig/nftables.conf include "/etc/nftables/example.nft"
- nftables 서비스를 활성화시킵니다.
# systemctl enable --now nftables.service
7. nftables의 테이블, 체인, 룰을 만드는 방법
- 테이블을 만들 때는 다음 address family 중에 하나를 설정해야 합니다.
- ip: IPv4 패킷만 일치합니다. address family을 지정하지 않은 경우 이것이 기본값입니다.
- ip6: IPv6 패킷만 일치합니다.
- inet: IPv4 및 IPv6 패킷을 모두 일치시킵니다.
- arp: IPv4 ARP 패킷과 일치합니다.
- bridge: 브리지 장치를 통과하는 패킷을 일치시킵니다.
- netdev: 수신 패킷을 일치시킵니다.
- 테이블 만드는 방법
- native syntax
table <table_address_family> <table_name> { }
- shell script
nft add table <table_address_family> <table_name>
- native syntax
- 체인을 만들려면 chain type과 chain priority를 지정해야 합니다.
Chain types Type Address families Hooks Description filter all all Standard chain type nat ip, ip6, inet prerouting, input, output, postrouting 이 유형의 체인은 연결 추적 항목을 기반으로 기본 주소 변환을 수행합니다. 첫 번째 패킷만 이 체인 유형을 통과합니다. route ip, ip6 output 이 체인 유형을 통과하는 허용된 패킷은 IP 헤더의 관련 부분이 변경된 경우 새로운 경로 조회를 발생시킵니다. Chain priorities Textual value Numeric value Address families Hooks raw -300 ip, ip6, inet all mangle -150 ip, ip6, inet all. dstnat -100 ip, ip6, inet prerouting dstnat -300 bridge prerouting filter 0 ip, ip6, inet, arp, netdev all filter -200 bridge all security 50 ip, ip6, inet all srcnat 100 ip, ip6, inet postrouting srcnat 300 bridge postrouting out 100 bridge output - 체인 만드는 방법: Chain policy는 accept (default)나 drop를 지정
- native syntax
table <table_address_family> <table_name> { chain <chain_name> { type <type> hook <hook> priority <priority> policy <policy> ; } }
- shell script
nft add chain <table_address_family> <table_name> <chain_name> { type <type> hook <hook> priority <priority> \; policy <policy> \; }
- native syntax
- 룰 만드는 방법
- native syntax
table <table_address_family> <table_name> { chain <chain_name> { type <type> hook <hook> priority <priority> ; policy <policy> ; <rule> } }
- shell script
nft add rule <table_address_family> <table_name> <chain_name> <rule>
- native syntax
nftables를 사용하여 다양한 방화벽 정책을 등록할 수 있습니다. 예제를 사용한 설명은 너무 방대해서 구글링을 통해 필요한 정책을 찾아 등록하길 권장합니다.