-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathflowgraph.rs
More file actions
142 lines (121 loc) · 4.57 KB
/
flowgraph.rs
File metadata and controls
142 lines (121 loc) · 4.57 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
use binaryninja::flowgraph::edge::EdgeStyle;
use binaryninja::flowgraph::FlowGraphNode;
use binaryninja::function::FunctionViewType;
use binaryninja::interaction::form::Form;
use binaryninja::interaction::handler::{
register_interaction_handler, InteractionHandler, InteractionHandlerTask,
};
use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet, MessageBoxIcon};
use binaryninja::{
architecture::BranchType,
binary_view::{BinaryView, BinaryViewExt},
disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenKind},
flowgraph::{EdgePenStyle, FlowGraph, ThemeColor},
};
use std::time::Duration;
pub struct GraphPrinter;
impl GraphPrinter {
pub fn print_graph(&self, graph: &FlowGraph) {
println!("Printing flow graph:");
for node in &graph.nodes() {
// Print all disassembly lines in the node
println!("Node @ {:?}:", node.position());
println!("------------------");
println!("Disassembly lines:");
for line in &node.lines() {
println!(" {}", line);
}
// Print outgoing edges
println!("Outgoing edges:");
for edge in &node.outgoing_edges() {
println!(" {:?} => {:?}", edge.branch_type, edge.target.position());
}
println!("------------------");
}
}
}
impl InteractionHandler for GraphPrinter {
fn show_message_box(
&mut self,
_title: &str,
_text: &str,
_buttons: MessageBoxButtonSet,
_icon: MessageBoxIcon,
) -> MessageBoxButtonResult {
MessageBoxButtonResult::CancelButton
}
fn open_url(&mut self, _url: &str) -> bool {
false
}
fn run_progress_dialog(
&mut self,
_title: &str,
_can_cancel: bool,
_task: &InteractionHandlerTask,
) -> bool {
false
}
fn show_plain_text_report(&mut self, _view: Option<&BinaryView>, title: &str, contents: &str) {
println!("Plain text report");
println!("Title: {}", title);
println!("Contents: {}", contents);
}
fn show_graph_report(&mut self, _view: Option<&BinaryView>, title: &str, graph: &FlowGraph) {
println!("Graph report");
println!("Title: {}", title);
self.print_graph(graph);
}
fn get_form_input(&mut self, _form: &mut Form) -> bool {
false
}
}
fn test_graph() {
let graph = FlowGraph::new();
let disassembly_lines_a = vec![DisassemblyTextLine::new(vec![
InstructionTextToken::new("Li", InstructionTextTokenKind::Text),
InstructionTextToken::new("ne", InstructionTextTokenKind::Text),
InstructionTextToken::new(" 1", InstructionTextTokenKind::Text),
])];
let node_a = FlowGraphNode::new(&graph);
node_a.set_lines(disassembly_lines_a);
node_a.set_position(1337, 7331);
let node_b = FlowGraphNode::new(&graph);
node_b.set_position(100, 200);
let disassembly_lines_b = vec![DisassemblyTextLine::new(vec![
InstructionTextToken::new("Li", InstructionTextTokenKind::Text),
InstructionTextToken::new("ne", InstructionTextTokenKind::Text),
InstructionTextToken::new(" 2", InstructionTextTokenKind::Text),
])];
node_b.set_lines(disassembly_lines_b);
graph.append(&node_a);
graph.append(&node_b);
let edge = EdgeStyle::new(EdgePenStyle::DashDotDotLine, 2, ThemeColor::AddressColor);
node_a.add_outgoing_edge(BranchType::UserDefinedBranch, &node_b, edge);
node_b.add_outgoing_edge(
BranchType::UnconditionalBranch,
&node_a,
EdgeStyle::default(),
);
graph.show("Rust Example Graph");
}
fn main() {
println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
println!("Loading binary...");
let bv = headless_session
.load("/bin/cat")
.expect("Couldn't open `/bin/cat`");
// Register the interaction handler so we can see the graph report headlessly.
register_interaction_handler(GraphPrinter);
test_graph();
for func in bv.functions().iter().take(5) {
let graph = func.create_graph(FunctionViewType::MediumLevelIL, None);
// It is important to call this, otherwise no nodes will be placed.
graph.request_layout_and_wait(Duration::from_secs(5));
let func_name = func.symbol().short_name();
let title = func_name.to_string_lossy();
bv.show_graph_report(&title, &graph);
}
}