SSH安全威胁概述

常见攻击类型

  1. 暴力破解攻击:尝试大量用户名和密码组合
  2. 中间人攻击:拦截和篡改SSH通信
  3. 密钥泄露:私钥被恶意获取
  4. 会话劫持:劫持已建立的SSH会话
  5. 侧信道攻击:通过时间、功耗等信息推断密钥

安全风险评估

  • 网络环境:公网、内网、VPN
  • 数据敏感性:机密数据、个人信息、系统配置
  • 用户权限:普通用户、管理员、root
  • 合规要求:行业标准、法规要求

服务器端安全配置

基础安全设置

# /etc/ssh/sshd_config

# 1. 修改默认端口
Port 2222

# 2. 限制协议版本
Protocol 2

# 3. 禁用root直接登录
PermitRootLogin no

# 4. 禁用密码认证
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

# 5. 仅允许公钥认证
PubkeyAuthentication yes
AuthenticationMethods publickey

# 6. 禁用空密码
PermitEmptyPasswords no

访问控制

# 用户和组限制
AllowUsers admin developer
DenyUsers guest
AllowGroups ssh-users admin
DenyGroups nossl

# IP地址限制
# 在防火墙中配置
sudo ufw allow from 192.168.1.0/24 to any port 2222
sudo ufw deny 2222

# 使用TCP Wrappers
# /etc/hosts.allow
sshd: 192.168.1.0/24
sshd: .trusted-domain.com

# /etc/hosts.deny
sshd: ALL

会话安全

# 会话超时设置
ClientAliveInterval 300
ClientAliveCountMax 2

# 最大认证尝试次数
MaxAuthTries 3

# 最大会话数
MaxSessions 4

# 最大启动连接数
MaxStartups 10:30:60

# 登录宽限时间
LoginGraceTime 60

加密算法配置

# 强制使用安全的加密算法
Ciphers aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr

# MAC算法
MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha2-512

# 密钥交换算法
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512

# 主机密钥算法
HostKeyAlgorithms ssh-ed25519,rsa-sha2-256,rsa-sha2-512

客户端安全配置

安全连接设置

# ~/.ssh/config

Host *
    # 严格主机密钥检查
    StrictHostKeyChecking yes
    
    # 禁用不安全的功能
    ForwardX11 no
    ForwardAgent no
    
    # 使用安全的算法
    Ciphers aes256-gcm@openssh.com,aes128-gcm@openssh.com
    MACs hmac-sha2-256,hmac-sha2-512
    
    # 哈希已知主机
    HashKnownHosts yes
    
    # 仅使用指定的身份文件
    IdentitiesOnly yes

密钥安全管理

# 1. 生成强密钥
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/secure_key

# 2. 设置密钥权限
chmod 600 ~/.ssh/secure_key
chmod 644 ~/.ssh/secure_key.pub
chmod 700 ~/.ssh

# 3. 使用密钥密码短语
# 生成时设置强密码短语

# 4. 定期轮换密钥
# 建议每年更换一次

网络层安全

防火墙配置

# UFW防火墙配置
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 仅允许特定IP访问SSH
sudo ufw allow from 192.168.1.100 to any port 2222
sudo ufw allow from 10.0.0.0/8 to any port 2222

# 启用防火墙
sudo ufw enable

# iptables配置
# 限制SSH连接频率
sudo iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m recent --set
sudo iptables -A INPUT -p tcp --dport 2222 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP

VPN和跳板机

# 通过VPN访问
# 1. 建立VPN连接
# 2. 通过VPN网络访问SSH

# 跳板机配置
Host production-server
    HostName 10.0.1.100
    ProxyJump bastion.company.com
    User app

Host bastion.company.com
    User admin
    Port 2222
    IdentityFile ~/.ssh/bastion_key

监控和审计

日志配置

# SSH服务器日志级别
LogLevel VERBOSE

# 系统日志配置
# /etc/rsyslog.conf
auth,authpriv.*                 /var/log/auth.log

# 查看SSH登录日志
sudo tail -f /var/log/auth.log | grep sshd

# 查看失败的登录尝试
sudo grep "Failed password" /var/log/auth.log

# 查看成功的登录
sudo grep "Accepted" /var/log/auth.log

入侵检测

# 安装fail2ban
sudo apt install fail2ban

# 配置fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

# 启动fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

# 查看被封禁的IP
sudo fail2ban-client status sshd

连接监控

# 查看当前SSH连接
who
w
last

# 查看网络连接
ss -tuln | grep :2222
netstat -tuln | grep :2222

# 实时监控连接
watch "ss -tuln | grep :2222"

高级安全技术

双因素认证(2FA)

# 安装Google Authenticator
sudo apt install libpam-google-authenticator

# 用户配置2FA
google-authenticator

# PAM配置
# /etc/pam.d/sshd
auth required pam_google_authenticator.so

# SSH配置
# /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

证书认证

# 生成CA密钥
ssh-keygen -t ed25519 -f ca_key

# 生成用户证书
ssh-keygen -s ca_key -I user_id -n username -V +30d ~/.ssh/id_ed25519.pub

# 服务器配置
# /etc/ssh/sshd_config
TrustedUserCAKeys /etc/ssh/ca_key.pub

端口敲门(Port Knocking)

# 安装knockd
sudo apt install knockd

# 配置knockd
# /etc/knockd.conf
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 5
command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn

[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 5
command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn

应急响应

安全事件处理

# 1. 立即断开可疑连接
sudo pkill -u suspicious_user

# 2. 临时禁用SSH
sudo systemctl stop sshd

# 3. 检查系统完整性
sudo aide --check

# 4. 分析日志
sudo grep -i "suspicious_activity" /var/log/auth.log

# 5. 更换密钥
ssh-keygen -t ed25519 -f ~/.ssh/new_key

恢复程序

# 1. 清理authorized_keys
# 移除可疑的公钥

# 2. 重置用户密码
sudo passwd username

# 3. 更新SSH配置
# 加强安全设置

# 4. 重启SSH服务
sudo systemctl restart sshd

# 5. 监控后续活动
tail -f /var/log/auth.log

合规性考虑

常见标准要求

  • PCI DSS:支付卡行业数据安全标准
  • SOX:萨班斯-奥克斯利法案
  • HIPAA:健康保险便携性和责任法案
  • ISO 27001:信息安全管理体系

合规配置示例

# 强制加密和完整性
Ciphers aes256-gcm@openssh.com
MACs hmac-sha2-256

# 审计日志
LogLevel VERBOSE
SyslogFacility AUTHPRIV

# 访问控制
PermitRootLogin no
PasswordAuthentication no
MaxAuthTries 3

# 会话管理
ClientAliveInterval 900
ClientAliveCountMax 0

安全检查清单

服务器端检查

  • [ ] 修改默认SSH端口
  • [ ] 禁用root登录
  • [ ] 禁用密码认证
  • [ ] 配置防火墙规则
  • [ ] 启用fail2ban
  • [ ] 配置强加密算法
  • [ ] 设置会话超时
  • [ ] 启用详细日志

客户端检查

  • [ ] 使用强密钥类型
  • [ ] 设置密钥密码短语
  • [ ] 配置严格主机检查
  • [ ] 禁用不必要的转发
  • [ ] 定期轮换密钥
  • [ ] 安全存储私钥

网络层检查

  • [ ] 配置网络防火墙
  • [ ] 使用VPN或专线
  • [ ] 实施网络分段
  • [ ] 监控网络流量

小结

SSH安全是一个多层次的体系,需要从服务器配置、客户端设置、网络防护、监控审计等多个方面综合考虑。安全配置应该根据实际的威胁模型和合规要求进行调整,并且需要定期审查和更新。

记住:安全是一个持续的过程,而不是一次性的配置。


上一章节SSH连接配置
下一章节SSH高级用法