• ssh安全配置.md


    SSH安全配置

    介绍

    ​ 我需要在公网开放ssh服务,以便于登录服务器。但是公网的扫描太多,口令爆破也很严重,所以必须加固ssh安全,保证服务安全。口令登录是不可取的,设置一个随机的长口令,虽然不错,但是记不住!找找archwiki上发现有OTP(一次性密码)教程,觉得不错。也配置了好几次,最终发现还是需要记录一下。仅在archlinux/manjaro下测试。

    ​ 安全目标如下:

    1. 禁止root登录。
    2. 禁止空密码;
    3. 更换ssh服务为不常用端口;
    4. 达到以下任意条件可登录:
     - 公钥验证提供;
     - OTP验证通过,同时密码验证通过;(方便在一台没有公钥的机器上登录服务器)
    

    步骤

    客户端操作:

    1. 在客户端生成key

      ssh-keygen -t ed25519
      
    2. 上传客户端密钥

      ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 22 username@ip
      

    服务端操作:

    1. 安装OTP模块和二维码显示工具:

      yay -S libpam-google-authenticator qrencode
      
    2. 手机安装 身份认证器

      image-20220418202746003

    3. 生成OTP密钥文件

         $ google-authenticator
         Do you want authentication tokens to be time-based (y/n) y
         <这里是自动生成的二维码>
         Your new secret key is: ZVZG5UZU4D7MY4DH          (验证器配置密钥)
         Your verification code is 269371                  (输入验证器生成的验证码)
         Your emergency scratch codes are:                 (备用令牌码)
           70058954
           97277505
           99684896
           56514332
           82717798
         
         Do you want me to update your "/home/username/.google_authenticator" file (y/n) y
         (是否重新生成登录配置文件?)
         
         Do you want to disallow multiple uses of the same authentication
         token? This restricts you to one login about every 30s, but it increases
         your chances to notice or even prevent man-in-the-middle attacks (y/n) y
         (是否拒绝多次重复使用相同的令牌?这将限制你每30s仅能登录一次,但会提醒/阻止中间人攻击。)
         
         By default, tokens are good for 30 seconds and in order to compensate for
         possible time-skew between the client and the server, we allow an extra
         token before and after the current time. If you experience problems with poor
         time synchronization, you can increase the window from its default
         size of 1:30min to about 4min. Do you want to do so (y/n) n
         (是否将窗口时间由1分30秒增加到约4分钟?这将缓解时间同步问题。)
         
         If the computer that you are logging into is not hardened against brute-force
         login attempts, you can enable rate-limiting for the authentication module.
         By default, this limits attackers to no more than 3 login attempts every 30s.
         Do you want to enable rate-limiting (y/n) y
         (是否启用此模块的登录频率限制,登录者将会被限制为最多在30秒内登录3次。)
      
    4. 编辑/etc/ssh/sshd_config

      # 修改端口
      Port XXXXX
      
      # 禁止root登录 (应该会被PAM覆盖,此项可能无用)
      PermitRootLogin no
      # 禁止密码登录 (应该会被PAM覆盖,此项可能无用)
      PasswordAuthentication no
      # 禁止空密码 (应该会被PAM覆盖,此项可能无用)
      PermitEmptyPasswords no
      
      # 启用键盘交互 
      KbdInteractiveAuthentication yes
      # 使用PAM
      UsePAM yes
      # 开启应答
      ChallengeResponseAuthentication yes
      # 认证方式: 公钥认证 或 键盘交互
      AuthenticationMethods publickey keyboard-interactive:pam
      
      # Banner
      Banner none
      
    5. 编辑/etc/pam.d/sshd

      # 要给以下配置加到首行 [PAM是按照行依次执行的,遇到不成功的会立即失败](个人理解)
      # disable remote root
      auth      required      pam_securetty.so     
      auth      required      pam_google_authenticator.so
      
    6. 不要退出服务端登录,重启sshd服务。在客户端尝试新的连接以确定改动正确。

    7. 最后重启sshd服务 sudo systemctl restart sshd.service

    额外: 任何出错请查看错误信息,systemctl status sshd

    个人理解

    配置核心以下几处:

    1. sshd_config文件的 UsePAM yes,让sshd认证通过PAM。
    2. sshd_config文件的AuthenticationMethods publickey keyboard-interactive:pam,公钥认证或键盘交互(PAM)。
    3. pam.d/ssd文件的auth required pam_google_authenticator.so,PAM要求必须通过google_authenticator(OTP)认证。

    参考资料

    1. OpenSSH archwiki
    2. Google Authenticator
  • 相关阅读:
    Java:线程的六种状态及转化
    Java:多线程概述与创建方式
    小白学Java:RandomAccessFile
    如何用IDEA开启断言
    如何通过IDEA添加serialVersionUID
    小白学Java:I/O流
    更改IDEA相对路径
    小白学Java:File类
    小白学Java:内部类
    Leetcode数组题*3
  • 原文地址:https://www.cnblogs.com/nsfoxer/p/16321420.html
Copyright © 2020-2023  润新知