-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathctrlp.py
More file actions
796 lines (654 loc) · 27.5 KB
/
ctrlp.py
File metadata and controls
796 lines (654 loc) · 27.5 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
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
# coding: utf-8
# @author msm
# @category Search
# @menupath Search.Palette
# @toolbar
import re
from ghidra.program.flatapi import FlatProgramAPI
from ghidra.program.model.symbol import SymbolType, SourceType
from ghidra.program.model.listing import BookmarkType
from ghidra.app.services import ConsoleService, CodeViewerService
from ghidra.util.task import TaskMonitor
from ghidra.app.script import GhidraScriptUtil, GhidraState
from ghidra.app.util.viewer.field import ListingColors
from javax.swing import JFrame, JTextField, JList, JScrollPane, SwingUtilities, JPanel, DefaultListCellRenderer, SwingWorker, UIManager
from javax.swing.event import DocumentListener
from java.lang import Object, System
from java.awt import BorderLayout, Color, Font, GraphicsEnvironment, Window
from java.awt.event import KeyAdapter, KeyEvent, ComponentAdapter
from java.util import Vector
from java.awt import Toolkit
from java.awt.datatransfer import StringSelection
from __main__ import (
getBytes,
currentAddress,
getState,
getCurrentProgram,
toAddr,
goTo,
monitor,
)
def matches(name, query):
"""Baby fuzzy matcher - splits query by whitespace, and matches
if name contains every resulting element. For example,
"aaa bbb ccc" matches "aaa ccc" but not "aaaccc" or "aaa ddd" """
name = name.lower()
query = query.lower()
chunks = query.split()
for c in chunks:
# Special case - filtering by type, for example user searches for `wnd script`
if c in ["fnc", "dat", "lbl", "bkm", "wnd", "act", "scr", "txt", "ref"]:
if not name.startswith(c):
return False
ndx = name.find(c)
if ndx < 0:
return False
return True
def makeState():
"""Creates a new (current) state object and returns it
We can't just use getState(), because it's a snapshot of program state.
For tool and project it doesn't matter. For program it also doesn't matter,
but we need to remember to use getCurrentProgram() instead of currentProgram
in the script code (the variable is also constant, while in some cases -
like multitab windows - current project may change for a CtrlP window.
Finally, selection changes all the time, so we need to update it here."""
oldState = getState()
codeViewerService = oldState.getTool().getService(CodeViewerService)
if codeViewerService:
currLocation = codeViewerService.getCurrentLocation()
currSelection = codeViewerService.getCurrentSelection()
currHighlight = codeViewerService.getListingPanel().getProgramHighlight()
else:
currLocation = oldState.getCurrentLocation()
currSelection = oldState.getCurrentSelection()
currHighlight = oldState.getCurrentHighlight()
return GhidraState(
oldState.getTool(), # I think this can't change
oldState.getProject(), # I think this can't change
getCurrentProgram(),
currLocation,
currSelection,
currHighlight,
)
def transientGoto(addr):
"""Goto, but without saving it in the history"""
codeViewerService = makeState().getTool().getService(CodeViewerService)
if codeViewerService:
from ghidra.program.util import ProgramLocation
codeViewerService.goTo(ProgramLocation(getCurrentProgram(), addr), True)
else:
# fallback
goTo(addr)
def wrap_goto(addr):
"""This is a wrapper for goTo, returns a function that goToes to the addr
the point is to capture addr in a closure (something that won't happen in a lambda"""
return lambda: goTo(addr)
class ScriptExecutor(SwingWorker):
def __init__(self, script):
super(ScriptExecutor, self).__init__()
self.script = script
def doInBackground(self):
con = makeState().getTool().getService(ConsoleService)
prov = GhidraScriptUtil.getProvider(self.script)
inst = prov.getScriptInstance(self.script, con.getStdOut())
inst.execute(makeState(), monitor, con.getStdOut())
def done(self):
pass
class SymbolLoader(SwingWorker):
def __init__(self, parent):
super(SymbolLoader, self).__init__()
self.parent = parent
def get_everything(self):
everything = []
everything += get_symbols()
everything += get_component_providers()
everything += get_bookmarks()
everything += get_actions()
everything += get_scripts()
return everything
def doInBackground(self):
try:
return self.get_everything()
except:
# uncomment this for debug info:
# import traceback
# state.getTool().getService(ConsoleService).println(traceback.format_exc())
# BUG TODO FIXME
# When Ghidra window is closed and then reopened, the references in the window stop making sense.
# and this thread/wtf is in a broken state.
# We should probably watch when ghidra window exits and then cleanup, but...
# Just kill ourselves and let user try again.
self.parent.dispose()
return [] # Just so we don't raise an exception in a second
def done(self):
def refresh_data():
ndx = self.parent.symbolList.getSelectedIndex()
self.parent.updateList(self.parent.inputField.getText())
self.parent.symbolList.setSelectedIndex(ndx)
try:
symbols = self.get()
self.parent.symbols = symbols
SwingUtilities.invokeLater(refresh_data)
except Exception as e:
print("Error loading symbols" + str(e))
def prettyPrintAddress(source):
func_manager = getCurrentProgram().getFunctionManager()
xref_func = func_manager.getFunctionContaining(source)
if xref_func is None:
codeunit = getCurrentProgram().getListing().getCodeUnitContaining(source)
if codeunit is not None:
text = "lbl {:x} {}".format(source.getOffset(), str(codeunit))
else:
text = "dat {:x}".format(source.getOffset())
else:
offset = source.subtract(xref_func.getEntryPoint())
text = "fnc {}+{:x}".format(xref_func.getPrototypeString(True, False), offset)
return text
def get_color(sym):
kind = sym.text.split()[0]
return {
"fnc": ListingColors.FunctionColors.NAME,
"dat": ListingColors.REGISTER,
"lbl": ListingColors.MnemonicColors.NORMAL,
"bkm": ListingColors.FunctionColors.VARIABLE,
"wnd": ListingColors.CommentColors.REPEATABLE,
"act": ListingColors.XrefColors.DEFAULT,
"scr": ListingColors.MnemonicColors.OVERRIDE,
"txt": ListingColors.MnemonicColors.NORMAL,
"ref": ListingColors.REGISTER,
}[kind]
class SymbolFilterWindow(JFrame):
def __init__(self, title, symbols):
super(SymbolFilterWindow, self).__init__(title)
self.special_symbols = []
self.symbols = symbols
self.filtered_symbols = symbols
self.initUI()
self.selected_index = 0
self.initial_address = currentAddress
# special_symbols are currently used in the "xref search mode" -
# we are searching in them instead of self.symbols
# Special search mode is enabled when self.special_symbols is not empty.
# We don't reuse self.symbols for this, because populating self.symbols
# takes time, and we want to have cached results when opening ctrl+p.
self.recent_symbols = {}
# keep track of recently used symbols. We want to show recent symbols
# at the top of the search list, so it's easy to repeat the search.
def initUI(self):
self.setSize(1200, 600)
self.setResizable(False)
self.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
self.getContentPane().setLayout(BorderLayout())
me = self
class MyComponentAdapter(ComponentAdapter):
def componentShown(self, event):
codeViewerService = makeState().getTool().getService(CodeViewerService)
if codeViewerService:
# We can't just use currentAddress because of a technicality:
# the variable in script is never updated and stays the same.
new_address = codeViewerService.getCurrentLocation().getAddress()
me.initial_address = new_address # so we can cancel navigation
me.special_symbols = [] # disable special search mode when showing
me.inputField.setText("") # clear the input field
if me.symbols:
me.symbolList.setSelectedIndex(0)
me.symbolList.ensureIndexIsVisible(me.symbolList.getSelectedIndex())
SymbolLoader(me).execute() # start updating symbols in the background
def componentHidden(self, event): pass
def componentMoved(self, event): pass
def componentResized(self, event): pass
self.addComponentListener(MyComponentAdapter())
inputPanel = JPanel(BorderLayout())
self.inputField = JTextField()
self.inputField.addKeyListener(FilterKeyAdapter(self))
inputPanel.add(self.inputField, BorderLayout.CENTER)
fontname = None
FONTS = ["FiraCode Nerd Font Mono", "Monospaced"]
for fontname in FONTS:
g = GraphicsEnvironment.getLocalGraphicsEnvironment()
if fontname in g.getAvailableFontFamilyNames():
break
assert fontname is not None
font = Font(fontname, Font.PLAIN, 14)
self.inputField.setFont(font)
self.inputField.getDocument().addDocumentListener(MyDocumentListener(self))
self.symbolList = JList(Vector([]))
self.updateList("")
self.symbolList.setCellRenderer(SymbolCellRenderer(self))
self.symbolList.addKeyListener(FilterKeyAdapter(self))
self.symbolList.setFont(font)
self.scrollPane = JScrollPane(self.symbolList)
self.getContentPane().add(inputPanel, BorderLayout.NORTH)
self.getContentPane().add(self.scrollPane, BorderLayout.CENTER)
if self.symbols:
self.symbolList.setSelectedIndex(0)
self.symbolList.setFocusable(False)
self.inputField.requestFocusInWindow()
def entries_by_search(self, needle, ignore_case):
if not needle:
return [SearchEntry(
"dat (entering search mode)",
None,
lambda: None
)]
pattern = re.escape(needle)
if ignore_case:
pattern = "(?i)" + pattern
filtered_symbols = []
flatapi = FlatProgramAPI(getCurrentProgram())
occurs = list(flatapi.findBytes(getCurrentProgram().getMinAddress(), pattern, 100))
mem = getCurrentProgram().getMemory()
filtered_symbols = []
for addr in occurs:
start = addr.add(-10)
rng = mem.getRangeContaining(addr)
if start < rng.getMinAddress():
start = rng.getMinAddress()
context = getBytes(start, 130)
filtered_symbols.append(SearchEntry(
"dat " + str(addr) + " " + "".join(chr(b % 256) if 32 <= b < 127 else '.' for b in context),
addr,
wrap_goto(addr)
))
return filtered_symbols
def quick_exec(self, command):
try:
result = eval(command, {"__builtins__": None}, {})
except Exception as e:
result = e
def set_clipboard(txt):
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
string_selection = StringSelection(txt)
clipboard.setContents(string_selection, None)
if isinstance(result, int) or isinstance(result, long): # type: ignore (py2)
strings = [
"hex {:x}".format(result),
"dec {}".format(result),
"oct {:o}".format(result),
"bin {:b}".format(result),
]
func = getCurrentProgram().getFunctionManager().getFunctionContaining(toAddr(result))
if func:
off = toAddr(result).subtract(func.getEntryPoint())
strings.append("sym " + func.getName() + ("+{:x}".format(off) if off else ""))
elif isinstance(result, str):
strings = [
"str " + result,
"hex " + result.encode("hex"), # type: ignore (py2)
"base64 " + result.encode("base64"), # type: ignore (py2)
]
try:
strings.append("unhex " + result.replace(" ", "").decode("hex")) # type: ignore (py2)
except TypeError:
pass
try:
strings.append("unbase64 " + result.decode("base64")) # type: ignore (py2)
except: # binascii.error
pass
elif isinstance(result, list):
strings = [str(r) for r in result]
else:
strings = [
"str " + str(result)
]
def set_clipboard_wrap(content):
return lambda: set_clipboard(content)
return [SearchEntry("txt " + s, None, set_clipboard_wrap(s[4:])) for s in strings]
def get_order(self, sym):
kind = sym.text.split()[0]
primary_order = -self.recent_symbols.get(sym.text, -1)
secondary_order = {
"fnc": 0,
"dat": 1,
"lbl": 2,
"bkm": 3,
"wnd": 4,
"act": 5,
"scr": 6,
"txt": 7,
"ref": 8,
}[kind]
return (primary_order, secondary_order)
def updateList(self, filter_text):
if filter_text and filter_text[0] == '"':
filtered_symbols = self.entries_by_search(filter_text[1:], False)
elif filter_text and filter_text[0] == "'":
filtered_symbols = self.entries_by_search(filter_text[1:], True)
elif filter_text and filter_text[0] == "=":
filtered_symbols = self.quick_exec(filter_text[1:])
elif filter_text and filter_text[0] == "{":
try:
needle = filter_text[1:].replace(" ", "").decode("hex")
except:
needle = ""
filtered_symbols = self.entries_by_search(needle, False)
else:
symbols_to_search = self.symbols
if self.special_symbols:
symbols_to_search = self.special_symbols
filtered_symbols = [
sym for sym in symbols_to_search if matches(sym.text, filter_text)
]
# we have to search first, because we can't skip high-priority symbols :(
filtered_symbols = sorted(filtered_symbols, key=self.get_order)
if len(filtered_symbols) > 1000:
overflow = len(filtered_symbols) - 1000
filtered_symbols = filtered_symbols[:1000]
filtered_symbols.append(SearchEntry(
"txt and " + str(overflow) + " more...",
None,
lambda: None
))
self.filtered_symbols = filtered_symbols
self.symbolList.setListData(Vector([sym.text for sym in filtered_symbols]))
if filtered_symbols:
self.symbolList.setSelectedIndex(0)
else:
self.symbolList.clearSelection()
def current_symbol(self):
selected_index = self.symbolList.getSelectedIndex()
if selected_index < 0:
return None
return self.filtered_symbols[selected_index]
def updateRecent(self, selected_symbol):
next_index = len(self.recent_symbols)
self.recent_symbols[selected_symbol.text] = next_index
def runSelectedAction(self):
selected_symbol = self.current_symbol()
if selected_symbol:
self.updateRecent(selected_symbol)
selected_symbol.action()
def navigateToSelectedSymbol(self):
selected_symbol = self.current_symbol()
if selected_symbol and selected_symbol.address:
transientGoto(selected_symbol.address)
else:
transientGoto(self.initial_address)
def enterXrefMode(self):
selected_symbol = self.current_symbol()
if selected_symbol and selected_symbol.address:
ref_manager = getCurrentProgram().getReferenceManager()
self.special_symbols = []
for ref in ref_manager.getReferencesTo(selected_symbol.address):
source = ref.getFromAddress()
text = prettyPrintAddress(source)
sym = SearchEntry(
text,
source,
wrap_goto(source)
)
self.special_symbols.append(sym)
self.updateList(self.inputField.getText())
def bookmarkSelectedLocation(self):
selected_symbol = self.current_symbol()
if selected_symbol and selected_symbol.address:
transaction = getCurrentProgram().startTransaction("Add Bookmark")
# Flip the bookmarkstate
if selected_symbol.has_bookmark:
for bm in getCurrentProgram().getBookmarkManager().getBookmarks(selected_symbol.address):
getCurrentProgram().getBookmarkManager().removeBookmark(bm)
else:
getCurrentProgram().getBookmarkManager().setBookmark(
selected_symbol.address,
BookmarkType.NOTE,
"CtrlP",
"Quick bookmark. Query: " + self.inputField.getText()
)
selected_symbol.has_bookmark_cache = not selected_symbol.has_bookmark_cache
getCurrentProgram().endTransaction(transaction, True)
# Update the bookmark "star"
ndx = self.symbolList.getSelectedIndex()
self.updateList(self.inputField.getText())
self.symbolList.setSelectedIndex(ndx)
def copyToClipboard(self):
selected_symbol = self.current_symbol()
if selected_symbol:
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
string_selection = StringSelection(selected_symbol.text)
clipboard.setContents(string_selection, None)
def copyAddressToClipboard(self):
selected_symbol = self.current_symbol()
if selected_symbol and selected_symbol.address:
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()
string_selection = StringSelection("0x" + str(selected_symbol.address))
clipboard.setContents(string_selection, None)
def goToFirstXRef(self):
success = False
selected_symbol = self.current_symbol()
if selected_symbol and selected_symbol.address:
ref_manager = getCurrentProgram().getReferenceManager()
if ref_manager.getReferenceCountTo(selected_symbol.address) > 0:
goTo(ref_manager.getReferencesTo(selected_symbol.address).next().getFromAddress())
success = True
return success
def cancelNavigation(self):
goTo(self.initial_address)
class MyDocumentListener(DocumentListener):
def __init__(self, parent):
self.parent = parent
def insertUpdate(self, e): self.update()
def removeUpdate(self, e): self.update()
def changedUpdate(self, e): self.update()
def update(self):
self.parent.updateList(self.parent.inputField.getText())
class FilterKeyAdapter(KeyAdapter):
def __init__(self, parent):
self.parent = parent
def navigate(self, diff):
symlist = self.parent.symbolList
curr_pos = symlist.getSelectedIndex()
curr_pos += diff
if curr_pos < 0:
curr_pos = 0
if curr_pos >= symlist.getModel().getSize():
curr_pos = symlist.getModel().getSize() - 1
symlist.setSelectedIndex(curr_pos)
symlist.ensureIndexIsVisible(symlist.getSelectedIndex())
self.parent.navigateToSelectedSymbol()
def keyPressed(self, event):
if event.isControlDown() and event.getKeyCode() == KeyEvent.VK_ENTER:
if self.parent.goToFirstXRef():
self.parent.setVisible(False)
elif event.getKeyCode() == KeyEvent.VK_ENTER:
self.parent.setVisible(False)
self.parent.runSelectedAction()
elif event.getKeyCode() == KeyEvent.VK_UP:
self.navigate(-1)
elif event.getKeyCode() == KeyEvent.VK_DOWN:
self.navigate(1)
elif event.getKeyCode() == KeyEvent.VK_ESCAPE:
if self.parent.special_symbols:
# If we are in a special mode, clean it instead of closing entirely
self.parent.special_symbols = []
self.parent.updateList(self.parent.inputField.getText())
else:
self.parent.cancelNavigation()
self.parent.setVisible(False)
elif event.getKeyCode() == KeyEvent.VK_PAGE_DOWN:
self.navigate(20)
elif event.getKeyCode() == KeyEvent.VK_PAGE_UP:
self.navigate(-20)
elif event.getKeyCode() == KeyEvent.VK_END:
self.navigate(2**30)
elif event.getKeyCode() == KeyEvent.VK_HOME:
self.navigate(-2**30)
elif event.isControlDown() and event.getKeyCode() == KeyEvent.VK_D:
self.parent.bookmarkSelectedLocation()
elif event.isControlDown() and event.getKeyCode() == KeyEvent.VK_R:
self.parent.enterXrefMode()
elif event.isControlDown() and event.isShiftDown() and event.getKeyCode() == KeyEvent.VK_C:
self.parent.copyAddressToClipboard()
elif event.isControlDown() and event.getKeyCode() == KeyEvent.VK_C:
self.parent.copyToClipboard()
elif event.isControlDown() and event.getKeyCode() == KeyEvent.VK_Q:
self.parent.dispose()
System.gc()
class SymbolCellRenderer(DefaultListCellRenderer):
def __init__(self, parent):
self.window = parent
def getListCellRendererComponent(self, list, value, index, isSelected, cellHasFocus):
component = super(SymbolCellRenderer, self).getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus)
symbol = self.window.filtered_symbols[index]
component.setForeground(symbol.color)
return component
class SearchEntry:
def __init__(self, text, address, action):
self.raw_text = text
self.address = address
self.action = action
self.has_bookmark_cache = None
@property
def color(self):
return get_color(self)
@property
def text(self):
if self.has_bookmark:
return self.raw_text + u" [*]"
return self.raw_text
@property
def has_bookmark(self):
if self.has_bookmark_cache is None:
self.has_bookmark_cache = self.address and len(getCurrentProgram().getBookmarkManager().getBookmarks(self.address)) > 0
return self.has_bookmark_cache
def data_symbol_entry(sym):
listing = getCurrentProgram().getListing()
data = listing.getDataAt(sym.getAddress())
addr = toAddr(sym.getAddress().getOffset())
if data is not None:
textrepr = data.getDefaultValueRepresentation()
if len(textrepr) > 80:
textrepr = textrepr[:80]
if textrepr:
textrepr = " (" + textrepr + ")"
return SearchEntry(
"dat " + data.getDataType().displayName + " " + sym.getName() + textrepr,
addr,
lambda: goTo(addr)
)
return SearchEntry(
"lbl " + sym.getName(),
addr,
lambda: goTo(addr)
)
def function_symbol_entry(sym):
listing = getCurrentProgram().getListing()
func = listing.getFunctionAt(sym.getAddress())
addr = toAddr(sym.getAddress().getOffset())
return SearchEntry(
"fnc " + func.getPrototypeString(True, False),
addr,
lambda: goTo(addr)
)
def action_entry(context, act):
def execme():
act.actionPerformed(context)
suffix = ""
if act.keyBinding:
binding = str(act.keyBinding)
binding = binding.replace("pressed ", "")
binding = binding.replace(" ", "+")
# This will produce things like ctrl+shift+alt+A.
# I prefer emacs notation, so C-S-M-a, but I guess not everyone knows it.
suffix = " (" + binding + ")"
return SearchEntry(
"act " + act.name + suffix,
None,
execme
)
def run_script(scr_file):
scr = GhidraScriptUtil.findScriptByName(scr_file.getName())
ScriptExecutor(scr).execute()
def script_entry(scr):
return SearchEntry(
"scr " + scr.getName(),
None,
lambda: run_script(scr),
)
def component_provider_entry(cp):
def show_and_focus():
makeState().getTool().showComponentProvider(cp, True)
cp.toFront()
return SearchEntry(
"wnd " + str(cp),
None,
show_and_focus
)
def bookmark_entry(bookmark):
addr = toAddr(bookmark.getAddress().getOffset())
category = bookmark.getCategory()
if category:
category = " (" + category + ")"
return SearchEntry(
"bkm " + str(bookmark.getComment()) + category,
addr,
lambda: goTo(addr)
)
def get_actions():
prov = makeState().getTool().getActiveComponentProvider()
if prov is None:
return []
symbols = []
context = prov.getActionContext(None)
for act in makeState().getTool().getAllActions():
if not issubclass(type(context), act.getContextClass()):
continue
try:
if not act.isValidContext(context):
continue
except:
# Sometimes this raises an exception - even though it shouldn't
continue
if not act.isEnabledForContext(context):
continue
symbols.append(action_entry(context, act))
return symbols
def get_symbols():
symbols = []
symbolTable = getCurrentProgram().getSymbolTable()
mem = getCurrentProgram().getMemory()
for symbol in symbolTable.getAllSymbols(True):
if not mem.contains(symbol.getAddress()):
continue
if symbol.source == SourceType.DEFAULT:
if symbol.getName().startswith("LAB_"):
# Really boring symbols.
continue
if symbol.symbolType == SymbolType.FUNCTION:
symbols.append(function_symbol_entry(symbol))
else:
symbols.append(data_symbol_entry(symbol))
return symbols
def get_component_providers():
symbols = []
for cp in getState().getTool().getWindowManager().getComponentProviders(Object):
symbols.append(component_provider_entry(cp))
return symbols
def get_scripts():
symbols = []
for script_dir in GhidraScriptUtil.getScriptSourceDirectories():
script_files = script_dir.listFiles()
for scr_file in script_files:
symbols.append(script_entry(scr_file))
return symbols
def get_bookmarks():
symbols = []
for mark in getCurrentProgram().getBookmarkManager().getBookmarksIterator():
symbols.append(bookmark_entry(mark))
return symbols
WINDOW_NAME = "CtrlP - " + str(getCurrentProgram().getDomainFile())
def run():
symbols = []
SwingUtilities.invokeLater(lambda: SymbolFilterWindow(WINDOW_NAME, symbols).setVisible(True))
def run_or_restore():
for window in Window.getWindows():
if isinstance(window, JFrame):
if window.getTitle() == WINDOW_NAME and window.isDisplayable():
if not window.isShowing():
window.setVisible(True)
else:
print("Window is alredy visible. Doing nothing")
return
run()
if __name__ == "__main__":
run_or_restore()