iptables 설정

Linux/Linux 일반 : 2009. 9. 17. 14:22
반응형
출처 : http://blog.daum.net/_blog/BlogView.do?blogid=0FSvI&articleno=7913842#ajax_history_home

▣ iptables 설정

 

방화벽입니다~~

대부분 아실거라고 생각하지만 중요한 점은 규칙을 정할 때의 순서!!! 입니다.

IF 모든 패킷을 거부하는 규칙이 가장 먼저 나온다면 그 이후의 규칙은 무시가 되어버립니다.

그리하여 허용되는 규칙이 먼저 나오고 나중에 거부하는 규칙을 정의해야 합니다. ^^

 

- 기본 시스템 환경조건

1. 여러대의 서버가 존재합니다.
2. 각 서버간에는 사설아이피(192.168.0.x)로 네트워킹을 합니다.
3. 모든 웹서버는 단 하나의 클라이언트와 단하나의 웹사이트를 운영한다.(트래픽이 높은 사이트에서 L4를 이용한 로드밸런싱을 하고 있다.)

- iptables와 hosts.deny, hosts.allow 를 이용한 ip허용/차단에 대한 간략한 설명

ssh를 이용한 원격접속
shell > ssh -l userid hostip

-- hosts.deny로 아이피 차단시 보여지는 현상

아래의 메시지를 띄우고 ssh 접속 멈춤
ssh_exchange_identification: Connection closed by remote host

-- iptables 로 차단시 보여지는 현상
shell > ssh -l userid hostip

==> 계속 대기상태(언제 끝나려나..??)

이를 보면, hosts.deny로 아이피/서비스를 막게 되면 시스템에 접속하여 거부당했고, iptables로 차단했을 경우에는 시스템 자체에 접속을 할수가 없다.

보 안관련 공부를 약간이라도 한사람이라면 hosts.deny는 시스템 차원에서의 block이고, iptables는 network차원에서의 block이다.  network는 physical 계층보다 한단계 위의 계층임으로 system에서 하는 일을 network가 대신해 주는 것이다. 그러면 2중으로 차단하는것도 좋을 것이다.

 

◇ 옵션 :

-A   체인에 새로운 규칙 추가
-D   체인의 어떤 지점의 규칙 삭제
-F   체인으로부터 모든 규칙 삭제
-L   어떤 체인의 규칙 보기

-I    체인의 어떤 지점에 규칙을 삽입

-i    ( input interface, // i eth0 는 eth0로 들어오는 모든 패킷 )

-o   ( output interface )

-R   체인의 어떤 지점의 규칙을 교환

-s   출발지 주소 ( 패킷 출처 IP 지정/도메인 또는 192.168.10.0/24 처럼 지정할수 있다  )
-d   목적지 주소 ( 패킷 도착지 IP 지정 )
--sport   출발지 포트 번호 제어
--dport   목적지 포트 번호 제어
-p   프로토콜 제어 ( p옵션의 인자는 TCP,UDP,ICMP 가 될수 있다 ) 
-j   규칙 설정

 

 ※ 참고

INPUT  은  패킷이 들어오는 체인에

DROP 은 패킷을 버리라는 뜻 


iptables.sh라는 셀스크립트를 하나 생성하자.
--------------------------------------------------
#!/bin/sh

## iptables를 초기화
iptables -F

## 랜카드 사용을 모두 허용
iptables -A INPUT -p ALL -i eth0 -j ACCEPT
iptables -A OUTPUT -p ALL -o eth0 -j ACCEPT
iptables -A INPUT -p ALL -i eth1 -j ACCEPT
iptables -A OUTPUT -p ALL -o eth1 -j ACCEPT

## TELNET 포트 막음
iptables -A INPUT -p TCP --dport 23 -s ip앞 세자리.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 23 -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 23 -j DROP

## FTP 포트 막음
iptables -A INPUT -p TCP --dport 21 -s ip앞 세자리.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 21 -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 21 -j DROP

## SSH 포트 막음
iptables -A INPUT -p TCP --dport 22  -s ip앞 세자리.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 22  -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -p TCP --dport 22 -j DROP
--------------------------------------------------------
이렇게 파일을 생성해서 실행권한을 부여 chmod 744 filename

여기서는 다른 포트는 신경쓰지 말고  telnet, ftp, ssh 만 고려했다.
모든 포트를 열어 둔 상태에서(어차피 다른 port는 /etc/hosts.deny, hosts.allow에서 또한번 block될것이다.) telnet, ftp, ssh 포트는 특정ip군과 사설아이피만 허용하고 나머지는 모두 drop 했다. 정확한 사용법을 모르는 상태에서 모든 포트를 블럭한 상태에서 시작하는 것보다는 열려있는 상태에서 필요한 포트를 하나씩 막아나가는것이 좋을듯합니다.
service iptables save 하면 현재 룰셋이 /etc/sysconfig/iptables 로 저장되고 리부팅 후에도 적용됩니다.

 

- 설정 예제)

[root@bridge ~]# iptables -L              // 현재 정의된 규칙 보여주기
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     ipv6-crypt--  anywhere             anywhere
ACCEPT     ipv6-auth--  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
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

 

[root@bridge ~]# iptables -F               // 규칙 초기화
[root@bridge ~]# iptables -L
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

Chain RH-Firewall-1-INPUT (0 references)
target     prot opt source               destination

 

[root@bridge ~]# iptables -A INPUT -p icmp -s 127.0.0.1 -j DROP 
// 출발지가 127.0.0.1인 (-s 127.0.0.1) icmp 프로토콜 (-p icmp) 패킷을 거부(-j DROP) 하는 규칙을 추가(-A) 합니다.

[root@bridge ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       icmp --  bridge               anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (0 references)
target     prot opt source               destination


[root@bridge ~]# ping 127.0.0.1           // ping 이 먹히질 않죠 ㅋ
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

--- 127.0.0.1 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 3999ms


[root@bridge ~]# iptables -A INPUT -p tcp --dport 23 -j DROP
// 목적지 포트가 23번(--dport 23)이고 tcp 프로토콜 (-p tcp)인 패킷을 거부(-j DROP)하는 규칙을 추가(-A) 합니다.

 

[root@bridge ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       icmp --  bridge               anywhere
DROP       tcp  --  anywhere             anywhere            tcp dpt:telnet
// 자동으로 23번포트 telnet 으로 설정

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (0 references)
target     prot opt source               destination


[root@bridge ~]# iptables -D INPUT 1
//INPUT 란의 2번째 (0,1,2...) 설정을 삭제 하는 기능입니다.


반응형

'Linux > Linux 일반' 카테고리의 다른 글

xhost Xwindow를 이용해서 원격으로 쓰기.  (0) 2009.09.19
Device Driver 만들기.  (0) 2009.09.10
Linux Kernel 소스분석.  (0) 2009.09.10
Posted by Real_G