-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_custdata.py
More file actions
237 lines (214 loc) · 7.74 KB
/
ex_custdata.py
File metadata and controls
237 lines (214 loc) · 7.74 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
from __future__ import print_function
# -----------------------------------------------------------------------
# This is an example illustrating how to use custom data types in Python
# (c) Hex-Rays
#
from idaapi import data_type_t, data_format_t, NW_OPENIDB, NW_CLOSEIDB, NW_TERMIDA, NW_REMOVE, COLSTR
import struct
import ctypes
import platform
# -----------------------------------------------------------------------
class pascal_data_type(data_type_t):
def __init__(self):
data_type_t.__init__(
self,
"py_pascal_string",
2,
"Pascal string",
None,
"pstr")
def calc_item_size(self, ea, maxsize):
# Custom data types may be used in structure definitions. If this case
# ea is a member id. Check for this situation and return 1
if idaapi.is_member_id(ea):
return 1
# get the length byte
n = idaapi.get_byte(ea)
# string too big?
if n > maxsize:
return 0
# ok, accept the string
return n + 1
class pascal_data_format(data_format_t):
FORMAT_NAME = "py_pascal_string_pstr"
def __init__(self):
data_format_t.__init__(
self,
pascal_data_format.FORMAT_NAME)
def printf(self, value, current_ea, operand_num, dtid):
# Take the length byte
n = ord(value[0])
o = ['"']
for ch in value[1:]:
b = ord(ch)
if b < 0x20 or b > 128:
o.append(r'\x%02x' % ord(ch))
else:
o.append(ch)
o.append('"')
return "".join(o)
# -----------------------------------------------------------------------
class simplevm_data_type(data_type_t):
ASM_KEYWORD = "svm_emit"
def __init__(
self,
name="py_simple_vm",
value_size=1,
menu_name="SimpleVM",
asm_keyword=ASM_KEYWORD):
data_type_t.__init__(
self,
name,
value_size,
menu_name,
None,
asm_keyword)
def calc_item_size(self, ea, maxsize):
if idaapi.is_member_id(ea):
return 1
# get the opcode and see if it has an imm
n = 5 if (idaapi.get_byte(ea) & 3) == 0 else 1
# string too big?
if n > maxsize:
return 0
# ok, accept
return n
class simplevm_data_format(data_format_t):
def __init__(
self,
name="py_simple_vm_format",
menu_name="SimpleVM"):
data_format_t.__init__(
self,
name,
0,
menu_name)
# Some tables for the disassembler
INST = {1: 'add', 2: 'mul', 3: 'sub', 4: 'xor', 5: 'mov'}
REGS = {1: 'r1', 2: 'r2', 3: 'r3'}
def disasm(self, inst):
"""A simple local disassembler. In reality one can use a full-blown disassembler to render the text"""
opbyte = ord(inst[0])
op = opbyte >> 4
if not (1<=op<=5):
return None
r1 = (opbyte & 0xf) >> 2
r2 = opbyte & 3
sz = 0
if r2 == 0:
if len(inst) != 5:
return None
imm = struct.unpack_from('L', inst, 1)[0]
sz = 5
else:
imm = None
sz = 1
text = "%s %s, %s" % (
COLSTR(simplevm_data_format.INST[op], idaapi.SCOLOR_INSN),
COLSTR(simplevm_data_format.REGS[r1], idaapi.SCOLOR_REG),
COLSTR("0x%08X" % imm, idaapi.SCOLOR_NUMBER) if imm is not None else COLSTR(simplevm_data_format.REGS[r2], idaapi.SCOLOR_REG))
return (sz, text)
def printf(self, value, current_ea, operand_num, dtid):
r = self.disasm(value)
if not r:
return None
if dtid == 0:
return "%s(%s)" % (simplevm_data_type.ASM_KEYWORD, r[1])
return r[1]
# -----------------------------------------------------------------------
# This format will display DWORD values as MAKE_DWORD(0xHI, 0xLO)
class makedword_data_format(data_format_t):
def __init__(self):
data_format_t.__init__(
self,
"py_makedword",
4,
"Make DWORD")
def printf(self, value, current_ea, operand_num, dtid):
if len(value) != 4: return None
w1 = struct.unpack_from("H", value, 0)[0]
w2 = struct.unpack_from("H", value, 2)[0]
return "MAKE_DWORD(0x%04X, 0x%04X)" % (w2, w1)
# -----------------------------------------------------------------------
# This format will try to load a resource string given a number
# So instead of displaying:
# push 66h
# call message_box_from_rsrc_string
# It can be rendered as;
# push RSRC("The message")
# call message_box_from_rsrc_string
#
# The get_rsrc_string() is not optimal since it loads/unloads the
# DLL each time for a new string. It can be improved in many ways.
class rsrc_string_format(data_format_t):
def __init__(self):
data_format_t.__init__(
self,
"py_w32rsrcstring",
1,
"Resource string")
self.cache_node = idaapi.netnode("$ py_w32rsrcstring", 0, 1)
def get_rsrc_string(self, fn, id):
"""
Simple method that loads the input file as a DLL with LOAD_LIBRARY_AS_DATAFILE flag.
It then tries to LoadString()
"""
k32 = ctypes.windll.kernel32
u32 = ctypes.windll.user32
hinst = k32.LoadLibraryExA(fn, 0, 0x2)
if hinst == 0:
return ""
buf = ctypes.create_string_buffer(1024)
r = u32.LoadStringA(hinst, id, buf, 1024-1)
k32.FreeLibrary(hinst)
return buf.value if r else ""
def printf(self, value, current_ea, operand_num, dtid):
# Is it already cached?
val = self.cache_node.supval(current_ea)
# Not cached?
if val == None:
# Retrieve it
num = idaapi.struct_unpack(value)
val = self.get_rsrc_string(idaapi.get_input_file_path(), num)
# Cache it
self.cache_node.supset(current_ea, val)
# Failed to retrieve?
if val == "" or val == "\x00":
return None
# Return the format
return "RSRC_STR(\"%s\")" % COLSTR(val, idaapi.SCOLOR_IMPNAME)
# -----------------------------------------------------------------------
# Table of formats and types to be registered/unregistered
# If a tuple has one element then it is the format to be registered with dtid=0
# If the tuple has more than one element, the tuple[0] is the data type and tuple[1:] are the data formats
new_formats = [
(pascal_data_type(), pascal_data_format()),
(simplevm_data_type(), simplevm_data_format()),
(makedword_data_format(),),
(simplevm_data_format(),),
]
try:
if platform.system() == 'Windows':
new_formats.append((rsrc_string_format(),))
except:
pass
# -----------------------------------------------------------------------
def nw_handler(code, old=0):
# delete notifications
if code == NW_OPENIDB:
if not idaapi.register_data_types_and_formats(new_formats):
print("Failed to register types!")
elif code == NW_CLOSEIDB:
idaapi.unregister_data_types_and_formats(new_formats)
elif code == NW_TERMIDA:
idaapi.notify_when(NW_TERMIDA | NW_OPENIDB | NW_CLOSEIDB | NW_REMOVE, nw_handler)
# -----------------------------------------------------------------------
# Check if already installed
if idaapi.find_custom_data_type(pascal_data_format.FORMAT_NAME) == -1:
if not idaapi.register_data_types_and_formats(new_formats):
print("Failed to register types!")
else:
idaapi.notify_when(NW_TERMIDA | NW_OPENIDB | NW_CLOSEIDB, nw_handler)
print("Formats installed!")
else:
print("Formats already installed!")