-
Notifications
You must be signed in to change notification settings - Fork 12
/
XMLParser.java
303 lines (246 loc) · 8.96 KB
/
XMLParser.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//Parse XML file which contains genes, and print the output in a table. Implemented in Java.
//@author Nicole
//@category
//@keybinding
//@menupath
//@toolbar
import java.io.File;
import ghidra.app.script.GhidraScript;
import ghidra.app.tablechooser.AddressableRowObject;
import ghidra.app.tablechooser.StringColumnDisplay;
import ghidra.app.tablechooser.TableChooserDialog;
import ghidra.app.tablechooser.TableChooserExecutor;
import ghidra.program.model.address.Address;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.nio.file.Paths;
public class XMLParser extends GhidraScript {
@Override
protected void run() throws Exception {
String file_directory = System.getenv("intezer_analyze_ghidra_export_file_path");
if (file_directory == null) {
file_directory = System.getProperty("user.dir");
}
String filePath = Paths.get(file_directory, "items.xml").toString();
parse(filePath);
}
TableChooserDialog tableDialog;
private void parse(String path) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
IntezerAnalyzeHandler handler = new IntezerAnalyzeHandler();
saxParser.parse(new File(path), handler);
List<Row> rowList = handler.getRowList();
fillTable(rowList);
} catch (ParserConfigurationException | SAXException | IOException e) {
println("An exception has ocurred while getting the genes");
println(e.toString());
e.printStackTrace();
}
}
private void fillTable(List<Row> rowList) {
TableChooserExecutor executor = null;
tableDialog = createTableChooserDialog("Intezer Analyze Genes", executor);
configureTableColumns(tableDialog);
try {
for (Row row : rowList) {
tableDialog.add(row);
}
} catch (Exception e) {
println("An exception has ocurred while trying to display the gene table");
}
tableDialog.show();
}
public class IntezerAnalyzeHandler extends DefaultHandler {
private List<Row> rowList = new ArrayList<>();
private Row row = new Row();
private StringBuilder data = null;
public List<Row> getRowList() {
return rowList;
}
private boolean isAddr = false;
private boolean isName = false;
private boolean isSoftType = false;
private boolean isCodeReuse = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "gene":
break;
case "function_address":
isAddr = true;
break;
case "function_name":
isName = true;
break;
case "software_type":
isSoftType = true;
break;
case "code_reuse":
isCodeReuse = true;
break;
}
data = new StringBuilder();
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "gene":
row = new Row();
break;
case "function_address":
row.setFunctionAddr(data.toString());
isAddr = false;
break;
case "function_name":
row.setFunctionName(data.toString());
isName = false;
break;
case "software_type":
row.setSoftwareType(data.toString());
isSoftType = false;
break;
case "code_reuse":
row.setCodeReuse(data.toString());
isCodeReuse = false;
break;
}
if (qName.equalsIgnoreCase("gene")) {
rowList.add(row);
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
data.append(new String(ch, start, length));
}
}
class Row implements AddressableRowObject {
private Address functionAddressAddr;
private String functionAddressHex;
private String functionName;
private String softwareType;
private String codeReuse;
@Override
public Address getAddress() {
return functionAddressAddr;
}
public String getFunctionAddr() {
return functionAddressHex;
}
public String getFunctionName() {
return functionName;
}
public String getSoftwareType() {
return softwareType;
}
public String getCodeReuse() {
return codeReuse;
}
public void setFunctionAddr(String functionAddressHex) {
this.functionAddressHex = functionAddressHex;
Address addr = currentProgram.getAddressFactory().getAddress(functionAddressHex);
this.functionAddressAddr = addr;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public void setSoftwareType(String softwareType) {
this.softwareType = softwareType;
}
public void setCodeReuse(String codeReuse) {
this.codeReuse = codeReuse;
}
@Override
public String toString() {
return "Gene:: function_address=" + this.functionAddressHex + " Name=" + this.functionName + " sof. type=" + this.softwareType +
" code reuse=" + this.codeReuse;
}
}
private TableChooserExecutor createTableExecutor() {
TableChooserExecutor executor = new TableChooserExecutor() {
@Override
public String getButtonName() {
return "Hit Me";
}
@Override
public boolean execute(AddressableRowObject rowObject) {
return true;
}
};
return executor;
}
private void configureTableColumns(TableChooserDialog tableDialog) {
StringColumnDisplay functionAddress = new StringColumnDisplay() {
@Override
public String getColumnValue(AddressableRowObject rowObject) {
Row table = (Row) rowObject;
try {
return (table.getAddress()).toString();
} catch (NullPointerException e) {
return "";
}
}
@Override
public String getColumnName() {
return "Function Address";
}
};
StringColumnDisplay functionName = new StringColumnDisplay() {
@Override
public String getColumnValue(AddressableRowObject rowObject) {
Row table = (Row) rowObject;
try {
return table.getFunctionName();
} catch (NullPointerException e) {
return "";
}
}
@Override
public String getColumnName() {
return "Function Name";
}
};
StringColumnDisplay softwareType = new StringColumnDisplay() {
@Override
public String getColumnValue(AddressableRowObject rowObject) {
Row table = (Row) rowObject;
try {
return table.getSoftwareType();
} catch (NullPointerException e) {
return "";
}
}
@Override
public String getColumnName() {
return "Software Type";
}
};
StringColumnDisplay codeReuse = new StringColumnDisplay() {
@Override
public String getColumnValue(AddressableRowObject rowObject) {
Row table = (Row) rowObject;
try {
return table.getCodeReuse();
} catch (NullPointerException e) {
return "";
}
}
@Override
public String getColumnName() {
return "Code Reuse";
}
};
tableDialog.addCustomColumn(functionAddress);
tableDialog.addCustomColumn(functionName);
tableDialog.addCustomColumn(softwareType);
tableDialog.addCustomColumn(codeReuse);
}
}