debian 系 iptables 設定
自宅サーバで公開しているサイトへの SEO 関連の robot である semrush.com からのアクセスが、少しうざい。
(SEO 関連のロボットであるという根拠は、リファラー内情報 https://www.semrush.com/bot/ に記載されている)
robots.txt でクローリングを拒否しているけど、その robots.txt 自体はアクセス拒否ができない。
(これを拒否すると、アクセス拒否を伝えられない)
しばらく放置していたけど、/robots.txt へのアクセスが連日あるので iptables で拒否設定しようと思った。
まず、semrush.com が保有している IP アドレス帯域の調査から。
https://awebanalysis.com/ja/ip-lookup/46.229.168.158/
46.229.160.0/20 の帯域は、オランダに割り当てられている模様。
ちょっと範囲が広すぎるので、もう少し狭めてみる。
ドメイン名・IPアドレス検索 (ANSI Whois) - Asuka.IO
ここで、IPアドレスを入力。
https://ja.asuka.io/whois/46.229.168.149
一部を抜粋。
inetnum: 46.229.168.0 - 46.229.169.255
netname: ADVANCEDHOSTERS-NET
descr: Advanced Hosters B.V.
country: US
admin-c: AH36-RIPE
tech-c: AH36-RIPE
status: ASSIGNED PA
ふむ。
46.229.168.0/23 の帯域で、アメリカに割り当てられていることは分かった。
いったん、46.229.168.0/24 の帯域で、まるっと逆引きしてみる。
# for i in {1..254}; do > tmp=$(dig -x 46.229.168.$i +short) > if [ -n "$tmp" ]; then > echo "### 46.229.168.$i ### ${tmp}" > fi > done ### 46.229.168.11 ### docker6-iad.ag1.thousandeyes.com. ### 46.229.168.14 ### ns2.z5o.net. ### 46.229.168.26 ### turanga-leela.ompr.io. ### 46.229.168.27 ### amy-wong.ompr.io. ### 46.229.168.49 ### leela.ompr.io. ### 46.229.168.56 ### mx3.xyzsmtpservice.com. ### 46.229.168.57 ### mx2.thewebsupports.su. ### 46.229.168.129 ### crawl1.bl.semrush.com. ### 46.229.168.130 ### crawl2.bl.semrush.com. ### 46.229.168.131 ### crawl3.bl.semrush.com. ### 46.229.168.132 ### crawl4.bl.semrush.com. ### 46.229.168.133 ### crawl5.bl.semrush.com. ### 46.229.168.134 ### crawl6.bl.semrush.com. ### 46.229.168.135 ### crawl7.bl.semrush.com. ### 46.229.168.136 ### crawl8.bl.semrush.com. ### 46.229.168.137 ### crawl9.bl.semrush.com. ### 46.229.168.138 ### crawl10.bl.semrush.com. ### 46.229.168.139 ### crawl11.bl.semrush.com. ### 46.229.168.140 ### crawl12.bl.semrush.com. ### 46.229.168.141 ### crawl13.bl.semrush.com. ### 46.229.168.142 ### crawl14.bl.semrush.com. ### 46.229.168.143 ### crawl15.bl.semrush.com. ### 46.229.168.144 ### crawl16.bl.semrush.com. ### 46.229.168.145 ### crawl17.bl.semrush.com. ### 46.229.168.146 ### crawl18.bl.semrush.com. ### 46.229.168.147 ### crawl19.bl.semrush.com. ### 46.229.168.148 ### crawl20.bl.semrush.com. ### 46.229.168.149 ### crawl21.bl.semrush.com. ### 46.229.168.150 ### crawl22.bl.semrush.com. ### 46.229.168.151 ### crawl23.bl.semrush.com. ### 46.229.168.152 ### crawl24.bl.semrush.com. ### 46.229.168.153 ### crawl25.bl.semrush.com. ### 46.229.168.154 ### crawl26.bl.semrush.com. ### 46.229.168.155 ### crawl27.bl.semrush.com. ### 46.229.168.156 ### crawl28.bl.semrush.com. ### 46.229.168.157 ### crawl29.bl.semrush.com.
おー。
見事に semrush.com が利用中なIPアドレスが表示されたね。
46.229.168.129 ~ 157 の 29 個ということは、おそらく /27 の 32 個を確保しているんじゃないかな……という予測がたつ。
46.229.168.128 は、おそらくネットワークアドレス
46.229.168.159 は、おそらくブロードキャストアドレス
TCP/IP の CIDR から、こうであろうという予測。
……46.229.168.158 だけが宙に浮いてるけど、この IP アドレス帯域を利用しているのは、上記の割当情報から US であり、つまりはアメリカの人たち。
サイトは日本語しかないし、拒否しても大丈夫かな?
一応、何に使われているのか調査してみる。
46.229.168.158 への ping は、応答あり。なきゃ良かったのに……
ICMP の他、www とか ssh とか 応答なし。
ん~、これに関してのみ、とりあえず許可する方向でいくか。
ということで、設定内容としては 46.229.168.128/27 を Drop することに。
ただし様子見のため、拒否設定の save はしない。
46.229.168.158 は ACCEPT を入れる。
それでは、拒否設定の投入。
# iptables -A OUTPUT -d 46.229.168.128/27 -o eth0 -j DROP # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DROP all -- anywhere 46.229.168.0/24 # # iptables -A OUTPUT -d 46.229.168.158/32 -o eth0 -j ACCEPT # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination DROP all -- anywhere 46.229.168.128/27 ACCEPT all -- anywhere 46.229.168.158 #
ここまで設定してから「DROP と ACCEPT の優先順位ってどうなってたっけ?」と思って、ググってみた。
参考サイト様
……見事に逆順で設定してたね。
初期化したいときは --flush か -F を使う。
# iptables -F # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination #
初期化できた。
# iptables -A OUTPUT -d 46.229.168.158/32 -o eth0 -j ACCEPT # iptables -A OUTPUT -d 46.229.168.128/27 -o eth0 -j DROP # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere 46.229.168.158 DROP all -- anywhere 46.229.168.128/27 #
順番が入れ替わってるね。
さて、しばらくこれで様子見。
一応、保存したくなった時のことを考えて、調べておく。
環境としては Raspbian を利用している。
# cat /etc/os-release PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)" NAME="Raspbian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)" ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
で、いろいろググってみたんだけど、RHEL (CentOS) の情報が多くて、適用できるかどうかが不明だった。
いちおう、これなら大丈夫かなという参照先を。
参考サイト:
https://wiki.debian.org/iptables
ここで書かれている iptables 設定情報の保存方法は2通り。
一つ目は、シェルスクリプトを作って起動時に読み込ませる方法。
Debian では、保存先のファイルを /etc/iptables.up.rules としているみたい。
すでに iptables コマンドで設定しているときは、iptables-save の標準出力を、ファイルにリダイレクトする。
# # ls -l /etc/iptables.up.rules ls: '/etc/iptables.up.rules' にアクセスできません: そのようなファイルやディレクトリはありません # iptables-save > /etc/iptables.up.rules
で、これをリストアするときは、次の簡単なシェルスクリプトを書けと。
/etc/network/if-pre-up.d/iptables
#!/bin/sh /sbin/iptables-restore < /etc/iptables.up.rules
二つ目は、管理パッケージを使う方法。
iptables-persistent
# apt-cache search iptables-persistent iptables-persistent - boot-time loader for netfilter rules, iptables plugin netscript-ipfilter - Linux 2.6/3.x iptables management system.
「boot-time loader for netfilter rules, iptables plugin」は、意訳すると「起動時にフィルタルールを読み込む iptables のプラグイン」かな。
これをインストールしてから、次のファイルを作る。
iptables-save > /etc/iptables/rules.v4 ip6tables-save > /etc/iptables/rules.v6
うーん、やるならパッケージのインストールかな?
# dpkg -l iptables iptables-persistent 要望=(U)不明/(I)インストール/(R)削除/(P)完全削除/(H)保持 | 状態=(N)無/(I)インストール済/(C)設定/(U)展開/(F)設定失敗/(H)半インストール/(W)トリガ待ち/(T)トリガ保留 |/ エラー?=(空欄)無/(R)要再インストール (状態,エラーの大文字=異常) ||/ 名前 バージョン アーキテクチャ 説明 +++-====================-===============-===============-============================================== ii iptables 1.6.0+snapshot2 armhf administration tools for packet filtering and dpkg-query: iptables-persistent に一致するパッケージが見つかりません
ということなので、自分がやるなら次の手順。
$ sudo su - # apt-get update # apt-get install iptables-persistent # # iptables -F # iptables -A OUTPUT -d 46.229.168.158/32 -o eth0 -j ACCEPT # iptables -A OUTPUT -d 46.229.168.128/27 -o eth0 -j DROP # iptables -L # # iptables-save > /etc/iptables/rules.v4 # ip6tables-save > /etc/iptables/rules.v6 # # exit $