-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathida-efitools2.py
168 lines (125 loc) · 4.78 KB
/
ida-efitools2.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
import os
import sys
from uuid import UUID
from struct import pack
from builtins import bytes # add bytes semantics from py3 to py2
from ida_funcs import get_func
from ida_idaapi import PLUGIN_KEEP, plugin_t
from idc import get_bytes, o_displ, o_phrase, o_imm
from ida_kernwin import action_handler_t, action_desc_t, register_action, unregister_action
from ida_kernwin import get_screen_ea, attach_action_to_popup
from ida_kernwin import AST_ENABLE_FOR_WIDGET, AST_DISABLE_FOR_WIDGET
from ida_kernwin import BWN_DISASM, BWN_LOCTYPS
# this eliminates package name 'efitools2' from class paths
# efitools2.core.objects.Structure != core.objects.Structure
EFITOOLS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "efitools2")
sys.path.append(EFITOOLS_DIR)
from efitools import EfiTools, FIX_CODE_SEGMENT_PERMISSIONS, IMPORT_EXTERNAL_TYPES
from core.objects import Function
# customize hotkeys here
RUN_PLUGIN_HOTKEY = "Ctrl-Alt-E"
PRINT_EFI_GUID_HOTKEY = "Ctrl-Alt-G"
SYNC_LOCAL_TYPES_HOTKEY = "F5"
def extract_guid(ea):
function = Function(ea)
guidname = None
fields = dict()
for item in function.items(ea):
if item.mnem == "mov" and item.operands_num == 2:
displ = item[0]
imm = item[1]
if displ.type in (o_displ, o_phrase) and imm.type == o_imm:
if ".Data" not in displ.displ_str:
continue
lvar, field = displ.displ_str.split(".")
if guidname is None:
guidname = lvar
if lvar == guidname:
fields[field] = imm.value & (1 << 32) - 1
if len(fields) == 4:
break
else:
raise LookupError("unable to find all EFI_GUID fields")
return guidname, pack("<IIII", fields["Data1"], fields["Data2"], fields["Data4"], fields["Data4+4"])
class PrintEfiGuid(action_handler_t):
name = "my:PrintEfiGuid"
description = "Print EFI_GUID at current location"
hotkey = PRINT_EFI_GUID_HOTKEY
def __init__(self):
action_handler_t.__init__(self)
def activate(self, ctx):
"""
:param ctx: idaapi.action_activation_ctx_t
:return: None
"""
ea = get_screen_ea()
if get_func(ea) is None:
print("address = " + hex(ea))
data = get_bytes(ea, 16)
guid = str(UUID(bytes_le=data))
else:
name, data = extract_guid(ea)
guid = str(UUID(bytes_le=data))
print("Local variable EFI_GUID extraction for " + name)
print("data = " + " ".join("%02x" % x for x in bytes(data)))
print("guid = " + guid)
try:
import clipboard
clipboard.copy(guid)
except ImportError:
print("clipboard module is not available.")
def update(self, ctx):
if ctx.widget_type == BWN_DISASM:
attach_action_to_popup(ctx.widget, None, self.name)
return AST_ENABLE_FOR_WIDGET
return AST_DISABLE_FOR_WIDGET
class SyncLocalTypes(action_handler_t):
name = "my:SyncLocalTypes"
description = "Synchronize with external types"
hotkey = SYNC_LOCAL_TYPES_HOTKEY
def __init__(self):
action_handler_t.__init__(self)
def activate(self, ctx):
"""
:param ctx: idaapi.action_activation_ctx_t
:return: None
"""
EfiTools.import_types()
def update(self, ctx):
if ctx.widget_type == BWN_LOCTYPS:
attach_action_to_popup(ctx.widget, None, self.name)
return AST_ENABLE_FOR_WIDGET
return AST_DISABLE_FOR_WIDGET
class EfiToolsPlugin(plugin_t):
flags = 0
comment = "Plugin for augmenting UEFI reverse engineering capabilities"
help = ""
wanted_name = "efitools2"
wanted_hotkey = RUN_PLUGIN_HOTKEY
executed = False
@staticmethod
def register_action(action, *args):
register_action(action_desc_t(action.name, action.description, action(*args), action.hotkey))
@staticmethod
def init():
EfiToolsPlugin.register_action(SyncLocalTypes)
EfiToolsPlugin.register_action(PrintEfiGuid)
return PLUGIN_KEEP
@staticmethod
def run(arg):
if not EfiToolsPlugin.executed:
EfiTools.image_rebase()
if IMPORT_EXTERNAL_TYPES:
EfiTools.import_types()
# arg = 1 means running in batch mode
# BinExport requires CODE segment to be R+E
if arg != 1 and FIX_CODE_SEGMENT_PERMISSIONS:
EfiTools.set_text_segment_rwe()
EfiToolsPlugin.executed = True
EfiTools.do_the_magic()
@staticmethod
def term():
unregister_action(SyncLocalTypes.name)
unregister_action(PrintEfiGuid.name)
def PLUGIN_ENTRY():
return EfiToolsPlugin()