-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExportSymbolsScript.java
More file actions
82 lines (65 loc) · 3.13 KB
/
ExportSymbolsScript.java
File metadata and controls
82 lines (65 loc) · 3.13 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
//Exports Symbols to a bash script which calls objcopy with --add-symbol to add symbol to a given elf binary.
//@category SimonTheCoder
//@menupath Tools.SimonScripts.ExportSymbols
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.mem.Memory;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.symbol.*;
import ghidra.util.exception.CancelledException;
public class ExportSymbolsScript extends GhidraScript {
/**
* @see ghidra.app.script.GhidraScript#run()
*/
@Override
public void run() {
try {
File directory = askDirectory("Directory", "Choose directory:");
monitor.setMessage("Finding symbols...");
SymbolTable st = state.getCurrentProgram().getSymbolTable();
SymbolIterator iter = st.getSymbolIterator(true);
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append("#!/bin/bash\n");
sb.append("# Create for U by SimonTheCoder.\n");
sb.append("# Feel free to modify this script.\n");
sb.append("# You may need to chmod +x on this script to run it.\n");
sb.append("# BACKUP YOUR BINARY BEFORE RUN.\n");
sb.append("# To add symbols to your bin, run:\n");
sb.append("# ./add_symbol.sh YOUR_BINARY_FILE.\n");
sb.append("\n");
sb.append("export OBJCOPY='objcopy'\n");
sb.append("echo Adding Symbols ...\n");
Memory mem = this.currentProgram.getMemory();
while (iter.hasNext() && !monitor.isCancelled()) {
Symbol sym = iter.next();
if (sym != null) {
MemoryBlock mb = mem.getBlock(sym.getAddress());
println(sym.getName() + "\t" + sym.getAddress()+"\t\t" + sym.isGlobal()+ "mb:" + mb.getName());
//sb.append(sym.getAddress() + " " + "A" + " " + sym.getName() + "\n");
if(sym.isGlobal()){
long offset_in_text = sym.getAddress().subtract(mb.getStart());
sb.append("$OBJCOPY --add-symbol ");
sb.append(sym.getName()+ "="+mb.getName()+":0x"+ Long.toHexString(offset_in_text));
sb.append(" $@\n");
count+=1;
}
}
}
println(count + " symbols added.");
sb.append("echo " + count + " symbols added.\n");
String path = directory.toString()+"/add_symbol.sh";
File file = new File(path);
String content = sb.toString();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
println(path + " generated.");
} catch (CancelledException | IOException e) {
println("Error!");
e.printStackTrace();
}
}
}