1. iptables 란

- 리눅스의 패킷 필터링(Packet Filtering) 도구로서 방화벽 구성이나 NAT(Network Address Translation)에 사용합니다.

- 패킷 필터링 기능을 사용해 지나가는 패킷의 해더를 보고 그 전체 패킷을 제어할 수 있습니다.

  (패킷은 헤더와 데이터를 가지며, 헤더에 필터링할 정보인 수신지 IP:PORT, 송신지 IP:PORT, checksum, 프로토콜 옵션 등을 가지게 됩니다.)

- 특정 조건을 가지고 있는 패킷에 대해 허용(ACCEPT)과 차단(DROP)등을 지정할 수 있으며, 특정 조건 등을 통해 다양한 방식의 패킷 필터링과 처리 방식을 지원합니다.

- 방화벽은 순차적 실행으로 즉 등록 순서에 있어서 먼저 등록한 부분에 대해서 효력이 유효하기 때문에 등록 시에는 순서가 매우 중요합니다.

- 모든 입출력 패킷에 대해 거부하는 설정이 먼저 등록되면 그 이후에 포트를 열어주는 설정을 하여도 효과가 없습니다.

- 설정 파일의 위치: /etc/sysconfig/iptables 

  (명령어 대신 vi, vim 등을 이용해서 수정해도 됩니다.)

1.1. iptables 설치

- CentOS 7(RHEL 7)부터는 방화벽을 관리하는 데몬이 firewalld로 변경되었습니다. 따라서 iptables 사용을 위해선 firewalld 중단 후 iptables를 설치해서 실행해야 합니다. 

1.1.1. firewalld 중지 및 서비스 마스킹

- iptables 사용을 위해 같은 기능을 하는 firewalld를 중지하고 마스킹을 등록해 충돌을 방지합니다.

- 마스킹할 경우 실수로 실행해도 실행이 되지 않습니다.

1. firewalld를 중지 합니다.
# systemctl stop firewalld

2. firewalld 상태를 확인 합니다.
# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 일 2021-08-22 15:16:04 KST; 3h 43min ago
     Docs: man:firewalld(1)
  Process: 2675 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 2675 (code=exited, status=0/SUCCESS)

 8월 22 15:16:02 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
 8월 22 15:16:04 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

3. 충돌 방지를 위해 firewalld를 마스킹 합니다. (systemctl mask/unmask)
# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.

4. 마스킹 된 firewalld는 실수로 실행을 하게 되더라도 실행이 되지 않습니다.
# systemctl start firewalld
Failed to start firewalld.service: Unit is masked.

1.1.2. iptables 설치

1. yum을 이용해 설치해 줍니다.
# yum install -y iptables-services

2. iptables를 실행해 줍니다.
# systemctl start iptables

3. 재부팅 시에도 자동으로 실행되도록 서비스를 등록해 줍니다.
# systemctl enable iptables​

1.1.3. iptables 데몬 제어 명령어

1. 실행
# systemctl start iptables
# service iptables start

2. 중지
# systemctl stop iptables
# service iptables stop

3. 재시작
# systemctl restart iptables
# service iptablese restart

4. 상태 확인
# systemctl status iptables
# service iptables status

5. 정책 저장(정책을 변경하게 되면 무조건 저장 후 재시작 해야 합니다.)
# service iptables save

1.2. iptables 기본 명령어

# iptables  [-t table] [action] [chain] [matches] [-j target]

1.2.1. [테이블(table)]

- filter, nat, mangle, raw가 있으며 주로 사용하는 필터링 규칙에는 filter 테이블을 사용합니다.

- 생략하게 되면 자동으로 filter로 적용됩니다. 

1.2.2. [액션(action)]

-A(--append) : APPEND : 새로운 정책을 추가합니다.
-I(--insert) : INSERT : 위치를 선택하여 정책을 삽입합니다.
-D(--delete) : DELETE : 정책을 삭제합니다.
-R(--replace) : REPLACE : 정책을 교체합니다.
-F(--flush) : FLUSH : 체인으로부터 모든 정책 삭제합니다.
-P(--policy) : POLICY : 기본 정책을 설정합니다.
-L(--list) : LIST : 정책 목록을 확인합니다.

1.2.3. [체인(chain)]

iptables chain

- INPUT : 호스트(서버) 컴퓨터를 향한 모든 패킷(서버로 들어오는 패킷은 Input chain을 통과)
- OUTPUT : 호스트(서버) 컴퓨터에서 발생하는 모든 패킷(서버에서 나가는 패킷은 output chain을 통과)
- FORWARD : 호스트(서버) 컴퓨터가 목적지가 아닌 모든 패킷, 즉 라우터로 사용되는 호스트 컴퓨터를 통과하는 패킷      (컴퓨터를 지나가는 모든 패킷은 forward chain을 통과)

1.2.4. [매치(matches)]

-s(--source, --src) : 출발지 매칭. 도메인, IP 주소, 넷마스크 값을 이용하여 표기(패킷의 송신지를 제어)
-d(--destination, --dst) : 목적지 매칭. 도메인, IP 주소, 넷마스크 값을 이용하여 표기(패킷의 수신지를 제어)
-p : 프로토콜과 매칭. TCP, UDP, ICMP와 같은 이름을 사용하고 대소문자는 구분하지 않음(프로토콜을 제어)
-i(--in-interface) : 입력 인터페이스와 매칭
-o(--out-interface) : 출력 인터페이스와 매칭
-j(--jump) : 매치되는 패킷을 어떻게 처리할지 지정(방화벽을 지난 후 패킷의 상태를 제어)

-f (--fragment): 분절된 패킷 

--sport: 송신지 포트와 매칭(송신지 포트를 제어, 외부의 서비스하는 것을 제어)
--dport: 수신지 포트와 매칭(수신지 포트를 제어, 자신이 서비스하는 것을 제어)

1.2.5. [타깃( -j target)]

패킷이 규칙과 일치할 때 취하는 동작을 지정합니다.

ACCEPT : 패킷을 허용합니다.
DROP : 패킷을 버립니다.(패킷이 전송된 적이 없던 것처럼)
REJECT : 패킷을 버리고 이와 동시에 적절한 응답 패킷을 전송합니다.
LOG : 패킷을 syslog에 기록합니다.
RETURN : 호출 체인 내에서 패킷 처리를 계속한다.

 


2. iptables 특정 포트 및 서비스 열기 / 닫기

- 22 port(SSH)의 포트를 차단(DROP)하면 원격 접속이 불가능할 수도 있으니 주의해야 합니다. 

2.1. 포트 허용(ACCEPT)

- INPUT Chain ACCEPT에서 설정 시작합니다. (로컬인 경우 DROP상태에서 시작해도 됩니다.)

- 예시를 위해 화이트 리스트 방식(Chain INPUT DROP) 상태에서 포트를 허용합니다.

- 모든 접근을 차단한 상태에서 정책을 등록해서 허용하는 방식입니다.

- 정책에 ACCEPT로 포트/서비스를 등록하지 않으면 외부에서 접근 불가능합니다.

- 화이트 리스트 방식에서 포트 ACCEPT를 삭제할 경우 접근이 불가능하게 됩니다.

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

ssh 포트 허용
# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
http 포트 허용
# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 
https 포트 허용
# iptables -A INPUT -p tcp --dport 443 -j ACCEPT

DNS 포트 허용(UDP)
# iptables -A INPUT -p udp --dport 53 -j ACCEPT

SMTP 포트 허용
# iptables -A INPUT -p tcp --dport 25 -j ACCEPT

INPUT chain 기본 정책 차단으로 변경
# iptables -P INPUT DROP

정책 저장(필수)
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

iptables 재시작(필수)
# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

정책 리스트 확인
# 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
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:25

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 

- Ping(ICMP)도 활용할 수 없게 되는데 필요하다면 ICMP도 등록해 줍니다. (내부 --> 외부, 외부 ---> 내부 불가능)

IPCM 허용
# iptables -A INPUT -p icmp -j ACCEPT

# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

# ping 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=57 time=9.87 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=57 time=8.50 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=57 time=7.38 ms
64 bytes from 1.1.1.1: icmp_seq=4 ttl=57 time=12.5 ms
64 bytes from 1.1.1.1: icmp_seq=5 ttl=57 time=10.8 ms
64 bytes from 1.1.1.1: icmp_seq=6 ttl=57 time=10.1 ms
64 bytes from 1.1.1.1: icmp_seq=7 ttl=57 time=7.26 ms
^C
--- 1.1.1.1 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6166ms
rtt min/avg/max/mdev = 7.263/9.505/12.521/1.771 ms

 

- ★ 위와 같이 설정한 상태로는 외부 서비스(yum, curl, wget 등)를 사용할 수 없습니다. 사용을 원하면 아래와 같이 정책을 추가해 줍니다.

 (--sport를 사용해 외부 서비스를 사용 가능하도록 제어합니다.)

# iptables -A INPUT -p udp --sport 53 -j ACCEPT
# iptables -A INPUT -p tcp --sport 80 -j ACCEPT
# iptables -A INPUT -p tcp --sport 443 -j ACCEPT

# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

# 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
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:25
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp spt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:443

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

2.2. 포트 차단(DROP)

- 예시를 위해 블랙리스트 방식(Chain INPUT ACCEPT) 상태에서 포트를 차단합니다.

- 모든 접근을 허용한 상태에서 차단하는 방식입니다.

- 정책에 DROP으로 포트/서비스를 차단해야 외부에서 접근 불가능합니다.

- 블랙리스트 방식에서 포트 정책 DROP을 삭제할 경우 접근이 가능하게 됩니다.

 

- 위에서 설정한 세팅을 원래대로 변경합니다. (모든 포트를 허용한 상태)

INPUT chain 기본 정책 허용으로 변경
# iptables -P INPUT ACCEPT

실습을 위헤 위에서 설정한 정책을 모두 삭제 합니다.
# iptables -F

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 

- 모든 포트가 열린 상태에서 포트를 차단합니다.

  (SSH (22 port)는 접근을 위해 실습에서도 차단하지 않습니다.)

http 포트 차단
# iptables -A INPUT -p tcp --dport 80 -j DROP
 
https 포트 차단
# iptables -A INPUT -p tcp --dport 443 -j DROP

정책 저장(필수)
# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

iptables 재시작(필수)
# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 

- 80 port를 차단해서 접근이 안 되는 것을 확인할 수 있습니다.

2.3. 정책 삭제(-D)

- 정책등록하는 방법과 마찬가지로 명령어에서 -A(--append)를 -D(delete)로 변경하면 삭제가 가능합니다. 

    iptables -A INPUT -p tcp --dport 80 -j DROP
    → iptables -D INPUT -p tcp --dport 80 -j DROP

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

등록한 정책 삭제
# iptables -D INPUT -p tcp --dport 80 -j DROP
# iptables -D INPUT -p tcp --dport 443 -j DROP

# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 

 

자료 출처 : https://veneas.tistory.com/entry/Linux-iptables-%ED%8A%B9%EC%A0%95-%ED%8F%AC%ED%8A%B8-%EC%97%B4%EA%B8%B0-%EB%8B%AB%EA%B8%B0-iptables-%EC%82%AC%EC%9A%A9%EB%B2%95#1._iptables_%EB%9E%80

복사했습니다!