Linux/iptables

[Linux] iptables 방화벽 정책 구성

GGkeeper 2021. 11. 13. 04:40

iptables 구조는 table -> chain -> rule의 구조로 이루어져 있다.
table : 방화벽에서 특정 기능을 제공하는 것으로 filter, nat, mangle, raw, security 등이 있다.
chain : 패킷이 이동하는 경로를 나타내는 것으로 각 테이블마다 체인이 존재한다.
rule : 각 chain에 설정하는 방화벽 정책으로 사용자가 지정해서 설정한다.

호스트명 변경 : nmtui명령어를 실행해서 호스트명을 설정한다.
putty로 재접속하면 변경된 호스트명을 볼 수 있다.

# echo $PS1
[\u@\h \W]\$

호스트명을 변경하면 /etc/hosts 파일에 등록해야 한다.
[root@firewall ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   firewall.linuxmaster.net firewall

firewalld : CentOS 7 에서 iptables 를 이용한 방화벽
firewalld 서비스 중지하고 비활성화 시킨다.

[root@firewall ~]# systemctl stop firewalld
[root@firewall ~]# systemctl disable firewalld

[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

iptables-services 패키지를 설치한다.
[root@firewall ~]# yum -y install iptables-services

[root@firewall ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

[root@firewall ~]# systemctl enable iptables 
[root@firewall ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: inactive (dead)


iptables 사용법 : iptables --help
[root@firewall ~]# iptables --help

넷필터 공식 사이트 : https://www.netfilter.org/

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)


각 테이블에 체인이 기본값으로 정의되어 있다.
Built-in Chain : 이미 만들어져 있는 체인
사용자 정의 Chain  : 사용자가 생성하는 체인


# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=41.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=47.3 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=48.3 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 41.593/45.766/48.390/2.988 ms


filter 테이블의 FORWARD 체인의 기본 정책을 ACCEPT -> DROP 으로 변경하기
iptables -t filter -P FORWARD DROP 

fileter 테이블의 INPUT 체인의 기본 정책을 ACCEPT -> DROP 으로 변경하기
iptables -P INPUT DROP

filter 테이블의 OUTPUT 체인의 기본 정책을 ACCEPT -> DROP 으로 변경하기
iptables -P OUTPUT DROP


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

테이블 : filter
정책 : INPUT DROP

-A : 룰을 추가한다.
-A INPUT 룰형식 

ssh (22) : -p tcp --dport 22 ACCEPT
-j ACCEPT : 패킷 허용

[root@firewall ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[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

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

[root@firewall ~]# iptables -nL INPUT

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22


C:\Users\user1>ping 192.168.108.3

Ping 192.168.108.3 32바이트 데이터 사용:
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64

192.168.108.3에 대한 Ping 통계:
    패킷: 보냄 = 4, 받음 = 4, 손실 = 0 (0% 손실),
왕복 시간(밀리초):
    최소 = 0ms, 최대 = 0ms, 평균 = 0ms

[root@firewall ~]# iptables -P INPUT DROP
[root@firewall ~]# iptables -nL

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

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 

C:\Users\user1>ping 192.168.108.3

Ping 192.168.108.3 32바이트 데이터 사용:
요청 시간이 만료되었습니다.
요청 시간이 만료되었습니다.
요청 시간이 만료되었습니다.
요청 시간이 만료되었습니다.

192.168.108.3에 대한 Ping 통계:
    패킷: 보냄 = 4, 받음 = 0, 손실 = 4 (100% 손실),


[root@firewall ~]# iptables -P INPUT ACCEPT

C:\Users\user1>ping 192.168.108.3

Ping 192.168.108.3 32바이트 데이터 사용:
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간<1ms TTL=64
192.168.108.3의 응답: 바이트=32 시간=1ms TTL=64

192.168.108.3에 대한 Ping 통계:
    패킷: 보냄 = 4, 받음 = 4, 손실 = 0 (0% 손실),
왕복 시간(밀리초):
    최소 = 0ms, 최대 = 1ms, 평균 = 0ms


[root@firewall ~]# iptables -A INPUT -s 1.1.1.1 -j ACCEPT
[root@firewall ~]# iptables -A INPUT -s 2.2.2.2 -j DROP
[root@firewall ~]# iptables -nL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  1.1.1.1              0.0.0.0/0
DROP       all  --  2.2.2.2              0.0.0.0/0

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


filter 테이블에서 소스IP주소가 1.1.1.1 이면 허용한다.
[root@firewall ~]# iptables -D INPUT -s 1.1.1.1 -j ACCEPT
[root@firewall ~]# iptables -nL INPUT

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  2.2.2.2              0.0.0.0/0


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

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
   DROP       all  --  2.2.2.2              0.0.0.0/0

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination


filter 테이블에서 룰번호 1을 삭제한다.
[root@firewall ~]# iptables -t filter -D INPUT 1
[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


-F 옵션을 이용해서 룰을 초기화할 때 policy 부분에 DROP 으로 설정되어 있다면 바로 막힌다. (!!! 조심 !!!)
[root@firewall ~]# iptables -F

서버로 들어오는 패킷에서 ssh로 접속하는 패킷을 허용하는 룰을 설정하시오.
ssh(Secure SHell) : 22
[root@firewall ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@firewall ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   47  3416 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 25 packets, 2632 bytes)
 pkts bytes target     prot opt in     out     source               destination


-Z 옵션을 이용해서 패킷 카운터를 초기화 한다.
[root@firewall ~]# iptables -Z
[root@firewall ~]# iptables -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    6   432 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
 pkts bytes target     prot opt in     out     source               destination


방화벽에 설정된 룰을 확인한다.
-t filter 테이블이 생략되면 기본값은 filter 테이블이다.
[root@firewall ~]# iptables -nL   #기본값 filter 테이블

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination