インターネットに公開したサーバは、不正アクセス・改竄など常に攻撃の的となっています。
セキュリティ対策を行なわないと、自サーバはもちろんの事、第三者への攻撃の踏み台となってしまう可能性があります。
どんな対策をしても完全に防ぐことができるわけではありませんが、最低限知っておきたいセキュリティについて紹介します。
外部からサーバへの侵入の可能性を減らすために不要なサービスは停止しましょう。
ルータやファイアウォールでブロックしているから大丈夫と思ってはいけません。
LAN内にサーバが一つだけなら管理もし易いですが、もし複数存在していた場合に別のサーバが侵入されて踏台として悪用されるケースもあります。
サーバの負荷も減り是非とも行なっておきたい事の一つです。
サービス状態の表示
【SysV services 系の場合】
# service --status-all
jserverは停止しています
httは停止しています
:
:
xfs (pid 1342) を実行中...
xinetd (pid 1316) を実行中...
ypbindは停止しています
【systemd services 系の場合】
# systemctl -a
UNIT LOAD ACTIVE SUB JOB DESCRIPTION
crond.service loaded active running Command Scheduler
cups.service loaded inactive dead CUPS Printing Service
:
:
postfix.service loaded inactive dead LSB: start and stop postfix
poweroff.service loaded inactive dead Power-Off
prefdm.service loaded active running Display Manager
例)FTPサーバの停止
【SysV services 系の場合】
# /etc/rc.d/init.d/vsftpd stop
vsftpd を停止中: [ OK ]
【systemd services 系の場合】
注) vsftpdはバージョンによって停止方法が異なるので注意『FTPサーバの構築(vsFTPD)』参照
# systemctl stop vsftpd.service
サービスを止めただけではOSの再起動時に自動起動してしまいます。
以下のコマンドを入力し再起動時に起動しないようにします
OS起動時にFTPサーバを起動しない
【SysV services 系の場合】
# chkconfig vsftpd off
# chkconfig --list vsftpd
vsftpd 0:オフ 1:オフ 2:オフ 3:オフ 4:オフ 5:オフ 6:オフ
【systemd services 系の場合】
注) vsftpdはバージョンによって自動起動停止・確認方法が異なるので注意『FTPサーバの構築(vsFTPD)』参照
# systemctl disable vsftpd.service
# systemctl status vsftpd.service
vsftpd.service - Vsftpd ftp daemon
Loaded: loaded (/lib/systemd/system/vsftpd.service; disabled)
Active: inactive (dead)
CGroup: name=systemd:/system/vsftpd.service
|
サーバの上位にルータが存在している場合は、ルータの機能を使用しポートを閉鎖しましょう。
例えば、上記で例えると、「telnet」が使用するポート番号は23番です。Puttyに変更しSSHにすれば22番を開きます。
また、「ftp」は20と21番でWinSCPに変更すれば、こちらもSSHで22番になります。
SSHはセキュアな為、telnet・ftpより安全です。
サーバが直接外部とつながっている場合は、特に確認下さい。
不要ポートが開いていればいる程、危険性が増します。
nmapのインストール # yum -y install nmap ポート状況確認 # nmap localhost Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2007-02-19 23:23 JST Interesting ports on localhost.localdomain (127.0.0.1): Not shown: 1679 closed ports PORT STATE SERVICE 22/tcp open ssh Nmap finished: 1 IP address (1 host up) scanned in 0.401 seconds ポートの閉じ方は該当するサービスを停止するか、コマンドの「iptables」を使用します。 「iptables」コマンドの使用方法は他のサイトを参照してください |
ターミナルをsshに暗号化したからといって十分ではありません。何せ通信内容が暗号化されただけですから・・・
当URLで紹介しているSSHサーバの構築(OpenSSH)で、まず、基本セキュアを実施してください。(rootでログオンさせない等・・)
以下の設定を行なうことにより、接続元のネットワークを限定する事が出来ます。
サーバへsshログオンを許可するネットワークの指定 # vi /etc/hosts.allow # # hosts.allow This file describes the names of the hosts which are # allowed to use the local INET services, as decided # by the '/usr/sbin/tcpd' server. # sshd: 192.168.1. サーバへsshログオンを遮断するネットワークの指定 # vi /etc/hosts.deny # # hosts.deny This file describes the names of the hosts which are # *not* allowed to use the local INET services, as decided # by the '/usr/sbin/tcpd' server. # # The portmap line is redundant, but it is left to remind you that # the new secure portmap uses hosts.deny and hosts.allow. In particular # you should know that NFS uses portmap! sshd: ALL 「hosts.allow」で指定されたもの以外が「hosts.denny」になります。 分かりやすく説明すると上記の設定では、192.168.1.xxxからはsshログオンを認めるが、 それ以外は認めないという様になります。 複数指定したい場合はカンマで区切ります。「192.168.1. , 211.100.123. , ・・・」 |
以下の設定を行なうことにより、ログオンユーザを限定する事が出来ます。上記と組み合わせる事により、ネットワーク内の限定ユーザのみログオン可という様に強化する事が出来ます。
サーバへsshログオンを許可するユーザの指定
# vi /etc/pam.d/sshd
#%PAM-1.0
先頭行に以下を追加
account required pam_access.so
auth required pam_sepermit.so
auth include system-auth
account required pam_nologin.so
account include system-auth
password include system-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session optional pam_keyinit.so force revoke
session include system-auth
# vi /etc/security/access.conf
# Login access control table.
#
:
:
最終行に以下を追加
-:ALL EXCEPT wheel linux:ALL
上記を設定する事により、sshログインは「wheel」グループと「linux」ユーザのみになります。
|
root のパスワードを知っていれば誰でもroot(管理者)になることができてしまいます。
以下の設定を行なうことにより、一般ユーザからroot(管理者)になれるユーザを限定する事が可能になります。(suコマンドの制限)
サーバへsshログオンを許可するユーザの指定 # vi /etc/login.defs 最終行に以下を追加 SU_WHEEL_ONLY yes # vi /etc/pam.d/su #%PAM-1.0 auth sufficient pam_rootok.so # Uncomment the following line to implicitly trust users in the "wheel" group. #auth sufficient pam_wheel.so trust use_uid # Uncomment the following line to require a user to be in the "wheel" group. コメントを外す auth required pam_wheel.so use_uid auth include system-auth account sufficient pam_succeed_if.so uid = 0 use_uid quiet account include system-auth password include system-auth session include system-auth session optional pam_xauth.so ユーザ"linux"のセカンダリグループに"wheel"を追加 # usermod -G wheel linux 上記を設定する事により、一般ユーザ"linux"がroot(管理者)になれます。 *:逆に言うとセカンダリグループ"wheel"に属さないユーザはroot(管理者)に なれないという事になります |
メールの送受信は行ないたいが、サーバログインはさせたくない場合があると思います。
以下の設定を行なうことによりログインシェルを無効にさせてログインさせなくする事が出来ます。
新規にユーザを作成する場合 # useradd -s /bin/false <ユーザ名> 既に存在しているユーザに設定する場合 # usermod -s /bin/false <ユーザ名> |