-
Notifications
You must be signed in to change notification settings - Fork 71
/
ghida.py
1060 lines (851 loc) · 33.1 KB
/
ghida.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##############################################################################
# #
# GhIDA: Ghidra decompiler for IDA Pro #
# #
# Copyright 2019 Andrea Marcelli, Cisco Talos #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
##############################################################################
try:
from pygments.lexers import CLexer
from pygments.token import Token
except Exception:
# Missing library is managed at the plugin entry
pass
import ida_kernwin
import idaapi
import idautils
import idc
import ghida_plugin as gl
DECOMP_VIEW = None
GHIDA_CONF = None
DECOMPILED_CACHE = None
COMMENTS_CACHE = None
# ------------------------------------------------------------
# IDA MENU - HANDLERS
# ------------------------------------------------------------
class ShowSettingsHandler(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Say hello when invoked.
def activate(self, ctx):
display_configuration_form()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class ShowDecompWindowHandler(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Say hello when invoked.
def activate(self, ctx):
if DECOMP_VIEW:
DECOMP_VIEW.Show()
else:
# print("GhIDA:: [DEBUG] DECOMP_VIEW non existing")
pass
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
# ------------------------------------------------------------
# DECOMPILED VIEW -- POP-UP HANDLERS
# ------------------------------------------------------------
class GoToCustViewerHandler(idaapi.action_handler_t):
def __init__(self, view):
idaapi.action_handler_t.__init__(self)
self.view = view
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] GoToCustViewerHandler HELLO")
goto()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class AddCommentCustViewerHandler(idaapi.action_handler_t):
def __init__(self, view):
idaapi.action_handler_t.__init__(self)
self.view = view
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] AddCommentCustViewerHandler HELLO")
DECOMP_VIEW.add_comment()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class RenameCustViewerHandler(idaapi.action_handler_t):
def __init__(self, view):
idaapi.action_handler_t.__init__(self)
self.view = view
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] RenameCustViewerHandler HELLO")
DECOMP_VIEW.rename_symbol()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
# ------------------------------------------------------------
# ScreenEAHook
# ------------------------------------------------------------
class ScreenEAHook(ida_kernwin.View_Hooks):
def __init__(self):
ida_kernwin.View_Hooks.__init__(self)
print("GhIDA [DEBUG] ScreenEAHook initialized")
def view_loc_changed(self, widget, curloc, prevloc):
"""
view_loc_changed is called each time the user clicks
somwhere. This is used to synchronize the IDA DISASM
view with the IDA DECOM view. The synchronization is
active only when the decompile view has been created
and the synch option has been selected in the pop-up
menu.
"""
# Check if the selected address has changed
# if curloc.plce.toea() != prevloc.plce.toea():
# return
# Hooking the IDA DISASM view only
if idaapi.get_widget_type(widget) != idaapi.BWN_DISASM:
return
# If the DECOMP view has already been created.
if DECOMP_VIEW:
# Get the new address
ca = curloc.plce.toea()
ea = gl.convert_address(ca)
# This is a valid function address
if ea:
# The synch is active
if GHIDA_CONF.disasm_tracker:
# The address in DECOMP view is different
if ea != DECOMP_VIEW.ea:
# Update DECOMP view
DECOMP_VIEW.switch_to_address(ea)
# Update the selection
return gl.highlight_symbol_in_DECOMP()
# This is not a valid function address
if not ea:
# If the synch is active
if GHIDA_CONF.disasm_tracker:
DECOMP_VIEW.clear(msg="[!] Function not found.",
do_show=False)
return
# ------------------------------------------------------------
# GOTO utils
# ------------------------------------------------------------
def goto(shift=False):
# print("GhIDA:: [DEBUG] goto called")
symbol = None
ret = ida_kernwin.get_highlight(ida_kernwin.get_current_viewer())
if ret and ret[1]:
symbol = ret[0]
if not symbol:
return False
address = gl.get_address_for_symbol(symbol)
if not address:
return False
print("OnDblClick, shift=%d, selection:%s, address:%s" %
(shift, symbol, address))
# Update IDA DISASM view
idaapi.jumpto(address)
# Update IDA DECOMP view
ea = gl.convert_address(address)
# print("GhIDA:: [DEBUG] update view to %s" % ea)
DECOMP_VIEW.switch_to_address(ea)
return True
# ------------------------------------------------------------
# SIMPLECUSTVIEWER FOR THE DECOMPILED RESULT
# ------------------------------------------------------------
# Check this example: https://github.com/nologic/idaref/blob/master/idaref.py
class DecompiledViewer_t(idaapi.simplecustviewer_t):
def color_line(self, line):
"""
"""
lexer = CLexer()
tokens = list(lexer.get_tokens(line))
new_line = ""
for t in tokens:
ttype = t[0]
ttext = str(t[1])
if ttype == Token.Text:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_INSN)
elif ttype == Token.Text.Whitespace:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_INSN)
elif ttype == Token.Error:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_ERROR)
elif ttype == Token.Other:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_DSTR)
elif ttype == Token.Keyword:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_KEYWORD)
elif ttype == Token.Name:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_LIBNAME)
elif ttype == Token.Literal:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_LOCNAME)
elif ttype == Token.Literal.String:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_STRING)
elif ttype == Token.Literal.Number:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_DNUM)
elif ttype == Token.Operator:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_ALTOP)
elif ttype == Token.Punctuation:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_SYMBOL)
elif ttype == Token.Comment:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_REGCMT)
elif ttype == Token.Comment.Single:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_REGCMT)
elif ttype == Token.Generic:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_CREFTAIL)
else:
new_line += idaapi.COLSTR(ttext, idaapi.SCOLOR_CREFTAIL)
return new_line
def add_colored_text(self, text):
"""
Parse the code with the C lexer and display the colored text
in the decompiled view.
"""
for line in text.splitlines():
new_line = self.color_line(line)
self.AddLine(new_line)
return
def Create(self, decompiled, ea):
"""
Create a new view with the decompiled code
"""
title = "Decompiled Function"
self.__ea = ea
self.__decompiled = decompiled
# Create the customviewer
if not idaapi.simplecustviewer_t.Create(self, title):
return False
# for line in decompiled.splitlines():
# self.AddLine(str(line))
self.add_colored_text(decompiled)
return True
def clear(self, msg=None, do_show=True):
"""
Clear the view content
"""
self.ClearLines()
self.__ea = None
self.__decompiled = None
if msg:
for line in msg.splitlines():
self.AddLine(line)
self.Refresh()
if do_show:
self.Show()
return
def update(self, ea, decompiled, do_show=True):
"""
Update the content of the view with the new decompiled code
"""
self.__ea = ea
self.__decompiled = decompiled
self.ClearLines()
self.add_colored_text(decompiled)
self.Refresh()
if do_show:
self.Show()
# Update the cache
DECOMPILED_CACHE.update_decompiled_cache(ea, decompiled)
# print("GhIDA:: [DEBUG] GhIDA DECOM view updated to %s" % ea)
return
def switch_to_address(self, ea):
"""
The IDA DIASM view switched to a new address, change
the decompiled view accordingly.
"""
self.__ea = ea
decompile_function_wrapper(cache_only=True, do_show=False)
return
def add_comment(self):
"""
Add a commment to the selected line
"""
# print("GhIDA:: [DEBUG] add_comment called")
colored_line = self.GetCurrentLine(notags=1)
if not colored_line:
idaapi.warning("Select a line")
return False
# Use pygments to parse the line to check if there are comments
line = idaapi.tag_remove(colored_line)
lexer = CLexer()
tokens = list(lexer.get_tokens(line))
text = ""
text_comment = ""
for t in tokens:
ttype = t[0]
ttext = str(t[1])
if ttype == Token.Comment.Single:
text_comment = ttext.replace('//', '').strip()
else:
text += ttext
# Get the new comment
comment = gl.display_comment_form(text_comment)
if not comment or len(comment) == 0:
return False
comment = comment.replace("//", "").replace("\n", " ")
comment = comment.strip()
# Create the new text
full_comment = "\t// %s" % comment
text = text.rstrip()
new_text = text + full_comment
text_colored = self.color_line(new_text)
num_line = self.GetLineNo()
self.EditLine(num_line, text_colored)
self.RefreshCurrent()
# Add comment to cache
COMMENTS_CACHE.add_comment_to_cache(self.__ea, num_line, full_comment)
# print("GhIDA:: [DEBUG] Added comment to #line: %d (%s)" %
# (num_line, new_text))
return
def add_comments(self, comment_list):
"""
Updated the view with the available comments
"""
for item in comment_list:
lineno = item[0]
comment = item[1]
if len(comment) == 0:
continue
line = self.GetLine(lineno)
if not line:
print("GhIDA:: [!] line not found")
continue
line_text = line[0]
if not line_text:
print("GhIDA:: [!] line-text not found")
continue
line_text = idaapi.tag_remove(line_text) + comment
new_line = self.color_line(line_text)
self.EditLine(lineno, new_line)
self.Refresh()
# print("GhIDA:: [DEBUG] updated comments terminated")
return
def rename_symbol(self):
"""
Rename the symbol "symbol" with the new name
provided by the user in the Pop-Up
"""
# Get the symbol
symbol = None
ret = ida_kernwin.get_highlight(ida_kernwin.get_current_viewer())
if ret and ret[1]:
symbol = ret[0]
if not symbol:
idaapi.warning("Select a symbol")
return False
# Get the address
address = gl.get_address_for_symbol(symbol)
if not address:
print("GhIDA:: [!] Symbol %s not found" % symbol)
return False
# Display a Pop-up to get the new name
new_name = gl.display_rename_form(address, symbol)
if not new_name or len(new_name) == 0:
return
# Check for white_spaces in the new symbol name
for letter in new_name:
if not (letter.isdigit() or letter.isalpha() or letter == '_'):
print("GhIDA:: [!] symbol name contains invalid char")
return
# Check if new_name is already used
if gl.check_if_symbol_is_used(new_name):
print("GhIDA:: [!] symble name already used")
return
# Update symbol name in SYMBLE DICT:
gl.updated_symbol_name_for_address(symbol, address, new_name)
# Update symbol name in IDA DISASM view.
# print("GhIDA:: [DEBUG] New symbol name: %s" % new_name)
# Update symbol name in the decompiled view
new_code = gl.rename_variable_in_text(
self.__decompiled,
symbol,
new_name)
self.update(self.__ea, new_code)
# Add comments
comment_list = COMMENTS_CACHE.get_comments_cache(self.__ea)
if comment_list:
self.add_comments(comment_list)
print("GhIDA:: [INFO] Symbol name updated in IDA DECOMP view.")
if idc.set_name(address, new_name):
# Refresh the view
idaapi.request_refresh(idaapi.IWID_DISASMS)
# Highlight the new identifier
gl.highlight_symbol_in_DISASM()
print("GhIDA:: [INFO] Symbol name updated in IDA DISASM view.")
return
print("GhIDA:: [!] IDA DISASM rename error")
return
def OnClick(self, shift):
"""
User clicked in the view
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
return gl.highlight_symbol_in_DISASM()
def OnDblClick(self, shift):
"""
User dbl-clicked in the view
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
return goto(shift)
def OnKeydown(self, vkey, shift):
"""
User pressed a key
@param vkey: Virtual key code
@param shift: Shift flag
@return: Boolean. True if you handled the event
"""
# print("OnKeydown, vk=%d shift=%d" % (vkey, shift))
# # Esc --> close the window
if vkey == 27:
# TODO.. go back in the history, as disasm does
return True
# N or n --> rename a symbol
if vkey == ord('N'):
self.rename_symbol()
# : --> add a comment
elif vkey == 186 and shift == 1:
self.add_comment()
else:
return False
return True
@property
def ea(self):
return self.__ea
# ------------------------------------------------------------
# HANDLERS FOR THE POP-UP MENU IN DISASMS VIEW
# ------------------------------------------------------------
class InvalidateCache(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] InvalidateCache HELLO")
address = gl.get_current_address()
if not address:
# print("GhIDA:: [DEBUG] address not found")
return
DECOMPILED_CACHE.invalidate_cache(address)
gl.force_export_XML_file()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class DisasmTracker(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] DisasmTracker HELLO")
if GHIDA_CONF.disasm_tracker:
GHIDA_CONF.disasm_tracker = False
print("GhIDA:: [INFO] synchronization disabled")
# Update the description in the pop-up menu.
ida_kernwin.update_action_label(
"my:disasmtracker", "Enable decompile view synchronization")
else:
GHIDA_CONF.disasm_tracker = True
print("GhIDA:: [INFO] synchronization activated")
# Update the description in the pop-up menu.
ida_kernwin.update_action_label(
"my:disasmtracker", "Disable decompile view synchronization")
# Update the DECOMP view to the current address
ea = gl.get_current_address()
if ea:
DECOMP_VIEW.switch_to_address(ea)
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class DisasmsHandler(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
# Say hello when invoked.
def activate(self, ctx):
# print("GhIDA:: [DEBUG] DisasmsHandler HELLO")
decompile_function_wrapper()
return 1
# This action is always available.
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class DisasmsHooks(idaapi.UI_Hooks):
def finish_populating_tform_popup(self, form, popup):
# TODO - Attach to the functions view.
# if idaapi.get_tform_type(form) == idaapi.BWN_FUNCS:
# idaapi.attach_action_to_popup(
# form, popup, "my:disasmsaction", None)
# Attach to the disassembler view only
if idaapi.get_tform_type(form) == idaapi.BWN_DISASMS:
idaapi.attach_action_to_popup(
form, popup, "my:disasmsaction", None)
idaapi.attach_action_to_popup(
form, popup, "my:disasmtracker", None)
idaapi.attach_action_to_popup(
form, popup, "my:invalidatecache", None)
def register_handlers():
"""
Register the handlers for the pop-up menu to interact with the UI
"""
# print("GhIDA:: [DEBUG] Registering handlers")
# Load a custom icon
icon_path = gl.plugin_resource("ghida.png")
icon_data = str(open(icon_path, "rb").read())
icon_ghida = idaapi.load_custom_icon(data=icon_data)
idaapi.register_action(idaapi.action_desc_t(
"my:disasmsaction",
"Decompile function with GhIDA",
DisasmsHandler(),
None,
'IDA plugin for Ghidra decompiler',
icon_ghida))
disasmtracker_action = idaapi.action_desc_t(
"my:disasmtracker",
"Disable decompile view synchronization",
DisasmTracker(),
None,
None,
icon_ghida)
idaapi.register_action(disasmtracker_action)
idaapi.register_action(idaapi.action_desc_t(
"my:invalidatecache",
"Clear cache for current function",
InvalidateCache(),
None,
None,
icon_ghida))
# Add the settings item in the menu
show_settings_action = idaapi.action_desc_t(
'my:showsettingsaction',
'GhIDA Settings',
ShowSettingsHandler(),
None,
'GhIDA Settings',
icon_ghida)
idaapi.register_action(show_settings_action)
idaapi.attach_action_to_menu(
'Edit/Settings/GhIDA Settings',
'my:showsettingsaction',
idaapi.SETMENU_APP)
# Add the view decompile window in the menu
show_decomp_window_action = idaapi.action_desc_t(
'my:showdecompilewindowaction',
'GhIDA decomp view',
ShowDecompWindowHandler(),
None,
'GhIDA decomp view',
icon_ghida)
idaapi.register_action(show_decomp_window_action)
idaapi.attach_action_to_menu(
'View/Open subviews/GhIDA',
'my:showdecompilewindowaction',
idaapi.SETMENU_APP)
return
# ------------------------------------------------------------
# DEFINING GLOBAL VARS AND GHIDA INITIAL CONFIGURATION
# ------------------------------------------------------------
def load_configuration():
"""
"""
global GHIDA_CONF
global DECOMPILED_CACHE
global COMMENTS_CACHE
# Loading the plugin configuration
# print("GhIDA:: [DEBUG] Reading GhIDA configuration")
GHIDA_CONF = gl.GhidaConfiguration()
print("GHIDA_CONF.load_save_cached_code",
GHIDA_CONF.load_save_cached_code)
print("GHIDA_CONF.load_save_cached_comments",
GHIDA_CONF.load_save_cached_comments)
md5 = idautils.GetInputFileMD5()
# Initalize the cache (and load cached objects)
DECOMPILED_CACHE = gl.DecompiledCache(
file_id=md5,
use_cache=GHIDA_CONF.load_save_cached_code)
COMMENTS_CACHE = gl.CommentsCache(
file_id=md5,
use_cache=GHIDA_CONF.load_save_cached_comments)
return
# ------------------------------------------------------------
# HANDLERS FOR THE POP-UP MENU IN DECOMP VIEW
# ------------------------------------------------------------
def register_actions_and_handlers_decompile_view():
"""
Attach the following actions in the pop-up menu of the
decompiled view.
"""
# Load a custom icon
icon_path = gl.plugin_resource("ghida.png")
icon_data = str(open(icon_path, "rb").read())
icon_ghida = idaapi.load_custom_icon(data=icon_data)
decompiler_widget = idaapi.find_widget('Decompiled Function')
# TODO alternative
# decompiler_widget = idaapi.get_current_tform()
# Add Rename to the pop-up
action_renamecustviewer = idaapi.action_desc_t(
'my:renamecustviewerhandler',
'Rename',
RenameCustViewerHandler(
DECOMP_VIEW),
None,
None,
icon_ghida)
decompiler_widget = idaapi.find_widget('Decompiled Function')
idaapi.register_action(action_renamecustviewer)
idaapi.attach_action_to_popup(decompiler_widget,
None,
"my:renamecustviewerhandler",
None)
# Add add-comment to the pop-up
action_addcommentcustviewer = idaapi.action_desc_t(
'my:addcommentcustviewer',
'Add comment',
AddCommentCustViewerHandler(
DECOMP_VIEW),
None,
None,
icon_ghida)
idaapi.register_action(action_addcommentcustviewer)
idaapi.attach_action_to_popup(decompiler_widget,
None,
"my:addcommentcustviewer",
None)
# Add goto to the pop-up
action_gotocustviewerhandler = idaapi.action_desc_t(
'my:gotocustviewerhandler',
'Goto',
GoToCustViewerHandler(
DECOMP_VIEW),
None,
None,
icon_ghida)
idaapi.register_action(action_gotocustviewerhandler)
idaapi.attach_action_to_popup(decompiler_widget,
None,
"my:gotocustviewerhandler",
None)
return
# ------------------------------------------------------------
# GHIDA Configuration FORM
# ------------------------------------------------------------
def display_configuration_form():
"""
Display a configuration dialog for the user.
"""
f = gl.GhIDASettingsForm()
f.Compile()
f.GhidraInstallationPath.value = GHIDA_CONF.ghidra_install_path
f.GhidraaasURL.value = GHIDA_CONF.ghidra_server_url
r = f.Execute()
if r == 1: # OK
# Do not display the settings menu anymore
GHIDA_CONF.global_settings = True
if f.cGroup.value == 0:
# Use local Ghidra
GHIDA_CONF.use_ghidra_server = False
GHIDA_CONF.ghidra_install_path = f.GhidraInstallationPath.value
else:
# Use Ghidra server (Ghidraaas)
GHIDA_CONF.use_ghidra_server = True
GHIDA_CONF.ghidra_server_url = f.GhidraaasURL.value
if f.cGroup1.value == 0:
# Do not display the popup at startup
GHIDA_CONF.show_settings = True
else:
# Dispaly the menu at startup
GHIDA_CONF.show_settings = False
if f.cGroup2.value == 0:
# Do not save cache to file
GHIDA_CONF.load_save_cached_code = False
GHIDA_CONF.load_save_cached_comments = False
else:
# Save cache to file
GHIDA_CONF.load_save_cached_code = True
GHIDA_CONF.load_save_cached_comments = True
print("GHIDA_CONF.global_settings", GHIDA_CONF.global_settings)
print("GHIDA_CONF.use_ghidra_server", GHIDA_CONF.use_ghidra_server)
print("GHIDA_CONF.ghidra_install_path", GHIDA_CONF.ghidra_install_path)
print("GHIDA_CONF.ghidra_server_url", GHIDA_CONF.ghidra_server_url)
print("GHIDA_CONF.show_settings", GHIDA_CONF.show_settings)
print("GHIDA_CONF.load_save_cached_code",
GHIDA_CONF.load_save_cached_code)
print("GHIDA_CONF.load_save_cached_comments",
GHIDA_CONF.load_save_cached_comments)
# Save configuration to file
GHIDA_CONF.dump_to_json()
return True
# Canceled
return False
# ------------------------------------------------------------
# DECOMPILE FUNCTION - CORE
# ------------------------------------------------------------
def decompile_function_wrapper(cache_only=False, do_show=True):
"""
Perform all the operations to decompile the code of the selected
function and display in the decompile view.
"""
try:
global DECOMP_VIEW
ea = gl.get_current_address()
if not ea:
# This is not a function
# GhIDA can decompile only IDA recognized functions.
return
# Set the base_image
image_base = idaapi.get_imagebase()
if GHIDA_CONF.image_base is None:
GHIDA_CONF.image_base = image_base
# Check if the program has been rebased
if GHIDA_CONF.image_base != image_base:
print(
"GhIDA:: [DEBUG] program has been rebased. Invalidating caches.")
DECOMPILED_CACHE.invalidate_cache()
COMMENTS_CACHE.invalidate_cache()
gl.force_export_XML_file()
# Display the Configuration form
if GHIDA_CONF.show_settings and \
not GHIDA_CONF.global_settings:
canceled = not(display_configuration_form())
if canceled:
return
# If exists, clear the decompile view.
if DECOMP_VIEW:
DECOMP_VIEW.clear(msg="Decompiling function...",
do_show=do_show)
# Call export XML file. It also populates the SYMBOL DICT TABLE (S_D_T)
# for the highlighting, renaming, etc.
# Do it here because I need S_D_T it also if the result is in the
# cache.
gl.export_ida_project_to_xml()
# Check the cache
decompiled = DECOMPILED_CACHE.get_decompiled_cache(ea)
# Cache miss - opt1: do not use Ghidra, just display what is in the
# cache
if not decompiled and cache_only:
# This is a redundant check...
if DECOMP_VIEW:
msg = "Function 0x%s\n\n" % ea
msg += "Decompiled code not available in cache.\n"
msg += "Press Ctrl-Alt-D or Right click GhIDA decompiler "
msg += "to decompile the function."
DECOMP_VIEW.clear(msg=msg, do_show=do_show)
# print("GhIDA:: [DEBUG] Function code not available in cache.")
return
# Cache miss - opt2: Use Ghidra to decompile the function
if not decompiled:
decompiled = gl.decompile_function(
address=ea,
use_ghidra_server=GHIDA_CONF.use_ghidra_server,
ghidra_headless_path=GHIDA_CONF.ghidra_headless_path,
ghidra_plugins_path=GHIDA_CONF.ghidra_plugins_path,
ghidra_server_url=GHIDA_CONF.ghidra_server_url)
if decompiled:
# Add decompiled_code to cache
DECOMPILED_CACHE.add_decompiled_to_cache(ea, decompiled)
else:
# Something went wrong or was interrupted
if DECOMP_VIEW:
DECOMP_VIEW.clear(msg="Decompiling interrupted.",
do_show=do_show)
print("GhIDA:: [!] Decompilation interrupted.")
return
# Decompiled code is available.
if DECOMP_VIEW:
# The view exists, update it
DECOMP_VIEW.update(ea, decompiled, do_show=do_show)
else:
# Create the view
DECOMP_VIEW = DecompiledViewer_t()
if not DECOMP_VIEW.Create(decompiled, ea):
print("GhIDA:: [!] Error creating the view")
return
DECOMP_VIEW.Show()
register_actions_and_handlers_decompile_view()
# Add comments
comment_list = COMMENTS_CACHE.get_comments_cache(ea)
if comment_list:
DECOMP_VIEW.add_comments(comment_list)
return
except Exception as e:
print("GhIDA:: [!] Decompilation wrapper error: {}".format(e))
idaapi.warning("GhIDA decompilation wrapper error {}".format(e))
# ------------------------------------------------------------
# GHIDRA DECOMPILER PLUGIN
# ------------------------------------------------------------
class GhIDADecompiler_t(idaapi.plugin_t):
comment = "GhIDA Decompiler for IDA Pro"
help = "GhIDA Decompiler shortcut key is Ctrl-Alt-D"
wanted_name = "GhIDA Decompiler"
wanted_hotkey = "Ctrl-Alt-D"
flags = idaapi.PLUGIN_KEEP
def init(self):
# Print header
print("=" * 60)
print("GhIDA Decompiler v{0}".format(gl.ghida_vv))
print("Andrea Marcelli <anmarcel@cisco.com>")
print("Cisco Talos, June 2019")
print("GhIDA Decompiler shortcut key is Ctrl-Alt-D")
print("=" * 60)