Skip to content

Commit 91ecb46

Browse files
committed
feat: rust insert-dylib with docs, CI, and release workflow
0 parents  commit 91ecb46

9 files changed

Lines changed: 1195 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
build:
12+
name: Build / Lint (macOS aarch64)
13+
runs-on: macos-14
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Install Rust toolchain
20+
uses: dtolnay/rust-toolchain@stable
21+
with:
22+
components: rustfmt, clippy
23+
24+
- name: Add target
25+
run: rustup target add aarch64-apple-darwin
26+
27+
- name: Cache cargo artifacts
28+
uses: Swatinem/rust-cache@v2
29+
30+
- name: Check formatting
31+
run: cargo fmt --all -- --check
32+
33+
- name: Check compilation
34+
run: cargo check --all-targets --target aarch64-apple-darwin
35+
36+
- name: Run clippy
37+
run: cargo clippy --all-targets --target aarch64-apple-darwin -- -D warnings
38+
39+
- name: Run tests
40+
run: cargo test --target aarch64-apple-darwin

.github/workflows/release.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
build:
13+
name: Build release asset (macOS aarch64)
14+
runs-on: macos-14
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Install Rust toolchain
21+
uses: dtolnay/rust-toolchain@stable
22+
23+
- name: Add target
24+
run: rustup target add aarch64-apple-darwin
25+
26+
- name: Cache cargo artifacts
27+
uses: Swatinem/rust-cache@v2
28+
29+
- name: Build release binary
30+
run: cargo build --release --target aarch64-apple-darwin
31+
32+
- name: Package archive
33+
shell: bash
34+
run: |
35+
set -euo pipefail
36+
package_dir="insert-dylib-macos-aarch64"
37+
mkdir -p dist "$package_dir"
38+
cp "target/aarch64-apple-darwin/release/insert-dylib" "$package_dir/"
39+
cp README.md README.cn.md LICENSE "$package_dir/"
40+
tar -czf "dist/${package_dir}.tar.gz" "$package_dir"
41+
42+
- name: Upload workflow artifact
43+
uses: actions/upload-artifact@v4
44+
with:
45+
name: insert-dylib-macos-aarch64
46+
path: dist/*.tar.gz
47+
48+
publish:
49+
name: Publish GitHub Release
50+
needs: build
51+
runs-on: ubuntu-latest
52+
53+
steps:
54+
- name: Download built artifacts
55+
uses: actions/download-artifact@v4
56+
with:
57+
path: dist
58+
59+
- name: Flatten artifacts
60+
run: |
61+
set -euo pipefail
62+
mkdir -p release-files
63+
find dist -type f -name '*.tar.gz' -exec cp {} release-files/ \;
64+
65+
- name: Generate SHA256 checksums
66+
run: |
67+
set -euo pipefail
68+
cd release-files
69+
sha256sum *.tar.gz > SHA256SUMS.txt
70+
71+
- name: Create GitHub Release
72+
uses: softprops/action-gh-release@v2
73+
with:
74+
files: |
75+
release-files/*.tar.gz
76+
release-files/SHA256SUMS.txt
77+
generate_release_notes: true
78+
env:
79+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "insert-dylib"
3+
version = "0.1.0"
4+
edition = "2024"
5+
authors = ["YinMo19 <me@yinmo19.top>"]
6+
description = "Inject LC_LOAD_DYLIB / LC_LOAD_WEAK_DYLIB into Mach-O binaries."
7+
readme = "README.md"
8+
homepage = "https://blog.yinmo19.top"
9+
repository = "https://github.com/YinMo19/insert-dylib"
10+
license = "MIT"
11+
keywords = ["macho", "dylib", "patcher", "binary", "macos"]
12+
categories = ["command-line-utilities"]
13+
14+
[package.metadata.docs.rs]
15+
default-target = "aarch64-apple-darwin"
16+
targets = ["aarch64-apple-darwin"]
17+
18+
[dependencies]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 YinMo19
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.cn.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# insert-dylib(Rust 版)
2+
3+
这是 `insert_dylib` 的 Rust 重写版本,用于向 Mach-O 二进制注入新的 `LC_LOAD_DYLIB`(或 `LC_LOAD_WEAK_DYLIB`)load command。
4+
5+
> 该工具会直接修改二进制文件。请务必先备份,并在分发前自行验证结果。
6+
7+
## 平台支持
8+
9+
- 仅支持:**macOS aarch64(Apple Silicon)**
10+
- 其它目标会在编译期直接报错(设计如此)。
11+
12+
## 功能
13+
14+
- 注入 `LC_LOAD_DYLIB``LC_LOAD_WEAK_DYLIB`
15+
- 支持 thin Mach-O 与 fat binary
16+
- 支持原地修改(in-place)或输出到新文件
17+
- 支持可选移除代码签名 load command(并尝试调整相关 `__LINKEDIT` 信息)
18+
- 交互式安全确认,支持 `--all-yes` 无人值守
19+
20+
## 构建
21+
22+
```bash
23+
cargo build --release --target aarch64-apple-darwin
24+
```
25+
26+
生成的可执行文件位于:
27+
28+
- `target/aarch64-apple-darwin/release/insert-dylib`
29+
30+
## 用法
31+
32+
```bash
33+
insert-dylib [options] <dylib_path> <binary_path> [new_binary_path]
34+
```
35+
36+
若未提供 `new_binary_path`(且未使用 `--inplace`),默认输出为:
37+
38+
- `<binary_path>_patched`
39+
40+
### 选项
41+
42+
- `--inplace` 直接修改输入文件
43+
- `--weak` 使用 `LC_LOAD_WEAK_DYLIB`,而不是 `LC_LOAD_DYLIB`
44+
- `--overwrite` 允许无提示覆盖输出文件
45+
- `--strip-codesig` 在可行时强制移除 `LC_CODE_SIGNATURE`
46+
- `--no-strip-codesig` 不移除 `LC_CODE_SIGNATURE`
47+
- `--all-yes` 对所有交互提示自动回答 `yes`
48+
49+
## 示例
50+
51+
注入 dylib,输出到默认文件:
52+
53+
```bash
54+
insert-dylib @executable_path/libHook.dylib MyApp
55+
```
56+
57+
使用弱依赖并原地修改:
58+
59+
```bash
60+
insert-dylib --weak --inplace @rpath/libHook.dylib MyApp
61+
```
62+
63+
强制剥离代码签名并覆盖输出:
64+
65+
```bash
66+
insert-dylib --strip-codesig --overwrite @loader_path/libHook.dylib MyApp MyApp.patched
67+
```
68+
69+
## 代码签名说明
70+
71+
若移除了 `LC_CODE_SIGNATURE`,原签名会失效。需要的话请对补丁后的文件重新签名:
72+
73+
```bash
74+
codesign --force --sign - MyApp.patched
75+
```
76+
77+
## CI 与自动发布
78+
79+
- CI 工作流:`.github/workflows/ci.yml`
80+
-**macOS aarch64** 上执行 `fmt``check``clippy``test`
81+
- Release 工作流:`.github/workflows/release.yml`
82+
- 当推送形如 `v1.0.0` 的 tag 时自动触发
83+
- 构建 **macOS aarch64** 发布二进制并上传到 GitHub Release。
84+
85+
发布示例:
86+
87+
```bash
88+
git tag v0.1.0
89+
git push origin v0.1.0
90+
```
91+
92+
## 发布到 crates.io
93+
94+
发布前建议执行:
95+
96+
```bash
97+
cargo package
98+
cargo publish
99+
```
100+
101+
## 法律与安全
102+
103+
请仅对你有权限修改的二进制使用本工具。

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# insert-dylib (Rust)
2+
3+
A Rust rewrite of `insert_dylib`: injects a new `LC_LOAD_DYLIB` (or `LC_LOAD_WEAK_DYLIB`) load command into Mach-O binaries.
4+
5+
> This tool modifies binary files directly. Always keep backups and validate outputs before distribution.
6+
7+
## Platform Support
8+
9+
- Supported platform: **macOS aarch64 (Apple Silicon)**
10+
- Unsupported targets fail at compile time by design.
11+
12+
## Features
13+
14+
- Inject `LC_LOAD_DYLIB` or `LC_LOAD_WEAK_DYLIB`
15+
- Works with thin Mach-O and fat binaries
16+
- Optional in-place patching or writing to a new output file
17+
- Optional code-signature load command stripping (with related `__LINKEDIT` adjustments)
18+
- Interactive safety prompts, with `--all-yes` for non-interactive runs
19+
20+
## Build
21+
22+
```bash
23+
cargo build --release --target aarch64-apple-darwin
24+
```
25+
26+
The binary will be at:
27+
28+
- `target/aarch64-apple-darwin/release/insert-dylib`
29+
30+
## Usage
31+
32+
```bash
33+
insert-dylib [options] <dylib_path> <binary_path> [new_binary_path]
34+
```
35+
36+
If `new_binary_path` is omitted (and `--inplace` is not used), output defaults to:
37+
38+
- `<binary_path>_patched`
39+
40+
### Options
41+
42+
- `--inplace` patch the input binary in place
43+
- `--weak` use `LC_LOAD_WEAK_DYLIB` instead of `LC_LOAD_DYLIB`
44+
- `--overwrite` allow overwriting output without prompt
45+
- `--strip-codesig` force removing `LC_CODE_SIGNATURE` when possible
46+
- `--no-strip-codesig` never remove `LC_CODE_SIGNATURE`
47+
- `--all-yes` auto-answer `yes` to all interactive prompts
48+
49+
## Examples
50+
51+
Inject a dylib and write to default output:
52+
53+
```bash
54+
insert-dylib @executable_path/libHook.dylib MyApp
55+
```
56+
57+
Inject weak dylib in place:
58+
59+
```bash
60+
insert-dylib --weak --inplace @rpath/libHook.dylib MyApp
61+
```
62+
63+
Force code-signature stripping and overwrite output:
64+
65+
```bash
66+
insert-dylib --strip-codesig --overwrite @loader_path/libHook.dylib MyApp MyApp.patched
67+
```
68+
69+
## Code Signing Note
70+
71+
If `LC_CODE_SIGNATURE` is removed, the binary's signature is invalidated. Re-sign the patched binary if needed:
72+
73+
```bash
74+
codesign --force --sign - MyApp.patched
75+
```
76+
77+
## CI and Release
78+
79+
- CI workflow: `.github/workflows/ci.yml`
80+
- Runs `fmt`, `check`, `clippy`, and `test` on **macOS aarch64**.
81+
- Release workflow: `.github/workflows/release.yml`
82+
- Triggered when pushing tags like `v1.0.0`
83+
- Builds **macOS aarch64** release binaries and publishes GitHub Release assets.
84+
85+
Create a release tag:
86+
87+
```bash
88+
git tag v0.1.0
89+
git push origin v0.1.0
90+
```
91+
92+
## Publish to crates.io
93+
94+
Before publishing:
95+
96+
```bash
97+
cargo package
98+
cargo publish
99+
```
100+
101+
## Legal / Safety
102+
103+
Use this tool only on binaries you are authorized to modify.

0 commit comments

Comments
 (0)