forked from KeenSecurityLab/BinAbsInspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinAbsInspector.java
162 lines (154 loc) · 6 KB
/
BinAbsInspector.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
//
//@author Tencent KeenLab
//@category Analysis
//@keybinding
//@menupath Analysis.BinAbsInspector
//@toolbar keenlogo.gif
import com.bai.checkers.CheckerManager;
import com.bai.env.funcs.FunctionModelManager;
import com.bai.util.Config.HeadlessParser;
import generic.continues.RethrowContinuesFactory;
import ghidra.app.util.bin.MemoryByteProvider;
import ghidra.app.util.bin.format.elf.ElfException;
import ghidra.app.util.bin.format.elf.ElfHeader;
import ghidra.program.model.address.Address;
import ghidra.program.model.lang.Language;
import ghidra.program.model.listing.Program;
import com.bai.util.CWEReport;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Function;
import com.bai.solver.InterSolver;
import com.bai.util.Architecture;
import com.bai.util.Config;
import com.bai.util.GlobalState;
import com.bai.util.Logging;
import com.bai.util.Utils;
import java.awt.Color;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
public class BinAbsInspector extends GhidraScript {
protected boolean prepareProgram() {
GlobalState.currentProgram = this.currentProgram;
GlobalState.flatAPI = this;
Language language = GlobalState.currentProgram.getLanguage();
return language != null;
}
protected boolean analyzeFromMain() {
List<Function> functions = GlobalState.currentProgram.getListing().getGlobalFunctions("main");
if (functions == null || functions.size() == 0) {
return false;
}
Function entryFunction = functions.get(0);
if (entryFunction == null) {
Logging.error("Cannot find entry function");
return false;
}
Logging.info("Running solver on \"" + entryFunction + "()\" function");
InterSolver solver = new InterSolver(entryFunction, true);
solver.run();
return true;
}
protected boolean analyzeFromAddress(Address entryAddress) {
Function entryFunction = GlobalState.flatAPI.getFunctionAt(entryAddress);
if (entryAddress == null) {
Logging.error("Could not find entry function at " + entryAddress);
return false;
}
Logging.info("Running solver on \"" + entryFunction + "()\" function");
InterSolver solver = new InterSolver(entryFunction, false);
solver.run();
return true;
}
/**
* Start analysis with following steps:
* 1. Start from specific address if user provided, the address must be the entrypoint of a function.
* 2. Start from "main" function if step 1 fails.
* 3. Start from "e_entry" address from ELF header if step 2 fails.
* @return
*/
protected boolean analyze() {
Program program = GlobalState.currentProgram;
if (program == null) {
Logging.error("Import program error.");
return false;
}
String entryAddressStr = GlobalState.config.getEntryAddress();
if (entryAddressStr != null) {
Address entryAddress = GlobalState.flatAPI.toAddr(entryAddressStr);
return analyzeFromAddress(entryAddress);
} else {
GlobalState.eEntryFunction = Utils.getEntryFunction();
if (GlobalState.eEntryFunction == null) {
Logging.error("Cannot find entry function, maybe unsupported file format or corrupted header.");
return false;
}
if (!analyzeFromMain()) {
Logging.info("Start from entrypoint");
Logging.info("Running solver on \"" + GlobalState.eEntryFunction + "()\" function");
InterSolver solver = new InterSolver(GlobalState.eEntryFunction, false);
solver.run();
return true;
}
}
return true;
}
private void guiProcessResult() {
if (!GlobalState.config.isGUI()) {
return;
}
String msg = "Analysis finish!\n Found " + Logging.getCWEReports().size() + " CWE Warning.";
GlobalState.ghidraScript.popup(msg);
Logging.info(msg);
for (CWEReport report : Logging.getCWEReports().keySet()) {
GlobalState.ghidraScript.setBackgroundColor(report.getAddress(), Color.RED);
GlobalState.ghidraScript.setEOLComment(report.getAddress(), report.toString());
Logging.warn(report.toString());
}
}
@Override
public void run() throws Exception {
GlobalState.config = new Config();
if (isRunningHeadless()) {
String allArgString = StringUtils.join(getScriptArgs()).strip();
GlobalState.config = HeadlessParser.parseConfig(allArgString);
} else {
GlobalState.ghidraScript = this;
GlobalState.config = new Config();
GlobalState.config.setGUI(true);
ConfigDialog dialog = new ConfigDialog(GlobalState.config);
dialog.showDialog();
if (!dialog.isSuccess()) {
return;
}
}
if (!Logging.init()) {
return;
}
FunctionModelManager.initAll();
if (GlobalState.config.isEnableZ3() && !Utils.checkZ3Installation()) {
return;
}
Logging.info("Preparing the program");
if (!prepareProgram()) {
Logging.error("Failed to prepare the program");
return;
}
if (isRunningHeadless()) {
if (!Utils.registerExternalFunctionsConfig(GlobalState.currentProgram, GlobalState.config)) {
return;
}
} else {
Utils.loadCustomExternalFunctionFromLabelHistory(GlobalState.currentProgram);
}
GlobalState.arch = new Architecture(GlobalState.currentProgram);
boolean success = analyze();
if (!success) {
Logging.error("Failed to analyze the program: no entrypoint.");
return;
}
Logging.info("Running checkers");
CheckerManager.runCheckers(GlobalState.config);
guiProcessResult();
GlobalState.reset();
}
}