#bindings #disassembly #high-level #engine #framework #name #capstone-engine

no-std capstone

High level bindings to capstone disassembly engine (https://capstone-engine.org/)

18 releases (breaking)

0.12.0 Feb 25, 2024
0.11.0 May 2, 2022
0.10.0 Aug 9, 2021
0.9.0 Jul 14, 2021
0.0.3 Apr 5, 2015

#94 in Debugging

Download history 8748/week @ 2023-11-20 8526/week @ 2023-11-27 8531/week @ 2023-12-04 8874/week @ 2023-12-11 8135/week @ 2023-12-18 6759/week @ 2023-12-25 7245/week @ 2024-01-01 8933/week @ 2024-01-08 9788/week @ 2024-01-15 10984/week @ 2024-01-22 10838/week @ 2024-01-29 10770/week @ 2024-02-05 10992/week @ 2024-02-12 10375/week @ 2024-02-19 11102/week @ 2024-02-26 10069/week @ 2024-03-04

43,341 downloads per month
Used in 41 crates (26 directly)

MIT license

32MB
1M SLoC

Bitbake 859K SLoC // 0.1% comments C 52K SLoC // 0.1% comments Rust 33K SLoC // 0.0% comments Python 22K SLoC // 0.1% comments C# 19K SLoC // 0.1% comments Java 15K SLoC // 0.0% comments OCaml 14K SLoC // 0.0% comments VB6 3K SLoC // 0.2% comments Shell 855 SLoC // 0.1% comments PowerShell 519 SLoC // 0.3% comments C++ 484 SLoC // 0.1% comments Visual Studio Project 310 SLoC Batch 290 SLoC // 0.0% comments RPM Specfile 121 SLoC // 0.0% comments Visual Studio Solution 68 SLoC Cython 43 SLoC Prolog 28 SLoC

Contains (Mach-o exe, 9KB) examples/darwin

capstone-rs

Crates.io Badge

Linux Github Workflow CI Badge | Windows Appveyor CI Badge | FreeBSD Cirrus CI Badge

codecov

API Documentation

Bindings to the capstone library disassembly framework.

The Capstone struct is the main interface to the library.

Requirements

capstone-rs uses the capstone-sys crate to provide the low-level bindings to the Capstone C library.

See the capstone-sys page for the requirements and supported platforms.

  • Minimum Rust Version: 1.60.0

Example

extern crate capstone;

use capstone::prelude::*;

const X86_CODE: &'static [u8] = b"\x55\x48\x8b\x05\xb8\x13\x00\x00\xe9\x14\x9e\x08\x00\x45\x31\xe4";

/// Print register names
fn reg_names(cs: &Capstone, regs: &[RegId]) -> String {
    let names: Vec<String> = regs.iter().map(|&x| cs.reg_name(x).unwrap()).collect();
    names.join(", ")
}

/// Print instruction group names
fn group_names(cs: &Capstone, regs: &[InsnGroupId]) -> String {
    let names: Vec<String> = regs.iter().map(|&x| cs.group_name(x).unwrap()).collect();
    names.join(", ")
}

fn main() {
    let cs = Capstone::new()
        .x86()
        .mode(arch::x86::ArchMode::Mode64)
        .syntax(arch::x86::ArchSyntax::Att)
        .detail(true)
        .build()
        .expect("Failed to create Capstone object");

    let insns = cs.disasm_all(X86_CODE, 0x1000)
        .expect("Failed to disassemble");
    println!("Found {} instructions", insns.len());
    for i in insns.as_ref() {
        println!();
        println!("{}", i);

        let detail: InsnDetail = cs.insn_detail(&i).expect("Failed to get insn detail");
        let arch_detail: ArchDetail = detail.arch_detail();
        let ops = arch_detail.operands();

        let output: &[(&str, String)] = &[
            ("insn id:", format!("{:?}", i.id().0)),
            ("bytes:", format!("{:?}", i.bytes())),
            ("read regs:", reg_names(&cs, detail.regs_read())),
            ("write regs:", reg_names(&cs, detail.regs_write())),
            ("insn groups:", group_names(&cs, detail.groups())),
        ];

        for &(ref name, ref message) in output.iter() {
            println!("{:4}{:12} {}", "", name, message);
        }

        println!("{:4}operands: {}", "", ops.len());
        for op in ops {
            println!("{:8}{:?}", "", op);
        }
    }
}

Produces:

Found 4 instructions

0x1000: pushq %rbp
    read regs:   rsp
    write regs:  rsp
    insn groups: mode64

0x1001: movq 0x13b8(%rip), %rax
    read regs:
    write regs:
    insn groups:

0x1008: jmp 0x8ae21
    read regs:
    write regs:
    insn groups: jump

0x100d: xorl %r12d, %r12d
    read regs:
    write regs:  rflags
    insn groups:

To see more demos, see the examples/ directory. More complex demos welcome!

Features

  • full: do not compile Capstone C library in diet mode
  • std: enable std-only features, such as the Error trait
  • use_bindgen: run bindgen to generate Rust bindings to Capstone C library instead of using pre-generated bindings (not recommended).

: enabled by default

Reporting Issues

Please open a Github issue

Author

You may find a full list of contributors on Github.

License

MIT

Dependencies