# rust_fake_file **Repository Path**: greycode/rust_fake_file ## Basic Information - **Project Name**: rust_fake_file - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-26 - **Last Updated**: 2026-05-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # rust_fake_file 生成随机图片,将 ZIP 归档追加到图片后面,并在之后恢复嵌入的文件。 **原理:** 工具创建一张随机图片(或复用已有图片),将你的文件压缩为 ZIP 归档,然后把 ZIP 字节追加到图片末尾。由于 ZIP 读取器从文件末尾扫描中央目录,最终的文件既是一张合法图片,也是一个合法 ZIP 归档。之后你可以解码来提取原始文件。 ## 安装 ### 从源码构建 ```bash git clone https://gitee.com/greycode/rust_fake_file.git cd rust_fake_file cargo build --release # 产物:target/release/rust_fake_file ``` ### 最小体积构建(Windows) ```bash cargo build --profile release-small # 产物:target/release-small/rust_fake_file.exe ``` 该 profile 启用了 `opt-level = "z"`、跨 crate LTO、单 codegen unit、`panic = "abort"` 和符号剥离。`zip` 依赖也排除了未使用的压缩后端(bzip2、lzma、zstd、zopfli、deflate64),仅保留 AES 加密和 flate2 支持的 Deflate。 ## 快速配置 构建完成后,将二进制文件重命名为更短的名称并加入 PATH,即可在任意位置直接调用。 ### Windows **方式 A — 复制到默认 PATH 目录(无需额外配置):** Windows 10/11 的用户 PATH 默认包含 `%LOCALAPPDATA%\Microsoft\WindowsApps`,直接复制即可: ```powershell copy target\release\rust_fake_file.exe "%LOCALAPPDATA%\Microsoft\WindowsApps\fake.exe" ``` 复制后重启终端,即可直接使用 `fake` 命令。 **方式 B — 复制到 Cargo bin 目录:** 既然已经安装了 Cargo,说明 `%USERPROFILE%\.cargo\bin` 已经在 PATH 中,直接复制即可: ```powershell copy target\release\rust_fake_file.exe "%USERPROFILE%\.cargo\bin\fake.exe" ``` 无需额外配置,重启终端后即可使用 `fake` 命令。 **方式 C — 创建自定义工具目录:** ```powershell mkdir C:\Tools copy target\release\rust_fake_file.exe C:\Tools\fake.exe ``` 然后将 `C:\Tools` 添加到用户 PATH: 1. 打开 **系统属性** → **环境变量** 2. 在 **用户变量** 中选择 `Path` → **编辑** → **新建** → 输入 `C:\Tools` 3. 重启终端 之后就可以用 `fake` 代替完整名称: ```powershell fake input.txt fake decode out.png fake info image.png ``` ### Linux / macOS ```bash # 复制到已有的 PATH 目录 cp target/release/rust_fake_file ~/.local/bin/fake # 或全局安装 sudo cp target/release/rust_fake_file /usr/local/bin/fake ``` 之后就可以用 `fake` 代替完整名称: ```bash fake input.txt fake decode out.png fake info image.png ``` ## 快速上手 ```bash # 将文件编码到随机图片中 rust_fake_file mydoc.txt # → 创建 mydoc.png # 解码并提取 rust_fake_file mydoc.png # → 在当前目录恢复 mydoc.txt # 编码整个文件夹 rust_fake_file myproject/ # → 创建 myproject.png # 使用密码保护编码 rust_fake_file secret.txt --password s3cret # → 创建 secret.png(AES-256 加密 ZIP) # 使用密码解码 rust_fake_file secret.png --password s3cret # → 恢复 secret.txt ``` ## 使用方式 ### 自动模式(默认) 不使用显式子命令时,工具会根据输入自动路由: | 命令 | 行为 | |------|------| | `rust_fake_file input.txt` | 编码 `input.txt` → `input.png` | | `rust_fake_file mydir/` | 编码目录 `mydir/` → `mydir.png` | | `rust_fake_file input.png` | 优先尝试解码;若无嵌入内容,则编码 → `input-encoded.png` | | `rust_fake_file https://example.com/hidden.png` | 下载并解码网络图片 | | `rust_fake_file` | 询问是否将当前目录压缩到 `<目录名>.png` | | `rust_fake_file -y` | 跳过确认,立即压缩当前目录 | | `rust_fake_file input.txt -o out.png` | 编码到指定输出路径 | 自动模式也支持 `--password`、图片尺寸/格式选项,以及编码时的分卷选项。 网络图片输入支持 `http://`、`https://` 和 `ftp://`;它们只会被解码,不会回退到编码。 ### 显式子命令 | 命令 | 描述 | |------|------| | `gen_image` | 生成随机图片(`png`、`jpg`、`jpeg` 或 `bmp`) | | `encode` | 将文件或目录打包进随机图片或指定图片 | | `decode` | 提取嵌入的 ZIP 载荷并恢复原始文件 | | `info` | 不解压,检查图片中的嵌入状态 | | `version` | 打印包版本 | 也接受别名 `gen-image`。 ### 示例 ```bash # --- 生成随机图片 --- rust_fake_file gen_image --output out.png rust_fake_file gen_image -w 800 -H 600 -f jpg -o out.jpg # --- 编码文件 --- rust_fake_file encode input.txt --output out.png rust_fake_file encode input.txt --image cover.png --output out.png rust_fake_file encode input.txt -i cover.png -o out.png rust_fake_file encode input.txt -w 800 -H 600 -f bmp -o out.bmp # --- 使用 PNG LSB 模式编码单个原始文件 --- rust_fake_file encode payload.bin --output hidden.png --mode lsb --width 128 --height 128 rust_fake_file encode payload.bin --image cover.png --output hidden.png --mode lsb --bit-index 1 # --- 带密码编码(AES-256) --- rust_fake_file encode input.txt --output encrypted.png --password secret rust_fake_file encode input.txt --output encrypted.png --password-file ~/.secret_key # 或在自动模式中: rust_fake_file input.txt --output encrypted.png --password secret # --- 将大型载荷分散到多张图片 --- rust_fake_file encode myproject/ --output album.png --part-size 50MiB # → 创建 album.part001-of003.png、album.part002-of003.png、... rust_fake_file encode myproject/ --output album.png --parts 5 rust_fake_file encode myproject/ --image cover.png --output hidden.png --parts 3 # --- 解码 --- rust_fake_file decode out.png rust_fake_file decode https://example.com/hidden.png --output extracted/ rust_fake_file decode ftp://example.com/hidden.png --output extracted/ rust_fake_file decode encrypted.png --password secret rust_fake_file decode encrypted.png --password-file ~/.secret_key rust_fake_file decode out.png --output extracted/ rust_fake_file decode album.part001-of003.png --output extracted/ rust_fake_file decode album.part003-of003.png album.part001-of003.png album.part002-of003.png rust_fake_file decode hidden.png --output restored.bin --mode lsb --bit-index 1 # --- 不解压查看信息 --- rust_fake_file info out.png # --- 自动模式快捷操作 --- rust_fake_file input.txt # 编码 → input.png rust_fake_file input.txt --output custom.png # 编码 → custom.png rust_fake_file input.png # 有嵌入内容则解码,否则编码 rust_fake_file https://example.com/hidden.png # 下载并解码 rust_fake_file # 询问是否压缩当前目录 rust_fake_file -y # 跳过确认,压缩当前目录 # --- 帮助与版本 --- rust_fake_file help rust_fake_file version ``` ## 选项 ### 常用选项 | 选项 | 描述 | |------|------| | `-o, --output ` | 输出文件或目录 | | `-i, --image ` | 作为 `encode` 载体的图片文件 | | `--mode ` | 显式 `encode`/`decode` 命令使用的编码模型 | | `--bit-index <0-7>` | `--mode lsb` 使用的 PNG LSB 位索引;默认值为 `0` | | `-p, --password ` | 加密或解密 ZIP 载荷(AES-256) | | `--password-file ` | 从文件第一行读取密码 | | `--no-verify` | 解码或查看信息时跳过嵌入载荷完整性校验 | | `--quiet` | 禁用进度输出 | | `--strip-metadata` | 编码前清除 JPEG/PNG 载体图片元数据 | | `--part-size ` | 按每张图片最多承载的 ZIP 分卷大小进行拆分 | | `--parts ` | 将 ZIP 载荷固定拆分为指定数量的图片 | | `-y, --yes` | 跳过当前目录确认提示 | | `-h, --help` | 打印帮助 | | `-V, --version` | 打印包版本 | ### 图片选项 `gen_image`、`encode` 和自动模式都支持以下选项: | 选项 | 描述 | 默认值 | |------|------|--------| | `-w, --width ` | 图片宽度 | `400` | | `-H, --height ` | 图片高度 | `400` | | `-f, --format ` | 生成图片的格式 | `png` | 生成的图片默认为 `400x400` PNG 文件。 使用 `encode` 配合 `--image` 时,工具会将 ZIP 追加到指定的载体图片,而非生成新的随机图片: ```bash rust_fake_file encode input.txt --image cover.png --output out.png rust_fake_file encode input.txt --image photo.jpg --output out.jpg --strip-metadata ``` 对 JPEG 或 PNG 载体图片使用 `--strip-metadata` 可以在追加 ZIP 前移除 EXIF/XMP/IPTC/ICC 或 PNG 文本元数据。随机生成的图片本身不需要此选项。 在自动模式中,更改格式时需显式指定输出路径,使文件扩展名与生成格式一致: ```bash rust_fake_file input.txt --width 800 --height 600 --format jpg --output input.jpg ``` ## 分卷 当 ZIP 载荷会让单张图片过大时,可以使用 `--part-size ` 或 `--parts `。这两个选项互斥,不能同时使用。 ```bash # 按每张图片最多承载的 ZIP 分片大小拆分 rust_fake_file encode myproject/ --output album.png --part-size 50MiB # 固定拆分为指定数量的图片 rust_fake_file encode myproject/ --output album.png --parts 5 ``` 大小后缀支持 `KB`、`KiB`、`MB`、`MiB`、`GB` 和 `GiB`;纯数字表示字节数。 分卷输出会保留请求的输出路径: ```text album.part001-of003.png album.part002-of003.png album.part003-of003.png ``` 使用 `--image cover.png` 时,同一张载体图片会被复制到每个分卷中: ```bash rust_fake_file encode myproject/ --image cover.png --output hidden.png --parts 3 ``` 解码时可以只传入一个分卷,也可以按任意顺序传入全部分卷。只传入一个分卷时,工具会在同目录查找同组剩余分卷: ```bash rust_fake_file decode album.part001-of003.png --output extracted/ rust_fake_file decode album.part003-of003.png album.part001-of003.png album.part002-of003.png ``` ## 编码模式:append-zip 与 LSB 对比 本工具支持两种本质不同的嵌入策略。根据你的威胁模型、载荷类型和容量需求选择合适的模式。 ### 默认模式(append-zip) 默认模式将 ZIP 归档追加到图片数据之后。由于 ZIP 读取器从文件末尾扫描中央目录,最终的文件既是一张合法图片,也是一个合法 ZIP 归档。图片像素完全不被修改——载荷仅存在于文件尾部追加的字节中。 - **可检测性:** 任何检查文件尾部的人都会看到 ZIP 结构(本地文件头、中央目录),文件体积也会超出图片尺寸应有的正常范围。 - **容量:** 几乎无限,仅受文件系统限制。分卷功能可以透明地将超大载荷拆分到多张图片。 - **多文件支持:** 目录、多文件和文件树会先被压缩为 ZIP 再追加。 - **加密:** 通过 `--password` 内置 AES-256 ZIP 加密。 - **完整性:** 在 ZIP 后追加 SHA-256 校验和,用于数据损坏检测。 ### LSB 模式(`--mode lsb`) LSB 模式将载荷直接写入 PNG 像素字节的最低有效位。每个像素贡献一个比特(或更多,取决于 `--bit-index`)用于存储。由于翻转颜色通道的最低比特几乎不会产生可感知的变化,图片在视觉上仍然自然。 - **可检测性:** 文件中不存在任何 ZIP 结构,载荷分散在像素数据中。普通观察者只能看到一张普通 PNG。统计分析仍可检测到 LSB 嵌入,但需要专业工具。 - **容量:** 受图片尺寸限制。W×H 图片在 bit 0 可存储 `W × H × 3 ÷ 8` 字节(每个 RGB 通道贡献 1 比特)。例如,400×400 的图片约可存储 60 KB。使用更高的位索引能倍增容量,但会增加可见伪影。 - **仅支持单文件:** 不创建 ZIP 归档,原始文件字节直接写入像素。解码时必须显式指定 `--output` 路径。 - **无加密:** 载荷按位原样存储。如需机密性,请在编码前自行加密文件。 - **无完整性校验:** LSB 模式不追加 `[sha256][RFKSUM]`。请自行验证往返文件的一致性。 ### 对比一览 | 属性 | append-zip(默认) | LSB(`--mode lsb`) | |---|---|---| | 嵌入方式 | ZIP 追加到图片数据之后 | 覆盖像素通道的低位比特 | | 是否修改像素 | 否 | 是(最低位) | | 视觉伪影 | 无 | bit 0 时细微,更高位时可见 | | 容量 | 无限(支持分卷) | bit 0 时 `W×H×3÷8` 字节 | | 多文件 / 目录 | 支持(压缩为 ZIP) | 不支持(单个原始文件) | | 加密 | AES-256,通过 `--password` | 不内置,需手动预加密 | | 完整性校验 | 自动追加 SHA-256 | 无 | | 载体格式 | PNG、JPG、JPEG、BMP | 仅 PNG | | 可检测性 | 文件尾部可见 ZIP 结构 | 无显式结构,需统计分析 | | 文件体积变化 | 随载荷增大 | 不变(图片尺寸不变) | | 分卷 | 支持 `--part-size` / `--parts` | 不支持 | | 元数据清理 | 支持 `--strip-metadata` | 不支持 | | `info` 命令 | 支持 | 不支持 | | 自动模式 | 支持 | 不支持 | ### 适用场景 **使用 append-zip 当:** - 需要打包多个文件或目录 - 关注容量(大载荷、分卷) - 想要内置 AES-256 加密 - 需要解码时的完整性校验 - 希望完全不修改像素 - 不介意文件在简单检查下被识别为包含附加数据 **使用 LSB 当:** - 需要图片通过常规视觉检查,不暴露明显的 ZIP 尾部 - 嵌入的是单个(可能是预先加密的)载荷 - 希望保持图片尺寸不变 - 接受较低的容量和需要手动验证的折衷 - 所处环境会标记末尾附加 ZIP 数据的文件 ### 用法 ```bash # 将单个原始文件编码到 PNG 像素中(bit 0,视觉影响最小) rust_fake_file encode payload.bin --output hidden.png --mode lsb --width 128 --height 128 # 使用更高位索引获得更大容量(bit 1 = 更多数据,视觉影响稍大) rust_fake_file encode payload.bin --image cover.png --output hidden.png --mode lsb --bit-index 1 # 解码需要使用相同的 --bit-index 并显式指定输出路径 rust_fake_file decode hidden.png --output restored.bin --mode lsb --bit-index 1 ``` ## 密码 在 `encode` 和 `decode` 中使用相同密码来源即可加密和解密 ZIP 载荷。加密使用 AES-256。 ```bash # 环境变量 export RUST_FAKE_FILE_PASSWORD=s3cret rust_fake_file encode secrets/ --output vault.png rust_fake_file decode vault.png # 密码文件 rust_fake_file encode secrets/ --output vault.png --password-file ~/.secret_key rust_fake_file decode vault.png --password-file ~/.secret_key # 命令行密码(兼容旧用法,但会暴露在 shell history / 进程列表中) rust_fake_file encode secrets/ --output vault.png --password mypass rust_fake_file decode vault.png --password mypass ``` 密码优先级为 `--password`、`--password-file`、`RUST_FAKE_FILE_PASSWORD`、无密码。`--password` 和 `--password-file` 互斥。`decode` 检测到加密 ZIP 且没有任何密码来源时,会提示输入密码。 ## 完整性校验 `encode` 会自动在嵌入的 ZIP 载荷后追加 SHA-256 校验信息: ```text [image bytes][zip bytes][sha256][RFKSUM] ``` `decode` 会在解压前校验该哈希;校验失败会提示数据可能已损坏。分卷模式下每个 part 独立校验。只有在明确处理已截断或手动修改的文件时才使用 `--no-verify`。 ## Info `info` 会显示是否包含嵌入数据、载荷大小、是否加密、分卷信息、文件/目录数量、未加密时的条目列表、完整性状态,以及载体图片格式和尺寸: ```bash rust_fake_file info out.png ``` 退出码:`0` 表示包含嵌入数据,`1` 表示不包含嵌入数据,`2` 表示文件不存在或读取失败。 ## 进度条 输入超过 10 MiB 的操作会在 stderr 显示进度;小文件默认不显示,避免闪烁。使用 `--quiet` 可完全禁用进度输出。 ## 解码行为 - `rust_fake_file decode out.png` — 提取文件到 `out.png` 所在目录 - `rust_fake_file decode out.png -o extracted/` — 提取到指定目录 - `rust_fake_file decode https://example.com/hidden.png -o extracted/` — 下载图片并提取到指定目录 - `rust_fake_file decode album.part001-of003.png` — 自动发现同组分卷,重组 ZIP 后提取 - 自动模式(`rust_fake_file out.png`)优先尝试解码;若无嵌入 ZIP 则回退到编码 - 自动模式遇到 `http://`、`https://` 或 `ftp://` 时会下载并解码 URL,不会回退到编码 ## 开发 ```bash # 运行所有测试 cargo test # 使用 release 优化运行 cargo test --release # Clippy 检查 cargo clippy ``` ## 注意事项 - 工具生成的图片文件可以在任何图片查看器中正常打开 - ZIP 归档追加在图片数据之后,因此图片始终可正常查看 - 支持的载体图片格式:PNG、JPG/JPEG、BMP - 密码加密通过 `zip` crate 使用 AES-256