浅谈服务器设置"双子验证"

本文介绍了一种增强服务器安全性的方法——双因素认证,通过结合Google Authenticator和SSH密钥,为服务器访问提供双重保护。文章详细讲解了安装配置过程及如何在Xshell中实现。

浅谈服务器设置"双子验证"


双子验证
   为了保证连接服务器的安全,一般都是选择用xshell配合公钥的手法来访问,但是这个也存在私钥失窃的风险,为了保证数据安全,再加一层验证手段。

  • 双子验证包含:
    • 基于时间的一次性密码Time-based One-time Password(TOTP)算法。Google Authenticator 用作服务器应用程序
    • 基于SSH协议的xshell密钥访问

Google Authenticator

  • 安装Google Authenticator
    yum -y install Google Authenticator

  • 运行应用程序
    google-authenticator

  • 提供了二维码连接以及超级数字密码

[root@Centos6 ~]#google-authenticator 
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@Centos6.localdomain%3Fsecret%3DB2OLIRUTI4P72DP6
Your new secret key is: B2OLIRUTI4P72DP6
Your verification code is 796823
Your emergency scratch codes are:
  86797884
  13337815
  76420985
  59997949
  46300756
  • 这个应用程序为你提供一个密钥、验证码和恢复码。把它们放在安全的地方。如果你丢失了手机,恢复码是访问服务器的唯一方式。
  • 该应用程序提供了一系列问题。下面的片段展示了如何进行合理的安全设置:
Do you want authentication tokens to be time-based (y/n) y
Do you want me to update your "/home/user/.google_authenticator" file (y/n)? y
  • 设置手机验证
  • 在你的手机上安装验证器应用程序(FreeOTP)。Google Authenticator 会在屏幕上显示一个二维码。打开手机上的 FreeOTP 应用程序,选择添加新账户,在应用程序顶部选择二维码形状工具,然后扫描二维码即可。设置完成后,在每次远程连接服务器时,你必须提供验证器应用程序生成的随机数。
  • 编辑服务端的sshdpam模块
    • vim /etc/pam.d/sshd
    • sed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd 添加下行保存

配置SSH的密钥验证

  • 先用xshell跟服务器建立一个连接
  • 在xshell里面生成密钥对
    在这里插入图片描述
  • 把生成的公钥文件传给服务器端
    cat id_rsa.pub >> /root/.ssh/auauthorized_keys
    chmod 600 authorized_keys
  • 修改服务端配置文件/etc/ssh/sshd_config
    sed -i 's/.*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config
    sed -i 's/.*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
  • 重启sshd服务
    systemctl restart sshd

一键配置Google Authenticator脚本

#安装epel
yum install -y epel-release.noarch 
yum makecache 
#安装google authenticator
yum install -y google-authenticator.x86_64


echo -e "\033[31mDo you want me to update your "/root/.google_authenticator" file? (y/n) y"
echo -e "\033[31m你希望我更新你的“/root/.google_authenticator”文件吗(y/n)?\033[0m"
echo -e "\033[31mDo you want to disallow multiple uses of the same authentication"
echo -e "\033[31mtoken? This restricts you to one login about every 30s, but it increases"
echo -e "\033[31myour chances to notice or even prevent man-in-the-middle attacks (y/n) y"
echo -e "\033[31m你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?\033[0m"
echo -e "\033[31mBy default, a new token is generated every 30 seconds by the mobile app."
echo -e "\033[31mIn order to compensate for possible time-skew between the client and the server,"
echo -e "\033[31mwe allow an extra token before and after the current time. This allows for a"
echo -e "\033[31mtime skew of up to 30 seconds between authentication server and client. If you"
echo -e "\033[31mexperience problems with poor time synchronization, you can increase the window"
echo -e "\033[31mfrom its default size of 3 permitted codes (one previous code, the current"
echo -e "\033[31mcode, the next code) to 17 permitted codes (the 8 previous codes, the current"
echo -e "\033[31mcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes"
echo -e "\033[31mbetween client and server."
echo -e "\033[31mDo you want to do so? (y/n) y"
echo -e "\033[31m默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,\033[0m"
echo -e "\033[31m我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3个可通过验证码增加到17个可通过验证码,\033[0m"
echo -e "\033[31m这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)?\033[0m"
echo -e "\033[31mIf the computer that you are logging into isn't hardened against brute-force"
echo -e "\033[31mlogin attempts, you can enable rate-limiting for the authentication module."
echo -e "\033[31mBy default, this limits attackers to no more than 3 login attempts every 30s."
echo -e "\033[31mDo you want to enable rate-limiting? (y/n) y"
echo -e "\033[31m如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块\033[0m"
echo -e "\033[31m启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。 你希望启用尝试次数限制吗(y/n)?\033[0m"
echo -e "\033[32m 在App Store 搜索Google Authenticator 进行App安装 \033[0m"


google-authenticator


#/etc/pam.d/sshd文件,修改或添加下行保存
#auth required pam_google_authenticator.so
sed -i '1a\auth       required     pam_google_authenticator.so' /etc/pam.d/sshd
#编辑/etc/ssh/sshd_config找到下行
#ChallengeResponseAuthentication no
#更改为
#ChallengeResponseAuthentication yes
sed -i 's/.*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config

#重启SSH服务
service sshd restart

**完**
要解决双子设备(Twin Device)的密钥(KEY)无法与服务器匹配的问题,需要从密钥的生成、存储、传输以及认证流程等多个方面进行排查和优化。 ### 问题成因与解决方法 #### 1. 密钥生成与配置不一致 双子设备通常使用预共享密钥(PSK)进行身份验证,若设备端与服务器端的密钥配置不一致,将导致认证失败。应确保设备与服务器在密钥生成过程中使用相同的种子值或主密钥(PMK),并按照相同的算法派生出成对临时密钥(PTK)和组临时密钥(GTK)[^1]。若密钥硬编码在设备固件中,需确保其与服务器配置的共享密钥完全一致,包括大小写和特殊字符。 #### 2. 密钥存储与加密问题 密钥应以加密形式存储在设备中,避免因固件提取导致密钥泄露或配置错误。例如,服务器端的密钥配置文件(如 FreeRADIUS 的 `raddb/secrets` 文件)应采用加密存储机制,防止明文暴露。设备端也应使用安全的存储机制,如硬件安全模块(HSM)或可信执行环境(TEE)来保护密钥[^2]。 #### 3. 认证协议与加密套件不兼容 确保设备与服务器使用相同的认证协议(如 EAP-TLS、EAP-PEAP、EAP-TTLS 等)和加密算法(如 AES、TKIP 等)。若设备使用 TKIP 而服务器仅支持 AES,可能导致密钥派生失败。应检查服务器与设备的加密配置,确保二者支持的加密算法和完整性校验码(MIC)一致[^1]。 #### 4. 密钥生命周期与同步问题 PTK 和 GTK 是动态生成的临时密钥,若设备与服务器的时间戳或随机数(Nonce)不同步,可能导致密钥派生失败。应确保设备与服务器之间的时间同步,并检查随机数生成机制是否正常[^1]。 #### 5. 使用 RADIUS 协议进行调试 若使用 RADIUS 服务器进行认证,可利用 `radtest` 工具进行本地测试,确认密钥是否正确传递。例如: ```bash radtest alice P@ssw0rd123 192.168.1.100 0 mysecret ``` 该命令可模拟客户端向 RADIUS 服务器发送认证请求,验证用户名、密码及共享密钥是否匹配[^4]。 #### 6. 启用增强协议保障通信安全 建议启用 RADIUS over TLS(RadSec)或 IPSec 隧道,替代传统的明文 UDP 传输方式,防止密钥在传输过程中被篡改或窃听,从而影响密钥匹配[^2]。 ### 安全建议 - **避免使用默认密钥**:禁用公开的默认密钥(如 testing123),防止被恶意利用。 - **密钥分离**:为不同设备或区域分配独立密钥,减少单点泄露带来的风险。 - **加密存储**:对配置文件中的密钥进行加密存储,防止因配置泄露导致认证失败。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值