initial commit
This commit is contained in:
105
examples/basic_script/CMakeLists.txt
Normal file
105
examples/basic_script/CMakeLists.txt
Normal file
@@ -0,0 +1,105 @@
|
||||
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
|
||||
|
||||
project(test_headless)
|
||||
|
||||
file(GLOB PLUGIN_SOURCES
|
||||
${PROJECT_SOURCE_DIR}/Cargo.toml
|
||||
${PROJECT_SOURCE_DIR}/src/*.rs)
|
||||
|
||||
file(GLOB API_SOURCES
|
||||
${PROJECT_SOURCE_DIR}/../../../binaryninjacore.h
|
||||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/build.rs
|
||||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/Cargo.toml
|
||||
${PROJECT_SOURCE_DIR}/../../binaryninjacore-sys/src/*
|
||||
${PROJECT_SOURCE_DIR}/../../Cargo.toml
|
||||
${PROJECT_SOURCE_DIR}/../../src/*.rs)
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/debug)
|
||||
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target)
|
||||
else()
|
||||
set(TARGET_DIR ${PROJECT_BINARY_DIR}/target/release)
|
||||
set(CARGO_OPTS --target-dir=${PROJECT_BINARY_DIR}/target --release)
|
||||
endif()
|
||||
|
||||
set(OUTPUT_FILE basic_script${CMAKE_EXECUTABLE_SUFFIX})
|
||||
set(OUTPUT_PATH ${CMAKE_BINARY_DIR}/out/bin/${OUTPUT_FILE})
|
||||
|
||||
|
||||
if(NOT BN_API_BUILD_EXAMPLES)
|
||||
# Out-of-tree build
|
||||
find_path(
|
||||
BN_API_PATH
|
||||
NAMES binaryninjaapi.h
|
||||
HINTS ../../.. binaryninjaapi
|
||||
REQUIRED
|
||||
)
|
||||
add_subdirectory(${BN_API_PATH} api)
|
||||
endif()
|
||||
|
||||
add_custom_target(test_headless ALL DEPENDS ${OUTPUT_PATH})
|
||||
add_dependencies(test_headless binaryninjaapi)
|
||||
|
||||
# Get API source directory so we can find BinaryNinjaCore
|
||||
get_target_property(BN_API_SOURCE_DIR binaryninjaapi SOURCE_DIR)
|
||||
message(STATUS "${BN_API_SOURCE_DIR}")
|
||||
list(APPEND CMAKE_MODULE_PATH "${BN_API_SOURCE_DIR}/cmake")
|
||||
|
||||
# BinaryNinjaCore has the user plugins dir define that we want
|
||||
find_package(BinaryNinjaCore REQUIRED)
|
||||
|
||||
find_program(RUSTUP_PATH rustup REQUIRED HINTS ~/.cargo/bin)
|
||||
set(RUSTUP_COMMAND ${RUSTUP_PATH} run ${CARGO_API_VERSION} cargo build)
|
||||
|
||||
if(APPLE)
|
||||
if(UNIVERSAL)
|
||||
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/debug/${OUTPUT_FILE})
|
||||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/debug/${OUTPUT_FILE})
|
||||
else()
|
||||
set(AARCH64_LIB_PATH ${PROJECT_BINARY_DIR}/target/aarch64-apple-darwin/release/${OUTPUT_FILE})
|
||||
set(X86_64_LIB_PATH ${PROJECT_BINARY_DIR}/target/x86_64-apple-darwin/release/${OUTPUT_FILE})
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_PATH}
|
||||
COMMAND ${CMAKE_COMMAND} -E env
|
||||
MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_INSTALL_BIN_DIR}
|
||||
${RUSTUP_COMMAND} --target=aarch64-apple-darwin ${CARGO_OPTS}
|
||||
COMMAND ${CMAKE_COMMAND} -E env
|
||||
MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_INSTALL_BIN_DIR}
|
||||
${RUSTUP_COMMAND} --target=x86_64-apple-darwin ${CARGO_OPTS}
|
||||
COMMAND lipo -create ${AARCH64_LIB_PATH} ${X86_64_LIB_PATH} -output ${OUTPUT_PATH}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
|
||||
else()
|
||||
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
||||
set(LIB_PATH ${PROJECT_BINARY_DIR}/target/debug/${OUTPUT_FILE})
|
||||
else()
|
||||
set(LIB_PATH ${PROJECT_BINARY_DIR}/target/release/${OUTPUT_FILE})
|
||||
endif()
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_PATH}
|
||||
COMMAND ${CMAKE_COMMAND} -E env
|
||||
MACOSX_DEPLOYMENT_TARGET=10.14 BINARYNINJADIR=${BN_INSTALL_BIN_DIR}
|
||||
${RUSTUP_COMMAND} ${CARGO_OPTS}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${LIB_PATH} ${OUTPUT_PATH}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
|
||||
endif()
|
||||
elseif(WIN32)
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_PATH}
|
||||
COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BN_INSTALL_BIN_DIR} ${RUSTUP_COMMAND} ${CARGO_OPTS}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
|
||||
else()
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTPUT_PATH}
|
||||
COMMAND ${CMAKE_COMMAND} -E env BINARYNINJADIR=${BN_INSTALL_BIN_DIR} ${RUSTUP_COMMAND} ${CARGO_OPTS}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${TARGET_DIR}/${OUTPUT_FILE} ${OUTPUT_PATH}
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
DEPENDS ${PLUGIN_SOURCES} ${API_SOURCES})
|
||||
endif()
|
||||
8
examples/basic_script/Cargo.toml
Normal file
8
examples/basic_script/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "basic_script"
|
||||
version = "0.1.0"
|
||||
authors = ["KyleMiles <kyle@vector35.com>"]
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
binaryninja = {path="../../"}
|
||||
68
examples/basic_script/build.rs
Normal file
68
examples/basic_script/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());
|
||||
}
|
||||
}
|
||||
37
examples/basic_script/src/main.rs
Normal file
37
examples/basic_script/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use binaryninja::architecture::Architecture;
|
||||
use binaryninja::binaryview::{BinaryViewBase, BinaryViewExt};
|
||||
|
||||
fn main() {
|
||||
println!("Loading plugins..."); // This loads all the core architecture, platform, etc plugins
|
||||
let headless_session = binaryninja::headless::Session::new();
|
||||
|
||||
println!("Loading binary...");
|
||||
let bv = headless_session
|
||||
.load("/bin/cat")
|
||||
.expect("Couldn't open `/bin/cat`");
|
||||
|
||||
println!("Filename: `{}`", bv.file().filename());
|
||||
println!("File size: `{:#x}`", bv.len());
|
||||
println!("Function count: {}", bv.functions().len());
|
||||
|
||||
for func in &bv.functions() {
|
||||
println!(" `{}`:", func.symbol().full_name());
|
||||
for basic_block in &func.basic_blocks() {
|
||||
// TODO : This is intended to be refactored to be more nice to work with soon(TM)
|
||||
for addr in basic_block.as_ref() {
|
||||
print!(" {} ", addr);
|
||||
if let Some((_, tokens)) = func.arch().instruction_text(
|
||||
bv.read_buffer(addr, func.arch().max_instr_len())
|
||||
.unwrap()
|
||||
.get_data(),
|
||||
addr,
|
||||
) {
|
||||
tokens
|
||||
.iter()
|
||||
.for_each(|token| print!("{}", token.text().as_str()));
|
||||
println!();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user