국가 IP Drop 메뉴얼 (GeoIP, IPtables 를 활용한)
작성일 : 2010.12.27
작성자 : (주)스마일서브 이선규 (seroot.com)


공지사항!

1) 이 설정 메뉴얼은 2010.12.27 일자 서버에 테스트, 적용되었습니다.

2) 아래 설치과정중 특이하게 주의할 사항은 없으나 각자 환경에 따라 iptables 룰셋 적용시
    돌발사항이 발생할 수도 있으니, 테스트 서버에서 충분히 테스트 한후 적용하기 바랍니다.

3) 커널 업데이트시 적용이 되는지는 확인되지 않았습니다.

 

0. 요구사항 체크

  레드헷계열 OS (CentOS, Fedora) OS
  iptables 1.4.3 버전 이상 (CentOS 5.5 버전에서 rpm으로 설치된 버전은 1.3.5 일 것임)
  geoip
  xtables-addons


1. 프로그램 파일 준비

  /usr/local/src 로 이동하여 적당한 디렉토리를 만들고, 그 안에 아래 파일들을 다운로드 받습니다.
  *주) xtables-addons 최신 버전도 존재하나 tar.xz 파일 구조로 되어 있어, 아래 1.15 버전 사용을 추천합니다.

  # wget http://nchc.dl.sourceforge.net/sourceforge/xtables-addons/xtables-addons-1.15.tar.bz2
  # wget http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
  # wget http://jengelh.medozas.de/files/geoip/geoip_src.tar.bz2
  # wget ftp://ftp.netfilter.org/pub/iptables/iptables-1.4.3.2.tar.bz2


2. 압축해제 및 프로그램 설치


 1) iptables

  # tar xvfj iptables-1.4.3.2.tar.bz2
  # cd iptables-1.4.3.2
  # ./configure
  # make
  # make install

 2) xtables-addons

  # tar xvfj xtables-addons-1.15.tar.bz2
  # xtables-addons-1.15
  # ./configure --with-xtables=/usr/local
  # make 치면 다음과 같은 에러가 나올 것이다.
 
---------------------------------------------------------------------------
make  all-recursive
make[1]: Entering directory `/usr/local/src/20101224/xtables-addons-1.15'
Making all in extensions
make[2]: Entering directory `/usr/local/src/20101224/xtables-addons-1.15/extensions'
  GEN      modules
make[3]: Entering directory `/usr/src/kernels/2.6.18-194.26.1.el5-i686'
  CC [M]  /usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.o
In file included from /usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.c:20:
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_skbuff.h:29: error: redefinition of 'skb_reset_network_header'
include/linux/skbuff.h:1122: error: previous definition of 'skb_reset_network_header' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_skbuff.h:33: error: redefinition of 'tcp_hdr'
include/linux/tcp.h:169: error: previous definition of 'tcp_hdr' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_skbuff.h:37: error: redefinition of 'udp_hdr'
include/linux/udp.h:33: error: previous definition of 'udp_hdr' was here
In file included from /usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.c:21:
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtnu.h:9: error: redefinition of typedef 'bool'
include/linux/types.h:36: error: previous declaration of 'bool' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtnu.h:10: error: redeclaration of enumerator 'false'
include/linux/stddef.h:16: error: previous definition of 'false' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtnu.h:10: error: redeclaration of enumerator 'true'
include/linux/stddef.h:18: error: previous definition of 'true' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtnu.h:131: error: redefinition of 'csum_unfold'
include/net/checksum.h:88: error: previous definition of 'csum_unfold' was here
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.c: In function 'xtnu_ip_route_me_harder':
/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.c:376: error: too many arguments to function 'ip_route_me_harder'
make[4]: *** [/usr/local/src/20101224/xtables-addons-1.15/extensions/compat_xtables.o] 오류 1
make[3]: *** [_module_/usr/local/src/20101224/xtables-addons-1.15/extensions] 오류 2
make[3]: Leaving directory `/usr/src/kernels/2.6.18-194.26.1.el5-i686'
make[2]: *** [modules] 오류 2
make[2]: Leaving directory `/usr/local/src/20101224/xtables-addons-1.15/extensions'
make[1]: *** [all-recursive] 오류 1
make[1]: Leaving directory `/usr/local/src/20101224/xtables-addons-1.15'
make: *** [all] 오류 2
---------------------------------------------------------------------------

  다음과 같이 처리 후 진행하도록 한다.


  # vi extensions/compat_skbuff.h

28~39 라인 주석처리

     28 /* static inline void skb_reset_network_header(struct sk_buff *skb)
     29 {
     30     skb->nh.raw = skb->data;
     31 }
     32 static inline struct tcphdr *tcp_hdr(const struct sk_buff *skb)
     33 {
     34     return (void *)skb_transport_header(skb);
     35 }
     36 static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
     37 {
     38     return (void *)skb_transport_header(skb);
     39 } */


  # vi extensions/compat_xtnu.h

9~10 라인 주석처리

      9 /* typedef _Bool bool;
     10 enum { false = 0, true = 1, };
     11 */


  # vi extensions/compat_xtables.c

372 라인 변경

 372 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 17) 
     --> #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) 로 수정


  # vi extensions/compat_xtnu.h

131~134라인 주석처리

    131 /*static inline __wsum csum_unfold(__sum16 n)
    132 {
    133     return (__force __wsum)n;
    134 }*/


  # make
  # make install


 3) geoip_src 설치

  # mkdir -p /var/geoip/LE
  # tar xvfj geoip_src.tar.bz2
  # unzip GeoIPCountryCSV.zip
  # cp geoip_csv_iv0.pl /var/geoip/LE
  # mv GeoIPCountryWhois.csv /var/geoip/LE
  # cd /var/geoip/LE

  아래 명령을 실행하면 에러메세지가 나온다.

---------------------------------------------------------------------------
  # ./geoip_csv_iv0.pl GeoIPCountryWhois.csv
Can't locate Text/CSV_XS.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./geoip_csv_iv0.pl line 12.
BEGIN failed--compilation aborted at ./geoip_csv_iv0.pl line 12.
---------------------------------------------------------------------------

  다음과 같이 처리한다음 진행한다.
 
  # perl -MCAPN -e shell

---------------------------------------------------------------------------
  모든 과정을 따로 설정해줄 필요는 없으므로, 엔터로 진행한다.
  참, 실행전 자체 방화벽이 올려져 있다면 잠시 내려놓도록한다.
---------------------------------------------------------------------------

  엔터로 진행중 계속 물어보는 부분이 나타날 것이다.  국가 선택부분인데.

  # 대륙/국가를 선택한다.

(1) Africa
(2) Asia
(3) Australasia
(4) Central America
(5) Europe
(6) North America
(7) Oceania
(8) South America
Select your continent (or several nearby continents) [] 2
Sorry! since you don't have any existing picks, you must make a
geographic selection.

(1) China
(2) Hong Kong
(3) India
(4) Indonesia
(5) Japan
(6) Republic of Korea
(7) Russia
(8) Singapore
(9) Taiwan
(10) Thailand
(11) Turkey
Select your country (or several nearby countries) [] 6
Sorry! since you don't have any existing picks, you must make a
geographic selection.


  # 적당하게 파일 받을 곳(URL)을 선택한다.

(1) ftp://cpan.mirror.cdnetworks.com/CPAN/
(2) ftp://cpan.sarang.net/CPAN/
(3) ftp://ftp.kaist.ac.kr/pub/CPAN
Select as many URLs as you like (by number),
put them on one line, separated by blanks, e.g. '1 4 5' [] 1 2 3

Enter another URL or RETURN to quit: []
New set of picks:
 
ftp://cpan.mirror.cdnetworks.com/CPAN/
  ftp://cpan.sarang.net/CPAN/
  ftp://ftp.kaist.ac.kr/pub/CPAN


commit: wrote /usr/lib/perl5/5.8.8/CPAN/Config.pm
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.7602)
ReadLine support available (try 'install Bundle::CPAN')

  cpan> 프롬프트가 떨어졌다면 다음과 같이 설치 명령을 내린다.

  capn> install Getopt::Long
  capn> install IO::Handle
  capn> install Text::CSV_XS

  설치를 마쳤다면 quit 명령으로 빠져 나오자.

  cpan> quit

  이제 에러가 났던 명령을 다시 실행하면 국가별로 KR.iv0 와 같은 파일이 생성된다.

  # ./geoip_csv_iv0.pl GeoIPCountryWhois.csv

-------------------------------------------------
[생략]
 2973 ranges for IT Italy
   87 ranges for JE Jersey
   76 ranges for JM Jamaica
   94 ranges for JO Jordan
 1745 ranges for JP Japan
  150 ranges for KE Kenya
   38 ranges for KG Kyrgyzstan
   75 ranges for KH Cambodia
    2 ranges for KI Kiribati
    5 ranges for KM Comoros
   63 ranges for KN Saint Kitts and Nevis
    5 ranges for KP Korea, Democratic People's Republic of
  632 ranges for KR Korea, Republic of
  166 ranges for KW Kuwait
   32 ranges for KY Cayman Islands
  175 ranges for KZ Kazakhstan
   14 ranges for LA Lao People's Democratic Republic
[생략]
-------------------------------------------------


3. 국가대역 막아보기

  이제 실제로 국가대역을 막아볼 차례다.
  중국에서 들어오는 IP 에 대해 서버의 80번 포트 서비스 요청을 거부하는 명령을 내려본다.

  # /usr/local/sbin/iptables -A INPUT -p tcp --dport 80 -m geoip --src-cc CN -j DROP

  정상적으로 프롬프트가 떨어졌다면 성공이다.

 주) /usr/local/sbin/iptables 로 실행하는 것은 소스로 설치한 iptables 1.4.3.2 버전이
 이 위치로 설치되기때문, rpm 으로 설치한 버전은 geoip 라이브러리를 인식하지 못하기 때문에
 에러 메세지가 나올 수 있다.

 ---------------------------------------------------------------------------------------
 iptables v1.3.5: Couldn't load match `geoip':/lib/iptables/libipt_geoip.so: cannot open shared object file: No such file or directory
 ---------------------------------------------------------------------------------------

  아래와 같이 추가되었다면 성공한 것이다.

  # iptables -L -n

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 Source country: CN

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


#### 서버에서 적용 ####

1. 먼저, ACCEPT 시킬 IP 에 대해 열거, 후에 해당 국가 IP 대역 DROP 정책 실시 (순서 바뀌면 ACCEPT 적용 안됨)
2. --src-cc 뒤에 국가 코드는 고정 두자리로 /var/geoip/LE 내에  **.iv0 파일의 국가 코드와 일치함.

  예) 한국 KR, 중국 CN, 일본 JP, 미국 US, 캐나다 CA 등


#### 중국 IP 대역중 특정 IP에 대해서만 접근 허용 ####
$IPTABLES -A INPUT -s 11.22.33.11 -j ACCEPT

#### 중국 IP 대역 모든 서비스 접근 금지 ####
$IPTABLES  -A INPUT -m geoip --src-cc CN -j DROP




* 이 글과 관련한 문의사항은 댓글 남겨주시면 답변드리겠습니다.

 

 

 

 

 

 

 

 


 

2010/12/27 15:36 2010/12/27 15:36








마음튼튼 이 작성.

당신의 의견을 작성해 주세요.

[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다



1. 초당 15번의 web 요청이 있는 경우 차단합니다.

iptables -A INPUT -p tcp --dport 80 -m recent --update --seconds 1 --hitcount 15 --name HTTP -j DROP


2. 분당 10번이상의 ssh 새로운 접근이 있는 경우 차단합니다.

iptables -I INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 11 -j DROP


3. syn_food 정책을 만들고 초당 10번의 요청이 있는 경우 차단합니다.

iptables -N syn_flood
iptables -A INPUT -p tcp --syn -j syn_flood
iptables -A syn_flood -m limit --limit 1/s --limit-burst 10 -j RETURN
iptables -A syn_flood -j DROP


http://www.netfilter.org/documentation/HOWTO/netfilter-extensions-HOWTO-3.html#ss3.16




출처 : http://www.jinmoda.com/bbs/board.php?bo_table=board_01&wr_id=98&page=

참조 url : http://blog.pages.kr/624
2010/06/09 18:19 2010/06/09 18:19








마음튼튼 이 작성.

당신의 의견을 작성해 주세요.

[로그인][오픈아이디란?]
오픈아이디로만 댓글을 남길 수 있습니다