Repository navigation
Expand file tree
/
Copy pathExportCfg.java
More file actions
141 lines (116 loc) · 4.57 KB
/
Copy pathExportCfg.java
File metadata and controls
141 lines (116 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
//A script for exporting the complete CFG to a file
//@author Michael Chesser
//@category custom
//@keybinding
//@menupath
//@toolbar
import ghidra.app.script.GhidraScript;
import ghidra.program.model.block.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.HashMap;
public class ExportCfg extends GhidraScript {
@Override
public void run() throws Exception {
if (this.currentProgram == null) {
return;
}
var outFile = this.askFile("File to export the CFG to", "Export");
try (var writer = new BufferedWriter(new FileWriter(outFile))) {
writer.write("{\n");
var listing = this.currentProgram.getListing();
// Get all functions
writer.write("\"functions\": {\n");
var first = true;
for (var function : listing.getFunctions(true)) {
var name = function.getName(true);
// Ensure that the name has no `"` characters in it.
name = name.replace("\"", "\\\"");
var address = function.getEntryPoint().getOffset();
if (first) {
first = false;
} else {
writer.write(",\n");
}
writer.write(String.format("\"%d\": \"%s\"", address, name));
}
writer.write("\n},\n"); // End of functions
// Keep track of all the edges we've seen, we allow one edge per (from, to) pair
// whichmay have one of serveral types.
var edges = new HashMap<Edge, String>();
// Iterate over all blocks and write the range of addresses to the file
writer.write("\"blocks\": [\n");
var model = new SimpleBlockModel(this.currentProgram, false);
var iter = model.getCodeBlocks(this.monitor);
while (iter.hasNext()) {
var b = iter.next();
var start = b.getMinAddress().getOffset();
var end = b.getMaxAddress().getOffset();
Long funcAddr = null;
var func = listing.getFunctionContaining(b.getMinAddress());
if (func != null) {
funcAddr = func.getEntryPoint().getOffset();
}
writer.write(String.format("{\"start\": %d, \"end\": %d, \"func\": %d}", start, end, funcAddr));
// Iterate over all outgoing edges in the block
var dests = b.getDestinations(monitor);
while (dests.hasNext()) {
var blockRef = dests.next();
var type = blockRef.getFlowType().toString();
var from = blockRef.getSourceAddress().getOffset();
var to = blockRef.getDestinationAddress().getOffset();
edges.put(new Edge(from, to), type);
}
if (iter.hasNext()) {
writer.write(",\n");
}
}
writer.write("\n]\n"); // End of block list
// Iterate over all edges and write them to the file
writer.write(",\n\"edges\": [\n");
var firstEdge = true;
for (var edge : edges.keySet()) {
if (firstEdge) {
firstEdge = false;
} else {
writer.write(",\n");
}
writer.write(String.format("{\"from\": %d, \"to\": %d, \"kind\": \"%s\"}", edge.from,
edge.to, edges.get(edge)));
}
writer.write("\n]\n"); // End of edge list
writer.write("}"); // End of JSON
}
}
static class Edge {
public final long from;
public final long to;
public Edge(long from, long to) {
this.from = from;
this.to = to;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (from ^ (from >>> 32));
result = prime * result + (int) (to ^ (to >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Edge other = (Edge) obj;
if (from != other.from)
return false;
if (to != other.to)
return false;
return true;
}
}
}