상세 컨텐츠

본문 제목

GeoLite2 Country 활용한 한국 IP list 추출 & iptables 에 적용하기

Dev.Stroy/Web

by zNine 2021. 7. 22. 11:23

본문

728x90
반응형

Web server에 iptables 설정으로 한국 IP 대역만 접속을 허용하도록 하기 위해서는

일종의 한국 IP 대역에 대한 whitelist 데이터베이스가 필요하다.

이를 제공하는 사이트가 있는데,

기존에는 wget 형식으로 URL로 바로 다운로드가 되었으나 현재 막혀있다.

회원가입 & 로그인 후 다운로드 가능하니 아래 절차로~


https://dev.maxmind.com/geoip/geolite2-free-geolocation-data

 

https://dev.maxmind.com/geoip/geolite2-free-geolocation-data

IP Geolocation Usage IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP database should not be used to identify a particular address or household.

dev.maxmind.com

Sign Up for GeoLite2 해서 가입

이메일 확인 -> 링크 접속 -> 패스워드 설정

이메일, 패스워드 입력 로그인

GeoLite2 Country: CSV Format 선택해서 다운로드

 

 

Linux shell에서 아래와 같이 실행, 한국 ip 대역만 파일로 저장

# 압축 풀기
unzip GeoLite2-Country-CSV_20210720.zip

# 한국 id 추출
grep 'South Korea' GeoLite2-Country-CSV_20210720/GeoLite2-Country-Locations-en.csv  | cut -d, -f1

# 1835841 으로 나옴

# 한국 ip 대역 추출해서 파일로 저장
grep 1835841 GeoLite2-Country-CSV_20210720/GeoLite2-Country-Blocks-IPv4.csv | cut -d, -f1 > kr.zone

 

그리고 iptables에 적용하기 위해서 아래와 같은 스크립트를 만들어 적용했다.

#!/bin/bash

IPTABLES=/sbin/iptables
ZONE_FILE=kr.zone

# check zone file
if [ ! -f ${ZONE_FILE} ]
then
  echo 'Cannot open zone file'
  exit 1
fi

# clear rules
${IPTABLES} -P INPUT ACCEPT
${IPTABLES} -F

# add new allow rules
for IP_ZONE in `egrep -v '^#|^$' ${ZONE_FILE}`
do
  ${IPTABLES} -A INPUT -p all -s ${IP_ZONE} -j ACCEPT
done

# drop all other input
${IPTABLES} -P INPUT DROP

 

728x90
반응형

관련글 더보기