- #!/bin/sh
- # インストール
- apt install -y openssh-server fail2ban;
- systemctl enable ssh;
- # ----
- # 削除
- # ----
- Func(){
- sed "/$1/d" $2 > /tmp/$$.tmp;
- mv -f /tmp/$$.tmp $2;
- return 0;
- };
- # プロトコル
- Func '^Protocol' '/etc/ssh/sshd_config';
- # 接続を許可するユーザ
- Func '^AllowUsers' '/etc/ssh/sshd_config';
- # 接続のインターバル
- Func '^ClientAliveInterval' '/etc/ssh/sshd_config';
- # 試行回数の最大値
- Func '^ClientAliveCountMax' '/etc/ssh/sshd_config';
- # rootでのログイン
- Func '^PermitRootLogin' '/etc/ssh/sshd_config';
- # 空のパスワード
- Func '^PermitEmptyPasswords' '/etc/ssh/sshd_config';
- # 公開鍵での認証
- Func '^PubkeyAuthentication' '/etc/ssh/sshd_config';
- # パスワードでの認証
- Func '^PasswordAuthentication' '/etc/ssh/sshd_config';
- # 一時ファイルの削除
- rm -r -f /tmp/$$*;
- # ----
- # 追加
- # ----
- # プロトコルを指定
- echo 'Protocol 2' >> /etc/ssh/sshd_config;
- # 許可するユーザ
- echo "AllowUsers $(logname)@127.0.0.1" >> /etc/ssh/sshd_config;
- echo "AllowUsers $(logname)@192.168.*" >> /etc/ssh/sshd_config;
- # インターバル
- echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config;
- # 試行回数
- echo 'ClientAliveCountMax 2' >> /etc/ssh/sshd_config;
- # rootでのログインを拒否
- echo 'PermitRootLogin no' >> /etc/ssh/sshd_config;
- # 空のパスワードでのログインを拒否
- echo 'PermitEmptyPasswords no' >> /etc/ssh/sshd_config;
- # パスワードでの認証を許可
- echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config;
- # 公開鍵でのログインを許可
- echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config;
- # 公開鍵での認証を許可し、パスワードでの認証を許可しないようにする場合
- # echo 'PasswordAuthentication no' >> /etc/ssh/sshd_config;
- # sshのリスタート
- systemctl restart ssh;
- # fail2banの設定ファイルを作成
- cat /dev/null > /etc/fail2ban/jail.local;
- # 書き込み
- echo '[DEFAULT]' >> /etc/fail2ban/jail.local;
- echo 'ignoreip = 127.0.0.1/8' >> /etc/fail2ban/jail.local;
- # BANする時間(秒)
- echo 'bantime = 86400' >> /etc/fail2ban/jail.local;
- # findtimeの時間内にmaxretry回続けば遮断される
- echo 'findtime = 600' >> /etc/fail2ban/jail.local;
- echo 'maxretry = 3' >> /etc/fail2ban/jail.local;
- echo '[sshd]' >> /etc/fail2ban/jail.local;
- echo 'enabled = true' >> /etc/fail2ban/jail.local;
- # ポートを変えてる場合、設定する
- echo '# port = 22' >> /etc/fail2ban/jail.local;
- # 再起動
- systemctl stop fail2ban;
- systemctl start fail2ban;
- exit;
- わざとパスワードを間違えてアクセスするとlogファイルにBanされた情報が確認できる
- /var/log/fail2ban.log
- NOTICE [sshd] Ban 192.168.*.*
0 件のコメント:
コメントを投稿