Skip to content

Instantly share code, notes, and snippets.

@mfakane
Last active February 15, 2018 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfakane/6e48d0c4535a9f25a30063911adbf6ee to your computer and use it in GitHub Desktop.
Save mfakane/6e48d0c4535a9f25a30063911adbf6ee to your computer and use it in GitHub Desktop.
国による IP ブロック
#!/bin/bash
cd /tmp
curl -O http://nami.jp/ipv4bycc/cidr.txt.gz && gunzip -f cidr.txt.gz
ipset create -exist WHITELIST hash:net
ipset flush WHITELIST
sed -n 's/^JP\t//p' cidr.txt | while read ADDRESS; do ipset add WHITELIST $ADDRESS; done
ipset create -exist BLACKLIST hash:net
ipset flush BLACKLIST
sed -n 's/^\(CH\|AU\)\t//p' cidr.txt | while read ADDRESS; do ipset add BLACKLIST $ADDRESS; done
ipset save
iptables -F
iptables -X
iptables -Z
# IN 既定でドロップ
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# INPUT
# ループバックは許可
iptables -A INPUT -i lo -j ACCEPT
# セッション確立後の通信は許可
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# SSH 許可
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -m set --match-set WHITELIST src -j ACCEPT
iptables-save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment