emproof-com/nyxstone
GitHub: emproof-com/nyxstone
Nyxstone 是一个基于 LLVM 的高效汇编/反汇编库,通过 C++、Rust 和 Python 绑定为多种 CPU 架构提供指令级编码与解码能力。
Stars: 409 | Forks: 22
# Nyxstone
[](https://github.com/emproof-com/nyxstone/actions/workflows/cpp.yml)
[](https://crates.io/crates/nyxstone)
[](https://pypi.org/project/nyxstone)
[](https://emproof-com.github.io/nyxstone/)
Nyxstone 是一个基于 LLVM 构建的快速汇编和反汇编库。它不需要对 LLVM 源码树打补丁,并且可以链接到大多数 Linux 发行版、Homebrew 和 `apt.llvm.org` 提供的标准 LLVM 库。其核心是一个 C++ 库,并提供了 Rust 和 Python 的绑定。Nyxstone 支持所链接的 LLVM 附带的所有架构,并允许你配置特定于架构的 CPU 和功能设置。

## 目录
1. [核心功能](#core-features)
2. [使用 Nyxstone](#using-nyxstone)
1. [前置条件](#prerequisites)
2. [CLI 工具](#cli-tool)
3. [C++ 库](#c-library)
4. [Rust 绑定](#rust-bindings)
5. [Python 绑定](#python-bindings)
3. [工作原理](#how-it-works)
4. [基准测试](#benchmarks)
5. [路线图](#roadmap)
6. [许可证](#license)
7. [贡献](#contributing)
8. [维护者](#maintainers)
## 核心功能
* 为所链接 LLVM 支持的每种架构汇编和反汇编代码,包括 x86、ARM、AArch64、MIPS、RISC-V 等。
* 基于 LLVM 构建的 C++ 库,并提供 Rust 和 Python 绑定。
* 原生支持 Linux 和 macOS 平台。
* 支持汇编器中的标签,包括用户提供的标签到地址的映射。
* 输出原始字节、文本反汇编,或者同时包含地址、原始字节和汇编文本的详细指令对象。
* 反汇编可以限制为用户指定的指令数量。
* 可配置的各架构目标设置(CPU、ISA 扩展、硬件功能)。
* 汇编常见的数据指令(`.byte`、`.word`、`.org`、`.nops`、`.align`、`.fill`、`.uleb128`、…)以及 ARM/AArch64 的 `ldr rX, =const` 字面量池。
* 对于无法表示的输入(例如,切换到 `.text` 以外的 section),会报告明确的错误,而不是静默丢弃受影响的字节。
有关支持的架构列表,请运行 `clang -print-targets`。有关各架构的特性,请运行 `llc -march=ARCH -mattr=help`。
## 使用 Nyxstone
本部分提供了如何开始使用 Nyxstone 的说明,涵盖必要的前置条件、如何使用 CLI 工具,以及通过 C++、Rust 和 Python 使用该库的分步指南。
### 前置条件
在构建 Nyxstone 之前,请确保你的系统上安装了 clang 和 LLVM。**Nyxstone 支持 LLVM 的 15-20 主版本。** 这些主版本内的任何次要/修补版本都可以使用;构建过程会选择它能找到的最新版 LLVM,除非你指定了特定版本。
构建过程按以下顺序解析 LLVM:
1. `$NYXSTONE_LLVM_PREFIX`(如果已设置)。构建过程会专门搜索该前缀(忽略系统路径),因此当安装了多个版本时,这是固定使用特定版本的方法。
2. 按照从新到旧的顺序探测已知的各主版本安装布局:`/usr/lib/llvm-`(Debian/Ubuntu)、`/opt/homebrew/opt/llvm@`(Apple Silicon 上的 Homebrew)、`/usr/local/opt/llvm@`(x86 macOS 上的 Homebrew)、`/opt/brew/opt/llvm@`(Linux 上自定义前缀的 Homebrew)。
3. CMake 默认的 `find_package(LLVM)` 搜索。
如果解析到的版本不在 15-20 之间,配置步骤将失败并显示明确的错误。
#### 安装
* **Debian / Ubuntu**
sudo apt install llvm-${version} llvm-${version}-dev
Debian trixie 提供 17-19 版本,Ubuntu 在默认仓库中提供 15-17 版本。对于你的发行版仓库中没有的版本,请按照 [apt.llvm.org](https://apt.llvm.org/) 上的说明进行操作。脚本 `apt.llvm.org/llvm.sh ` 是最简单的方法。
* **Arch**
sudo pacman -S llvm llvm-libs
* **Homebrew (macOS / Linux)**
brew install llvm@20
export NYXSTONE_LLVM_PREFIX="$(brew --prefix llvm@20)"
* **从源码构建**
在 Windows 上,请从 Visual Studio 2022 x64 命令提示符中运行这些命令,并将 `~/lib/my-llvm-20` 替换为你选择的路径。
git clone -b release/20.x --single-branch https://github.com/llvm/llvm-project.git
cd llvm-project
cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_PARALLEL_LINK_JOBS=1
cmake --build build
cmake --install build --prefix ~/lib/my-llvm-20
export NYXSTONE_LLVM_PREFIX=~/lib/my-llvm-20
你可能还需要 LLVM 编译时所依赖的任何系统库。使用 `llvm-config --system-libs` 进行检查;在 Debian/Ubuntu 上,这通常是 `zlib1g-dev` 和 `libzstd-dev`。
### CLI 工具
Nyxstone 提供了一个用于一次性汇编和反汇编的 [CLI 工具](examples/nyxstone-cli.cpp)。克隆仓库并使用 CMake 构建它:
```
git clone https://github.com/emproof-com/nyxstone
cd nyxstone
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j
```
生成的 `nyxstone` 二进制文件位于 `build/` 目录下。其帮助菜单如下:
```
$ ./nyxstone -h
Usage: nyxstone [-t=] [-p=] [-d]
Examples:
# Assemble an instruction with the default architecture ('x86_64').
nyxstone 'push eax'
# Disassemble the bytes 'ffc300d1' as AArch64 code.
nyxstone -t aarch64 -d ffc300d1
Options:
-t, --triple= LLVM target triple or alias, e.g. 'aarch64'
-c, --cpu= LLVM CPU specifier, e.g. 'cortex-a53'
-f, --features=
- LLVM architecture/CPU feature list, e.g. '+mte,-neon'
-p, --address=
- Label-to-address mappings (used when assembling only)
-d, --disassemble Treat as bytes to disassemble instead of assembly
-h, --help Show this help and usage message
Notes:
The '--triple' parameter also supports aliases for common target triples:
'x86_32' -> 'i686-linux-gnu'
'x86_64' -> 'x86_64-linux-gnu'
'armv6m' -> 'armv6m-none-eabi'
'armv7m' -> 'armv7m-none-eabi'
'armv8m' -> 'armv8m.main-none-eabi'
'aarch64' -> 'aarch64-linux-gnueabihf'
The CPUs for a target can be found with 'llc -mtriple=
标签:Bash脚本, C++, LLVM, Python, Rust, 可视化界面, 底层开发, 数据擦除, 无后门, 汇编/反汇编, 网络流量审计, 逆向工具, 通知系统