본문 바로가기
리눅스

[Linux] 리눅스 방화벽 Iptables 개념정리

by 공대냥이 2018. 8. 14.
반응형

Iptables 넷필터(netfilter) 프로젝트에서 개발했으며 광범위한 프로토콜 상태 추적

패킷 애플리케이션 계층검사속도 제한필터링 정책을 명시하기 위한 강력한 매커니즘을 제공한다.

 

 

 

 

리눅스 2.4 커널 버전이상에서 사용하는 서버 방화벽(iptables)이다.

리눅스 2.2 커널 버전에서는 ipchains 사용하였는데 구조와 사용하는 방법은 비슷하다.

kernel 2.2(ipchains) -> kernel 2.4(iptables) -> kernel 2.6(iptables)

 

(주의) iptables/ipchains 동시에 사용하면 안된다.

 

RHEL/CentOS 7 부터는 방화벽을 iptables 대신 firewalld라는 데몬으로 교체하였다.firewalld 관련해서는 다음에 포스팅 하도록 하자.

 

iptables 논리적인 3개의 사슬(chains)으로 구성되어 있고, 각각 INPUT, OUTPUT, FORWARD 라고 한다.

 

또한 관리자가 정의하여 새로운 사슬도 생성할 수 있다. (기본 사슬의 이름은 대문자이다.)

 

 

# iptables -L (# iptables -L -t filter) 아무 설정이 없을 경우

  
   Chain INPUT (policy ACCEPT)            # 리눅스 박스를 향해 들어오는 패킷들이 거치는 체인
  target prot opt source destination
   Chain FORWARD (policy ACCEPT)      # 리눅스 박스를 거쳐 OUTPUT 체인을 향하는 체인
  target prot opt source destination
   Chain OUTPUT (policy ACCEPT)         # 리눅스 박스를 나가는 패킷들이 들어가는 체인
  target prot opt source destination 

 

 

 

패킷 필터링 규칙(Packet Filtering Rule)

() 들어 오는 패킷을 순서대로 정의된 규칙(Rule)들을 점검하고 처음 일치되는 규칙을 적용한다.

() 하나의 필터링 규칙에 여러가지 조건으로 지정하면 모든 조건에 일치해야 규칙 적용이 된다.

() 내장된 체인(Chain) 안을 모두 검사했지만 일치 항목이 없을 경우에는 체인의 기본 정책이 적용된다. 

 "사용자 정의 체인"의 경우 일치 항목이 없을 경우에는 제어권은 "사용자 정의 체인"이 호출되었던 체인으로 다시 돌아간다.

 

 

규칙 타겟(Rule Target) : iptables는 패킷이 규칙과 일치할 때 동작을 취하는 타겟을 지원한다.

내장 타겟

    ACCEPT : 패킷을 허용하는 옵션이다. (Allow)

    DROP : 패킷을 완전히 무시한다. (Deny)

확장 타겟

    REJECT : 패킷을 허용하지 않는다는 메세지를 보내면서 거부한다. 사슬 전체 정책 설정(-P)에서는 

               사용할 수 없. 패킷을 버리고 이와 동시에 적절한 응답 패킷을 전송한다.

    LOG : 시스템 로그 커널 장치로 연결한다. 패킷을 syslog에 기록한다.

    사용자 정의 체인 : 사용자가 정의하는 체인이다.

 

 Iptables 명령어

   - 전체 사슬(chains)에 대한 설정(대문자 옵션 사용: -P, -L, -N, -X )
   - 각 사슬(chains)에 대한 규칙(Rules)을 설정(소문자 옵션 사용: -s, -d, -p )

 

 

 

(명령어 형식)

# iptables [-t <table-name>] <command> <chain-name> <parameter-1><option-1> <parameter-n><option-n>

옵 션 설 명
-N -N, --new-chain chain
Create a new user-defined chain by the given name. There must be no target of that 
name already.


 
새로운 사슬을 만든다.
# iptables -N NEW 
# iptables -L
-X -X, --delete-chain [chain]
Delete the optional user-defined chain specified. There must be
no references to the chain. If there are, you must delete or
replace the referring rules before the chain can be deleted.
The chain must be empty, i.e. not contain any rules. If no argument is given, 
it will attempt to delete every non-builtin chain in the table.
 
비어있는 사슬을 제거한다. 3개의 기본 사슬(INPUT, OUTPUT, FORWARD)은 제거할 수 없다.
# iptables -X NEW 
# iptables -L
-P -P, --policy chain target
Set the policy for the chain to the given target. See the section TARGETS for the legal targets. 
Only built-in (non-user-defined) chains can have policies, and neither built-in nor
user-defined chains can be policy targets.
 
사슬의 정책을 설정한다. (EX: ACCEPT, DROP)
# iptables -P INPUT DROP ; iptables -L 
# iptables -P INPUT ACCEPT ; iptables -L
-L -L, --list [chain]
List all rules in the selected chain. If no chain is selected, all chains are listed. 
As every other iptables command, it applies to the specified table (filter is the default), 
so NAT rules get listed by
# iptables -t nat -n -L
Please note that it is often used with the -n option, in order to avoid long reverse DNS lookups. It is legal to specify the -Z (zero) option as well, in which case the chain(s) will be
atomically listed and zeroed. The exact output is affected by the other arguments given. 
The exact rules are suppressed until you use
# iptables -L -v
 
현재 사슬의 규칙을 나열한다.
# iptables -L 
# iptables -L -v (# iptables -vL, # service iptables status)
# iptables -L INPUT 
# iptables -L INPUT -v
-F -F, --flush [chain]
Flush the selected chain (all the chains in the table if none is given). 
This is equivalent to deleting all the rules one by one.
 
사슬으로부터 규칙을 제거한다.
# iptables -A INPUT -p icmp -j DROP ; iptables -L
# iptables -F ; iptables -L 
# iptables -A INPUT -p icmp -j DROP ; iptables -L
# iptables -F INPUT
-Z -Z, --zero [chain]
Zero the packet and byte counters in all chains. It is legal to specify the -L, --list (list) option as well, to see the counters immediately before they are cleared. (See above.)




사슬내의 모든 규칙들의 패킷과 바이트의 카운트를 '0'으로 만든다.
# iptables -Z 

 

 

 

(2) 사슬(chain) 내부의 규칙(Rules)에 대한 작동

 

옵 션 설 명
-A -A, --append chain rule-specification
Append one or more rules to the end of the selected chain. When
the source and/or destination names resolve to more than one
address, a rule will be added for each possible address combination.
 
사슬에 새로운 규칙을 추가한다. 해당 사슬에 맨 마지막 규칙으로 등록된다.
-I -I, --insert chain [rulenum] rule-specification
Insert one or more rules in the selected chain as the given rule
number. So, if the rule number is 1, the rule or rules are
inserted at the head of the chain. This is also the default if
no rule number is specified.
 
사슬에 규칙을 맨 첫부분에 설정한다.(i 대문자)
-R -R, --replace chain rulenum rule-specification
Replace a rule in the selected chain. If the source and/or destination 
names resolve to multiple addresses, the command will fail. Rules are numbered starting at 1.
 
사슬의 규칙을 교환한다.
-D -D, --delete chain rule-specification
-D, --delete chain rulenum
Delete one or more rules from the selected chain. There are two
versions of this command: the rule can be specified as a number
in the chain (starting at 1 for the first rule) or a rule to match.
 
사슬의 규칙을 제거한다.

 

 옵션과 관련된 규칙

-s(Source IP), -d(Destination IP)

-j(jump)

-!

-p(Protocol)

-i(In-interface), -o(Out-interface)

-t(Tables)

--sport(Source Port), --dport(Destination Port)

 

(기본적인 사용법)

 

# iptables -L /* -L : list */

# iptables -L -v

 

# iptables -F /* -F : Flush */

# iptables -F INPUT

 

# iptables -P INPUT DROP /* -P : Policy */

# iptables -P INPUT ACCEPT

 

# iptables -A INPUT -p tcp --dport 23 -j ACCEPT

 

# service iptables save

# service iptables start

# service iptables stop

# service iptables restart

# service iptables status

 

# chkconfig --list iptables

 

# chkconfig iptables on 

 

반응형