generated from Vector35/sample_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
353 lines (279 loc) · 10.7 KB
/
__init__.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
from struct import unpack
# capstone/keystone stuff
# binaryninja
# Qt stuff
# capstone/keystone stuff
# binaryninja
import pyperclip
# Qt stuff
from binaryninja import *
def u16(x): return unpack("<H", x)[0]
def u32(x): return unpack("<I", x)[0]
def u64(x): return unpack("<Q", x)[0]
def toDouble(x): return unpack("d", x)[0]
def toFloat(x): return unpack("f", x)[0]
def do_nothing(bv: BinaryView):
show_message_box("Do Nothing", "Congratulations! You have successfully done nothing.\n\n" +
"Pat yourself on the back.", MessageBoxButtonSet.OKButtonSet, MessageBoxIcon.ErrorIcon)
def preProcess(bv: BinaryView, start: int, size: int):
# print("startAddr:0x%x,length:0x%x" % (start, size))
end = start + size
selectRange = (start, end)
# print("selectRange:0x%x,0x%x" % (selectRange[0], selectRange[1]))
data = bv.read(selectRange[0], size)
if data:
print("\n[+] Dump 0x%X - 0x%X (%u bytes) :" %
(start, end, size))
return data
return b''
def convert2EscapedString(bv: BinaryView, start: int, size: int):
"""
\x11\x31\xf1
"""
data = preProcess(bv, start, size)
output = '"%s"' % "".join("\\x%02X" % b for b in data)
print(output)
pyperclip.copy(output)
def convert2Hex(bv: BinaryView, start: int, size: int):
"""
11ff2233dfe1
"""
data = preProcess(bv, start, size)
# hex string
output = "".join("%02X" % b for b in data)
print(output)
pyperclip.copy(output)
def convert2C_uint8_t(bv: BinaryView, start: int, size: int):
"""
Convert to C string
unsigned char data_434869[31] = {
0x48, 0x8B, 0x89, 0x00, 0x00, 0x00, 0x00, 0x48, 0x3B, 0x61, 0x10, 0x0F, 0x86, 0x34, 0x03, 0x00,
0x00, 0x48, 0x83, 0xEC, 0x50, 0x48, 0x89, 0x6C, 0x24, 0x48, 0x48, 0x8D, 0x6C, 0x24, 0x48
};
"""
data = preProcess(bv, start, size)
try:
name = bv.get_data_var_at(start).name
assert name
except:
name = "data_%X" % start
output = "uint8_t %s[%d] = {" % (name, size)
for i in range(size):
if i % 16 == 0:
output += "\n "
output += "0x%02X, " % data[i]
output = output[:-2] + "\n};"
print(output)
pyperclip.copy(output)
def convert2C_uint16_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
try:
name = bv.get_data_var_at(start).name
assert name
except:
name = "data_%X" % start
data += b"\x00"
array_size = (size + 1) // 2
output = "uint16_t %s[%d] = {" % (name, array_size)
for i in range(0, size, 2):
if i % 16 == 0:
output += "\n "
output += "0x%04X, " % u16(data[i:i + 2])
output = output[:-2] + "\n};"
print(output)
pyperclip.copy(output)
def convert2C_uint32_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
try:
name = bv.get_data_var_at(start).name
assert name
except:
name = "data_%X" % start
# C array dword
data += b"\x00" * 3
array_size = (size + 3) // 4
output = "uint32_t %s[%d] = {" % (name, array_size)
for i in range(0, size, 4):
if i % 32 == 0:
output += "\n "
output += "0x%08X, " % u32(data[i:i + 4])
output = output[:-2] + "\n};"
print(output)
pyperclip.copy(output)
def convert2C_uint64_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
try:
name = bv.get_data_var_at(start).name
assert name
except:
name = "data_%X" % start
# C array qword
data += b"\x00" * 7
array_size = (size + 7) // 8
output = "uint64_t %s[%d] = {" % (name, array_size)
for i in range(0, size, 8):
if i % 32 == 0:
output += "\n "
output += "%#018X, " % u64(data[i:i + 8])
output = output[:-2] + "\n};"
output = output.replace("0X", "0x")
print(output)
pyperclip.copy(output)
def convert2Puint8_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list
output = "[%s]" % ", ".join("0x%02X" % b for b in data)
print(output)
pyperclip.copy(output)
def convert2Puint16_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list word
data += b"\x00"
output = "[%s]" % ", ".join("0x%04X" % u16(data[i:i + 2])
for i in range(0, size, 2))
print(output)
pyperclip.copy(output)
def convert2Puint32_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list dword
data += b"\x00" * 3
output = "[%s]" % ", ".join("0x%08X" % u32(data[i:i + 4])
for i in range(0, size, 4))
print(output)
pyperclip.copy(output)
def convert2Puint64_t(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list qword
data += b"\x00" * 7
output = "[%s]" % ", ".join("%#018X" % u64(data[i:i + 8]) for i in range(0, size, 8)).replace("0X",
"0x")
print(output)
pyperclip.copy(output)
def convert2Pdouble(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list double
data += b"\x00" * 7
output = "[%s]" % ", ".join("%.10f" % toDouble(data[i:i + 8]) for i in range(0, size, 8))
print(output)
pyperclip.copy(output)
def convert2Pfloat(bv: BinaryView, start: int, size: int):
data = preProcess(bv, start, size)
# python list float
data += b"\x00" * 3
output = "[%s]" % ", ".join("%.10f" % toFloat(data[i:i + 4]) for i in range(0, size, 4))
print(output)
pyperclip.copy(output)
def getNop(arch):
if arch in ['x16', 'x32', 'x64', 'x16att', 'x32att', 'x64att', 'x16nasm', 'x32nasm', 'x64nasm']:
return b'\x90'
elif arch in ['arm', 'armv8']:
return b'\x00\xf0\x20\xe3'
elif arch in ['armbe', 'armv8be']:
return b'\xe3\x20\xf0\x00'
elif arch in ['thumb', 'thumbv8']:
return b'\x00\xbf'
elif arch in ['thumbbe', 'thumbv8be']:
return b'\xbf\x00'
elif arch in ['arm64']:
return b'\x1f\x20\x03\xd5'
elif arch in ['hexagon']:
return b'\x00\xc0\x00\x7f'
elif arch in ['mips', 'mipsbe', 'mips64', 'mips64be']:
return b'\x00\x00\x00\x00'
elif arch in ['ppc64']:
return b'\x00\x00\x00\x60'
elif arch in ['ppc32be', 'ppc64be']:
return b'\x60\x00\x00\x00'
elif arch in ['sparc']:
return b'\x00\x00\x00\x01'
elif arch in ['sparcbe', 'sparc64be']:
return b'\x01\x00\x00\x00'
raise Exception('no NOP for architecture: %s' % arch)
binja_to_ks = {
'aarch64': 'arm64',
'armv7': 'arm',
'armv7eb': 'armbe',
'thumb2': 'thumb',
'thumb2eb': 'thumbbe',
'mipsel32': 'mips',
'mips32': 'mipsbe',
'ppc': 'ppc32be',
'ppc_le': 'ERROR',
'ppc64': 'ppc64be',
'ppc64_le': 'ppc64',
'sh4': 'ERROR',
'x86_16': 'x16nasm',
'x86': 'x32nasm',
'x86_64': 'x64nasm'
}
def nopSelection(bv: BinaryView, start: int, size: int):
nopByte = getNop(binja_to_ks[bv.arch.name])
bv.write(start, nopByte * (size // len(nopByte)))
print(
f"[+]nop {(size // len(nopByte)) * len(nopByte)} bytes at 0x{start:x}\n{hex(start)}~{hex((size // len(nopByte)) * len(nopByte))}")
def uiPreProcess(bv: BinaryView):
start = get_address_input("start_address", "start_address")
end = get_address_input("end_address:if yourInput==startAddr,next you will input length", "end_address")
# print(end)
# print(type(end))
if end != start:
size = end - start
else:
size = get_int_input("length:int type", "length")
return start, size
def uiConvert2EscapedString(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2EscapedString(bv, start, size)
def uiConvert2C_uint8_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2C_uint8_t(bv, start, size)
def uiConvert2C_uint16_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2C_uint16_t(bv, start, size)
def uiConvert2C_uint32_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2C_uint32_t(bv, start, size)
def uiConvert2C_uint64_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2C_uint64_t(bv, start, size)
def uiConvert2Puint8_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Puint8_t(bv, start, size)
def uiConvert2Puint16_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Puint16_t(bv, start, size)
def uiConvert2Puint32_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Puint32_t(bv, start, size)
def uiConvert2Puint64_t(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Puint64_t(bv, start, size)
def uiConvert2Pdouble(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Pdouble(bv, start, size)
def uiConvert2Pfloat(bv: BinaryView):
start, size = uiPreProcess(bv)
convert2Pfloat(bv, start, size)
PluginCommand.register_for_range("Convert\\EscapedString", "", convert2EscapedString)
PluginCommand.register_for_range("Convert\\uint8_t(c)", "", convert2C_uint8_t)
PluginCommand.register_for_range("Convert\\uint16_t(c)", "", convert2C_uint16_t)
PluginCommand.register_for_range("Convert\\uint32_t(c)", "", convert2C_uint32_t)
PluginCommand.register_for_range("Convert\\uint64_t(c)", "", convert2C_uint64_t)
PluginCommand.register_for_range("Convert\\uint8_t(python)", "", convert2Puint8_t)
PluginCommand.register_for_range("Convert\\uint16_t(python)", "", convert2Puint16_t)
PluginCommand.register_for_range("Convert\\uint32_t(python)", "", convert2Puint32_t)
PluginCommand.register_for_range("Convert\\uint64_t(python)", "", convert2Puint64_t)
PluginCommand.register_for_range("Convert\\double(python)", "", convert2Pdouble)
PluginCommand.register_for_range("Convert\\float(python)", "", convert2Pfloat)
PluginCommand.register_for_range("Convert\\nop", "", nopSelection)
PluginCommand.register("UIConvert\\float(python)", "", uiConvert2Pfloat)
PluginCommand.register("UIConvert\\double(python)", "", uiConvert2Pdouble)
PluginCommand.register("UIConvert\\uint64_t(python)", "", uiConvert2Puint64_t)
PluginCommand.register("UIConvert\\uint32_t(python)", "", uiConvert2Puint32_t)
PluginCommand.register("UIConvert\\uint16_t(python)", "", uiConvert2Puint16_t)
PluginCommand.register("UIConvert\\uint8_t(python)", "", uiConvert2Puint8_t)
PluginCommand.register("UIConvert\\uint64_t(c)", "", uiConvert2C_uint64_t)
PluginCommand.register("UIConvert\\uint32_t(c)", "", uiConvert2C_uint32_t)
PluginCommand.register("UIConvert\\uint16_t(c)", "", uiConvert2C_uint16_t)
PluginCommand.register("UIConvert\\uint8_t(c)", "", uiConvert2C_uint8_t)
PluginCommand.register("UIConvert\\EscapedString", "", uiConvert2EscapedString)