국가별 IP iptables (xtable, geoip) 로 막기 (Country IP Block) Ubuntu 20

하늘 No.257 [컴퓨터] 4888
국가별 IP iptables (xtable, geoip) 로 막기 (Country IP Block)
-------------------------------------
Ubuntu 20.04 x64

$ sudo apt-get install xtables-addons-common
 함께 자동 설치 됨 : xtables-addons-dkms
 
$ sudo mkdir -p /usr/share/xt_geoip
$ cd /usr/share/xt_geoip

//https://db-ip.com/db/lite.php
//"https://download.db-ip.com/free/dbip-country-lite-$(date +'%Y-%m').csv.gz" 에서 자동 다운로드
$ sudo /usr/lib/xtables-addons/xt_geoip_dl
 - 생성됨 : dbip-country-lite.csv

$ sudo perl /usr/lib/xtables-addons/xt_geoip_build
 - 생성됨 : *.iv4, *.iv6
 
만일 에러 발생 : Can't locate Text/CSV_XS.pm
sudo perl -MCPAN -e'install Text::CSV_XS'

geoip + iptables 연동 테스트
CN,RU,HK,KP,SG : 국가코드는 하단에
JP 막으면 서울 vultr 가 걸릴 때가 있음
https://db-ip.com/db/download/ip-to-country-lite
dbip-country-lite-2022-09.csv
158.247.192.0,158.247.255.255,JP


// 차단
$ sudo iptables -A INPUT -m geoip --src-cc CN,RU,HK,KP,SG -j DROP

// 차단 정책 삭제
$ sudo iptables -D INPUT -m geoip --src-cc CN,RU,HK,KP,SG -j DROP

실행시 아무런 메세지가 나오지 않는다면 정상적으로 입력
확인 : 아래 항목이 출력됨.

$ sudo iptables -L

DROP all -- anywhere anywhere -m geoip --source-country CN,RU,HK,KP,SG

iptables 은 리부팅하면 저장 삭제됨.
만일 본인의 접속이나 정상적 접속이 불가하다면 리부팅.
정상적인걸 확인 후 iptables-persistent 으로 부팅때마다 지정되도록 함.

// 테스트
$ sudo iptables -L -n -v
$ sudo iptables -L
$ sudo iptables -A INPUT -m geoip --src-cc KR -j DROP
접속 안 됨, 리부팅 하면 iptables 규칙 사라짐. (혹시나 있으면 지우는 코드)
$ sudo iptables -D INPUT -m geoip --src-cc KR -j DROP

// 차단
// KR을 제외하고 모두 블럭
$ sudo iptables -A INPUT -m geoip ! --src-cc KR -j REJECT

// CN,RU,HK,KP,SG 을 블럭
$ sudo iptables -A INPUT -m geoip --src-cc CN,RU,HK,KP,SG -j DROP

// 저장 (iptables-persistent 설치되어 있어야 함)
$ sudo netfilter-persistent save

// 이후 아래 파일 바로 수정해도 됨.
$ sudo vi /etc/iptables/rules.v4

#Block Geoip (db-ip.com)
-A INPUT -m geoip --src-cc CN,RU,HK,KP,SG -j DROP

$ sudo netfilter-persistent reload
$ sudo iptables -L

Apache MMDB 없애도 되지만 그냥 둠.
리부팅후 sudo iptables -L 에서 iptables 에 규칙 있는지 확인

// https 인증서 테스트
$ sudo certbot certificates
$ sudo certbot renew --dry-run


참고 : https://blog.elmi.page/417
https://fmyson.tistory.com/231

국가코드 : https://eminwon.qia.go.kr/common/CountrySP.jsp
https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
http://idchowto.com/?p=17077

해외 접속 테스트 : https://www.webpagetest.org/
중국 출발 접속 테스트 : https://tool.chinaz.com/speedtest/skymoon.info

-------------------
z1. 추가 불량 코드 받기 (안해도 됨)

/heisme_Gen-mmdb/h_blockIP_dbip.csv : 자체 프로그램
  h_blockIP_dbip*.csv . 생성

$ cd /usr/share/xt_geoip
$ ls -l HS* h_*
$ sudo cp /www/svc.skymoon.info/backup/t/h_blockIP_dbip*.csv .

$ sudo cp dbip-country-lite.csv org_dbip-country-lite.csv
$ sudo cp h_blockIP_dbip-only.csv dbip-country-lite.csv
$ sudo perl /usr/lib/xtables-addons/xt_geoip_build
 - 생성됨 : HS.iv4 (259), HS.iv6 (자료 없음)
$ ls -l HS* h_*

HS.iv4 만 만들어 두고 더 이상 작업 안 함. (csv 원위치)
$ sudo cp org_dbip-country-lite.csv dbip-country-lite.csv
$ ls -l dbip-country-lite.csv

* 적용할려면 국가코드 HS 로 지정.
$ sudo iptables -A INPUT -m geoip --src-cc CN,RU,HK,KP,SG,HS -j DROP

//1. 국가별 IP svc 받기
//mmdb GeoIP CVS
//https://www.maxmind.com/en/accounts/297055/geoip/downloads
//
//GeoLite2 Country: CSV Format
//
//GeoLite2-Country-CSV_20220104.zip 파일 압축 풀기
// IP v4: GeoLite2-Country-Blocks-IPv4.csv
// IP v6: GeoLite2-Country-Blocks-IPv6.csv
// 국가코드(영문) : GeoLite2-Country-Locations-en.csv
//

===============
Ubuntu 간단한 방화벽 만들기 (iptables, iptables-persistent)


대부분의 방화벽은 호스팅에서 막고 꼭 필요한 IP만 블록킹
참고 : https://ruungji.tistory.com/entry/use-IPTables

설치
$ sudo apt install iptables-persistent

저장
$ sudo netfilter-persistent save
$ sudo netfilter-persistent reload

이후 아래 파일을 바로 편집해도 됨.
$ sudo vi /etc/iptables/rules.v4

# Generated by iptables-save v1.8.4 on Fri Jan 7 04:14:46 2022
*filter
:INPUT ACCEPT [2020:174023]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1902:1694142]
# Russia
-A INPUT -s 45.155.204.0/24 -j DROP
# Singapore ALL, Huawei
-A INPUT -s 114.119.128.0/18 -j DROP
COMMIT

--------
만들기

# Russia
iptables -A INPUT -s 45.155.204.6/24 -j DROP
# Singapore : Huawei
iptables -A INPUT -s 114.119.142.189/24 -j DROP

$ iptables -L -n -v
$ netfilter-persistent save
국가별 IP iptables (xtable,geoip) 로 막기 (Country IP Block) Ubuntu 20 Photo-Image

https://SkyMoon.info/a/HeismeNote/257  

난 어디에 있는 걸까? 천만 번 돌리다가 만 공중전화의 그 신호음 속에 숨어 있을까? 나의 참 모습은... [하늘-사람들 I (길위에서)]