2011年1月1日土曜日

さくらのVPSでredmine-CentOS基本設定

さくらのVPSのデフォルトOSはCentOSです、
無難なOSで使い慣れているOSなので、そのまま使用するのですが、最低限のセキュリティの設定を行う。

前回で使用準備完了して、コンソールコマンドが打てる状況です。

  • デフォルトだとSSHでroot(管理者権限)ログイン出来るが、悪い人がパスワード総当たり攻撃する時に、まず狙うユーザー名がrootなので、rootでログイン出来ない様にします。

#SSHでログインするユーザー"henohenomoheji"を作る。
useradd -m henohenomoheji
#作ったユーザーのバスワードを設定する。
passwd henohenomoheji


#設定ファイルのバックアップフォルダ作成
mkdir /etc/ssh/00Backup
#変更前の設定ファイルをコピー
cp /etc/ssh/sshd_config /etc/ssh/00Backup/sshd_config_`date +%Y%m%d%H%M`

#"/etc/ssh/sshd_config"ファイルを編集viの使い方はこちら
vi /etc/ssh/sshd_config
#"sshd_config"ファイルで"#PermitRootLogin yes"を探し、その下にでも"PermitRootLogin no"を挿入する。



#sshデーモンを再起動
/etc/init.d/sshd restart


#以上で次のログインからは、rootでログイン出来ないはずなので、実際にログインを試みて確認する。
  • ボットからの攻撃をさける為に、SSHのポート番号を変える。※今回の例では5022に変更する。
#変更前の設定ファイルをコピー
cp /etc/ssh/sshd_config /etc/ssh/00Backup/sshd_config_`date +%Y%m%d%H%M`
#"/etc/ssh/sshd_config"ファイルを編集
vi /etc/ssh/sshd_config
#"sshd_config"ファイルで"#Port 22"を探し、その下にでも"Port 5022"を挿入する。


#sshデーモンを再起動
/etc/init.d/sshd restart


#以上で設定完了※ポート番号が変わるので新しいポート番号で接続する。

  • 必要なポート以外を閉じる
#iptablesの設定スクリプトを作る。
vi ~/iptables-init.sh
#以下の内容で"~/iptables-init.sh"を保存する。

#!/bin/sh

iptables -F
iptables -X

iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -s 192.168.0.0/16 -j DROP

# iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#スクリプト実行
sh /root/iptables-init.sh
#設定結果表示
iptables -L
#結果が以下の内容なら成功

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
DROP       all  --  10.0.0.0/8           anywhere
DROP       all  --  172.16.0.0/12        anywhere
DROP       all  --  192.168.0.0/16       anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

#設定結果を保存
iptables-save > /etc/sysconfig/iptables
#iptablesを再起動
/etc/rc.d/init.d/iptables restart
#iptablesの起動設定を確認
chkconfig --list |grep iptables
#結果が以下の様に2/3/4/5がonならOK
#iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
#違っていたら以下を実行
# chkconfig --level 2345 iptables on

0 件のコメント:

コメントを投稿