如何使用Lynis扫描Linux 安全性
导读:本文共29989.5字符,通常情况下阅读需要100分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: Lynis是一个非常流行的开源安全审计工具能够强化基于Linux和unix的系统,自动生成一份关于机器安全的报。安装 Lynis你的 Linux 软件仓库中可能有 Lynis。如果有的话,你可以用以下方法安装它:dnf install lynis或apt install lynis然而,如果你的仓库中的版本不是最新的,你最好从 GitHub 上安装它。(我使用的... ...
目录
(为您整理了一些要点),点击可以直达。- (8):$
- (11):$
- (20):$
- (23):$
- (26):$
- (29):$
- (36):$
- (48):$
- (59):$
- (62):$
- (65):---------------------------
- (78):>
- (81):---------------------------
- (86):---------------------------
- (90): [..]
- (92): [.......
- (140):---------------------------
- (174):---------------------------
- (203):---------------------------
- (206):---------------------------
- (209):---------------------------
- (232):---------
Lynis是一个非常流行的开源安全审计工具能够强化基于Linux和unix的系统,自动生成一份关于机器安全的报。
你的 Linux 软件仓库中可能有 Lynis。如果有的话,你可以用以下方法安装它:
dnf install lynis
或
apt install lynis
然而,如果你的仓库中的版本不是最新的,你最好从 GitHub 上安装它。(我使用的是 Red Hat Linux 系统,但你可以在任何 Linux 发行版上运行它)。就像所有的工具一样,先在虚拟机上试一试是有意义的。要从 GitHub 上安装它:
$cat/etc/redhat-release
RedHatEnterpriseLinuxServerrelease7.8(Maipo)
$
$uname-r
3.10.0-1127.el7.x86_64
$
$gitclonehttps://github.com/CISOfy/lynis.git
Cloninginto'lynis'...
remote:Enumeratingobjects:30,done.
remote:Countingobjects:100%(30/30),done.
remote:Compressingobjects:100%(30/30),done.
remote:Total12566(delta15),reused8(delta0),pack-reused12536
Receivingobjects:100%(12566/12566),6.36MiB|911.00KiB/s,done.
Resolvingdeltas:100%(9264/9264),done.
$
一旦你克隆了这个版本库,那么进入该目录,看看里面有什么可用的。主要的工具在一个叫lynis的文件里。它实际上是一个 shell 脚本,所以你可以打开它看看它在做什么。事实上,Lynis 主要是用 shell 脚本来实现的:
$cdlynis/
$ls
CHANGELOG.md CONTRIBUTING.md db developer.prf FAQ includeLICENSE lynis.8README SECURITY.md
CODE_OF_CONDUCT.md CONTRIBUTORS.md default.prf extras HAPPY_USERS.md INSTALL lynis plugins README.md
$
$filelynis
lynis:POSIX shellscript,ASCII text executable,withverylonglines
$
通过给 Lynis 一个-h选项来查看帮助部分,以便有个大概了解:
$./lynis-h
你会看到一个简短的信息屏幕,然后是 Lynis 支持的所有子命令。
接下来,尝试一些测试命令以大致熟悉一下。要查看你正在使用的 Lynis 版本,请运行:
$./lynis show version
3.0.0
$
要查看 Lynis 中所有可用的命令:
$./lynis show commands
Commands:
lynis audit
lynis configure
lynis generate
lynis show
lynis update
lynis upload-only
$
要审计你的系统的安全态势,运行以下命令:
$./lynis audit system
这个命令运行得很快,并会返回一份详细的报告,输出结果可能一开始看起来很吓人,但我将在下面引导你来阅读它。这个命令的输出也会被保存到一个日志文件中,所以你可以随时回过头来检查任何可能感兴趣的东西。
Lynis 将日志保存在这里:
Files:
-Testanddebug information :/var/log/lynis.log
-Reportdata :/var/log/lynis-report.dat
你可以验证是否创建了日志文件。它确实创建了:
$ls-l/var/log/lynis.log
-rw-r-----.1root root341489Apr3005:52/var/log/lynis.log
$
$ls-l/var/log/lynis-report.dat
-rw-r-----.1root root638Apr3005:55/var/log/lynis-report.dat
$
Lynis 提供了相当全面的报告,所以我将介绍一些重要的部分。作为初始化的一部分,Lynis 做的第一件事就是找出机器上运行的操作系统的完整信息。之后是检查是否安装了什么系统工具和插件:
[+]Initializingprogram
------------------------------------
-DetectingOS... [DONE]
-Checkingprofiles... [DONE]
---------------------------------------------------
Programversion: 3.0.0
Operatingsystem: Linux
Operatingsystem name: RedHatEnterpriseLinuxServer7.8(Maipo)
Operatingsystem version:7.8
Kernelversion: 3.10.0
Hardwareplatform: x86_64
Hostname: example
---------------------------------------------------
>
[+]SystemTools
------------------------------------
-Scanningavailable tools...
-Checkingsystem binaries...
[+]Plugins(phase1)
------------------------------------
Note:plugins havemoreextensive testsandmay take several minutes to complete
-Plugin:pam
[..]
-Plugin:systemd
[................]
接下来,该报告被分为不同的部分,每个部分都以[+]符号开头。下面可以看到部分章节。(哇,要审核的地方有这么多,Lynis 是最合适的工具!)
[+]Bootandservices
[+]Kernel
[+]MemoryandProcesses
[+]Users,GroupsandAuthentication
[+]Shells
[+]Filesystems
[+]USBDevices
[+]Storage
[+]NFS
[+]Nameservices
[+]Portsandpackages
[+]Networking
[+]PrintersandSpools
[+]Software:e-mailandmessaging
[+]Software:firewalls
[+]Software:webserver
[+]SSHSupport
[+]SNMPSupport
[+]Databases
[+]LDAPServices
[+]PHP
[+]SquidSupport
[+]Loggingandfiles
[+]Insecureservices
[+]Bannersandidentification
[+]Scheduledtasks
[+]Accounting
[+]TimeandSynchronization
[+]Cryptography
[+]Virtualization
[+]Containers
[+]Securityframeworks
[+]Software:fileintegrity
[+]Software:Systemtooling
[+]Software:Malware
[+]FilePermissions
[+]Homedirectories
[+]KernelHardening
[+]Hardening
[+]Customtests
Lynis 使用颜色编码使报告更容易解读。
绿色。一切正常
黄色。跳过、未找到,可能有个建议
红色。你可能需要仔细看看这个
在我的案例中,大部分的红色标记都是在 “Kernel Hardening” 部分找到的。内核有各种可调整的设置,它们定义了内核的功能,其中一些可调整的设置可能有其安全场景。发行版可能因为各种原因没有默认设置这些,但是你应该检查每一项,看看你是否需要根据你的安全态势来改变它的值:
[+]KernelHardening
------------------------------------
-Comparingsysctlkey pairswithscan profile
-fs.protected_hardlinks(exp:1) [OK]
-fs.protected_symlinks(exp:1) [OK]
-fs.suid_dumpable(exp:0) [OK]
-kernel.core_uses_pid(exp:1) [OK]
-kernel.ctrl-alt-del(exp:0) [OK]
-kernel.dmesg_restrict(exp:1) [DIFFERENT]
-kernel.kptr_restrict(exp:2) [DIFFERENT]
-kernel.randomize_va_space(exp:2) [OK]
-kernel.sysrq(exp:0) [DIFFERENT]
-kernel.yama.ptrace_scope(exp:123) [DIFFERENT]
-net.ipv4.conf.all.accept_redirects(exp:0) [DIFFERENT]
-net.ipv4.conf.all.accept_source_route(exp:0) [OK]
-net.ipv4.conf.all.bootp_relay(exp:0) [OK]
-net.ipv4.conf.all.forwarding(exp:0) [OK]
-net.ipv4.conf.all.log_martians(exp:1) [DIFFERENT]
-net.ipv4.conf.all.mc_forwarding(exp:0) [OK]
-net.ipv4.conf.all.proxy_arp(exp:0) [OK]
-net.ipv4.conf.all.rp_filter(exp:1) [OK]
-net.ipv4.conf.all.send_redirects(exp:0) [DIFFERENT]
-net.ipv4.conf.default.accept_redirects(exp:0) [DIFFERENT]
-net.ipv4.conf.default.accept_source_route(exp:0) [OK]
-net.ipv4.conf.default.log_martians(exp:1) [DIFFERENT]
-net.ipv4.icmp_echo_ignore_broadcasts(exp:1) [OK]
-net.ipv4.icmp_ignore_bogus_error_responses(exp:1) [OK]
-net.ipv4.tcp_syncookies(exp:1) [OK]
-net.ipv4.tcp_timestamps(exp:01) [OK]
-net.ipv6.conf.all.accept_redirects(exp:0) [DIFFERENT]
-net.ipv6.conf.all.accept_source_route(exp:0) [OK]
-net.ipv6.conf.default.accept_redirects(exp:0) [DIFFERENT]
-net.ipv6.conf.default.accept_source_route(exp:0) [OK]
看看 SSH 这个例子,因为它是一个需要保证安全的关键领域。这里没有什么红色的东西,但是 Lynis 对我的环境给出了很多强化 SSH 服务的建议:
[+]SSHSupport
------------------------------------
-Checkingrunning SSH daemon [FOUND]
-SearchingSSH configuration [FOUND]
-OpenSSHoption:AllowTcpForwarding [SUGGESTION]
-OpenSSHoption:ClientAliveCountMax [SUGGESTION]
-OpenSSHoption:ClientAliveInterval [OK]
-OpenSSHoption:Compression [SUGGESTION]
-OpenSSHoption:FingerprintHash [OK]
-OpenSSHoption:GatewayPorts [OK]
-OpenSSHoption:IgnoreRhosts [OK]
-OpenSSHoption:LoginGraceTime [OK]
-OpenSSHoption:LogLevel [SUGGESTION]
-OpenSSHoption:MaxAuthTries [SUGGESTION]
-OpenSSHoption:MaxSessions [SUGGESTION]
-OpenSSHoption:PermitRootLogin [SUGGESTION]
-OpenSSHoption:PermitUserEnvironment [OK]
-OpenSSHoption:PermitTunnel [OK]
-OpenSSHoption:Port [SUGGESTION]
-OpenSSHoption:PrintLastLog [OK]
-OpenSSHoption:StrictModes [OK]
-OpenSSHoption:TCPKeepAlive [SUGGESTION]
-OpenSSHoption:UseDNS [SUGGESTION]
-OpenSSHoption:X11Forwarding [SUGGESTION]
-OpenSSHoption:AllowAgentForwarding [SUGGESTION]
-OpenSSHoption:UsePrivilegeSeparation [OK]
-OpenSSHoption:AllowUsers [NOT FOUND]
-OpenSSHoption:AllowGroups [NOT FOUND]
我的系统上没有运行虚拟机或容器,所以这些显示的结果是空的:
[+]Virtualization
------------------------------------
[+]Containers
------------------------------------
Lynis 会检查一些从安全角度看很重要的文件的文件权限:
[+]FilePermissions
------------------------------------
-Startingfilepermissions check
File:/boot/grub2/grub.cfg [SUGGESTION]
File:/etc/cron.deny [OK]
File:/etc/crontab [SUGGESTION]
File:/etc/group [OK]
File:/etc/group- [OK]
File:/etc/hosts.allow [OK]
File:/etc/hosts.deny [OK]
File:/etc/issue [OK]
File:/etc/issue.net [OK]
File:/etc/motd [OK]
File:/etc/passwd [OK]
File:/etc/passwd- [OK]
File:/etc/ssh/sshd_config [OK]
Directory:/root/.ssh [SUGGESTION]
Directory:/etc/cron.d [SUGGESTION]
Directory:/etc/cron.daily [SUGGESTION]
Directory:/etc/cron.hourly [SUGGESTION]
Directory:/etc/cron.weekly [SUGGESTION]
Directory:/etc/cron.monthly [SUGGESTION]
在报告的底部,Lynis 根据报告的发现提出了建议。每项建议后面都有一个 “TEST-ID”(为了下一部分方便,请将其保存起来)。
Suggestions(47):
----------------------------
*Ifnotrequired,considerexplicitdisabling of coredumpin/etc/security/limits.conffile[KRNL-5820]
https://cisofy.com/lynis/controls/KRNL-5820/
*CheckPAM configuration,add roundsifapplicableandexpire passwords to encryptwithnewvalues[AUTH-9229]
https://cisofy.com/lynis/controls/AUTH-9229/
Lynis 提供了一个选项来查找关于每个建议的更多信息,你可以使用show details命令和 TEST-ID 号来访问:
./lynis show details TEST-ID
这将显示该测试的其他信息。例如,我检查了 SSH-7408 的详细信息:
$./lynis show details SSH-7408
2020-04-3005:52:23PerformingtestID SSH-7408(CheckSSH specificdefinedoptions)
2020-04-3005:52:23Test:Checkingspecificdefinedoptionsin/tmp/lynis.k8JwazmKc6
2020-04-3005:52:23Result:added additional optionsforOpenSSH<7.5
2020-04-3005:52:23Test:CheckingAllowTcpForwardingin/tmp/lynis.k8JwazmKc6
2020-04-3005:52:23Result:OptionAllowTcpForwardingfound
2020-04-3005:52:23Result:OptionAllowTcpForwardingvalueisYES
2020-04-3005:52:23Result:OpenSSHoptionAllowTcpForwardingisina weak configuration stateandshould be fixed
2020-04-3005:52:23Suggestion:Considerhardening SSH configuration[test:SSH-7408][details:AllowTcpForwarding(setYES to NO)][solution:-]
如何使用Lynis扫描Linux 安全性的详细内容,希望对您有所帮助,信息来源于网络。