Skip to content

Commit 2b0d3cc

Browse files
committed
Adds data renderer UUID example
1 parent 7386c11 commit 2b0d3cc

File tree

6 files changed

+219
-2
lines changed

6 files changed

+219
-2
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"rust",
99
"arch/riscv",
1010
"arch/msp430",
11+
"rust/examples/data_renderer",
1112
"view/minidump",
1213
"plugins/dwarf/dwarf_import",
1314
"plugins/dwarf/dwarf_export",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "example_data_renderer"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
uuid = "1.18.1"
11+
binaryninjacore-sys = { path = "../../binaryninjacore-sys", default-features = false }
12+
binaryninja = { path = "../../" }
13+
log = "0.4.27"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Data Renderer Example
2+
3+
This example implements a simple data renderer for the Mach-O load command LC_UUID.
4+
You can try the renderer by loading the `/bin/cat` binary from macOS.
5+
6+
We're implementing a functionality similar to the one described in the Python data renderer blog post:
7+
https://binary.ninja/2024/04/08/customizing-data-display.html.
8+
9+
## Building
10+
11+
```sh
12+
# Build from the root directory (binaryninja-api)
13+
cargo build --manifest-path rust/examples/data_renderer/Cargo.toml
14+
# Link binary on macOS
15+
ln -sf $PWD/target/debug/libexample_data_renderer.dylib ~/Library/Application\ Support/Binary\ Ninja/plugins
16+
```
17+
18+
## Result
19+
20+
The following Mach-O load command be will be transformed from
21+
22+
```c
23+
struct uuid __macho_load_command_[10] =
24+
{
25+
enum load_command_type_t cmd = LC_UUID
26+
uint32_t cmdsize = 0x18
27+
uint8_t uuid[0x10] =
28+
{
29+
[0x0] = 0x74
30+
[0x1] = 0xa0
31+
[0x2] = 0x3a
32+
[0x3] = 0xbd
33+
[0x4] = 0x1e
34+
[0x5] = 0x19
35+
[0x6] = 0x32
36+
[0x7] = 0x67
37+
[0x8] = 0x9a
38+
[0x9] = 0xdc
39+
[0xa] = 0x42
40+
[0xb] = 0x99
41+
[0xc] = 0x4e
42+
[0xd] = 0x26
43+
[0xe] = 0xa2
44+
[0xf] = 0xb7
45+
}
46+
}
47+
```
48+
49+
into the following representation
50+
51+
```c
52+
struct uuid __macho_load_command_[10] =
53+
{
54+
enum load_command_type_t cmd = LC_UUID
55+
uint32_t cmdsize = 0x18
56+
uint8_t uuid[0x10] = UUID("74a03abd-1e19-3267-9adc-42994e26a2b7")
57+
}
58+
```
59+
60+
You can compare the shown UUID with the output of otool:
61+
```sh
62+
otool -arch all -l /bin/cat
63+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn main() {
2+
let link_path =
3+
std::env::var_os("DEP_BINARYNINJACORE_PATH").expect("DEP_BINARYNINJACORE_PATH not specified");
4+
5+
println!("cargo::rustc-link-lib=dylib=binaryninjacore");
6+
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());
7+
8+
#[cfg(target_os = "linux")]
9+
{
10+
println!(
11+
"cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}",
12+
link_path.to_string_lossy()
13+
);
14+
}
15+
16+
#[cfg(target_os = "macos")]
17+
{
18+
let crate_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME not set");
19+
let lib_name = crate_name.replace('-', "_");
20+
println!("cargo::rustc-link-arg=-Wl,-install_name,@rpath/lib{}.dylib", lib_name);
21+
}
22+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use binaryninja::binary_view::{BinaryView, BinaryViewBase};
2+
use binaryninja::data_renderer::{
3+
CustomDataRenderer, TypeContext, register_specific_data_renderer,
4+
};
5+
use binaryninja::disassembly::{
6+
DisassemblyTextLine, InstructionTextToken, InstructionTextTokenKind,
7+
};
8+
use binaryninja::types::{Type, TypeClass};
9+
use log::debug;
10+
use uuid::Uuid;
11+
12+
struct UuidDataRenderer {}
13+
14+
impl CustomDataRenderer for UuidDataRenderer {
15+
fn is_valid_for_data(
16+
&self,
17+
view: &BinaryView,
18+
addr: u64,
19+
type_: &Type,
20+
types: &[TypeContext],
21+
) -> bool {
22+
// We only want to render arrays with a size of 16 elements
23+
if type_.type_class() != TypeClass::ArrayTypeClass {
24+
return false;
25+
}
26+
if type_.count() != 0x10 {
27+
return false;
28+
}
29+
30+
// The array elements must be of the type uint8_t
31+
let Some(element_type_conf) = type_.element_type() else {
32+
return false;
33+
};
34+
let element_type = element_type_conf.contents;
35+
if element_type.type_class() != TypeClass::IntegerTypeClass {
36+
return false;
37+
}
38+
if element_type.width() != 1 {
39+
return false;
40+
}
41+
42+
// The array should be embedded in a named type reference with the id macho:["uuid"]
43+
for type_ctx in types {
44+
if type_ctx.type_().type_class() != TypeClass::NamedTypeReferenceClass {
45+
continue;
46+
}
47+
48+
let Some(name_ref) = type_ctx.type_().get_named_type_reference() else {
49+
continue;
50+
};
51+
52+
if name_ref.id() == "macho:[\"uuid\"]" {
53+
return true;
54+
}
55+
}
56+
57+
false
58+
}
59+
60+
fn lines_for_data(
61+
&self,
62+
view: &BinaryView,
63+
addr: u64,
64+
type_: &Type,
65+
prefix: Vec<InstructionTextToken>,
66+
width: usize,
67+
types_ctx: &[TypeContext],
68+
language: &str,
69+
) -> Vec<DisassemblyTextLine> {
70+
let mut tokens = prefix.clone();
71+
72+
let mut buf = [0u8; 0x10];
73+
let bytes_read = view.read(&mut buf, addr);
74+
75+
// Make sure that we've read all UUID bytes and convert them to token
76+
if bytes_read == 0x10 {
77+
tokens.extend([
78+
InstructionTextToken::new("UUID(\"", InstructionTextTokenKind::Text),
79+
InstructionTextToken::new(
80+
Uuid::from_bytes(buf).to_string(),
81+
InstructionTextTokenKind::String { value: 0 },
82+
),
83+
InstructionTextToken::new("\")", InstructionTextTokenKind::Text),
84+
]);
85+
} else {
86+
tokens.push(InstructionTextToken::new(
87+
"error: cannot read 0x10 bytes",
88+
InstructionTextTokenKind::Annotation,
89+
));
90+
}
91+
92+
vec![DisassemblyTextLine::new_with_addr(tokens, addr)]
93+
}
94+
}
95+
96+
#[allow(non_snake_case)]
97+
#[unsafe(no_mangle)]
98+
pub unsafe extern "C" fn CorePluginInit() -> bool {
99+
// Initialize logging
100+
binaryninja::logger::Logger::new("UUID Data Renderer")
101+
.with_level(log::LevelFilter::Debug)
102+
.init();
103+
104+
// Register data renderer
105+
register_specific_data_renderer(UuidDataRenderer {});
106+
107+
true
108+
}

0 commit comments

Comments
 (0)