Skip to content

Commit ad732c4

Browse files
committed
update
1 parent 36f9f30 commit ad732c4

2 files changed

Lines changed: 53 additions & 43 deletions

File tree

Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "binja_coolsigmaker"
33
version = "0.1.6"
44
authors = ["unknowntrojan"]
5-
edition = "2021"
5+
edition = "2024"
66

77
[lib]
88
crate-type = ["cdylib"]
@@ -12,18 +12,18 @@ crate-type = ["cdylib"]
1212
# binaryninja = { path = "../binaryninja-api/rust" }
1313

1414
log = "0.4.17"
15-
clipboard = "0.5.0"
16-
rayon = "1.7.0"
15+
clipboard = "0.5"
16+
rayon = "1.11"
1717
iced-x86 = "1.17.0"
18-
strum = { version = "0.26.1", features = ["derive"] }
19-
serde = { version = "1.0.152", features = ["std"] }
20-
serde_json = "1.0.93"
21-
thiserror = "1.0.43"
22-
coolfindpattern = "0.1.6"
23-
binary-search = "0.1.2"
18+
strum = { version = "0.27", features = ["derive"] }
19+
serde = { version = "1", features = ["std"] }
20+
serde_json = "1"
21+
thiserror = "2 "
22+
coolfindpattern = "0.1.7"
23+
binary-search = "0.1.3"
2424

2525

2626
# Dev
2727
# binaryninja = { git = "https://github.com/Vector35/binaryninja-api", branch = "dev" }
2828
# Stable
29-
binaryninja = { git = "https://github.com/Vector35/binaryninja-api", tag = "stable/5.1.8005" }
29+
binaryninja = { git = "https://github.com/Vector35/binaryninja-api", tag = "stable/5.1.8104" }

src/lib.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
//! along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
//!
2020
21-
#![feature(let_chains, iter_array_chunks)]
22-
use std::borrow::Cow;
21+
#![feature(iter_array_chunks)]
22+
use std::{borrow::Cow, sync::atomic::Ordering};
2323

2424
use std::fmt::Display;
2525
use std::ops::Range;
2626
use std::str::FromStr;
2727
use std::sync::atomic::AtomicUsize;
2828
use std::time::SystemTime;
2929

30-
use binary_search::{binary_search, Direction};
31-
use binaryninja::settings::Settings;
30+
use binary_search::{Direction, binary_search};
3231
use binaryninja::{
3332
architecture::Architecture,
3433
binary_view::{BinaryView, BinaryViewBase, BinaryViewExt},
3534
command::{self, AddressCommand, Command},
35+
settings::Settings,
3636
};
3737

3838
use clipboard::ClipboardProvider;
@@ -41,8 +41,7 @@ use iced_x86::{
4141
Code::{DeclareByte, DeclareDword, DeclareQword, DeclareWord},
4242
ConstantOffsets, FlowControl, Formatter, Instruction, NasmFormatter, OpKind,
4343
};
44-
use rayon::prelude::ParallelBridge;
45-
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
44+
use rayon::prelude::{IntoParallelRefIterator, ParallelBridge, ParallelIterator};
4645
use strum::{Display, EnumIter, EnumMessage, EnumString, IntoEnumIterator, VariantNames};
4746

4847
type OwnedPattern = Vec<Option<u8>>;
@@ -71,7 +70,9 @@ struct SigFinderCommand;
7170

7271
#[derive(thiserror::Error, Debug)]
7372
enum SignatureError {
74-
#[error("pattern is not unique even at size of {0} bytes! we either hit the limit or would have broken a function boundary.")]
73+
#[error(
74+
"pattern is not unique even at size of {0} bytes! we either hit the limit or would have broken a function boundary."
75+
)]
7576
NotUnique(u64),
7677
#[error("encountered an invalid instruction")]
7778
InvalidInstruction,
@@ -240,12 +241,14 @@ trait FromSignature {
240241
.map(|_| None)
241242
.collect::<Vec<Option<u8>>>();
242243

243-
let mut result = vec![u8::from_str_radix(&byte, 16)
244-
.map_err(|x| {
245-
log::error!("unable to parse pattern!");
246-
x
247-
})
248-
.ok()];
244+
let mut result = vec![
245+
u8::from_str_radix(&byte, 16)
246+
.map_err(|x| {
247+
log::error!("unable to parse pattern!");
248+
x
249+
})
250+
.ok(),
251+
];
249252

250253
result.extend(wildcards);
251254

@@ -285,16 +288,6 @@ trait FromSignature {
285288
}
286289
}
287290

288-
fn get_relocation_ranges(bv: &BinaryView) -> Vec<Range<u64>> {
289-
bv.relocation_ranges()
290-
.iter()
291-
.map(|range| Range {
292-
start: range.start,
293-
end: range.end,
294-
})
295-
.collect::<Vec<_>>()
296-
}
297-
298291
fn get_code(bv: &BinaryView) -> Vec<(u64, Vec<u8>)> {
299292
bv.segments()
300293
.into_iter()
@@ -446,10 +439,11 @@ fn is_pattern_unique(code_segments: &[(u64, Vec<u8>)], pattern: Pattern) -> bool
446439
let count = AtomicUsize::new(0);
447440

448441
iter.find_any(|_| {
449-
count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
450-
count.load(std::sync::atomic::Ordering::Relaxed) > 1
451-
})
452-
.is_none()
442+
count.fetch_add(1, Ordering::Relaxed);
443+
count.load(Ordering::Relaxed) > 1
444+
});
445+
446+
count.load(Ordering::Relaxed) == 1
453447
}
454448

455449
fn create_pattern_internal_binarysearch(
@@ -464,7 +458,7 @@ fn create_pattern_internal_binarysearch(
464458
let mut formatter = NasmFormatter::new();
465459
formatter.options_mut().set_rip_relative_addresses(true);
466460

467-
let relocations = get_relocation_ranges(bv);
461+
let relocations = bv.relocation_ranges();
468462

469463
let max_size = get_maximum_signature_size(bv);
470464

@@ -599,7 +593,7 @@ fn create_pattern_internal(
599593
let mut formatter = NasmFormatter::new();
600594
formatter.options_mut().set_rip_relative_addresses(true);
601595

602-
let relocations = get_relocation_ranges(bv);
596+
let relocations = bv.relocation_ranges();
603597

604598
let mut current_offset = 0u64;
605599
let mut current_buffer = vec![0u8; MAX_INSTRUCTION_LENGTH as usize];
@@ -733,7 +727,9 @@ fn create_pattern(bv: &BinaryView, addr: u64) -> Result<OwnedPattern, SignatureE
733727
};
734728

735729
let pattern = if !include_operands && matches!(pattern, Err(SignatureError::NotUnique(_))) {
736-
log::warn!("unable to find a unique pattern that didn't include operands. trying again with operands!");
730+
log::warn!(
731+
"unable to find a unique pattern that didn't include operands. trying again with operands!"
732+
);
737733
if get_binary_search(bv) {
738734
create_pattern_internal_binarysearch(bv, addr, &data, true)
739735
} else {
@@ -860,9 +856,23 @@ fn register_settings() {
860856

861857
settings.register_group("coolsigmaker", "CoolSigMaker");
862858

863-
register_setting::<bool>(&settings, "coolsigmaker.include_operands", "Include Operands", "Include immediate operands that aren't memory-relative or relocated when creating signatures. This results in smaller, but potentially more fragile, signatures. If no unique signature can be generated without operands, we fall back to including them.", "boolean", false);
859+
register_setting::<bool>(
860+
&settings,
861+
"coolsigmaker.include_operands",
862+
"Include Operands",
863+
"Include immediate operands that aren't memory-relative or relocated when creating signatures. This results in smaller, but potentially more fragile, signatures. If no unique signature can be generated without operands, we fall back to including them.",
864+
"boolean",
865+
false,
866+
);
864867

865-
register_setting::<bool>(&settings, "coolsigmaker.binary_search", "Use Binary Search", "Use a binary search to determine instruction uniqueness. For small binaries, this will be slower than the default, while for bigger binaries it might be faster. It starts scanning at half the maximum signature size. There is no heuristic implemented to automatically determine this yet.", "boolean", false);
868+
register_setting::<bool>(
869+
&settings,
870+
"coolsigmaker.binary_search",
871+
"Use Binary Search",
872+
"Use a binary search to determine instruction uniqueness. For small binaries, this will be slower than the default, while for bigger binaries it might be faster. It starts scanning at half the maximum signature size. There is no heuristic implemented to automatically determine this yet.",
873+
"boolean",
874+
false,
875+
);
866876

867877
register_setting::<u64>(
868878
&settings,
@@ -968,7 +978,7 @@ impl Command for SigFinderCommand {
968978
}
969979
}
970980

971-
#[no_mangle]
981+
#[unsafe(no_mangle)]
972982
pub extern "C" fn CorePluginInit() -> bool {
973983
let logger = binaryninja::logger::Logger::new("CoolSigMaker");
974984
logger.with_level(log::LevelFilter::Info).init();

0 commit comments

Comments
 (0)