Linux上网站屏蔽阻止海外IP流量访问的三种方法

目前除了外贸网站需求外,大部分企业个人网站目标客户都在内地其实都不需要海外IP的流量,因为它们基本不会带来任何实质的效益,有时还会占用大量的系统资源得不偿失,重要的是网上很多对网站的攻击注入等不安全因素也都来自海外IP。

linux屏蔽阻止海外IP

本文就是要说明 Linux上网站屏蔽阻止海外IP流量的三种方法,以从根本上断绝海外IP流量访问。个人推荐第三种方法最好。

第一种,使用网上大神的开源脚本,屏蔽指定国家地区的IP访问

  1. wget https://raw.githubusercontent.com/iiiiiii1/Block-IPs-from-countries/master/block-ips.sh
  2. sh block-ips.sh

第二种,使用 IPIP的数据库进行流量屏蔽(目前已支持 centos6、centos7还有 ubuntu系统)

  1. #!/bin/bash
  2. #判断是否具有root权限
  3. root_need() {
  4. if [[ $EUID -ne 0 ]]; then
  5. echo "Error:This script must be run as root!" 1>&2
  6. exit 1
  7. fi
  8. }
  9. #检查系统分支及版本(主要是:分支->>版本>>决定命令格式)
  10. check_release() {
  11. if uname -a | grep el7 ; then
  12. release="centos7"
  13. elif uname -a | grep el6 ; then
  14. release="centos6"
  15. yum install ipset -y
  16. elif cat /etc/issue |grep -i ubuntu ; then
  17. release="ubuntu"
  18. apt install ipset -y
  19. fi
  20. }
  21. #安装必要的软件(wget),并下载国 内IP网段文件(最后将局域网地址也放进去)
  22. get_china_ip() {
  23. #安装必要的软件(wget)
  24. rpm --help >/dev/null 2>&1 && rpm -qa |grep wget >/dev/null 2>&1 ||yum install -y wget ipset >/dev/null 2>&1
  25. dpkg --help >/dev/null 2>&1 && dpkg -l |grep wget >/dev/null 2>&1 ||apt-get install wget ipset -y >/dev/null 2>&1
  26. #该文件由 IPIP维护更新,大约一月一次更新
  27. [ -f china_ip_list.txt ] && mv china_ip_list.txt china_ip_list.txt.old
  28. wget https://github.com/17mon/china_ip_list/blob/master/china_ip_list.txt
  29. cat china_ip_list.txt |grep 'js-file-line">' |awk -F'js-file-line">' '{print $2}' |awk -F'< ' '{print $1}' >> china_ip.txt
  30. rm -rf china_ip_list.txt
  31. #wget https://qiniu.wsfnk.com/china_ip.txt
  32. #放行局域网地址
  33. echo "192.168.0.0/18" >> china_ip.txt
  34. echo "10.0.0.0/8" >> china_ip.txt
  35. echo "172.16.0.0/12" >> china_ip.txt
  36. }
  37. #只允许国 内IP访问
  38. ipset_only_china() {
  39. echo "ipset create whitelist-china hash:net hashsize 10000 maxelem 1000000" > /etc/ip-black.sh
  40. for i in $( cat china_ip.txt )
  41. do
  42. echo "ipset add whitelist-china $i" >> /etc/ip-black.sh
  43. done
  44. echo "iptables -I INPUT -m set --match-set whitelist-china src -j ACCEPT" >> /etc/ip-black.sh
  45. #拒绝非国 内和内网地址发起的tcp连接请求(tcp syn 包)(注意,只是屏蔽了入向的tcp syn包,该主机主动访问国 外资源不用影响)
  46. echo "iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 0 -j DROP" >> /etc/ip-black.sh
  47. #拒绝非国 内和内网发起的 ping探测(不影响本机 ping外部主机)
  48. echo "iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP" >> /etc/ip-black.sh
  49. #echo "iptables -A INPUT -j DROP" >> /etc/ip-black.sh
  50. rm -rf china_ip.txt
  51. }
  52. run_setup() {
  53. chmod +x /etc/rc.local
  54. sh /etc/ip-black.sh
  55. rm -rf /etc/ip-black.sh
  56. #下面这句主要是兼容 centos6 不能使用"-f"参数
  57. ipset save whitelist-china -f /etc/ipset.conf || ipset save whitelist-china > /etc/ipset.conf
  58. [ $release = centos7 ] && echo "ipset restore -f /etc/ipset.conf" >> /etc/rc.local
  59. [ $release = centos6 ] && echo "ipset restore < /etc/ipset.conf" >> /etc/rc.local
  60. echo "iptables -I INPUT -m set --match-set whitelist-china src -j ACCEPT" >> /etc/rc.local
  61. echo "iptables -A INPUT -p tcp --syn -m connlimit --connlimit-above 0 -j DROP" >> /etc/rc.local
  62. echo "iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP" >> /etc/rc.local
  63. #echo "iptables -A INPUT -j DROP" >> /etc/rc.local
  64. }
  65. main() {
  66. check_release
  67. get_china_ip
  68. ipset_only_china
  69. case "$release" in
  70. centos6)
  71. run_setup
  72. ;;
  73. centos7)
  74. chmod +x /etc/rc.d/rc.local
  75. run_setup
  76. ;;
  77. ubuntu)
  78. sed -i '/exit 0/d' /etc/rc.local
  79. run_setup
  80. echo "exit 0" >> /etc/rc.local
  81. ;;
  82. esac
  83. }
  84. main

看到这里可能会有人考虑如此以来是不是把海外的搜索引擎IP也屏蔽了,会造成海外搜索引擎不收录的情况。(其实目前我们的“大局域网”状况,海外搜索引擎也不会带来什么流量的)

不过也有办法解决这个问题,最近发现如果你用的是类似阿里云的域名服务,就可以试试本文推荐的第三种方法:设置不同的域名解析路线

首先设置好通常的 www默认访问路线,再将泛域名*解析路线选择设置为 境外 并指向本机 ip 127.0.0.1,这样如果要是有海外网站攻ji也是自己跟自己玩了,如图(注意设置阻止泛域名后你是否有其他二级域名需要海外访问)

屏蔽阻止海外IP

设置好后,你会发现即达到了屏蔽绝大部分海外IP流量的访问(仅有很少量的漏网之鱼),还有海外搜索引擎的正常来访(如 google 和 必应 等)

好了三种方法说完,你可以根据实际情况自己去试试了。

 

转载请注明链接地址:荐爱小站 » Linux上网站屏蔽阻止海外IP流量访问的三种方法

赞 (11) 赏 !

觉得文章有用就打赏一下吧,赠人玫瑰手有余香!

支付宝扫一扫打赏

微信扫一扫打赏