暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux系统安全及检测

Linux技术宅 2024-01-24
388

点击上方:Linux技术宅,关注我!!!

Every Day

广

[]  [朝代] 唐

西

用户系统安全

Root用户

  • 使用 Root 用户进行日常运维管理操作,存在极大的安全风险:

    • 更容易在意外执行有高风险命令时产生不可挽回的操作;

    • 需要对 Root 用户的账号信息进行分发,容易出现信息安全泄露的问题。


Sudo命令

    sudo 是一个以 root 用户(或其他用户)来控制运行命令访问的程序。它可以配置为允许一个用户像 root 用户一样来运行所有的命令,或者仅仅一些命令。也可以配置为无需密码即可使用 sudo 运行命令。


  • 使用sudo的好处

    1. 只在执行必要命令的时候使用 root 权限,日常运维使用其他权限,减少出现意外破坏情况的概率;

    2. 获取 root 权限时,用户需要输入自己的密码来进行安全校验,而不是输入 root 用户的密码来进行校验;

    3. sudo 命令可以按照用户进行安全审计,避免全都使用 root 用户时审计不便的问题;

    4. 关闭了 root 登录并配置 sudo 的情况下,可以减少黑客对 root 用户进行定向爆破成功的可能性。


  • 配置sudo权限

    • 编写 etc/sudoers 文件,可以编辑具体的 sudo 用户权限;

    • 使用 vim /etc/sudoers 或者 visudo 命令

    ## Allow root to run any commands anywhere
    root ALL=(ALL) ALL


    # Allows members of the 'sys' group to run networking, software,
    ## service management apps and more.
    # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS


    ## Allows people in group wheel to run all commands
    %wheel  ALL=(ALL)       ALL


    • 配置 sudo 用户只能执行特定的命令

      • 可以执行所有命令

      bestony ALL=(ALL) ALL
        • 只能执行一个命令

        bestony ALL=(ALL) /usr/bin/systemctl status sshd
          • 可以执行一组命令

          bestony ALL=(ALL) /usr/bin/systemctl * sshd
            • 可以执行多个命令

            bestony ALL=(ALL) usr/bin/systemctl status sshd, usr/bin/systemctl restart sshd
              • 只能以特定用户执行命令

              bestony ALL=(admin) usr/bin/echo_help


              # 执行
              sudo -u admin /usr/bin/echo_help


              • 查看当前用户 sudo 权限:sudo -l

                [root@wyh ~]# sudo -l
                Matching Defaults entries for root on wyh:
                !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG
                LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS
                _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin


                User root may run the following commands on wyh:
                    (ALL) ALL


                • 配置密码有效期

                  • 使用 change -l root 查看密码有效期

                  [root@wyh ~]# chage -l root
                  Last password change : Aug 24, 2023
                  Password expires : never
                  Password inactive : never
                  Account expires : never
                  Minimum number of days between password change : 0
                  Maximum number of days between password change : 99999
                  Number of days of warning before password expires       : 7
                    • 可以编辑 /etc/login.defs 设置密码过期时间

                    # Password aging controls:
                    #
                    # PASS_MAX_DAYS Maximum number of days a password may be used.
                    # PASS_MIN_DAYS Minimum number of days allowed between password changes.
                    # PASS_MIN_LEN Minimum acceptable password length.
                    # PASS_WARN_AGE Number of days warning given before a password expires.
                    #
                    PASS_MAX_DAYS 99999
                    PASS_MIN_DAYS 0
                    PASS_MIN_LEN 5
                    PASS_WARN_AGE   7


                    • 设置安全提醒信息

                      • /etc/motd 文件中存储的信息将会在每一次登陆后提示,可以确保在登陆到服务器后,即可看到相关内容。

                      [root@wyh ~]# cat etc/motd


                      Welcome to Alibaba Cloud Elastic Compute Service !
                        • /etc/issue 文件中存储的信息将会在每一次登录前展示,可以将一些信息设定在其中,确保可以让用户登陆时看到。

                        [root@wyh ~]# cat etc/issue
                        \S
                        Kernel \r on an \m

                        SSH安全

                        SSH安全增强与配置

                        • 关闭SSH Protocol 1

                          • SSH Protocol 是 SSH 协议的第一版,相比于已经在使用的第二版来说,已经不再安全,因此,需要关闭服务器的 SSH Protocol 1。

                          • 在 /etc/ssh/sshd_config 中查找 Protocol 1,2,移除其中的1即可关闭 SSH Protocol 1。

                          • CentOS系列从CentOS 7.4开始,便移除了该选项,默认使用第二版。


                        • 关闭root用户登录

                          • 在 etc/ssh/sshd_config 中查找 PermitRootLogin,并设置为no,即可禁止root用户登录。


                        • 关闭用户名密码登录

                          • 在 etc/ssh/sshd_config 中查找 PasswordAuthentication,并设置为no,即可禁止使用密码登录。


                        • 设置SSH日志输出

                          • 在 /etc/ssh/sshd_config 中查找 LogLevel,取消注释,并配置日志等级:

                            • QUIET

                            • FATAL

                            • ERROR

                            • INFO

                            • VERBOSE

                            • DEBUG or DEBUG1

                            • DEBUG2

                            • DEBUG3


                        • 设置SSH的端口为非22端口

                          •  /etc/ssh/sshd_config 中查找 Port,取消注释,并修改端口为目标端口即可。


                        • 创建一个SSH登录Banner

                          • 创建 etc/ssh/sshd-banner,并填入想要显示的内容。

                          • 在 etc/ssh/sshd_config 中查找 Banner, 取消注释,并设置其值为 etc/ssh/sshd-banner 即可。

                        安全检测软件

                        ClamAV

                        • 介绍

                          • ClamAV 杀毒是Linux平台最受欢迎的杀毒软件。

                          • 支持多种平台,如:Linux/Unix、MAC OS X、Windows、OpenVMS。

                          • ClamAV 是基于病毒扫描的命令行工具,但同时也有支持图形界面的ClamTK 工具。


                        • 安装

                          [root@localhost ~]# yum install -y epel-release
                          Loaded plugins: fastestmirror, langpacks


                          ......


                          Updated:
                          epel-release.noarch 0:7-14


                          Complete!
                          [root@localhost ~]# yum install -y clamav clamav-update
                          Loaded plugins: fastestmirror, langpacks
                          Loading mirror speeds from cached hostfile


                          ......


                          Installed:
                          clamav.x86_64 0:0.103.11-1.el7 clamav-update.x86_64 0:0.103.11-1.el7


                          Dependency Installed:
                          clamav-filesystem.noarch 0:0.103.11-1.el7 clamav-lib.x86_64 0:0.103.11-1.el7 gnutls.x86_64 0:3.3.29-9.el7_6 libprelude.x86_64 0:5.2.0-2.el7 nettle.x86_64 0:2.7.1-9.el7_9 pcre2.x86_64 0:10.23-2.el7
                          trousers.x86_64 0:0.3.14-2.el7


                          Complete!
                          • 更新病毒库:freshclam

                            [root@localhost ~]# freshclam
                            ClamAV update process started at Tue Jan 23 14:36:39 2024
                            daily database available for download (remote version: 27162)
                            Time: 5m 12s, ETA: 0.0s [========================>] 59.76MiB/59.76MiB
                            Testing database: '/var/lib/clamav/tmp.119be737ae/clamav-20d80f42317ba596548d1c77bfe208fc.tmp-daily.cvd' ...
                            Database test passed.
                            daily.cvd updated (version: 27162, sigs: 2051378, f-level: 90, builder: raynman)
                            main database available for download (remote version: 62)
                            Time: 1m 27s, ETA: 0.0s [========================>] 162.58MiB/162.58MiB
                            Testing database: '/var/lib/clamav/tmp.119be737ae/clamav-9d267c3ce6de037c7bccfe6ae620d445.tmp-main.cvd' ...
                            Database test passed.
                            main.cvd updated (version: 62, sigs: 6647427, f-level: 90, builder: sigmgr)
                            bytecode database available for download (remote version: 334)
                            Time: 3.4s, ETA: 0.0s [========================>] 285.12KiB/285.12KiB
                            Testing database: '/var/lib/clamav/tmp.119be737ae/clamav-0baf9a830c280e1620e9ec79b4bae302.tmp-bytecode.cvd' ...
                            Database test passed.
                            bytecode.cvd updated (version: 334, sigs: 91, f-level: 90, builder: anvilleg)
                            • 扫描文件或目录:clamscan

                              # 扫描文件
                              [root@localhost ~]# clamscan anaconda-ks.cfg
                              /root/anaconda-ks.cfg: OK


                              ----------- SCAN SUMMARY -----------
                              Known viruses: 8683109
                              Engine version: 0.103.11
                              Scanned directories: 0
                              Scanned files: 1
                              Infected files: 0
                              Data scanned: 0.00 MB
                              Data read: 0.00 MB (ratio 0.00:1)
                              Time: 39.211 sec (0 m 39 s)
                              Start Date: 2024:01:23 14:46:53
                              End Date: 2024:01:23 14:47:32


                              # 扫描目录
                              [root@localhost ~]# clamscan -r /root
                              /root/.bash_logout: OK
                              /root/.bash_profile: OK
                              /root/.bashrc: OK
                              /root/.cshrc: OK
                              /root/.tcshrc: OK
                              /root/anaconda-ks.cfg: OK
                              /root/.cache/abrt/lastnotification: OK
                              /root/.bash_history: OK
                              /root/install_check.sh: OK
                              /root/.viminfo: OK
                              /root/testfile: Empty file


                              ----------- SCAN SUMMARY -----------
                              Known viruses: 8683109
                              Engine version: 0.103.11
                              Scanned directories: 7
                              Scanned files: 10
                              Infected files: 0
                              Data scanned: 0.01 MB
                              Data read: 0.01 MB (ratio 1.50:1)
                              Time: 36.139 sec (0 m 36 s)
                              Start Date: 2024:01:23 14:47:58
                              End Date:   2024:01:23 14:48:35


                              Linux Malware Detect

                              • 介绍

                                • Linux Malware Detect (LMD)是采用 GPL v2 许可证发布的一款恶意软件扫描工具,专门为主机托管环境而设计。


                              • 下载源码

                                [root@localhost ~]# wget http://www.rfxn.com/downloads/maldetect-current.tar.gz
                                • 解压

                                  [root@localhost ~]# tar xzvf maldetect-current.tar.gz
                                  • 安装

                                    [root@localhost ~]# cd maldetect-1.6.5/
                                    [root@localhost maldetect-1.6.5]# ./install.sh
                                    • 查看命令及帮助

                                      [root@localhost maldetect-1.6.5]# maldet
                                      Linux Malware Detect v1.6.5
                                      (C) 2002-2023, R-fx Networks <proj@rfxn.com>
                                      (C) 2023, Ryan MacDonald <ryan@rfxn.com>
                                      This program may be freely redistributed under the terms of the GNU GPL v2


                                      signature set: 202401192782461
                                      usage maldet [-h|--help] [-a|--scan-all PATH] [-r|--scan-recent PATH DAYS]
                                      [-f|--file-list PATH] [-i|--include-regex] [-x|--exclude-regex]
                                      [-b|--background] [-m|--monitor] [-k|--kill-monitor] [-c|--checkout]
                                      [-q|--quarantine] [-s|--restore] [-n|--clean] [-l|--log] [-e|--report]
                                            [-u|--update-sigs] [-d|--update-ver]
                                      • 扫描文件和目录时,加-a参数跟绝对路径

                                        [root@localhost ~]# maldet -a /root/anaconda-ks.cfg
                                        Linux Malware Detect v1.6.5
                                        (C) 2002-2023, R-fx Networks <proj@rfxn.com>
                                        (C) 2023, Ryan MacDonald <ryan@rfxn.com>
                                        This program may be freely redistributed under the terms of the GNU GPL v2


                                        maldet(3656): {scan} signatures loaded: 17637 (14801 MD5 | 2053 HEX | 783 YARA | 0 USER)
                                        maldet(3656): {scan} building file list for /root/anaconda-ks.cfg, this might take awhile...
                                        maldet(3656): {scan} setting nice scheduler priorities for all operations: cpunice 19 , ionice 6
                                        maldet(3656): {scan} scan returned empty file list; check that path exists and contains files in scope of configuration.


                                        [root@localhost ~]# maldet -a /root
                                        Linux Malware Detect v1.6.5
                                        (C) 2002-2023, R-fx Networks <proj@rfxn.com>
                                        (C) 2023, Ryan MacDonald <ryan@rfxn.com>
                                        This program may be freely redistributed under the terms of the GNU GPL v2


                                        maldet(3829): {scan} signatures loaded: 17637 (14801 MD5 | 2053 HEX | 783 YARA | 0 USER)
                                        maldet(3829): {scan} building file list for /root, this might take awhile...
                                        maldet(3829): {scan} setting nice scheduler priorities for all operations: cpunice 19 , ionice 6
                                        maldet(3829): {scan} scan returned empty file list; check that path exists and contains files in scope of configuration.




                                        分享、在看与点赞

                                        只要你点,我们就是胖友

                                        文章转载自Linux技术宅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                        评论