Skip to content

Instantly share code, notes, and snippets.

@sezemiadmin
Last active April 18, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sezemiadmin/1e9c4f8414d1950c3d3e78d980d3e1eb to your computer and use it in GitHub Desktop.
Save sezemiadmin/1e9c4f8414d1950c3d3e78d980d3e1eb to your computer and use it in GitHub Desktop.
安全なLinux環境を構築してみよう のコマンドや設定ファイルなど
  1. 監査ルールを確認
# auditctl -l
No rules
  1. 監査ルールを指定
# vi /etc/audit/audit.rules
# This file contains the auditctl rules that are loaded
# whenever the audit daemon is started via the initscripts.
# The rules are simply the parameters that would be passed
# to auditctl.

# First rule - delete all
-D

# Increase the buffers to survive stress events.
# Make this bigger for busy systems
-b 320

# Feel free to add below this line. See auditctl man page
/* 一番下にルールを書く
サンプルの書式
-w /etc/passwd -p wa -k passwd_changes */
  1. 記録の確認
# tail /var/log/audit/audit.log
type=USER_AUTH msg=audit(1548727536.902:20): user pid=1368 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=success acct="root" exe="/usr/sbin/sshd" hostname=? addr=192.168.2.63 terminal=ssh res=success'
type=CRED_ACQ msg=audit(1548727536.902:21): user pid=1368 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:sshd_t:s0-s0:c0.c1023 msg='op=PAM:setcred acct="root" exe="/usr/sbin/sshd" hostname=192.168.2.63 addr=192.168.2.63 terminal=ssh res=success'
:
:

ただし、タイムスタンプが ctime の1970年4月1日から何時間経過したかとなっているので、これを変えましょう。

# ausearch -k password_changes | more
  • まずハッシュ化されたパスワードを作成
# grub-md5-crypt
Password:
Retype password:
$1$b/K2E0$g4EpYodue9JYMdiY8Jzdr1
  • 作成したパスワードを設定ファイルに入力
# vi /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu 
/* ここに先程のハッシュ値を入れる
password --md5 $1$b/K2E0$g4EpYodue9JYMdiY8Jzdr1 */
title CentOS (2.6.32-754.10.1.el6.x86_64)
  • 開いているポートを確認
# ss -natu
Netid  State      Recv-Q Send-Q     Local Address:Port       Peer Address:Port
udp    UNCONN     0      0                      *:68                    *:*
tcp    LISTEN     0      100                  ::1:25                   :::*
tcp    LISTEN     0      100            127.0.0.1:25                    *:*
tcp    LISTEN     0      128                   :::22                   :::*
tcp    LISTEN     0      128                    *:22                    *:*
:
  • ファイアウォールの設定を確認しましょう
# iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
 2028  240K ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere    
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere    
    1    52 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh
 2143  235K REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination 
    0     0 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 1335 packets, 216K bytes)
 pkts bytes target     prot opt in     out     source               destination
  • 雑だけど、SELinuxを一時的に停止して、改めて動作してみる
    • うまく行けば、SELinuxが原因、そうでなければ違うとして切り分け
  • # getenforce -> ステータス確認
    • Enforcing と表示されれば有効化されている
  • # setenforce 0 -> SELinuxをオフ
    • Permissive になり、遮断はしてないが、ログはとっているという状態
  • 他の方法
    • サービス毎にSELinuxの制限実施のON/OFFを設定
# getsebool -a *膨大に出てくるので関係しそうなもので grep するとよいです
abrt_anon_write --> off
abrt_handle_event --> off
allow_console_login --> on
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
allow_daemons_use_tcp_wrapper --> off
:
:

で、off のときは

# setsebool -P [サービス名] 1
$ ssh-keygen -t rsa
$ cd .ssh のディレクトリに移動
$ cat id_rsa.pub >> authorized_keys
$ chomd 600 authrized_keys # 600 は本人しか読み書きできない
  • 稼働中のサービスを確認
# chkconfig | grep 3:on # 3: が自動起動しているもの
auditd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
blk-availability        0:off   1:on    2:on    3:on    4:on    5:on    6:off
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
ip6tables       0:off   1:off   2:on    3:on    4:on    5:on    6:off
iptables        0:off   1:off   2:on    3:on    4:on    5:on    6:off
iscsi           0:off   1:off   2:off   3:on    4:on    5:on    6:off
iscsid          0:off   1:off   2:off   3:on    4:on    5:on    6:off
lvm2-monitor    0:off   1:on    2:on    3:on    4:on    5:on    6:off
mdmonitor       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netfs           0:off   1:off   2:off   3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
udev-post       0:off   1:on    2:on    3:on    4:on    5:on    6:off
yum-cron        0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 不要なサービスがあれば停止
# service iscsi stop
iscsi を停止中:                                            [  OK  ]
:
:

ただ、これを停止しても、reboot するとまた起動するので、自動起動を無効化することもできます。

# chkconfig iscsi off
  • コマンド
# yum -y install yum-cron
# vi /etc/sysconfig/yum-cron # vi エディタに慣れましょう
  • 設定ファイル編集
# Check to see if you can reach the repos before updating (valid: yes|no)
CHECK_FIRST=yes
:
:
# by default MAILTO is unset, so crond mails the output by itself
# example:  MAILTO=root
MAILTO=root
:
:
# you may set DAYS_OF_WEEK to the days of the week you want to run
# default is every day
#DAYS_OF_WEEK="0123456" // 実行する日 (0が日曜, 6が土曜)
  • コマンド (apply)
# service yum-cron start
夜間 yum 更新の有効化中:                                   [  OK  ]
# chkconfig yum-cron on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment