forked from mtxmiller/hotwheels-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghidra_decompile.java
More file actions
77 lines (70 loc) · 3.11 KB
/
Copy pathghidra_decompile.java
File metadata and controls
77 lines (70 loc) · 3.11 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
import ghidra.app.script.GhidraScript;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionManager;
import ghidra.util.task.ConsoleTaskMonitor;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ghidra_decompile extends GhidraScript {
private static final Set<String> EXPLICIT = new HashSet<>(Arrays.asList(
"AES_init_ctx", "AES_init_ctx_iv", "AES_ctx_set_iv", "AES_CTR_xcrypt_buffer",
"AES_ECB_encrypt", "AES_ECB_decrypt", "AES_CBC_encrypt_buffer", "AES_CBC_decrypt_buffer",
"HMAC_init", "HMAC_update", "HMAC_finish", "crc8_calc",
"sha256_init", "sha256_update", "sha256_final"));
private boolean want(String n) {
if (EXPLICIT.contains(n)) return true;
if (n.startsWith("mpid_")) return true;
if (n.contains("MpidLib")) return true;
if (n.startsWith("Java_com_mcpp_mattel")) return true;
if (n.startsWith("uECC_")) {
for (String k : new String[]{"shared_secret", "decompress", "make_key", "verify",
"sign", "valid_public_key", "compute_public_key"}) {
if (n.contains(k)) return true;
}
}
return false;
}
@Override
public void run() throws Exception {
String[] args = getScriptArgs();
String outPath = (args != null && args.length > 0) ? args[0] : "mpid_decompiled.c";
DecompInterface decomp = new DecompInterface();
decomp.openProgram(currentProgram);
FunctionManager fm = currentProgram.getFunctionManager();
List<Function> funcs = new ArrayList<>();
for (Function f : fm.getFunctions(true)) {
if (want(f.getName())) funcs.add(f);
}
funcs.sort(Comparator.comparingLong(f -> f.getEntryPoint().getOffset()));
PrintWriter w = new PrintWriter(new FileWriter(outPath));
w.printf("// Ghidra decompilation of MPID crypto glue%n// program: %s functions: %d%n%n",
currentProgram.getName(), funcs.size());
int ok = 0;
for (Function f : funcs) {
String c;
try {
DecompileResults res = decomp.decompileFunction(f, 120, new ConsoleTaskMonitor());
if (res != null && res.decompileCompleted()) {
c = res.getDecompiledFunction().getC();
ok++;
} else {
c = "// FAILED: " + f.getName() + " : " + (res != null ? res.getErrorMessage() : "null") + "\n";
}
} catch (Exception e) {
c = "// EXCEPTION " + f.getName() + ": " + e + "\n";
}
w.printf("%n// ===== %s @ %s =====%n", f.getName(), f.getEntryPoint());
w.print(c);
w.println();
}
w.close();
println("Decompiled " + ok + "/" + funcs.size() + " -> " + outPath);
}
}