unicorn/bindings/rust
Bet4 9c6134ca91
Add Rust bindings to master branch (#1401)
* Rust bindings (#1295)

* rust bindings init

* updated bindings/README

* Improved Rust bindings: (#1309)

* Added basic block hooking
* Changed confusing struct naming. Before: Protection::All -> R,W,X, Now: Permission::All -> R,W,X
* Fixed issue with remove_hook(..). Implementation tried to remove hook from incorrect hashmap.
* Made unused private vmmap(..) public.

* Improve Rust bindings (#1367)

* fixed tests

* constant readability

* HookType as bitflags

* Mode as bitflags

* improve bitflags

* cargo fmt

* removed unnecessary "as usize"

* fixed buggy deallocation of uc_context structs

* Remove data field in rust bindings

Co-authored-by: Lukas Seidel <pr0me@users.noreply.github.com>
Co-authored-by: Nikolas Eller <nikolas.e@mail.de>
Co-authored-by: Simon Wörner <git@simon-woerner.de>
Co-authored-by: floesen <floesen@users.noreply.github.com>
2021-05-26 23:05:12 +08:00
..
src Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00
tests Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00
build.rs Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00
Cargo.toml Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00
COPYING Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00
README.md Add Rust bindings to master branch (#1401) 2021-05-26 23:05:12 +08:00

unicorn-rs

Rust bindings for the Unicorn emulator with utility functions.

An extended version for fuzzing with AFL++ support can be found in https://github.com/aflplusplus/unicornafl.

use unicorn::RegisterARM;
use unicorn::unicorn_const::{Arch, Mode, Permission, SECOND_SCALE};

fn main() {
    let arm_code32: Vec<u8> = vec![0x17, 0x00, 0x40, 0xe2]; // sub r0, #23

    let mut unicorn = unicorn::Unicorn::new(Arch::ARM, Mode::LITTLE_ENDIAN).expect("failed to initialize Unicorn instance");
    let mut emu = unicorn.borrow();
    emu.mem_map(0x1000, 0x4000, Permission::ALL).expect("failed to map code page");
    emu.mem_write(0x1000, &arm_code32).expect("failed to write instructions");

    emu.reg_write(RegisterARM::R0 as i32, 123).expect("failed write R0");
    emu.reg_write(RegisterARM::R5 as i32, 1337).expect("failed write R5");

    let _ = emu.emu_start(0x1000, (0x1000 + arm_code32.len()) as u64, 10 * SECOND_SCALE, 1000);
    assert_eq!(emu.reg_read(RegisterARM::R0 as i32), Ok(100));
    assert_eq!(emu.reg_read(RegisterARM::R5 as i32), Ok(1337));
}

Further sample code can be found in tests/unicorn.rs.

Installation

This project has been tested on Linux, OS X and Windows.

To use unicorn-rs, simply add it as a dependency to the Cargo.toml of your program.

[dependencies]
unicorn = { path = "/path/to/bindings/rust", version="1.0.0" }

Acknowledgements

These bindings are based on Sébastien Duquette's (@ekse) unicorn-rs. We picked up the project, as it is no longer maintained. Thanks to all contributers.