forked from schlafwandler/ghidra_ExportToX64dbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportToX64dbg.py
246 lines (194 loc) · 8.1 KB
/
ExportToX64dbg.py
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
# Export function names and comments to x64dbg
#@author schlafwandler (Updated by charleslomboni)
#@category Examples
#@keybinding
#@menupath
#@toolbar
import json
from ghidra.app.decompiler import DecompInterface
from ghidra.app.decompiler import ClangStatement
def fixdata(obj_list):
"""
transforms numbers to hexstring and module names to lowercase
"""
for entry in obj_list:
monitor.checkCanceled() # throws exception if canceled
# fix addresses
if "address" in entry:
entry["address"] = hex(entry["address"])
if "start" in entry:
entry["start"] = hex(entry["start"])#[:-1] # strip trailing 'L'
if "end" in entry:
entry["end"] = hex(entry["end"])#[:-1] # strip trailing 'L'
# fix module name
if "module" in entry:
entry["module"] = entry["module"].lower()
def export_json(filename, data, lowercase_modulename=True):
monitor.setMessage("Exporting data")
# data transformation
for category in data.values():
fixdata(category)
with open(filename,"w") as f:
json.dump(data,f,sort_keys=True,indent=4)
def main():
output_filename = str(askFile("Select output file name","Save"))
if "64" in str(currentProgram.getLanguage()):
suffix = ".dd64"
else:
suffix = ".dd32"
if not output_filename.endswith(suffix):
output_filename = output_filename + suffix
functions,function_labels,prototype_comments = get_functions_labels()
data_labels = get_data_labels()
bookmarks,bookmark_comments = get_bookmarks()
statement_comments = get_clang_statements()
comments = get_comments()
data = dict()
data["labels"] = function_labels + data_labels
data["comments"] = prototype_comments + statement_comments + bookmark_comments + comments
data["functions"] = functions
data["bookmarks"] = bookmarks
export_json(output_filename,data)
def get_functions_labels():
monitor.setMessage("Collecting function labels and prototypes")
functions = list()
labels = list()
prototype_comments = list()
module_name = currentProgram.getName()
imagebase = currentProgram.getImageBase().getOffset()
fm = currentProgram.getFunctionManager()
for f in fm.getFunctionsNoStubs(1):
monitor.checkCanceled() # throws exception if canceled
if f.isExternal() or f.isThunk():
next
function_entry = dict()
function_entry["module"] = module_name
function_entry["manual"] = False
function_entry["icount"] = 0 # FIXME
function_entry["start"] = f.getBody().getMinAddress().getOffset() - imagebase
function_entry["end"] = f.getBody().getMaxAddress().getOffset() - imagebase
functions.append(function_entry)
# label
label_entry = dict()
label_entry["module"] = module_name
label_entry["manual"] = False
label_entry["address"] = f.getEntryPoint().getOffset() - imagebase
label_entry["text"] = f.getName()
labels.append(label_entry)
# function prototype comment
comment_entry = dict()
comment_entry["module"] = module_name
comment_entry["manual"] = False
comment_entry["address"] = f.getEntryPoint().getOffset() - imagebase
comment_entry["text"] = f.getPrototypeString(1,0)
prototype_comments.append(comment_entry)
return functions,labels,prototype_comments
def get_data_labels():
monitor.setMessage("Collecting data labels")
labels = list()
module_name = currentProgram.getName()
imagebase = currentProgram.getImageBase().getOffset()
listing = currentProgram.getListing()
for d in listing.getData(1):
monitor.checkCanceled() # throws exception if canceled
label = d.getLabel()
path = d.getPathName()
if label:
text = label
else:
text = None
if text:
# function label
label_entry = dict()
label_entry["module"] = module_name
label_entry["manual"] = False
label_entry["address"] = d.getAddress().getOffset() - imagebase
label_entry["text"] = text
labels.append(label_entry)
return labels
def get_bookmarks():
monitor.setMessage("Collecting bookmarks")
bookmarks = list()
bookmark_comments = list()
module_name = currentProgram.getName()
imagebase = currentProgram.getImageBase().getOffset()
bm = currentProgram.getBookmarkManager()
for b in bm.getBookmarksIterator():
monitor.checkCanceled() # throws exception if canceled
if not b.getTypeString() == "Note":
continue
bookmark_entry = dict()
bookmark_entry["module"] = module_name
bookmark_entry["address"] = b.getAddress().getOffset() - imagebase
bookmark_entry["manual"] = False
bookmarks.append(bookmark_entry)
bookmark_comment_entry = dict()
bookmark_comment_entry["module"] = module_name
bookmark_comment_entry["address"] = b.getAddress().getOffset() - imagebase
bookmark_comment_entry["manual"] = False
bookmark_comment_entry["text"] = b.getCategory() + ": " + b.getComment()
bookmark_comments.append(bookmark_comment_entry)
comment_addr = (getInstructionAfter(getInstructionAfter(item.getFromAddress()))).getAddress()
listing = currentProgram.getListing()
codeUnit = listing.getCodeUnitAt(comment_addr)
codeUnit.setComment(codeUnit.EOL_COMMENT, '[*] ' + decoded_str)
return bookmarks, bookmark_comments
def get_clang_statements():
monitor.setMessage("Collecting C statements")
module_name = currentProgram.getName()
imagebase = currentProgram.getImageBase().getOffset()
decomp = DecompInterface()
decomp.openProgram(currentProgram)
def token_walker(node,list):
if type(node) == ClangStatement:
list.append(node)
else:
for i in range(node.numChildren()):
token_walker(node.Child(i),list)
statement_nodes = list()
function_manager = currentProgram.getFunctionManager()
for f in function_manager.getFunctionsNoStubs(1):
monitor.checkCanceled() # throws exception if canceled
decres = decomp.decompileFunction(f,1000,monitor)
token_walker(decres.getCCodeMarkup(),statement_nodes)
statements = list()
for node in statement_nodes:
if node.getMinAddress() is not None:
statement_entry = dict()
statement_entry["module"] = module_name
statement_entry["address"] = node.getMinAddress().getOffset() - imagebase
statement_entry["manual"] = False
statement_entry["text"] = node.toString()
statements.append(statement_entry)
return statements
def get_comments():
monitor.setMessage("Collecting comments")
from ghidra.app.util import DisplayableEol
module_name = currentProgram.getName()
imagebase = currentProgram.getImageBase().getOffset()
comments = list()
fm = currentProgram.getFunctionManager()
listing = currentProgram.getListing()
funcs = fm.getFunctions(True) # True means iterate forward
comment_types = {
0: 'EOL',
1: 'PRE',
2: 'POST',
3: 'PLATE',
4: 'REPEATABLE',
}
for func in funcs:
addrSet = func.getBody()
codeUnits = listing.getCodeUnits(addrSet, True)
for codeUnit in codeUnits:
for i, comment_type in comment_types.items():
comment = codeUnit.getComment(i)
if comment is not None:
comment_entry = dict()
comment_entry["module"] = module_name
comment_entry["address"] = int(str(codeUnit.getAddress()), 16) - imagebase
comment_entry["manual"] = False
comment_entry["text"] = comment_type + ": " + comment
comments.append(comment_entry)
return comments
main()