溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Lynis掃描Linux 安全性

發(fā)布時間:2022-01-24 11:01:50 來源:億速云 閱讀:108 作者:kk 欄目:開發(fā)技術(shù)

這篇文章給大家介紹如何使用Lynis掃描Linux 安全性,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

Lynis是一個非常流行的開源安全審計工具能夠強化基于Linux和unix的系統(tǒng),自動生成一份關(guān)于機器安全的報。

安裝 Lynis

你的 Linux 軟件倉庫中可能有 Lynis。如果有的話,你可以用以下方法安裝它:

  1. dnf install lynis

  1. apt install lynis

然而,如果你的倉庫中的版本不是最新的,你最好從 GitHub 上安裝它。(我使用的是 Red Hat Linux 系統(tǒng),但你可以在任何 Linux 發(fā)行版上運行它)。就像所有的工具一樣,先在虛擬機上試一試是有意義的。要從 GitHub 上安裝它:

  1. cat /etc/redhat-release

  2. Red Hat Enterprise Linux Server release 7.8 (Maipo)

  3. $

  4. uname  -r

  5. 3.10.0-1127.el7.x86_64

  6. $

  7. git clone https://github.com/CISOfy/lynis.git

  8. Cloning into 'lynis'...

  9. remote: Enumerating objects: 30, done.

  10. remote: Counting objects: 100% (30/30), done.

  11. remote: Compressing objects: 100% (30/30), done.

  12. remote: Total 12566 (delta 15), reused 8 (delta 0), pack-reused 12536

  13. Receiving objects: 100% (12566/12566), 6.36 MiB | 911.00 KiB/s, done.

  14. Resolving deltas: 100% (9264/9264), done.

  15. $

一旦你克隆了這個版本庫,那么進入該目錄,看看里面有什么可用的。主要的工具在一個叫 lynis 的文件里。它實際上是一個 shell 腳本,所以你可以打開它看看它在做什么。事實上,Lynis 主要是用 shell 腳本來實現(xiàn)的:

  1. cd lynis/

  2. ls

  3. CHANGELOG.md        CONTRIBUTING.md  db           developer.prf  FAQ             include  LICENSE  lynis.8  README     SECURITY.md

  4. CODE_OF_CONDUCT.md  CONTRIBUTORS.md  default.prf  extras         HAPPY_USERS.md  INSTALL  lynis    plugins  README.md

  5. $

  6. file lynis

  7. lynis: POSIX shell script, ASCII text executable, with very long lines

  8. $

運行 Lynis

通過給 Lynis 一個 -h 選項來查看幫助部分,以便有個大概了解:

  1. ./lynis -h

你會看到一個簡短的信息屏幕,然后是 Lynis 支持的所有子命令。

接下來,嘗試一些測試命令以大致熟悉一下。要查看你正在使用的 Lynis 版本,請運行:

  1. ./lynis show version

  2. 3.0.0

  3. $

要查看 Lynis 中所有可用的命令:

  1. ./lynis show commands


  2. Commands:

  3. lynis audit

  4. lynis configure

  5. lynis generate

  6. lynis show

  7. lynis update

  8. lynis upload-only


  9. $

審計 Linux 系統(tǒng)

要審計你的系統(tǒng)的安全態(tài)勢,運行以下命令:

  1. ./lynis audit system

這個命令運行得很快,并會返回一份詳細的報告,輸出結(jié)果可能一開始看起來很嚇人,但我將在下面引導你來閱讀它。這個命令的輸出也會被保存到一個日志文件中,所以你可以隨時回過頭來檢查任何可能感興趣的東西。

Lynis 將日志保存在這里:

  1.   Files:

  2.   - Test and debug information      : /var/log/lynis.log

  3.   - Report data                     : /var/log/lynis-report.dat

你可以驗證是否創(chuàng)建了日志文件。它確實創(chuàng)建了:

  1. ls -/var/log/lynis.log

  2. -rw-r-----. 1 root root 341489 Apr 30 05:52 /var/log/lynis.log

  3. $

  4. ls -/var/log/lynis-report.dat

  5. -rw-r-----. 1 root root 638 Apr 30 05:55 /var/log/lynis-report.dat

  6. $

探索報告

Lynis 提供了相當全面的報告,所以我將介紹一些重要的部分。作為初始化的一部分,Lynis 做的第一件事就是找出機器上運行的操作系統(tǒng)的完整信息。之后是檢查是否安裝了什么系統(tǒng)工具和插件:

  1. [+] Initializing program

  2. ------------------------------------

  3.  - Detecting OS...                                           [ DONE ]

  4.  - Checking profiles...                                      [ DONE ]


  5.  ---------------------------------------------------

  6.  Program version:           3.0.0

  7.  Operating system:          Linux

  8.  Operating system name:     Red Hat Enterprise Linux Server 7.8 (Maipo)

  9.  Operating system version:  7.8

  10.  Kernel version:            3.10.0

  11.  Hardware platform:         x86_64

  12.  Hostname:                  example

  13.  ---------------------------------------------------

  14. >


  15. [+] System Tools

  16. ------------------------------------

  17.  - Scanning available tools...

  18.  - Checking system binaries...


  19. [+] Plugins (phase 1)

  20. ------------------------------------

  21. Note: plugins have more extensive tests and may take several minutes to complete


  22.  - Plugin: pam

  23.    [..]

  24.  - Plugin: systemd

  25.    [................]

接下來,該報告被分為不同的部分,每個部分都以 [+] 符號開頭。下面可以看到部分章節(jié)。(哇,要審核的地方有這么多,Lynis 是最合適的工具?。?/p>

  1. [+] Boot and services

  2. [+] Kernel

  3. [+] Memory and Processes

  4. [+] Users, Groups and Authentication

  5. [+] Shells

  6. [+] File systems

  7. [+] USB Devices

  8. [+] Storage

  9. [+] NFS

  10. [+] Name services

  11. [+] Ports and packages

  12. [+] Networking

  13. [+] Printers and Spools

  14. [+] Software: e-mail and messaging

  15. [+] Software: firewalls

  16. [+] Software: webserver

  17. [+] SSH Support

  18. [+] SNMP Support

  19. [+] Databases

  20. [+] LDAP Services

  21. [+] PHP

  22. [+] Squid Support

  23. [+] Logging and files

  24. [+] Insecure services

  25. [+] Banners and identification

  26. [+] Scheduled tasks

  27. [+] Accounting

  28. [+] Time and Synchronization

  29. [+] Cryptography

  30. [+] Virtualization

  31. [+] Containers

  32. [+] Security frameworks

  33. [+] Software: file integrity

  34. [+] Software: System tooling

  35. [+] Software: Malware

  36. [+] File Permissions

  37. [+] Home directories

  38. [+] Kernel Hardening

  39. [+] Hardening

  40. [+] Custom tests

Lynis 使用顏色編碼使報告更容易解讀。

  • 綠色。一切正常

  • 黃色。跳過、未找到,可能有個建議

  • 紅色。你可能需要仔細看看這個

在我的案例中,大部分的紅色標記都是在 “Kernel Hardening” 部分找到的。內(nèi)核有各種可調(diào)整的設(shè)置,它們定義了內(nèi)核的功能,其中一些可調(diào)整的設(shè)置可能有其安全場景。發(fā)行版可能因為各種原因沒有默認設(shè)置這些,但是你應該檢查每一項,看看你是否需要根據(jù)你的安全態(tài)勢來改變它的值:

  1. [+] Kernel Hardening

  2. ------------------------------------

  3.   - Comparing sysctl key pairs with scan profile

  4.     - fs.protected_hardlinks (exp: 1)                         [ OK ]

  5.     - fs.protected_symlinks (exp: 1)                          [ OK ]

  6.     - fs.suid_dumpable (exp: 0)                               [ OK ]

  7.     - kernel.core_uses_pid (exp: 1)                           [ OK ]

  8.     - kernel.ctrl-alt-del (exp: 0)                            [ OK ]

  9.     - kernel.dmesg_restrict (exp: 1)                          [ DIFFERENT ]

  10.     - kernel.kptr_restrict (exp: 2)                           [ DIFFERENT ]

  11.     - kernel.randomize_va_space (exp: 2)                      [ OK ]

  12.     - kernel.sysrq (exp: 0)                                   [ DIFFERENT ]

  13.     - kernel.yama.ptrace_scope (exp: 1 2 3)                   [ DIFFERENT ]

  14.     - net.ipv4.conf.all.accept_redirects (exp: 0)             [ DIFFERENT ]

  15.     - net.ipv4.conf.all.accept_source_route (exp: 0)          [ OK ]

  16.     - net.ipv4.conf.all.bootp_relay (exp: 0)                  [ OK ]

  17.     - net.ipv4.conf.all.forwarding (exp: 0)                   [ OK ]

  18.     - net.ipv4.conf.all.log_martians (exp: 1)                 [ DIFFERENT ]

  19.     - net.ipv4.conf.all.mc_forwarding (exp: 0)                [ OK ]

  20.     - net.ipv4.conf.all.proxy_arp (exp: 0)                    [ OK ]

  21.     - net.ipv4.conf.all.rp_filter (exp: 1)                    [ OK ]

  22.     - net.ipv4.conf.all.send_redirects (exp: 0)               [ DIFFERENT ]

  23.     - net.ipv4.conf.default.accept_redirects (exp: 0)         [ DIFFERENT ]

  24.     - net.ipv4.conf.default.accept_source_route (exp: 0)      [ OK ]

  25.     - net.ipv4.conf.default.log_martians (exp: 1)             [ DIFFERENT ]

  26.     - net.ipv4.icmp_echo_ignore_broadcasts (exp: 1)           [ OK ]

  27.     - net.ipv4.icmp_ignore_bogus_error_responses (exp: 1)     [ OK ]

  28.     - net.ipv4.tcp_syncookies (exp: 1)                        [ OK ]

  29.     - net.ipv4.tcp_timestamps (exp: 0 1)                      [ OK ]

  30.     - net.ipv6.conf.all.accept_redirects (exp: 0)             [ DIFFERENT ]

  31.     - net.ipv6.conf.all.accept_source_route (exp: 0)          [ OK ]

  32.     - net.ipv6.conf.default.accept_redirects (exp: 0)         [ DIFFERENT ]

  33.     - net.ipv6.conf.default.accept_source_route (exp: 0)      [ OK ]

看看 SSH 這個例子,因為它是一個需要保證安全的關(guān)鍵領(lǐng)域。這里沒有什么紅色的東西,但是 Lynis 對我的環(huán)境給出了很多強化 SSH 服務的建議:

  1. [+] SSH Support

  2. ------------------------------------

  3.   - Checking running SSH daemon                               [ FOUND ]

  4.     - Searching SSH configuration                             [ FOUND ]

  5.     - OpenSSH option: AllowTcpForwarding                      [ SUGGESTION ]

  6.     - OpenSSH option: ClientAliveCountMax                     [ SUGGESTION ]

  7.     - OpenSSH option: ClientAliveInterval                     [ OK ]

  8.     - OpenSSH option: Compression                             [ SUGGESTION ]

  9.     - OpenSSH option: FingerprintHash                         [ OK ]

  10.     - OpenSSH option: GatewayPorts                            [ OK ]

  11.     - OpenSSH option: IgnoreRhosts                            [ OK ]

  12.     - OpenSSH option: LoginGraceTime                          [ OK ]

  13.     - OpenSSH option: LogLevel                                [ SUGGESTION ]

  14.     - OpenSSH option: MaxAuthTries                            [ SUGGESTION ]

  15.     - OpenSSH option: MaxSessions                             [ SUGGESTION ]

  16.     - OpenSSH option: PermitRootLogin                         [ SUGGESTION ]

  17.     - OpenSSH option: PermitUserEnvironment                   [ OK ]

  18.     - OpenSSH option: PermitTunnel                            [ OK ]

  19.     - OpenSSH option: Port                                    [ SUGGESTION ]

  20.     - OpenSSH option: PrintLastLog                            [ OK ]

  21.     - OpenSSH option: StrictModes                             [ OK ]

  22.     - OpenSSH option: TCPKeepAlive                            [ SUGGESTION ]

  23.     - OpenSSH option: UseDNS                                  [ SUGGESTION ]

  24.     - OpenSSH option: X11Forwarding                           [ SUGGESTION ]

  25.     - OpenSSH option: AllowAgentForwarding                    [ SUGGESTION ]

  26.     - OpenSSH option: UsePrivilegeSeparation                  [ OK ]

  27.     - OpenSSH option: AllowUsers                              [ NOT FOUND ]

  28.     - OpenSSH option: AllowGroups                             [ NOT FOUND ]

我的系統(tǒng)上沒有運行虛擬機或容器,所以這些顯示的結(jié)果是空的:

  1. [+] Virtualization

  2. ------------------------------------


  3. [+] Containers

  4. ------------------------------------

Lynis 會檢查一些從安全角度看很重要的文件的文件權(quán)限:

  1. [+] File Permissions

  2. ------------------------------------

  3.   - Starting file permissions check

  4.     File: /boot/grub2/grub.cfg                                [ SUGGESTION ]

  5.     File: /etc/cron.deny                                      [ OK ]

  6.     File: /etc/crontab                                        [ SUGGESTION ]

  7.     File: /etc/group                                          [ OK ]

  8.     File: /etc/group-                                         [ OK ]

  9.     File: /etc/hosts.allow                                    [ OK ]

  10.     File: /etc/hosts.deny                                     [ OK ]

  11.     File: /etc/issue                                          [ OK ]

  12.     File: /etc/issue.net                                      [ OK ]

  13.     File: /etc/motd                                           [ OK ]

  14.     File: /etc/passwd                                         [ OK ]

  15.     File: /etc/passwd-                                        [ OK ]

  16.     File: /etc/ssh/sshd_config                                [ OK ]

  17.     Directory: /root/.ssh                                     [ SUGGESTION ]

  18.     Directory: /etc/cron.d                                    [ SUGGESTION ]

  19.     Directory: /etc/cron.daily                                [ SUGGESTION ]

  20.     Directory: /etc/cron.hourly                               [ SUGGESTION ]

  21.     Directory: /etc/cron.weekly                               [ SUGGESTION ]

  22.     Directory: /etc/cron.monthly                              [ SUGGESTION ]

在報告的底部,Lynis 根據(jù)報告的發(fā)現(xiàn)提出了建議。每項建議后面都有一個 “TEST-ID”(為了下一部分方便,請將其保存起來)。

  1. Suggestions (47):

  2.  ----------------------------

  3.  * If not required, consider explicit disabling of core dump in /etc/security/limits.conf file [KRNL-5820]

  4.      https://cisofy.com/lynis/controls/KRNL-5820/


  5.  * Check PAM configuration, add rounds if applicable and expire passwords to encrypt with new values [AUTH-9229]

  6.      https://cisofy.com/lynis/controls/AUTH-9229/

Lynis 提供了一個選項來查找關(guān)于每個建議的更多信息,你可以使用 show details 命令和 TEST-ID 號來訪問:

  1. ./lynis show details TEST-ID

這將顯示該測試的其他信息。例如,我檢查了 SSH-7408 的詳細信息:

  1. ./lynis show details SSH-7408

  2. 2020-04-30 05:52:23 Performing test ID SSH-7408 (Check SSH specific defined options)

  3. 2020-04-30 05:52:23 Test: Checking specific defined options in /tmp/lynis.k8JwazmKc6

  4. 2020-04-30 05:52:23 Result: added additional options for OpenSSH < 7.5

  5. 2020-04-30 05:52:23 Test: Checking AllowTcpForwarding in /tmp/lynis.k8JwazmKc6

  6. 2020-04-30 05:52:23 Result: Option AllowTcpForwarding found

  7. 2020-04-30 05:52:23 Result: Option AllowTcpForwarding value is YES

  8. 2020-04-30 05:52:23 Result: OpenSSH option AllowTcpForwarding is in a weak configuration state and should be fixed

  9. 2020-04-30 05:52:23 Suggestion: Consider hardening SSH configuration [test:SSH-7408] [details:AllowTcpForwarding (set YES to NO)] [solution:-]

什么是Linux系統(tǒng)

Linux是一種免費使用和自由傳播的類UNIX操作系統(tǒng),是一個基于POSIX的多用戶、多任務、支持多線程和多CPU的操作系統(tǒng),使用Linux能運行主要的Unix工具軟件、應用程序和網(wǎng)絡(luò)協(xié)議。

關(guān)于如何使用Lynis掃描Linux 安全性就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI