update
This commit is contained in:
16
examples/hlil_lifter/Cargo.toml
Normal file
16
examples/hlil_lifter/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "hlil_lifter"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# Uncomment this if you're writing a plugin (plugins are shared objects loaded by the core):
|
||||
# [lib]
|
||||
# crate-type = ["cdylib"]
|
||||
|
||||
# You can point at the BinaryNinja dependency in one of two ways, via path:
|
||||
[dependencies]
|
||||
binaryninja = {path="../../"}
|
||||
|
||||
# Or directly at the git repo:
|
||||
# [dependencies]
|
||||
# binaryninja = {git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"}
|
||||
68
examples/hlil_lifter/build.rs
Normal file
68
examples/hlil_lifter/build.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
static LASTRUN_PATH: (&str, &str) = ("HOME", "Library/Application Support/Binary Ninja/lastrun");
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
static LASTRUN_PATH: (&str, &str) = ("HOME", ".binaryninja/lastrun");
|
||||
|
||||
#[cfg(windows)]
|
||||
static LASTRUN_PATH: (&str, &str) = ("APPDATA", "Binary Ninja\\lastrun");
|
||||
|
||||
// Check last run location for path to BinaryNinja; Otherwise check the default install locations
|
||||
fn link_path() -> PathBuf {
|
||||
use std::io::prelude::*;
|
||||
|
||||
let home = PathBuf::from(env::var(LASTRUN_PATH.0).unwrap());
|
||||
let lastrun = PathBuf::from(&home).join(LASTRUN_PATH.1);
|
||||
|
||||
File::open(lastrun)
|
||||
.and_then(|f| {
|
||||
let mut binja_path = String::new();
|
||||
let mut reader = BufReader::new(f);
|
||||
|
||||
reader.read_line(&mut binja_path)?;
|
||||
Ok(PathBuf::from(binja_path.trim()))
|
||||
})
|
||||
.unwrap_or_else(|_| {
|
||||
#[cfg(target_os = "macos")]
|
||||
return PathBuf::from("/Applications/Binary Ninja.app/Contents/MacOS");
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
return home.join("binaryninja");
|
||||
|
||||
#[cfg(windows)]
|
||||
return PathBuf::from(env::var("PROGRAMFILES").unwrap())
|
||||
.join("Vector35\\BinaryNinja\\");
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
|
||||
let install_path = env::var("BINARYNINJADIR")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|_| link_path());
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
println!(
|
||||
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
|
||||
install_path.to_str().unwrap(),
|
||||
install_path.to_str().unwrap(),
|
||||
);
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
println!(
|
||||
"cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
|
||||
install_path.to_str().unwrap(),
|
||||
install_path.to_str().unwrap(),
|
||||
);
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
println!("cargo:rustc-link-lib=binaryninjacore");
|
||||
println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
|
||||
}
|
||||
}
|
||||
52
examples/hlil_lifter/src/main.rs
Normal file
52
examples/hlil_lifter/src/main.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::env;
|
||||
|
||||
use binaryninja::binaryview::BinaryViewExt;
|
||||
|
||||
// Standalone executables need to provide a main function for rustc
|
||||
// Plugins should refer to `binaryninja::command::*` for the various registration callbacks.
|
||||
fn main() {
|
||||
let mut args = env::args();
|
||||
let _ = args.next().unwrap();
|
||||
let Some(filename) = args.next() else {
|
||||
panic!("Expected input filename\n");
|
||||
};
|
||||
|
||||
// This loads all the core architecture, platform, etc plugins
|
||||
// Standalone executables probably need to call this, but plugins do not
|
||||
println!("Loading plugins...");
|
||||
binaryninja::headless::init();
|
||||
|
||||
// Your code here...
|
||||
println!("Loading binary...");
|
||||
let bv = binaryninja::load(filename).expect("Couldn't open binary file");
|
||||
|
||||
// Go through all functions in the binary
|
||||
for func in bv.functions().iter() {
|
||||
let sym = func.symbol();
|
||||
println!("Function {}:", sym.full_name());
|
||||
|
||||
let Ok(il) = func.high_level_il(true) else {
|
||||
println!(" Does not have HLIL\n");
|
||||
continue;
|
||||
};
|
||||
// Get the SSA form for this function
|
||||
let il = il.ssa_form();
|
||||
|
||||
// Loop through all blocks in the function
|
||||
for block in il.basic_blocks().iter() {
|
||||
// Loop though each instruction in the block
|
||||
for instr in block.iter() {
|
||||
// Uplift the instruction into a native rust format
|
||||
let lifted = instr.lift();
|
||||
let address = instr.address;
|
||||
|
||||
// print the lifted instruction
|
||||
println!("{address:08x}: {lifted:x?}");
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
// Important! Standalone executables need to call shutdown or they will hang forever
|
||||
binaryninja::headless::shutdown();
|
||||
}
|
||||
Reference in New Issue
Block a user