IPTABLES
๊ฐ์
iptables ๋ช ๋ น์ด๋ก ๋ฆฌ๋ ์ค ์๋ฒ IN/OUT ํจํท์ ํํฐ๋ง ํ๊ฑฐ๋ NAT ์ค์ ์ ํ ์ ์๋ค.ํน์ ์กฐ๊ฑด์ ํตํด ๋ค์ํ ๋ฐฉ์์ ํจํท ํํฐ๋ง๊ณผ ์ฒ๋ฆฌ ๋ฐฉ์์ ์ง์ ์์ฐจ์ ์คํ์ผ๋ก ์ ์ฑ Top-Down ๋ฐฉ์์ผ๋ก ์ ์ฑ ์ด ์ ํจํ๋ค.์ค์ ํ์ผ ์์น : /etc/sysconfig/iptables

iptables ์๋น์ค ์ค์น
RHEL/CentOS 7๋ถํฐ๋ ๋ฐฉํ๋ฒฝ์ firewalld ๋ผ๋ ๋ฐ๋ชฌ์ผ๋ก ๊ต์ฒดํ๊ณ ํ๋ก๊ทธ๋จ์ iptables ๋ช ๋ น์ด ๋์ CLI์์๋ firewall-cmd, GUI์์๋ firewall-config๋ฅผ ์ฌ์ฉํ๊ฒ ๋์๋ค.
1. firewalld ์๋น์ค ์ค๋จ ํ mask
$ systemctl stop firewalld
$ systemctl status firewalld
$ systemctl mask firewalld
2. iptables ์ค์น ํ ์คํ
$ yum install -y iptables-services
$ systemctl start iptables
$ systemctl enable iptablesiptables ๋ฐ๋ชฌ ์ ์ด ๋ช
๋ น์ด
1. ์คํ/์ค์ง/์ฌ์์/์ํํ์ธ
$ systemctl start iptables
$ systemctl stop iptables
$ systemctl restart iptables
$ systemctl status iptables
2. ์ ์ฑ
์ ์ฅ
$ service iptables save์ฌ์ฉ๋ฒ
iptables [-t table] [action] [chain] [matches] -j [target]table
filter - iptables์ ๊ธฐ๋ณธ ํ ์ด๋ธ๋ก ํจํท ํํฐ๋ง
nat - IP ์ฃผ์ ๋ณํ
mangle - ํจํท ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๋ ํน์ ๊ท์น์ ์ค์ ํ๊ฑฐ๋ ์ฑ๋ฅ ํฅ์์ ์ํ ToS ์ค์ raw - netfilter์ ์ฐ๊ฒฐ ์ถ์ ํ์ ์์คํ ๊ณผ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ํด์ผ ํ๋ ๊ท์น์ ์ค์
$ iptables -t nat -nvL # nat ํ
์ด๋ธ ํ์ธ
Chain PREROUTING (policy ACCEPT 2450 packets, 193K bytes)
pkts bytes target prot opt in out source destination
1190 61896 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 1508 packets, 105K bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 22608 packets, 2234K bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 22608 packets, 2234K bytes)
pkts bytes target prot opt in out source destination
16 1054 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0๋ช ์ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก filter
action
-A (--append) : ์๋ก์ด ์ ์ฑ ์ ์ถ๊ฐ
-I (--insert) : ์์น๋ฅผ ์ ํํ์ฌ ์ ์ฑ ์ ์ฝ์ # iptables -I INPUT 2 (2ํ์ ์ถ๊ฐ)
-D (--delete) : ์ ์ฑ ์ ์ญ์
-R (--replace) : ์ ์ฑ ์ ๊ต์ฒด
-F (--flush) : ์ฒด์ธ์ผ๋ก๋ถํฐ ๋ชจ๋ ์ ์ฑ ์ญ์
-P (--policy) : ๊ธฐ๋ณธ ์ ์ฑ ์ ์ค์
-L (--list) : ์ ์ฑ ๋ชฉ๋ก์ ํ์ธ
chain

INPUT - ์๋ฒ๋ก ๋ค์ด์ค๋ ํจํท์ INPUT ์ฒด์ธ์ ํต๊ณผ
OUTPUT - ์๋ฒ์์ ๋๊ฐ๋ ํจํท์ OUTPUT ์ฒด์ธ์ ํต๊ณผ
FORWARD - ์๋ฒ๊ฐ ๋ชฉ์ ์ง๊ฐ ์๋ ๋ชจ๋ ํจํท์ FORWARD ์ฒด์ธ์ ํต๊ณผ
match
๋งค์น ์ต์ ์ ํตํด์ ์ธ๋ถ์ ์ธ ํจํท์ ์ง์ ํ ์ ์์
-s (--source, --src) : ์ถ๋ฐ์ง, ๋๋ฉ์ธ or ip or ๋ท๋ง์คํฌ ๊ฐ
--sport : ์ถ๋ฐ์ง ํฌํธ
-d (--destination, --dst) : ๋ชฉ์ ์ง , ๋๋ฉ์ธ or, ip or ๋ท๋ง์คํฌ ๊ฐ
--dport : ๋ชฉ์ ์ง ํฌํธ -p : ํ๋กํ ์ฝ TCP, UDP, ICMP์ ๊ฐ์ IP ํ๋กํ ์ฝ
-i (--in-interface) : ์ ๋ ฅ ์ธํฐํ์ด์ค
-o (--out-interface) : ์ถ๋ ฅ ์ธํฐํ์ด์ค
-m (--match) : ํน์ ๋ชจ๋ ์ฌ์ฉ (๋ณดํต state ๋ชจ๋ ์ฌ์ฉ)
--state : ์ฐ๊ฒฐ ์ํ (NEW, ESTABLISHED, RELATED, INVALID)
--line : ์ค๋ฒํธ ์ถ๋ ฅ -j (--jump) : ๊ท์น์ ๋ง๋ ํจํท์ ํ๋
target
์กฐ๊ฑด์ ๋งค์นญ๋๋ ํจํท์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง ์ง์ ํ๋ ๋ถ๋ถ
ACCEPT : ๋งค์นญ๋๋ ํจํท ํ์ฉ
DROP : ๋งค์นญ๋๋ ํจํท ๊ฑฐ๋ถ
LOG : ๋งค์นญ๋๋ ํจํท ๋ก๊ทธ ์ ์ฅ
REJECT : ๋งค์นญ๋๋ ํจํท ๊ฑฐ๋ถ (TCP-RST, UDP-ICMP Port Unreachable)
RETURN : ์ฒด์ธ ๋ด์์ ํจํท ์ฒ๋ฆฌ๋ฅผ ๊ณ์ํจ (์ฌ๊ท)
๋ฐฑ์
, ๋ณต์
๊ท์น ์ ์ฅ
$ iptables-save > /root/firewall.rules
๊ท์น ๋ณต์
$ iptables-restore < /root/firewall.rules์ฐ๊ฒฐ ์ถ์ (Connection Tracking)
iptables๋ ์ฐ๊ฒฐ ์ถ์ (Connection Tracking)์ด๋ผ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ ๋ด๋ถ ๋คํธ์ํฌ ์ ์๋น์ค ์ฐ๊ฒฐ ์ํ์ ๋ฐ๋ผ์ ๊ทธ ์ฐ๊ฒฐ์ ๊ฐ์ํ๊ณ ์ ํํ ์ ์๊ฒ ํด์ค๋ค.
์ฐ๊ฒฐ ์ถ์ ๋ฐฉ์์ ์ฐ๊ฒฐ ์ํ๋ฅผ ํ์ ์ ์ฅํ๊ธฐ ๋๋ฌธ์, ๋ค์๊ณผ ๊ฐ์ ์ฐ๊ฒฐ ์ํ์์ ๋ฐ๋ผ์ ์์คํ ๊ด๋ฆฌ์๊ฐ ๊ฒฐ์ ํ์ฉํ๊ฑฐ๋ ๊ฑฐ๋ถํ ์ ์๋ค.
NEW - ์๋ก์ด ์ฐ๊ฒฐ์ ์์ฒญํ๋ ํจํท
ESTABLISHED - ๊ธฐ์กด ์ฐ๊ฒฐ์ ์ผ๋ถ์ธ ํจํท
RELATED - ๊ธฐ์กด ์ฐ๊ฒฐ์ ์ํ์ง๋ง ์๋ก์ด ์ฐ๊ฒฐ์ ์์ฒญํ๋ ํจํท
INVALID - ์ฐ๊ฒฐ ์ถ์ ํ์์ ์ด๋ ์ฐ๊ฒฐ์๋ ์ํ์ง ์์ ํจํท
์ํ์ ๊ธฐ๋ฐ(Stateful)ํ iptables ์ฐ๊ฒฐ ์ถ์ ๊ธฐ๋ฅ์ ์ด๋ ๋คํธ์ํฌ ํ๋กํ ์ฝ์์๋ ์ฌ์ฉ ๊ฐ๋ฅ, UDP์ ๊ฐ์ด ์ํ๋ฅผ ์ ์ฅํ์ง ์๋(Stateless) ํ๋กํ ์ฝ์์๋ ์ฌ์ฉํ ์ ์๋ค.
$ iptables -I INPUT 1 -t tcp --dport 22 -j ACCEPT
$ iptables -I OUTPUT 1 -m state --state ESTABLISHED -j ACCEPT์ ์์ ์ ๊ฐ์ด INPUT์์ SSH ํ์ฉ ํ OUTPUT์์ ESTABLISHED๋ฅผ ํ์ฉํด์ฃผ๋ฉด
OUTPUT์์ ๋ค๋ฅธ ์ ์ฑ ์ถ๊ฐ ์์ด Client๋ SSH ์ ์์ด ๊ฐ๋ฅํ๋ค.
์์
์๋ฒ๋ก ๋ค์ด์ค๋ TCP 22(SSH) ACCEPT ์ ์ฑ
์ 1ํ์ ์ถ๊ฐ
$ iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT
์๋ฒ๋ก ๋ค์ด์ค๋ UDP 53(DNS) DROP ์ ์ฑ
์ 1ํ์ ์ถ๊ฐ
$ iptables -I INPUT 1 -p udp --dport 53 -j DROP
์๋ฒ๋ก ๋ค์ด์ค๋ ์๋ก์ด ํต์ ์ด SYN ํจํท์ด ์๋ ๊ฒฝ์ฐ DROP ์ ์ฑ
์ 1ํ์ ์ถ๊ฐ
$ iptables -I INPUT 1 -p tcp ! --syn -m state --state NEW -j DROP
์๋ฒ์์ ๋๊ฐ๋ ์๋ก์ด ํต์ ์ ACCEPT ์ ์ฑ
์ 1ํ์ ์ถ๊ฐ
$ iptables -I OUTPUT 1 -m state --state NEW -j ACCEPT
์กฐ๊ฐ๋ ํจํท ์ฐจ๋จ
$ iptables -A INPUT -f -j DROPLast updated