コマンド

aptitude install dnsutils
dig 211.5.115.209
host -a  211.5.115.209
jwhois CF211005115209.cims.jp

TCP Wrapperの設定

特定サービスのみ許可する

# /etc/hosts.deny
ALL : ALL
dovecot :  ALL
# /etc/hosts.allow
ALL : 111.222.xxx.xxx 211.xxx.xxx.xxx
ALL : 192.168.1.
sshd : .jp EXCEPT 219.117.209.7 59.106.23.215
sshd : .jp example.com example.net
dovecot :  ALL

特定サービスのみ拒否する

例 接続元192.168.0.10からのデーモンプログラムftpd(21/tcp)に対するアクセスのみを禁止する。それ以外からのアクセスはすべて許可する。

# /etc/hosts.deny
ftpd: 192.168.0.10

# /etc/hosts.allow
なし

ファイアウォール

◇iptables -L 設定確認

——————————

◇設定リセット

方法1.

iptables -D チェイン ルール番号

iptables -D INPUT 1

ルール番号は↓のコマンドで調べられる

iptables -L –line-numbers

方法2.

iptables -D チェイン ルール内容

例えば

iptables -D INPUT -p tcp -s 192.168.1.1 –dport ssh -j DROP

◇特定IPアクセス拒否

iptables -I INPUT -s  185.12.222.42 -j DROP
iptables save 設定保存 でないと、再起動できえてしまう

※既存の /etc/sysconfig/iptables ファイルは /etc/sysconfig/iptables.save として保存される

設定したルールを任意のファイルに書き出すコマンドiptables-save

iptables-save > ファイル名

iptables-saveによって設定を書き出しておき、ホストマシンを再起動後、書き出しておいたファイルを下記のコマンドにより再読み込みすると、書き出されたルールが改めて設定される。

iptables-restore < ファイル名

設定内容は/etc/sysconfig/iptablesに保存される。当該ファイルを直接開いて、設定内容が反映されているか確認する。

◇接続回数/ipの制限

1. 接続回数を制限する(IPアドレスごと)

これにより特定ホストからの大量アクセス、DoS攻撃を緩和することが可能

#!/bin/bash

iptables -P INPUT DROP

#sshは5回まで新規接続許可それ以降は1分に1回に制限、6分間無接続なら制限解除
iptables -A INPUT -p tcp –dport 22 -m state –state NEW -m hashlimit \
  –hashlimit-name ssh_limit –hashlimit 1/m –hashlimit-burst 5 \
  –hashlimit-mode srcip –hashlimit-htable-expire 360000 -j ACCEPT
#webは700回まで新規接続許可それ以降は1秒に1回に制限、6分無接続なら制限解除
iptables -A INPUT -p tcp -m multiport –dport 80,443 -m state –state NEW -m hashlimit \
  –hashlimit-name web_limit –hashlimit 60/m –hashlimit-burst 700 \
  –hashlimit-mode srcip –hashlimit-htable-expire 360000 -j ACCEPT

2. 接続回数を制限する(サービスごと)

limitを使った制限は全ホストが等しく制限を受けるため、ssh等に設定すべきでない。
(攻撃を受けている間は自分たちも制限されるため)

#!/bin/bash
iptables -P INPUT DROP

#webは1000回までは許可、以降は1分間に120回までに制限する
iptables -A INPUT -p tcp -m state –state NEW –dport 80 -m limit –limit 120/m –limit-burst 1000 -j ACCEPT

3. 接続IPアドレスを限定する

IPアドレスの国別割り当てをAPNIC等から取得してコマンドを作る
この手のルールは長くなるので、ユーザー定義チェインにしたほうが見やすくなる

#!/bin/bash
iptables -P INPUT DROP
iptables -N ACCEPT_IP
#webは日本国内からのアクセスに限定
iptables -A INPUT -p tcp -m multiport –dport 80,443 -j ACCEPT_IP
#WebサーバへのDoS攻撃対処
iptables -A INPUT -p tcp -m multiport –dport 80,443 -m state –state NEW -m hashlimit \
    –hashlimit-name web_limit –hashlimit 60/m –hashlimit-burst 700 \
    –hashlimit-mode srcip –hashlimit-htable-expire 360000 -j ACCEPT

#SMTPS,IMAPSは日本国内からのアクセスに限定
iptables -A INPUT -p tcp -m multiport –dport 465,993 -j ACCEPT_IP

#25番はどこからでもアクセスさせる
iptables -A INPUT -p tcp –dport 25 -j ACCEPT

# 補足)
# この例では送信は587ではなくより安全な465(SMTPS)を使う
# 25番を開放しないと受信ができないため開放
# (メールサーバ間は25番でやりとりされるため)
# 第三者中継かどうかはあて先が自ドメインかそうでないかの判定が必要になるためIPベースでは行えない
# MTA側で設定すべき

あとはこんな感じのスクリプトを用意して
iptables.pl

#!/usr/bin/perl
while (<STDIN>){
    next unless /^apnic\|JP\|ipv4\|(.+)\|(.+)\|\d+\|allocated/;
    print “iptables -A $ARGV[0] -s $1/”.(32-log($2)/log(2)).” -j RETURN\n”;
}
print “iptables -A $ARGV[0] -j DROP\n”;

rootでこんな感じのコマンドを打てばIP制限チェインの出来上がり
# wget http://ftp.apnic.net/stats/apnic/delegated-apnic-latest \
-O – | ./iptables.pl ACCEPT_IP | bash

4 Replies to “サーバーセキュリティ”

    1. admin says:

      110ポートを外部アクセス不可にする

      iptables -F 設定リセット
      iptables -nL 設定確認
      iptables -P INPUT ACCEPT ※デフォ=受け入れ(すでに書いてあるから不要)

      iptables -A INPUT -p tcp –dport(宛先ポート) 110 -j DROP
      iptables -A INPUT -p tcp –sport(送信元ポート) 110 -j DROP ※不要?

      service iptables save 内容をセーブ①
      iptables-save 内容をセーブ②
      ※①②かどちらか実行

      ※-s ipアドレス
      iptables -A INPUT -p tcp –-dport 110 -s 211.5.115.209 -j ACCEPT

      返信
      1. admin says:

        pop110は、 211.5.115.209のみ受け入れ、その他は拒否 ※順番間違えると、すべてアクセスできなくなる!
        iptables -A INPUT -p tcp –-dport 110 -s 211.5.115.209 -j ACCEPT
        iptables -A INPUT -p tcp –dport(宛先ポート) 110 -j DROP

        返信
  1. admin says:

    まとめ

    110ポートを外部アクセス不可にする
    任意 iptables -F 設定リセット
    (ここから)
    iptables -nL
    iptables -P INPUT ACCEPT
    iptables -A INPUT -p tcp –-dport 110 -s 211.5.115.209 -j ACCEPT
    iptables -A INPUT -p tcp ––dport 110 -j DROP
    (ここまで)

    rebootすると設定が消えてしまう・・・

    iptables-persistentをインストールすると、それまでに登録されているiptablesの情報が永続化される。
    (/etc/iptables/rules.v4 と /etc/iptables/rules.v6が起動時に読み込まれる)

    インストール

    // 必要に応じてapt-get updateも
    $ sudo apt-get install iptables-persistent
    これで再起動しても残るようになる

    p.s

    保存やリロードは・・・

    $ sudo /etc/init.d/iptables-persistent save
    $ sudo /etc/init.d/iptables-persistent reload

    // または直で

    $ sudo iptables-restore < /etc/iptables/rules.v4

    返信

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です