2
0

More than 3 years have passed since last update.

【GCP】課金対象国からのアクセスを遮断する

Posted at

はじめに

ファイアウォール機能で中国、オーストラリアからの通信を遮断するシェルスクリプトです。
対象のIPアドレスはこちら(中国オーストラリア)から取得しました。
それぞれ5000ずつあります。

実行環境

以下の環境で動作確認しました。

  • Linux
  • bash
  • gcloud
  • Python3.8.2

FWルールの作成

FWルール1つあたりのIPアドレスが256個までに制限されているので、256個ずつルールを作成しています。
タグは国単位で付けました。
ブロックリストを変えると他のルールの作成が可能です。

firewall.sh
#!/bin/bash

# ソースIP範囲の最大数
MAX_RANGES=255
# ブロックリスト[連想配列]
#   key   : ファイアウォールのタグ名
#   value : ブロックしたいIPアドレスの一覧取得URL
declare -A BLOCK_LIST=(
        ["block-china"]="https://ipv4.fetus.jp/cn.txt"
        ["block-australia"]="https://ipv4.fetus.jp/au.txt"
)

# FireWallルール作成
create_firewall_rules() {
    local rules_no=`printf %03d ${1}`
    local rules_name=${2}
    local ip_ranges=${3}

    gcloud compute firewall-rules create ${rules_name}${rules_no} \
    --direction=ingress --priority=100 --action=deny --rules tcp \
    --source-ranges=${ip_ranges} --target-tags ${rules_name}
}

main() {
  for rules_name in "${!BLOCK_LIST[@]}"; do
    local URL=${BLOCK_LIST[${rules_name}]}
    local input_file=`basename ${URL%\?*}`
    wget ${URL} -O ${input_file}

    # ソースIP範囲
    local ip_ranges=
    # FWルール名の連番
    local rules_no=0
    # ルール数カウンター
    local line_count=0
    while read line; do
      line_count=`expr ${line_count} + 1`

      # IPアドレスをカンマ区切りで連結する
      if [ -z ${ip_ranges} ]; then
        local comma=
      else
        local comma=,
      fi
      ip_ranges=${ip_ranges}${comma}${line}

      # 1ルールの最大IPアドレス数になったら
      if [ ${line_count} -gt ${MAX_RANGES} ]; then
        rules_no=`expr ${rules_no} + 1`
        create_firewall_rules ${rules_no} ${rules_name} ${ip_ranges}

        # 初期化
        ip_ranges=
        line_count=0
      fi

    done << END
    `grep -v '^\s*#' ${input_file} | grep -v '^\s*$'`
END

    rules_no=`expr ${rules_no} + 1`
    create_firewall_rules ${rules_no} ${rules_name} ${ip_ranges}
  done

}

main

実行ログ

bash-5.0# ./firewall.sh 
Connecting to ipv4.fetus.jp (172.67.130.105:443)
saving to 'au.txt'
au.txt               100% |********************************| 89239  0:00:00 ETA
'au.txt' saved
Creating firewall...
..Created [https://www.googleapis.com/compute/v1/projects/home-283704/global/firewalls/block-australia001].
done.
NAME                NETWORK  DIRECTION  PRIORITY  ALLOW  DENY  DISABLED
block-australia001  default  INGRESS    100              tcp   False
Creating firewall...
..Created [https://www.googleapis.com/compute/v1/projects/home-283704/global/firewalls/block-australia002].
done.
NAME                NETWORK  DIRECTION  PRIORITY  ALLOW  DENY  DISABLED
block-australia002  default  INGRESS    100              tcp   False
Creating firewall...
..Created [https://www.googleapis.com/compute/v1/projects/home-283704/global/firewalls/block-australia003].
done.
〜(省略)〜

Search Consoleでファイアウォールの設定を見るとこんな感じでルールが作られます。
image.png

FWルールを適用する

最後にGCEインスタンスのネットワークタグに作成したタグを指定して保存します。
image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0