LoginSignup
38
42

More than 5 years have passed since last update.

Linuxセキュリティ監査ツール Lynis

Last updated at Posted at 2016-08-08

Lynis

https://cisofy.com/lynis/
2019/02/18 現在の最新版は 2019/01/30 リリースの Lynis 2.7.1 1 になる。

ランキング

ランキング 順位 発表日
Black Hat Arsenal Top 10 Security Tools as Voted by the Audience 6位 2018/01/24
2016 Top Security Tools as Voted by ToolsWatch.org Readers 10位 2017/02/11
2015 Top Security Tools as Voted by ToolsWatch.org Readers 2位 2016/02/23

インストール

CISOfy Software

cat << "_EOF_" >> /etc/yum.repos.d/cisofy-lynis.repo
[lynis]
name=CISOfy Software - Lynis package
baseurl=https://packages.cisofy.com/community/lynis/rpm/
enabled=1
gpgkey=https://packages.cisofy.com/keys/cisofy-software-rpms-public.key
gpgcheck=1
_EOF_
yum install lynis

コミュニティ版の公式リポジトリが用意されているが、repo ファイルは手打ちする必要がある。 2
最新版の Lynis 2.7.1 がインストールされる

EPEL

EPEL リポジトリでは 2.7.0 が、また EPEL Testing リポジトリでは 2.7.1 がインストールできる。

yum install epel-release
yum install lynis

プラグイン

epel リポジトリからインストールすると pam と systemd のプラグインが含まれていないので GitHub から入手する。 plugins ディレクトリに配置すれば自動で認識するため特に設定の変更は必要ない。

curl -s https://raw.githubusercontent.com/CISOfy/lynis/master/plugins/{plugin_systemd_phase1} -o /usr/share/lynis/plugins/#1
curl -s https://raw.githubusercontent.com/CISOfy/lynis/master/plugins/{plugin_pam_phase1} -o /usr/share/lynis/plugins/#1

検査

通常モード

lynis audit system
出力例
[ Lynis 2.4.8 ]

################################################################################
  Lynis comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
  welcome to redistribute it under the terms of the GNU General Public License.
  See the LICENSE file for details about using this software.

  2007-2017, CISOfy - https://cisofy.com/lynis/
  Enterprise support available (compliance, plugins, interface and tools)
################################################################################


[+] Initializing program
------------------------------------
  - Detecting OS...                                           [ DONE ]
  - Checking profiles...                                      [ DONE ]
  - Detecting language and localization                       [ ja ]
.
.
.
================================================================================

  Lynis security scan details:

  Hardening index : 69 [#############       ]
  Tests performed : 217
  Plugins enabled : 2

  Components:
  - Firewall               [V]
  - Malware scanner        [X]

  Lynis Modules:
  - Compliance Status      [?]
  - Security Audit         [V]
  - Vulnerability Scan     [V]

  Files:
  - Test and debug information      : /var/log/lynis.log
  - Report data                     : /var/log/lynis-report.dat

================================================================================

  Lynis 2.4.8

  Auditing, system hardening, and compliance for UNIX-based systems
  (Linux, macOS, BSD, and others)

  2007-2017, CISOfy - https://cisofy.com/lynis/
  Enterprise support available (compliance, plugins, interface and tools)

================================================================================

  [TIP]: Enhance Lynis audits by adding your settings to custom.prf (see /etc/lynis/default.prf for all settings)

詳細モード

lynis audit system --verbose

--verbose オプションをつけると詳細を出力してくれる。具体的には以下の差分が生じた。

45a46,50
> [+] Program Details
> ------------------------------------
>   - 詳細モード                                                     [ YES ]
>   - デバッグモード                                                   [ NO ]
>
133a139
>   - Querying FFS/UFS mount points (fstab)                     [ なし ]
142a149
>   - /var/tmp is not bound to /tmp                             [ INFO ]
157a165,166
>   - Checking default DNS search domain                        [ なし ]
>   - Checking /etc/resolv.conf options                         [ なし ]
159a169,173
>   - Checking nscd status                                      [ 見つかりません ]
>   - Checking Unbound status                                   [ 見つかりません ]
>   - Checking BIND status                                      [ 見つかりません ]
>   - Checking PowerDNS status                                  [ 見つかりません ]
>   - Checking ypbind status                                    [ 見つかりません ]
198a213
>   - Exim status                                               [ 見つかりません ]
200a216
>       - Postfix configuration errors                          [ OK ]
201a218,220
>   - Dovecot status                                            [ 見つかりません ]
>   - Qmail status                                              [ 見つかりません ]
>   - Sendmail status                                           [ 見つかりません ]
294a314
>   - Checking atd status                                       [ 起動していません ]
332a353,360
>     - AFICK                                                   [ 見つかりません ]
>     - AIDE                                                    [ 見つかりません ]
>     - Osiris                                                  [ 見つかりません ]
>     - Samhain                                                 [ 見つかりません ]
>     - Tripwire                                                [ 見つかりません ]
>     - OSSEC                                                   [ 見つかりません ]
>     - mtree                                                   [ 見つかりません ]
>     - osquery daemon (syscheck)                               [ 見つかりません ]
346a375,376
>     /etc/lilo.conf                                            [ 見つかりません ]
>     /root/.ssh                                                [ 見つかりません ]

検査結果

CentOS 7.3.1611 (1702-01) の minimal インストールの yum update 済みデフォルト状態では以下の内容が出力された。

Warnings (1):

  • Found mail_name in SMTP banner, and/or mail_name contains 'Postfix' MAIL-8818

Suggestions (34):

  • Configure minimum password age in /etc/login.defs AUTH-9286
  • Configure maximum password age in /etc/login.defs AUTH-9286
  • Default umask in /etc/profile could be more strict like 027 AUTH-9328
  • To decrease the impact of a full /tmp file system, place /tmp on a separated partition FILE-6310
  • To decrease the impact of a full /var file system, place /var on a separated partition FILE-6310
  • Disable drivers like USB storage when not used, to prevent unauthorized storage or data theft STRG-1840
  • Disable drivers like firewire storage when not used, to prevent unauthorized storage or data theft STRG-1846
  • Split resolving between localhost and the hostname of the system NAME-4406
  • Install package 'yum-utils' for better consistency checking of the package database PKGS-7384
  • Install ARP monitoring software like arpwatch NETW-3032
  • You are advised to hide the mail_name (option: smtpd_banner) from your postfix configuration. Use postconf -e or change your main.cf file (/etc/postfix/main.cf) MAIL-8818
  • Check iptables rules to see which rules are currently not used FIRE-4513
  • Consider hardening SSH configuration SSH-7408
    • Details: AllowTcpForwarding (YES --> NO)
    • Details: ClientAliveCountMax (3 --> 2)
    • Details: Compression (DELAYED --> NO)
    • Details: LogLevel (INFO --> VERBOSE)
    • Details: MaxAuthTries (6 --> 1)
    • Details: MaxSessions (10 --> 2)
    • Details: PermitRootLogin (YES --> NO)
    • Details: Port (22 --> )
    • Details: TCPKeepAlive (YES --> NO)
    • Details: UseDNS (YES --> NO)
    • Details: X11Forwarding (YES --> NO)
    • Details: AllowAgentForwarding (YES --> NO)
  • Add a legal banner to /etc/issue, to warn unauthorized users BANN-7126
  • Add legal banner to /etc/issue.net, to warn unauthorized users BANN-7130
  • Enable process accounting ACCT-9622
  • Enable sysstat to collect accounting (no results) ACCT-9626
  • Audit daemon is enabled with an empty ruleset. Disable the daemon or define rules ACCT-9630
  • Install a file integrity tool to monitor changes to critical and sensitive files FINT-4350
  • Determine if automation tools are present for system management TOOL-5002
  • One or more sysctl values differ from the scan profile and could be tweaked KRNL-6000
  • Harden compilers like restricting access to root user only HRDN-7222
  • Harden the system by installing at least one malware scanner, to perform periodic file system scans HRDN-7230

対応

Warnings

  1. SMTP banner に 'Postfix' を含む mail_name が見つかりました

    sed -i.org "s/#smtpd_banner = \$myhostname ESMTP \$mail_name$/smtpd_banner = \$myhostname ESMTP/" /etc/postfix/main.cf
    

    /etc/postfix/main.cf に smtpd_banner = $myhostname ESMTP を設定する。

  2. iptables モジュールがロードされていますが有効なルールがありません

Suggestions

  1. /etc/login.defs でパスワード変更不可能期間を設定してください

    sed -i.org 's/PASS_MIN_DAYS\t0/PASS_MIN_DAYS\t1/' /etc/login.defs
    

    PASS_MIN_DAYS はパスワードを変更してから次に変更できるようになるまでの最短日数。

  2. /etc/login.defs でパスワード有効期限を設定してください

    sed -i 's/PASS_MAX_DAYS\t99999/PASS_MAX_DAYS\t3650/' /etc/login.defs
    
  3. /etc/profile のデフォルト umask は 027 などより厳しく設定できます

    sed -i "s/umask 002/umask 027/" /etc/profile
    sed -i "s/umask 022/umask 027/" /etc/profile
    

    umask値 027 はパーミッションの 740 に対応する。

  4. /tmp の容量超過時の影響を減らすために /tmp を別のパーティションに置いてください
    /var の容量超過時の影響を減らすために /var を別のパーティションに置いてください

    6.10.4.5. 推奨されるパーティション設定スキーム - RHEL7 インストールガイド
    9.15.5. パーティション設定に関する推奨 - RHEL6 インストールガイド

  5. 不正なストレージやデータ窃盗を防ぐために USB ストレージなどのドライバーを使わない場合は無効にしてください

    echo "blacklist usb-storage" > /etc/modprobe.d/blacklist-usbstorage.conf
    
  6. 不正なストレージやデータ窃盗を防ぐために firewire ストレージなどのドライバーを使わない場合は無効にしてください

    echo "blacklist firewire_core" > /etc/modprobe.d/blacklist-firewire.conf
    echo "blacklist firewire_ohci" >> /etc/modprobe.d/blacklist-firewire.conf
    
  7. localhost とシステムの hostname の解決を分離してください

    sed -i '/::1/s/^/#/' /etc/hosts
    

    ::1 をコメントアウトすると Suggestion に出なくはなるが…。

  8. パッケージデータベースのチェックのより良い一貫性を保つために 'yum-utils' パッケージをインストールしてください

    yum install yum-utils
    
  9. arpwatch のような ARP モニタリングソフトウェアをインストールしてください

    yum install arpwatch
    systemctl enable arpwatch
    systemctl start arpwatch
    
  10. You are advised to hide the mail_name (option: smtpd_banner) from your postfix configuration. Use postconf -e or change your main.cf file (/etc/postfix/main.cf)

  11. SSH の設定のセキュリティ強化を検討してください

    cp -a /etc/ssh/sshd_config{,.org}
    
    1. Details: AllowTcpForwarding (YES --> NO)

      sed -i 's/#AllowTcpForwarding yes/AllowTcpForwarding no/' /etc/ssh/sshd_config
      
    2. Details: ClientAliveCountMax (3 --> 2)

      sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 2/' /etc/ssh/sshd_config
      
    3. Details: Compression (DELAYED --> NO)

      sed -i 's/#Compression delayed/Compression no/' /etc/ssh/sshd_config
      
    4. Details: LogLevel (INFO --> VERBOSE)

      sed -i 's/#LogLevel INFO/LogLevel VERBOSE/' /etc/ssh/sshd_config
      
    5. Details: MaxAuthTries (6 --> 1)

      sed -i 's/#MaxAuthTries 6/MaxAuthTries 1/' /etc/ssh/sshd_config
      
    6. Details: MaxSessions (10 --> 2)

      sed -i 's/#MaxSessions 10/MaxSessions 2/' /etc/ssh/sshd_config
      
    7. Details: PermitRootLogin (YES --> NO)

      sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
      
    8. Details: Port (22 --> )

      sed -i 's/#Port 22/Port 10022/' /etc/ssh/sshd_config
      
    9. Details: TCPKeepAlive (YES --> NO)

      sed -i 's/#TCPKeepAlive yes/TCPKeepAlive no/' /etc/ssh/sshd_config
      
    10. Details: UseDNS (YES --> NO)

      sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
      
    11. Details: X11Forwarding (YES --> NO)

      sed -i 's/X11Forwarding yes/X11Forwarding no/' /etc/ssh/sshd_config
      
    12. Details: AllowAgentForwarding (YES --> NO)

      sed -i 's/#AllowAgentForwarding yes/AllowAgentForwarding no/' /etc/ssh/sshd_config
      
  12. 不正ユーザーに警告するために legal banner を /etc/issue に追加してください

    (echo; echo "Unauthorized access to this machine is prohibited."; echo "Disconnect now if you are not an authorized user."; echo) > /etc/issue
    
  13. 不正ユーザーに警告するために legal banner を /etc/issue.net に追加してください

    (echo; echo "Unauthorized access to this machine is prohibited."; echo "Disconnect now if you are not an authorized user."; echo) > /etc/issue.net
    
  14. プロセスアカウンティングを有効にしてください

    yum install psacct
    systemctl enable psacct
    systemctl start psacct
    

    lastcomm コマンドで履歴を出力できるようになる。

  15. アカウンティングを収集するために sysstat を有効にしてください (no results)

    yum install sysstat
    
  16. Audit デーモンが空のルールセットで有効になっています。デーモンを無効にするか、ルールを定義してください

    echo '-a exit,always -F arch=b64 -F euid=0 -S execve' >> /etc/audit/audit.rules
    echo '-a exit,always -F arch=b32 -F euid=0 -S execve' >> /etc/audit/audit.rules
    service auditd restart
    

    systemctl restart auditd を実行すると Failed to restart auditd.service: Operation refused, unit auditd.service may be requested by dependency only. と言われて失敗するので service コマンドを使う。3

    7.5. Audit ルールの定義

  17. システム管理の自動化ツールがあれば決定してください

    yum install puppet-server
    systemctl enable puppetmaster
    systemctl start puppetmaster
    
    yum install salt-master
    systemctl enable salt-master
    systemctl start salt-master
    

    puppetmaster か salt-master のいずれか。

  18. 1つ以上の sysctl の値がスキャンプロファイルと異なり調整の余地があります

    lynis --quick --verbose --tests KRNL-6000
    grep "different value" /var/log/lynis.log | awk '{ gsub("Expected",""); gsub(",",""); print $6 $16 }'
    grep "different value" /var/log/lynis.log | awk '{ gsub("Expected",""); gsub(",",""); print $6 $16 }' >> /etc/sysctl.conf
    sysctl -p
    
  19. root ユーザーのみにアクセスを制限するなどコンパイラのセキュリティを強化してください

    lynis --quick --verbose --tests HRDN-7222
    grep "world executable" /var/log/lynis.log | awk '{ print $5 }' | xargs ls -l
    grep "world executable" /var/log/lynis.log | awk '{ print $5 }' | xargs chmod 700
    
  20. 定期的なファイルシステムのスキャンを実行するために、少なくとも一つのマルウェアスキャナーをインストールすることでシステムのセキュリティ強化してください

    総合ウイルス検出
    • ClamAV または商用の代替製品
    ルートキット検出
    Webサーバー用マルウェア検出

対策まとめ

sed -i.org "s/#smtpd_banner = \$myhostname ESMTP \$mail_name$/smtpd_banner = \$myhostname ESMTP/" /etc/postfix/main.cf
sed -i.org 's/PASS_MIN_DAYS\t0/PASS_MIN_DAYS\t1/' /etc/login.defs
sed -i 's/PASS_MAX_DAYS\t99999/PASS_MAX_DAYS\t3650/' /etc/login.defs
sed -i.org "s/umask 002/umask 027/" /etc/profile
sed -i "s/umask 022/umask 027/" /etc/profile
echo "blacklist usb-storage" > /etc/modprobe.d/blacklist-usbstorage.conf
echo "blacklist firewire_core" > /etc/modprobe.d/blacklist-firewire.conf
echo "blacklist firewire_ohci" >> /etc/modprobe.d/blacklist-firewire.conf
sed -i '/::1/s/^/#/' /etc/hosts
yum install yum-utils arpwatch psacct sysstat
systemctl enable arpwatch psacct
systemctl start arpwatch psacct
sed -i.org 's/#AllowTcpForwarding yes/AllowTcpForwarding no/' /etc/ssh/sshd_config
sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 2/' /etc/ssh/sshd_config
sed -i 's/#Compression delayed/Compression no/' /etc/ssh/sshd_config
sed -i 's/#LogLevel INFO/LogLevel VERBOSE/' /etc/ssh/sshd_config
sed -i 's/#MaxAuthTries 6/MaxAuthTries 1/' /etc/ssh/sshd_config
sed -i 's/#MaxSessions 10/MaxSessions 2/' /etc/ssh/sshd_config
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#Port 22/Port 10022/' /etc/ssh/sshd_config
sed -i 's/#TCPKeepAlive yes/TCPKeepAlive no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/X11Forwarding yes/X11Forwarding no/' /etc/ssh/sshd_config
sed -i 's/#AllowAgentForwarding yes/AllowAgentForwarding no/' /etc/ssh/sshd_config
(echo; echo "Unauthorized access to this machine is prohibited."; echo "Disconnect now if you are not an authorized user."; echo) > /etc/issue
(echo; echo "Unauthorized access to this machine is prohibited."; echo "Disconnect now if you are not an authorized user."; echo) > /etc/issue.net
lynis --quick --verbose --tests KRNL-6000
grep "different value" /var/log/lynis.log | awk '{ gsub("Expected",""); gsub(",",""); print $6 $16 }'
grep "different value" /var/log/lynis.log | awk '{ gsub("Expected",""); gsub(",",""); print $6 $16 }' >> /etc/sysctl.conf
sysctl -p
lynis --quick --verbose --tests HRDN-7222
grep "world executable" /var/log/lynis.log | awk '{ print $5 }' | xargs ls -l
grep "world executable" /var/log/lynis.log | awk '{ print $5 }' | xargs chmod 700
yum install epel-release
yum install rkhunter unhide
rkhunter -c --rwo --sk

セキュリティ関連記事

38
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
42