linux class 1 페이지

본문 바로가기
사이트 내 전체검색


회원로그인

linux class

리눅스 방화벽(iptables)

페이지 정보

작성자 admin 작성일15-12-29 16:48 조회326회 댓글2건

본문

iptables - administration tool for IPv4 packet filtering and NAT

SYNOPSIS
iptables [-t table] -A chain rule-specification [options] ; 선택한 chain 맨 아래쪽에 한개의상의 rule 추가
iptables [-t table] -I chain [rulenum] rule-specification [options] ; 선택된 chain에 한개 이상의 룰 추가
iptables [-t table] -R chain rulenum rule-specification [options] ; 선택된 chain 으로 부터 rule 변경
iptables [-t table] -D chain rulenum [options] ; 선택된 chain으로 부터 한개의상의 rule 삭제
iptables [-t table] -[LFZ] [chain] [options]
iptables [-t table] -N chain ; chain 생성
iptables [-t table] -X [chain] ; chain 삭제
iptables [-t table] -P chain target [options] ; target 에 대한 chain 정책 설정
iptables [-t table] -E old-chain-name new-chain-name ; chain 이름 변경
iptables [-t table ] -F chain ; 선택된 chain 삭제, -F 옵션뒤에 chain 을 명시하지 않으면 모든 체인 삭제.
(This is equivalent to deleting all the rules one by one.)

DESCRIPTION
Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. Several different tables
may be defined. Each table contains a number of built-in chains and may also contain user-defined chains.

Each chain is a list of rules which can match a set of packets. Each rule specifies what to do with a packet that matches. This is
called a 'target' which may be a jump to a user-defined chain in the same table.

iptables chain 종류

INPUT : 들어오는 패킷에 대한 설정을 위한 chain
OUTPUT : 나가는 패킷에 대한 설정을 위한 chain
FORWARD : 경유하는 패킷에 대한 설정을 위한 chain
이 세개의 기본체인은 수정이나 삭제가 불가.

기타.
RH-Firewall-1-INPUT : 사용자 정의 체인

iptables 사용방법

기본방화벽 정책

ex)
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

REJECT 와 DROP 의 의미
REJECT : 서비스에 접속하려는 사용자의 엑세스를 거부하고 connection refuesed 라는
오류 메시지늘 보여준다.
DROP : 어떠한 경고 메세지도 보여주지 않은 체 패킷을 drop 한다.


*. 방화벽 설정 리스트 출력 예.

Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere

Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:smtp
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
[root@centos1 tmp]#

*.
esp
This module matches the SPIs in ESP header of IPsec packets.

ah
This module matches the SPIs in Authentication header of IPsec packets.

INPUT, OUTPUT, FORWARD chain 을 제외한 나머지 모든 체인은 사용자
정의 체인으로서 사용자가 마음대로 만들고 삭제할 수 있는 체인이다.
그러나 기본체인에 포함(include) 되지 않으면 적용되지 않는다.

NEW- 새로운 연결을 요청하는 패킷,
established - 기존 연결의 일부인 패킷.
related - 기존 연결에 속하지만 새로운 연결을 요청하는 패킷, 예를 들면 접속포트가 20인
수동 ftp 의 경우 전송포트는 사용되지 않은 1024 이상의 어느포트라로 사용가능하다.

기존 rule 에 새로운 rule 을 넣으려면 -I 옵션 다음에 rule 번호를 사용하면 된다.

iptables -I INPUT 1 -i lo -p all -j ACCEPT

ex)
iptables -A INPUT -j DROP => 입력되는 모든 패킷을 버림.

-A : 룰을 추가한다.
INPUT : 입력 패킷
-j : 패킷허용여부
REJECT : 서비스에 접속하려는 사용자의 엑세스를 거부하고 connection refuesed 라는
오류 메시지를 보여준다.
DROP : 어떠한 경고 메세지도 보여주지 않은 채 패킷을 drop 한다.

specifying source and destination address
source : -s, --source, --src
destination: -d, --destitnation, --dst

specifying protocol
-p, --protocol

specifying an interface

-i, --in-interface
-o, --out-interface



parameters

-p, --protocol [!] protocol: 체크할 패킷 또는 룰에 대한 프로토콜
-s, --source [!] address[/mask] :
-d, --destination [!] address[/mask]
-j, --jump target

ex) -s ! 192.168.100.1 => source address가 192.168.100.1 이 아닌 주소.
* ! 은 부정(not)을 뜻함.

ex)
iptables -A INPUT -p tcp -j ACCEPT


입력 프로토콜중 tcp 프로토콜은 모두 허용

포트제어에 대한 옵션은
--sport , --dport
--sport : 소스패킷 포트
--dport : 타겟패킷 포트

ex) iptables -A INPUT -p tcp --dport 80 -j DROP

서비스 포트번호 대신 서비스 이름을 사용하여도 된다.
ex) --dport 80 => --dport http ; 서비스 이름은 /etc/services 파일에 등록되어 있는 이름이어야 한다.

ex)iptable 설정을 위한 기본 예.

iptables -A INPUT -p tcp --dport 23 -j REJECT
iptables -A INPUT -p tcp --dport 21 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -s 192.168.10.1 -p tcp --dport 23 -j ACCEPT ; telnet 서비스 허용
iptables -A INPUT -s 192.168.10.1 -d 192.168.10.3 -p tcp --dport 21 -j DROP ; ftp 서비스 거부
iptables -A INPUT -s 192.168.10.1 -p icmp --icmp-type echo-request -j DROP ; icmp request 거부
[root@linux101 /root]# iptables -L

여러포트를 동시에 지정하는 경우
--dport 1024 : 65535 1024~65535 번호까지.

*. 인터페이스 지정

-i (input interface) , -o (output interface)로 지정한다.

iptables -A INPUT -i eth0 -p tcp --dport(80) -j DROP ; eth0 장치로 입력되는 패킷중 destination port 주소가
80번 포트인 경우 차단.

*.랜카드가 한개뿐이라면 디바이스를 따로 명시할 필요가 없다.

command line 에서 설정한 iptables rule 은 방화벽이 새로 시작되면
방화벽 설정파일 내용대로 설정된다.
현재 설정 rule을 영구적으로 유지하고 싶으면 iptables-save 명령을 입력하면
설정파일이 현재 설정 내용으로 교체된다.

ip table 상태를 모니터링 하려면 iptstate 를 사용하면 된다.

ex)
# iptstate
IPTables - State Top
Version: 1.4 Sort: SrcIP s to change sorting
Source Destination Proto State TTL
172.16.0.1:514 172.16.0.101:514 udp 0:00:05
192.168.100.1:49494 192.168.100.3:22 tcp ESTABLISHED 119:59:59
192.168.100.1:138 192.168.100.255:138 udp 0:00:08
192.168.100.7:138 192.168.100.255:138 udp 0:00:29
192.168.100.7:137 192.168.100.255:137 udp 0:00:28

*. iptables 시작 및 종료

iptables start : /etc/init.d/ipstables start
iptables stop : /etc/init.d/iptables stop

firewall 초기화
iptables -F ; 모든 rule 설정 제거
iptables -X ; 사용자 정의 체인 삭제
iptables -Z ; rule 설정의 누적 패킷수를 0으로 초기화

기본 정책 설정

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

사용자 정의 chain 생성 및 추가
iptables -N 사용자 정의 chain명
iptables -A INPUT -j 사용자 정의 chain 명 : input chain 에 추가하는 경우

rule 설정 예
설정
iptables -A INPUT -s 127.0.0.1 -p icmp -j DROP

제거
iptables -D INPUT 1
iptables -D INPUT -s 127.0.0.1 -p icmp -j DROP


*. iptables 주요옵션
iptables -F : 방화벽 설정해제
iptables -L : 방화벽 설정보기

조건 옵션
-p proto ; protocol 지정 tcp,udp,icmp 등 (all 은 모든 프로토콜을 의미)
-i device ; 들어오는 패킷장치를 지정. ex) -i eth1
-o device ; 나가는 패킷장치를 지정. ex) -o eth1
-s ip address ; source ip 주소 지정
-d ip address ; destination ip 주소 지정
-s port num ; 패킷 소스 포트 번호지정
-d port num ; 패킷 목적지 포트 번호 지정

*. 조건 앞에 '!' 를 붙이면 not 을 의미한다.
ex) -p !tcp ; tcp 프로토콜을 제외한 나머지 프로토콜.
-i !eth0 ; eth0 장치를 제외한 나머지 장치

- 기타설정
*. mac 주소 지정

-m mac ; mac 주소로부터의 패킷

ex)
iptables -A input -m mac --mac-source 00:11:22:AB:CD:EF -j REJECT
iptables -A input -m mac --mac-source ! 00:22:33:AB:CD:EF -j REJECT


==================================================
아래는 설정 예제입니다.

루프백 접속 허용

iptables -A INPUT -i lo -j ACCEPT ; lo 로 입력되는 모든 패킷 허용
iptables -A OUTPUT -o lo -j ACCEPT ; lo 로 출력되는 모든패킷 허용

① DNS 포트 허용
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
*. OUTPUT 설정인 경우는
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT



② ICMP 핑 허용
iptables -A OUTPUT -o eth0 -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -i eth0 -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -o eth0 -p icmp --icmp-type echo-reply -j ACCEPT


③ SSH 포트 허용
iptables -A INPUT -s 192.168.10.0/24 -p tcp --sport 22 -j ACCEPT
iptables -A OUTPUT -d 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT


④ HTTP 포트 허용
iptables -A INPUT -i eth0 -p tcp --sport 80 --dport 1024:65535 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 1024:65535 --dport 80 -j ACCEPT

*. ssh 서비스 / 웹서비스 허용

1.
iptables -F
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p all -j REJECET
*. 여기까지만 설정하는 경우에는 외부 접속이 안된다.
iptables -I INPUT 1 -m state --state ESTALISHED -p all -j ACCEPT <-- 이 설정이 필요하다.

---------------------------------------------------------------------------------------------------
2. 좀 더 엄격한 rule 을 적용할 경우라면 아래처럼 설정
iptables -F
iptables -A INPUT -m state --state ESTABLISHED -p all -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 22(또는 --dport ssh) -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 80(또는 --dport http) -j ACCEPT
iptables -A INPUT -p all -j REJECT (또는 iptables -A INPUT -j REJECT)
---------------------------------------------------------------------------------------------------

ftp 서비스를 위한 방화벽 설정

*. ftp 서비스는 passive mode 와 active mode 의 두가지 모드중 한가지 모드를 선택하여
운영된다. 이 운영모드에 따라서 방화벽 설정 방법은 달라진다.

A. ftp 서비스가 active mode 로 운영되는 경우
1. ftp client ---------------> 21 번포트로 접속요청
2. 접속후 운영모드를 확인하여 active mode 라면
ftp client 가 data 전송을 위한 임의의 data port 를 열고 ftp server 에게 data port 를 알려준다.
3. ftp server 에서는 20번포트를 이용해서 ftp client 의 data port 로 데이터를 전송한다.

B. ftp 서비스가 passive mode 로 운영되는 경우
1. ftp client ---------------> 21 번포트로 접속요청
2. 접속후 운영모드를 확인하여 passive mode 라면
ftp server가 랜덤 또는 미리 설정된 data port를 열고 ftp client 에게 data port를 알려준다.
*passive mode 에서는 active mode 일때 사용된 data port 20번을 사용하지 않는다.
3. ftp client 는 ftp server 의 데이터 포트로 데이터를 전송한다.

vsftpd 서버인 경우 active mode 및 passive mode 설정은
vsftpd.conf 파일에서
pasv_enable 옵션으로 설정한다.
pasv_enable 의 디폴트 값은 YES 이므로 이 옵션 설정이 안되어 있으면
passive mode 로 동작한다.
active mode 로 운영할 경우에는 pasv_enable=NO 로 설정해야 한다.




*방화벽 rule 저장 및 복구

iptables-save > /etc/sysconfig/iptables ; 현재의 방화벽 설정을 디폴트 설정파일에 저장.
(service iptables save 와 같다)
iptables-resotre < 방화벽설정파일명 ; 설정된 파일로부터 방화벽 설정을 불러온다.



==================================================

예제. 1
방화벽 설정


A (iptables)

INPUT
loop back device(lo) 에 대해서는 모든 서비스 허용.
ssh 허용
ftp 허용
나머지포트(x)
------------------------------
OUTPUT
loop back device(lo) 에 대해서는 모든 서비스 허용.
목적지 주소가 172.20.0.0/16 네트워크에 대해서만 외부로 telnet 접속 허용
나머지포트(x)
-------------------------------------------------------------------

*. 설정하기전에 모든 rule과 사용자 정의 체인을 삭제하시오.
*. 시스템을 리부팅했을때에도 현재의 설정을 계속사용할수
있도록 default 설정으로 저장하시오.
*. clone 머신은 default 방화벽 설정상태에서 telnet 서비스 접속이
허용되게 룰을 추가하시오.



참고 - default 설정파일(centos)
-----------------------------------------------------------------
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT


-----------------------------------------------------------------------------
*. 기타

옵션
-m : iptable에서 확장모듈을 로드하기 위한 옵션
m state 는 /lib/iptables/libipt_state.so <== 이 모듈을 로드하기위한것입니다.
예를 들면 테스트를 위해서 아래처럼 없는 모듈을 옵션뒤에 넣어보면 쉽게 알수
있을것입니다.
[root@centos1 iptables]# iptables -m test
iptables v1.3.5: Couldn't load match `test':/lib/iptables/libipt_test.so: cannot open shared object file: No such file or directory
그리고 로드된 모듈은 /proc/net/ip_tables_matches 이파일에서 볼수 있습니다.

그리고 state 옵션은 뒤에 아래와 같은 네가지 tcp 상태 옵션이 올수 있습니다.
NEW : 새 연결을 시도하는 패킷
ESTABLISHED : 양쪽 방향에서 연결이 완료된 패킷과 관련이 있는 패킷
RELATED : 새 연결을 시도하는 패킷이지만 이전 연결과 관련있는 패킷
예를 들면 ftp data 전송 패킷.(예를 들면 ftp 서비스가 방화벽에서 허용되어 있고
연결되어 있는 상태라면 ftp data 패킷도 허용이 됩니다)
INVALID : 이전 연결과 전혀 상관이 없는 패킷.

댓글목록

mycolor님의 댓글

mycolor 작성일

INPUT
ssh 서비스 허용
나머지 모든 포트 차단

1. iptables -A INPUT -p tcp --dport 22  -j ACCEPT
2. iptables -A INPUT -p all -j REJECT
3. iptables -I INPUT 1 -m state --state ESTABLISHED -p all  -j ACCEPT
추가로 ftp 서비스 허용
--> iptables -I INPUT 3 -p tcp --dport 21 -j ACCEPT
iptables -I INPUT 2 -m state --state RELATED -p all  -j ACCEPT



OUTPUT
텔넷만 차단
나머지 모든 포트 허용
iptables -A OUTPUT -p tcp --dport 23    -j REJECT

mycolor님의 댓글

mycolor 작성일

iptables 실습예제.
------------------
centos , centos2 에서 둘다 iptables -F
centos 의 방화벽 설정
INPUT
ssh 허용
http 서비스 허용
lo(loop back interface) 장치에 대해서는 모두 허용
나머지 모든 포트 차단
----------------------------------------
OUTPUT
ssh 서비스 허용
http 서비스 허용
lo 에 대해서는 모두 허용
나머지 모든 포트 차단

설정완료 후  /root/newiptables 파일로 설정을 저장


접속자집계

오늘
41
어제
34
최대
611
전체
448,016
개인정보취급방침 서비스이용약관 twoseven.kr All rights reserved.
상단으로