7 个不稳定版本 (3 个破坏性更新)
0.3.0 | 2024 年 8 月 14 日 |
---|---|
0.2.0 | 2024 年 8 月 13 日 |
0.1.0 | 2024 年 7 月 24 日 |
0.0.3 | 2024 年 7 月 17 日 |
0.0.1 | 2024 年 5 月 26 日 |
#407 in 解析实现
每月下载量 1,609
用于 3 个 crate(2 个直接使用)
775KB
955 行
包含 (ELF 可执行文件/库,410KB) tests/exec_elf64,(Mach-o 可执行文件,400KB) tests/exec_mach64,(DOS 可执行文件,145KB) tests/exec_pe64
libsui
Sui (सुई) 是适用于可执行格式(ELF、PE、Mach-O)的注入工具,允许您将文件嵌入现有二进制文件并在运行时提取它们。
它生成的可执行文件在 macOS 和 Windows 上可以进行代码签名。
使用方法
cargo add libsui
将数据嵌入二进制文件
use libsui::{Macho, PortableExecutable};
let exe = std::fs::read("tests/exec_mach64")?;
let mut out = std::fs::File::create("out")?;
Macho::from(exe)?
.write_section("__hello", b"Hello, World!".to_vec())?
.build(&mut out)?;
let exe = std::fs::read("tests/exec_pe64")?;
let mut out = std::fs::File::create("out.exe")?;
PortableExecutable::from(exe)?
.write_resource("hello.txt", b"Hello, World!".to_vec())?
.build(&mut out)?;
从自身提取
use libsui::find_section;
let data = find_section("hello.txt")?;
设计
Mach-O
资源作为新段中的部分添加,加载命令被更新,偏移量被调整。 __LINKEDIT
保留在文件末尾。
它与链接器的 -sectcreate,__FOO,__foo,hello.txt
选项类似。
请注意,在 Apple Silicon 上,使用 Macho::build
将使现有的代码签名无效。内核拒绝运行具有坏签名的可执行文件。
使用 Macho::build_and_sign
重新签名二进制文件,并使用临时代签。有关详细信息,请参阅 apple_codesign.rs
。这类似于 codesign -s - ./out
命令。
Macho::from(exe)?
.write_section("__sect", data)?
.build_and_sign(&mut out)?;
$ codesign -d -vvv ./out
Executable=/Users/divy/gh/sui/out
Identifier=a.out
Format=Mach-O thin (arm64)
CodeDirectory v=20400 size=10238 flags=0x20002(adhoc,linker-signed) hashes=317+0 location=embedded
Hash type=sha256 size=32
CandidateCDHash sha256=6b1abb20f2291dd9b0dbcd0659a918cb2d0e6b18
CandidateCDHashFull sha256=6b1abb20f2291dd9b0dbcd0659a918cb2d0e6b1876153efa17f90dc8b3a8f177
Hash choices=sha256
CMSDigest=6b1abb20f2291dd9b0dbcd0659a918cb2d0e6b1876153efa17f90dc8b3a8f177
CMSDigestType=2
CDHash=6b1abb20f2291dd9b0dbcd0659a918cb2d0e6b18
Signature=adhoc
Info.plist=not bound
TeamIdentifier=not set
Sealed Resources=none
Internal requirements=none
PE
资源以 RT_RCDATA
类型添加到新的 PE 资源目录中,并在运行时通过 FindResource
和 LoadResource
提取。
ELF
数据简单地追加到文件末尾,并在运行时从 current_exe()
中提取。
这可能会改变,并且未来可能会使用 ELF 链接器注释(PT_NOTE
)。
测试
此包使用 LLVM 的 libFuzzer 进行模糊测试。请参阅 fuzz/。
exec_*
可执行文件在 tests/
中,由 tests/exec.rs
编译而成。
rustc exec.rs -o exec_elf64 --target x86_64-unknown-linux-gnu
许可证
MIT
依赖关系
~2–14MB
~122K SLoC