Linux/iptables

[Linux] iptables 상태 추적 모듈 사용 & 방화벽 룰 실습

GGkeeper 2021. 11. 13. 05:56

Client -------> HTTP(80)

실습> 내부망의 서버를 상태 추적을 위한 룰 설정하기

[root@firewall ~]# iptables -A FORWARD -p tcp -m multiport --port 80 -m state --state NEW -j ACCEPT
[root@firewall ~]# iptables -A FORWARD -j DROP
[root@firewall ~]# iptables -I FORWARD 2 -p tcp -m multiport --port 80 -m state --state ESTABLISHED -j ACCEPT
[root@firewall ~]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport ports 80 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport ports 80 state ESTABLISHED
DROP       all  --  0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


실습> 방화벽의 상태 추적을 위한 룰 설정하기

상태 추적
-m state : 패킷의 상태를 파악하는 모듈
형식 : -m state --state 상태
--state NEW : 새로운 연결 패킷
--state ESTABLISHED : 연결된 상태의 패킷
--state INVALID : 깨진 패킷

[root@firewall ~]# iptables -F
[root@firewall ~]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


[root@firewall ~]# systemctl start httpd
[root@firewall ~]# echo '192.168.108.3 Server!' > /var/www/html/index.html

http://192.168.108.3
192.168.108.3 Server!


SSH 원격 접속을 상태 추적으로 등록한다.
[root@firewall ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
[root@firewall ~]# iptables -A INPUT -p tcp --dport 22 -m state --state ESTABLISHED -j ACCEPT
[root@firewall ~]# iptables -A INPUT -j DROP
[root@firewall ~]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state ESTABLISHED
DROP       all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


상태 추척 모듈을 이용해서 웹서버의 80번 포트를 허용한다.
[root@firewall ~]# iptables -I INPUT -p tcp --dport 80 -m state --state ESTABLISHED -j ACCEPT
[root@firewall ~]# iptables -I INPUT 2 -p tcp --dport 80 -m state --state NEW -j ACCEPT
[root@firewall ~]# iptables -nL INPUT --line

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 state ESTABLISHED
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 state NEW
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state NEW
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state ESTABLISHED
5    DROP       all  --  0.0.0.0/0            0.0.0.0/0


웹서버로 접속할 때 아래처럼 잘 나오면 성공이다.
http://192.168.108.3
192.168.108.3 Server!

설정한 웹서버의 상태추적 룰을 삭제한다.
[root@firewall ~]# iptables -D INPUT 1
[root@firewall ~]# iptables -D INPUT 1
[root@firewall ~]# iptables -nL INPUT --line

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state NEW
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state ESTABLISHED
3    DROP       all  --  0.0.0.0/0            0.0.0.0/0


웹서버로 접속할 때 아래처럼 접속이 안되면 성공이다.
http://192.168.108.3

사이트에 연결할 수 없음192.168.108.3에서 응답하는 데 시간이 너무 오래 걸립니다.
다음 방법을 시도해 보세요.

연결 확인
프록시 및 방화벽 확인
Windows 네트워크 진단 프로그램 실행
ERR_CONNECTION_TIMED_OUT


실습> 방화벽 룰 타이핑하면서 익히기

[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -m state --state ESTABLISHED -j ACCEPT
[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -m state --state ESTABLISHED -j ACCEPT
[root@firewall ~]# iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
[root@firewall ~]# iptables -nL INPUT --line

실습> 아래 조건에 해당하는 방화벽 룰을 설정하시오.

-- 조건 --
1. filter 테이블
- INPUT 체인의 룰은 아래와 같다.
[root@firewall ~]# iptables -nL

target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            state INVALID
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp multiport ports 80,443,22,53
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            state NEW udp dpt:53
DROP       all  --  0.0.0.0/0            0.0.0.0/0


정답 :
iptables -F
iptables -A INPUT -p tcp -m state --state INVALID -j DROP
iptables -A INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m multiport --port 80,443,22,53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -m state --state NEW -j ACCEPT
iptables -A INPUT -j DROP


- OUTPUT 체인의 룰을 아래와 같다.

비정상적인 깨진 패킷은 모두 거부한다.
연결된 패킷은 모두 허용한다.
HTTP, HTTPS, SSH, DNS 요청 패킷은 모두 허용한다. 
DNS 요청, 응답은 허용한다.
나머지 패킷은 모두 거부한다.

정답 : 
iptables -A OUTPUT -p tcp -m state --state INVALID -j DROP
iptables -A OUTPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p tcp -m state --state NEW -m multiport --port 80,443,22,53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -j DROP



2. nat 테이블
- PREROUTING
DNAT 설정을 하시오.

외부망에서 192.168.108.101로 요청하면 내부망 서버인 192.168.102.101로 돌려주는 DNAT 설정을 한다.
외부망에서 192.168.108.102로 요청하면 내부망 서버인 192.168.102.102로 돌려주는 DNAT 설정을 한다.

정답 : 
iptables -t nat -A PREROUTING -d 192.168.108.101 -j DNAT --to 192.168.102.101 -m comment --comment "108.101 -> 102.101" 
iptables -t nat -A PREROUTING -d 192.168.108.102 -j DNAT --to 192.168.102.102


- POSTROUTING
SNAT 설정을 하시오.

내부망에서 192.168.102.101이 외부망 IP주소인 192.168.108.101로 돌려주는 SNAT 설정을 한다.
내부망에서 192.168.102.102가 외부망 IP주소인 192.168.108.102로 돌려주는 SNAT 설정을 한다.

정답 :
iptables -t nat -A POSTROUTING -s 192.168.102.101 -j SNAT --to 192.168.108.101 
iptables -t nat -A POSTROUTING -s 192.168.102.102 -j SNAT --to 192.168.108.102

[root@firewall ~]# iptables -t nat -nL

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DNAT       all  --  0.0.0.0/0            192.168.108.101      /* 108.101 -> 102.101 */ to:192.168.102.101
DNAT       all  --  0.0.0.0/0            192.168.108.102      to:192.168.102.102

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
SNAT       all  --  192.168.102.101      0.0.0.0/0            to:192.168.108.101
SNAT       all  --  192.168.102.102      0.0.0.0/0            to:192.168.108.102


[root@firewall ~]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            state INVALID
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW multiport ports 80,443,22,53
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53 state NEW
DROP       all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            state INVALID
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW multiport ports 80,443,22,53
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
DROP       all  --  0.0.0.0/0            0.0.0.0/0


[root@firewall ~]# iptables-save > /etc/sysconfig/iptables