summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorgerard <gerard@example.com>2026-07-10 16:23:34 +0200
committergerard <gerard@example.com>2026-07-10 16:23:34 +0200
commita7859eb641dd2f1d36e2a8d1c0fb7e626904f652 (patch)
treeb1140bef3dffe8e6dbaf5a2be516c4a93b3bee92 /src/lib.rs
parentad732c4cdd155f65ededda0011547fad815fcdef (diff)
update to latest binja version 5.4.10057HEADup-to-date
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs85
1 files changed, 44 insertions, 41 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 6dcd45e..3d171ce 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -28,9 +28,10 @@ use std::sync::atomic::AtomicUsize;
use std::time::SystemTime;
use binary_search::{Direction, binary_search};
+use binaryninja::settings::DEFAULT_INSTANCE_ID;
use binaryninja::{
architecture::Architecture,
- binary_view::{BinaryView, BinaryViewBase, BinaryViewExt},
+ binary_view::{BinaryView, BinaryViewBase},
command::{self, AddressCommand, Command},
settings::Settings,
};
@@ -43,6 +44,7 @@ use iced_x86::{
};
use rayon::prelude::{IntoParallelRefIterator, ParallelBridge, ParallelIterator};
use strum::{Display, EnumIter, EnumMessage, EnumString, IntoEnumIterator, VariantNames};
+use tracing::{error, info, warn};
type OwnedPattern = Vec<Option<u8>>;
type Pattern<'a> = &'a [Option<u8>];
@@ -168,7 +170,7 @@ impl FromSignature for OwnedPattern {}
trait FromSignature {
fn from_signature(mut pattern: String, signature_type: SignatureType) -> Option<OwnedPattern> {
- log::info!("attempting to parse a {signature_type}-style signature! \"{pattern}\"");
+ info!("attempting to parse a {signature_type}-style signature! \"{pattern}\"");
pattern = pattern.replace("\n", "");
pattern = pattern.replace("\r", "");
@@ -194,7 +196,7 @@ trait FromSignature {
} else {
let Ok(byte) = u8::from_str_radix(&format!("{}{}", byte[0], byte[1]), 16)
else {
- log::error!("unable to parse pattern!");
+ error!("unable to parse pattern!");
return None;
};
@@ -220,7 +222,7 @@ trait FromSignature {
let parts = pattern.split(" ").collect::<Vec<_>>();
if parts.len() != 2 {
- log::error!("unable to parse pattern!");
+ error!("unable to parse pattern!");
return None;
}
@@ -244,7 +246,7 @@ trait FromSignature {
let mut result = vec![
u8::from_str_radix(&byte, 16)
.map_err(|x| {
- log::error!("unable to parse pattern!");
+ error!("unable to parse pattern!");
x
})
.ok(),
@@ -271,7 +273,7 @@ trait FromSignature {
pattern.push(None);
}
_ => {
- log::error!("invalid char encountered!");
+ error!("invalid char encountered!");
}
}
}
@@ -452,7 +454,7 @@ fn create_pattern_internal_binarysearch(
data: &[(u64, Vec<u8>)],
include_operands: bool,
) -> Result<OwnedPattern, SignatureError> {
- log::info!("creating pattern for address {:#04X}", addr);
+ info!("creating pattern for address {:#04X}", addr);
let time = SystemTime::now();
let mut formatter = NasmFormatter::new();
@@ -477,7 +479,7 @@ fn create_pattern_internal_binarysearch(
.ok_or(SignatureError::InvalidSegment)?;
#[cfg(debug_assertions)]
- log::info!("max sig size: {max_size}");
+ info!("max sig size: {max_size}");
loop {
let instr_addr = addr + current_offset;
@@ -523,7 +525,7 @@ fn create_pattern_internal_binarysearch(
)?;
#[cfg(debug_assertions)]
- log::info!(
+ info!(
"{}: {}",
instr_string,
RustPattern(Cow::Borrowed(&instr_pattern))
@@ -555,7 +557,7 @@ fn create_pattern_internal_binarysearch(
let pat = &current_pattern[0..instr.0 as usize + instr.1];
#[cfg(debug_assertions)]
- log::info!("{}", RustPattern(Cow::Borrowed(&pat.to_vec())));
+ info!("{}", RustPattern(Cow::Borrowed(&pat.to_vec())));
match is_pattern_unique(&data, pat) {
false => Direction::Low(()),
@@ -573,7 +575,7 @@ fn create_pattern_internal_binarysearch(
current_pattern.pop();
}
- log::info!(
+ info!(
"binsearch created pattern in {}ms",
SystemTime::now().duration_since(time).unwrap().as_millis()
);
@@ -587,7 +589,7 @@ fn create_pattern_internal(
data: &[(u64, Vec<u8>)],
include_operands: bool,
) -> Result<OwnedPattern, SignatureError> {
- log::info!("creating pattern for address {:#04X}", addr);
+ info!("creating pattern for address {:#04X}", addr);
let time = SystemTime::now();
let mut formatter = NasmFormatter::new();
@@ -611,7 +613,7 @@ fn create_pattern_internal(
.ok_or(SignatureError::InvalidSegment)?;
#[cfg(debug_assertions)]
- log::info!("max sig size: {max_size}");
+ info!("max sig size: {max_size}");
while !pattern_unique {
let instr_addr = addr + current_offset;
@@ -657,7 +659,7 @@ fn create_pattern_internal(
)?;
#[cfg(debug_assertions)]
- log::info!(
+ info!(
"{}: {}",
instr_string,
RustPattern(Cow::Borrowed(&instr_pattern))
@@ -689,7 +691,7 @@ fn create_pattern_internal(
current_pattern.pop();
}
- log::info!(
+ info!(
"created pattern in {}ms",
SystemTime::now().duration_since(time).unwrap().as_millis()
);
@@ -709,14 +711,14 @@ fn create_pattern(bv: &BinaryView, addr: u64) -> Result<OwnedPattern, SignatureE
let _ = pattern
.as_ref()
- .map(|pat| log::info!("{}", RustPattern(Cow::Borrowed(&pat))));
+ .map(|pat| info!("{}", RustPattern(Cow::Borrowed(&pat))));
let _ = binsearch_pattern
.as_ref()
- .map(|pat| log::info!("{}", RustPattern(Cow::Borrowed(&pat))));
+ .map(|pat| info!("{}", RustPattern(Cow::Borrowed(&pat))));
if pattern.as_ref().unwrap() != binsearch_pattern.as_ref().unwrap() {
- log::error!("patterns dont match :(((");
+ error!("patterns dont match :(((");
}
}
@@ -727,7 +729,7 @@ fn create_pattern(bv: &BinaryView, addr: u64) -> Result<OwnedPattern, SignatureE
};
let pattern = if !include_operands && matches!(pattern, Err(SignatureError::NotUnique(_))) {
- log::warn!(
+ warn!(
"unable to find a unique pattern that didn't include operands. trying again with operands!"
);
if get_binary_search(bv) {
@@ -743,7 +745,7 @@ fn create_pattern(bv: &BinaryView, addr: u64) -> Result<OwnedPattern, SignatureE
.as_ref()
.is_ok_and(|pat| !is_pattern_unique(&data, &pat))
{
- log::error!("signature not unique, cannot proceed!");
+ error!("signature not unique, cannot proceed!");
return Err(SignatureError::NotUnique(
pattern.map_or(0u64, |pat| pat.len() as u64),
));
@@ -753,9 +755,9 @@ fn create_pattern(bv: &BinaryView, addr: u64) -> Result<OwnedPattern, SignatureE
}
fn emit_result(contents: String) {
- log::info!("{}", &contents);
+ info!("{}", &contents);
if let Err(e) = set_clipboard_contents(contents) {
- log::error!("unable to copy to clipboard: {}", e);
+ error!("unable to copy to clipboard: {}", e);
}
}
@@ -772,23 +774,25 @@ fn get_clipboard_contents() -> Result<String, Box<dyn std::error::Error>> {
}
fn get_maximum_signature_size(_bv: &BinaryView) -> u64 {
- Settings::new().get_integer("coolsigmaker.maximum_size")
+ Settings::new_with_id(DEFAULT_INSTANCE_ID).get_integer("coolsigmaker.maximum_size")
}
fn get_include_operands(_bv: &BinaryView) -> bool {
- Settings::new().get_bool("coolsigmaker.include_operands")
+ Settings::new_with_id(DEFAULT_INSTANCE_ID).get_bool("coolsigmaker.include_operands")
}
fn get_binary_search(_bv: &BinaryView) -> bool {
- Settings::new().get_bool("coolsigmaker.binary_search")
+ Settings::new_with_id(DEFAULT_INSTANCE_ID).get_bool("coolsigmaker.binary_search")
}
fn get_signature_type(_bv: &BinaryView) -> SignatureType {
- SignatureType::from_str(Settings::new().get_string("coolsigmaker.sig_type").as_str())
- .map_err(|_| {
- log::error!("invalid value for coolsigmaker.sig_type! falling back to default!")
- })
- .unwrap_or(SignatureType::IDATwo)
+ SignatureType::from_str(
+ Settings::new_with_id(DEFAULT_INSTANCE_ID)
+ .get_string("coolsigmaker.sig_type")
+ .as_str(),
+ )
+ .map_err(|_| error!("invalid value for coolsigmaker.sig_type! falling back to default!"))
+ .unwrap_or(SignatureType::IDATwo)
}
fn register_settings() {
@@ -852,7 +856,7 @@ fn register_settings() {
settings.register_setting_json(name, &properties);
}
- let settings = Settings::new();
+ let settings = Settings::new_with_id(DEFAULT_INSTANCE_ID);
settings.register_group("coolsigmaker", "CoolSigMaker");
@@ -937,7 +941,7 @@ impl AddressCommand for SigMakerCommand {
emit_result(format!("{}", pattern));
}
Err(e) => {
- log::error!("unable to create pattern! {e}");
+ error!("unable to create pattern! {e}");
}
}
}
@@ -951,7 +955,7 @@ impl AddressCommand for SigMakerCommand {
impl Command for SigFinderCommand {
fn action(&self, bv: &BinaryView) {
let Ok(sig) = get_clipboard_contents() else {
- log::error!("unable to get signature from clipboard!");
+ error!("unable to get signature from clipboard!");
return;
};
@@ -960,14 +964,14 @@ impl Command for SigFinderCommand {
let data = get_code(bv);
let Some(pattern) = OwnedPattern::from_signature(sig, get_signature_type(bv)) else {
- log::error!("failed to parse pattern.");
+ error!("failed to parse pattern.");
return;
};
find_patterns(&data, &pattern)
- .for_each(|occurrence| log::info!("found signature at {:#04X}", occurrence));
+ .for_each(|occurrence| info!("found signature at {:#04X}", occurrence));
- log::info!(
+ info!(
"scan finished in {}ms.",
SystemTime::now().duration_since(time).unwrap().as_millis()
);
@@ -980,15 +984,14 @@ impl Command for SigFinderCommand {
#[unsafe(no_mangle)]
pub extern "C" fn CorePluginInit() -> bool {
- let logger = binaryninja::logger::Logger::new("CoolSigMaker");
- logger.with_level(log::LevelFilter::Info).init();
+ binaryninja::tracing_init!();
// TODO: (maybe) if signature not found, maybe go back a few instructions and attempt to create a signature with an offset.
// TODO: introduce a setting for "dumb" searches, where we also search non-executable segments for uniqueness, incase the user doesn't want to check the segments before scanning them.
// TODO: make a fancy regex to distinguish signature types automagically (without accidental mismatches occurring)
- log::info!("binja_coolsigmaker by unknowntrojan loaded!");
- log::info!("say hello to the little ninja in your binja");
+ info!("binja_coolsigmaker by unknowntrojan loaded!");
+ info!("say hello to the little ninja in your binja");
#[cfg(debug_assertions)]
std::panic::set_hook(Box::new(|info| {
@@ -1002,7 +1005,7 @@ pub extern "C" fn CorePluginInit() -> bool {
// #[cfg(debug_assertions)]
let _ = std::fs::write("E:\\log.txt", &string);
- log::info!("{}", &string);
+ info!("{}", &string);
}));
register_settings();