-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_binary_comparison.rs
More file actions
154 lines (129 loc) · 5.58 KB
/
test_binary_comparison.rs
File metadata and controls
154 lines (129 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env rust-script
//! Test binary comparison functionality with real Binary Ninja servers
//!
//! ```cargo
//! [dependencies]
//! smart-diff-binary-ninja-client = { path = "../crates/binary-ninja-client" }
//! smart-diff-engine = { path = "../crates/diff-engine" }
//! tokio = { version = "1", features = ["full"] }
//! anyhow = "1"
//! ```
use anyhow::Result;
use smart_diff_binary_ninja_client::BinaryNinjaClient;
use smart_diff_engine::{BinaryFunctionInfo, BinaryFunctionMatcher};
#[tokio::main]
async fn main() -> Result<()> {
println!("=== Binary Ninja Comparison Test ===\n");
// Create client
let client = BinaryNinjaClient::new();
// Step 1: Discover servers
println!("Step 1: Discovering Binary Ninja servers...");
let servers = client.discover_servers().await?;
if servers.is_empty() {
println!("✗ No Binary Ninja servers found!");
println!(" Make sure Binary Ninja is running with binaries loaded");
println!(" Use 'MCP Server > Start Server for This Binary' in Binary Ninja");
return Ok(());
}
println!("✓ Found {} server(s):", servers.len());
for server in &servers {
println!(" - {}: {} (port {})", server.binary_id, server.filename, server.port);
}
println!();
if servers.len() < 2 {
println!("⚠ Need at least 2 binaries to test comparison");
println!(" Load another binary in Binary Ninja and start its server");
return Ok(());
}
// Step 2: Get function lists from both binaries
let binary_a = &servers[0];
let binary_b = &servers[1];
println!("Step 2: Comparing binaries:");
println!(" Binary A: {} ({})", binary_a.filename, binary_a.binary_id);
println!(" Binary B: {} ({})", binary_b.filename, binary_b.binary_id);
println!();
println!("Step 3: Fetching function lists...");
let functions_a_raw = client.list_functions(&binary_a.binary_id).await?;
let functions_b_raw = client.list_functions(&binary_b.binary_id).await?;
println!(" Binary A: {} functions", functions_a_raw.len());
println!(" Binary B: {} functions", functions_b_raw.len());
println!();
// Convert to BinaryFunctionInfo
let functions_a: Vec<BinaryFunctionInfo> = functions_a_raw
.into_iter()
.map(|f| BinaryFunctionInfo::new(f.name, f.address))
.collect();
let functions_b: Vec<BinaryFunctionInfo> = functions_b_raw
.into_iter()
.map(|f| BinaryFunctionInfo::new(f.name, f.address))
.collect();
// Step 4: Perform matching
println!("Step 4: Matching functions...");
let matcher = BinaryFunctionMatcher::new();
let matches = matcher.match_functions(&functions_a, &functions_b)?;
println!("✓ Found {} matches", matches.len());
println!();
// Step 5: Show statistics
println!("Step 5: Match Statistics:");
let exact_matches = matches.iter().filter(|m| m.name_similarity == 1.0).count();
let fuzzy_matches = matches.iter().filter(|m| m.name_similarity < 1.0).count();
let avg_similarity: f64 = matches.iter().map(|m| m.similarity).sum::<f64>() / matches.len() as f64;
println!(" Exact name matches: {}", exact_matches);
println!(" Fuzzy name matches: {}", fuzzy_matches);
println!(" Average similarity: {:.1}%", avg_similarity * 100.0);
println!();
// Step 6: Show top 10 most changed functions
println!("Step 6: Top 10 Most Changed Functions:");
let mut sorted_matches = matches.clone();
sorted_matches.sort_by(|a, b| a.similarity.partial_cmp(&b.similarity).unwrap());
for (i, m) in sorted_matches.iter().take(10).enumerate() {
println!(
" {}. {} <-> {} (similarity: {:.1}%, type: {:?})",
i + 1,
m.function_a.name,
m.function_b.name,
m.similarity * 100.0,
m.match_type
);
}
println!();
// Step 7: Test decompilation for first match
if let Some(first_match) = sorted_matches.first() {
println!("Step 7: Testing decompilation for most changed function:");
println!(" Function: {} <-> {}", first_match.function_a.name, first_match.function_b.name);
println!();
println!(" Decompiling from Binary A...");
match client.decompile_function(&binary_a.binary_id, &first_match.function_a.name).await {
Ok(code) => {
println!(" ✓ Decompiled ({} bytes)", code.len());
let lines: Vec<&str> = code.lines().take(10).collect();
for line in lines {
println!(" {}", line);
}
if code.lines().count() > 10 {
println!(" ... ({} more lines)", code.lines().count() - 10);
}
}
Err(e) => println!(" ✗ Failed: {}", e),
}
println!();
println!(" Decompiling from Binary B...");
match client.decompile_function(&binary_b.binary_id, &first_match.function_b.name).await {
Ok(code) => {
println!(" ✓ Decompiled ({} bytes)", code.len());
let lines: Vec<&str> = code.lines().take(10).collect();
for line in lines {
println!(" {}", line);
}
if code.lines().count() > 10 {
println!(" ... ({} more lines)", code.lines().count() - 10);
}
}
Err(e) => println!(" ✗ Failed: {}", e),
}
}
println!();
println!("=== Test Complete ===");
println!("✓ Binary comparison functionality is working!");
Ok(())
}