-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertDataCode.py
More file actions
606 lines (500 loc) · 21.9 KB
/
convertDataCode.py
File metadata and controls
606 lines (500 loc) · 21.9 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
import idc
import idaapi
import idautils
import ida_bytes
import ida_kernwin
import string
from nltk.corpus import words
import nltk
import ida_nalt
nltk.download("words")
import re
import ida_segment
try:
class SelectTop(idaapi.action_handler_t):
addr_top = 0
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
SelectTop.addr_top = idc.here()
print ("Select Top: ", hex(SelectTop.addr_top))
return SelectTop.addr_top
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class SelectBottom(idaapi.action_handler_t):
addr_bottom = 0
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
SelectBottom.addr_bottom = idc.here()
print ("Select Bottom: ", hex(SelectBottom.addr_bottom))
return SelectBottom.addr_bottom
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class ConvertData(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
if not addr_bottom or not addr_top:
print (addr_bottom, addr_top)
return 0
offset = addr_bottom % addr_top
addr_top = addr_top - offset
while addr_bottom != addr_top:
ida_bytes.del_items(addr_bottom)
addr_bottom = addr_bottom - 1
addr_bottom = SelectBottom.addr_bottom - 3
while addr_bottom != addr_top:
if(ida_bytes.create_dword(addr_bottom, 4)):
pass
else:
print ("create_dword: failed")
return 0
addr_bottom = addr_bottom - 4
return 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class ConvertArray4byte(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
if not addr_bottom or not addr_top:
print (addr_bottom, addr_top)
return 0
offset = addr_bottom % addr_top
addr_top = addr_top - offset
addr_bottom = SelectBottom.addr_bottom - 3
while addr_bottom >= addr_top:
if(ida_bytes.create_dword(addr_bottom, 4)):
pass
else:
print ("create_dword: failed")
return 0
addr_bottom = addr_bottom - 4
return 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class ConvertDataTop(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
print ("ConvertDataTop")
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
# if addr_top % 4 == 0:
# print addr_bottom, addr_top
# return 0
offset = addr_bottom % 4
addr_bottom = addr_bottom - offset
while addr_bottom > addr_top:
ida_bytes.del_items(addr_top)
addr_top = addr_top + 1
addr_top = SelectTop.addr_top
while addr_top <= addr_bottom:
if(ida_bytes.create_dword(addr_top, 4)):
pass
else:
print ("create_dword: failed")
return 0
addr_top = addr_top + 4
return 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class undefineData(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
print ("undefineData")
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
while addr_bottom > addr_top:
ida_bytes.del_items(addr_top)
addr_top = addr_top + 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class convertCodeThumb(idaapi.action_handler_t):
CODE = 2
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
print ("convertCode")
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
func_count = 0
code_count = 0
ea = addr_top
print ("\nLooking for undefined code starting at: %s:0x%X" % (idc.get_segm_name(ea), ea))
print ("Test nha")
print ("Addr TOP : " + hex(ea))
print ("Addr Bottom : " + hex(addr_bottom))
while ea <= addr_bottom:
try:
ida_bytes.del_items(ea)
if idc.get_segm_attr(ea, idc.SEGATTR_TYPE) == self.CODE:
if idc.get_func_name(ea) != '':
ea = idc.find_func_end(ea)
continue
else:
if idc.create_insn(ea):
code_count += 1
#if idc.MakeFunction(ea):
# print("Creat function success: " + hex(ea))
# func_count += 1
#else:
# print("Creat function fail: " + hex(ea))
except Exception as e:
print(e)
ea = ea + 2
print ("Created %d new functions and %d new code blocks\n" % (func_count, code_count))
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class findStrlit(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
self.english_words = set(words.words())
def make_strlit(self, start_addr, length):
ida_bytes.del_items(start_addr, length + 1)
if ida_bytes.create_strlit(start_addr, length + 1, ida_nalt.STRTYPE_C):
return True
else:
return False
def analyze_camel_case(self, text):
try:
filtered_words = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', text)
cnt = sum(1 for word in filtered_words if word.lower() in self.english_words)
if(cnt >= 1):
return True
else:
return False
except Exception as e:
return False
def split_words_dp(self, text, word_set):
dp = [None] * (len(text) + 1)
dp[0] = []
for i in range(1, len(text) + 1):
for j in range(i):
if dp[j] is not None and text[j:i] in word_set:
dp[i] = dp[j] + [text[j:i]]
break
return dp[-1]
def detect_format_strings(self, text):
pattern = r'%(?:\d+\$)?[+\-#0 ]?(?:\d+|\*)?(?:\.(?:\d+|\*))?[hlL]?[diuoxXfFeEgGaAcspn%]'
matches = re.findall(pattern, text)
print(matches)
return matches
def activate(self, ctx):
print("Find String")
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
ea = addr_top
stri = ''
start_addr = 0x00
while ea <= addr_bottom:
c = ida_bytes.get_byte(ea)
if (c == 0xa or c == 0x9 or c == 0xd or 31 < c < 126):
if(stri == ''):
# print(f"start 2: {hex(ea)}")
start_addr = ea
stri = stri + chr(c)
else:
if(c == 0x00):
# print(f"[{hex(ea)}]: hex({c}): start: {hex(start_addr)}")
if(c == 0x00 and start_addr != 0x00 and len(stri) >= 3):
if(len(stri) > 10):
self.make_strlit(start_addr, len(stri))
print(f"Found: {stri}")
elif(self.detect_format_strings(stri)):
self.make_strlit(start_addr, len(stri))
print(f"Found: {stri}")
elif(self.analyze_camel_case(stri)):
self.make_strlit(start_addr, len(stri))
print(f"Found: {stri}")
elif(self.split_words_dp(stri.lower(), self.english_words)):
self.make_strlit(start_addr, len(stri))
print(f"Found: {stri}")
else:
text = stri.lower()
text = re.sub(r'[@#!,.>=;-_%()/]', ' ', text)
text = re.sub(r'[^\w\s]', ' ', text)
text = re.sub(r'\s+', ' ', text).strip()
text = text.split(' ')
filtered_words = [word for word in text if len(word) >= 3]
meaningful_count = sum(1 for word in filtered_words if word.lower() in self.english_words)
if(meaningful_count >= 1):
self.make_strlit(start_addr, len(stri))
print(f"Found ({meaningful_count}): {filtered_words} {stri}")
stri = ''
ea = ea + 1
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class convertCode(idaapi.action_handler_t):
mode = 2
def __init__(self):
idaapi.action_handler_t.__init__(self)
self.english_words = set(words.words())
def activate(self, ctx):
print ("convertCode")
addr_bottom = SelectBottom.addr_bottom
addr_top = SelectTop.addr_top
func_count = 0
code_count = 0
ea = addr_top % self.mode + addr_top
insn = idaapi.insn_t()
print ("\nLooking for undefined code starting at: %s:0x%X" % (idc.get_segm_name(ea), ea))
print ("Test nha")
print ("Addr TOP : " + hex(ea))
print ("Addr Bottom : " + hex(addr_bottom))
while ea <= addr_bottom:
try:
if(idc.is_code(idc.get_full_flags(ea))):
ea = ea + self.mode
continue
next_ea = idc.get_item_end(ea)
print(f'{hex(ea)}')
if(idc.is_strlit(idc.get_full_flags(ea))):
size = ida_bytes.get_max_strlit_length(ea, ida_nalt.STRTYPE_C)
if(size >= 3 and size < 7):
text = ida_bytes.get_strlit_contents(ea, 4, ida_nalt.STRTYPE_C)
text = text.decode("utf-8")
text = text.lower()
text = re.sub(r'[@#!,.>=;-_%()/]', ' ', text)
text = re.sub(r'[^\w\s]', ' ', text)
text = re.sub(r'\s+', ' ', text).strip()
text = text.split(' ')
filtered_words = [word for word in text if len(word) >= 3]
meaningful_count = sum(1 for word in filtered_words if word.lower() in self.english_words)
if(meaningful_count < 1):
if idaapi.decode_insn(insn, ea) == 4:
ida_bytes.del_items(ea)
idaapi.split_sreg_range(ea, idaapi.str2reg("T"), 1, idaapi.SR_user)
ida_bytes.del_items(ea)
if idc.create_insn(ea):
code_count += 1
ea = ea + self.mode
continue
print(f"[{hex(ea)}]: ")
ea = next_ea + 1
continue
if idc.get_segm_attr(ea, idc.SEGATTR_TYPE) == ida_segment.SEG_CODE:
if idc.get_func_name(ea) != '':
ea = idc.find_func_end(ea)
continue
if (ea % self.mode != 0):
ea = ea + 1
continue
if idaapi.decode_insn(insn, ea) == 4:
ida_bytes.del_items(ea)
idaapi.split_sreg_range(ea, idaapi.str2reg("T"), 1, idaapi.SR_user)
ida_bytes.del_items(ea)
if idc.create_insn(ea):
code_count += 1
except Exception as e:
ida_bytes.create_data(ea, idc.FF_WORD, self.mode, idc.BADADDR)
print(e)
ea = ea + self.mode
print ("Created %d new functions and %d new code blocks\n" % (func_count, code_count))
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class convertStructData(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
print ("convertStructData")
i = SelectTop.addr_top
while i <= SelectBottom.addr_bottom:
ida_bytes.create_dword(i, 16)
i = i + 16
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class CopyDatatoSeg(idaapi.action_handler_t):
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
print ("CopyDatatoSeg")
addr_top = SelectTop.addr_top
addr_bottom = SelectBottom.addr_bottom
addr_save_seg = SelectPointSave.addr_save_seg
ida_kernwin.jumpto(addr_save_seg)
while addr_top < addr_bottom:
ida_bytes.patch_byte(addr_save_seg, ida_bytes.get_byte(addr_top))
addr_save_seg += 1
addr_top += 1
print ("Success")
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
class SelectPointSave(idaapi.action_handler_t):
addr_save_seg = 0
def __init__(self):
idaapi.action_handler_t.__init__(self)
def activate(self, ctx):
SelectPointSave.addr_save_seg = idc.here()
print ("Select Point Save Seg: ", hex(SelectPointSave.addr_save_seg))
return SelectPointSave.addr_save_seg
def update(self, ctx):
return idaapi.AST_ENABLE_ALWAYS
except AttributeError:
pass
class convertDataCode_t(idaapi.plugin_t, idaapi.UI_Hooks):
flags = 0
comment = ""
help = ""
wanted_name = "Convert Data or Code"
wanted_hotkey = ""
select_point_top = 'selecttop:action'
select_point_bottom = 'selectbottom:action'
convert_data = 'convertdata:action'
convert_data_top = 'convertdatatop:action'
undefine_data = 'undefinedata:action'
convert_code_top = 'convertcode:action'
find_string = 'find_string:action'
convert_code_thumb_top = 'convertcodethumb:action'
convert_struct_data = 'convertstructdata:action'
copy_data_to_segment = 'copydatatoseg:action'
select_point_save_seg = 'selectpointsave:action'
top_hotkey = 'Ctrl+['
bottom_hotkey = 'Ctrl+]'
convertdata_hotkey = 'Ctrl+;'
convertdata_hotkey_top = 'Ctrl+\''
undefinedata_hotkey = 'Ctrl+-'
convert_code_top_hotkey = 'Ctrl+='
find_string_top_hotkey = 'Ctrl+s'
convert_code_top_thumb_hotkey = 'Ctrl+=='
convert_struct_data_hotkey = 'Ctrl+\\'
copy_data_to_segment_hotkey = 'Ctrl+>'
select_point_save_hotkey = 'Ctrl+<'
menu_tab = 'Options/SlConvert/'
addr_top = 0
addr_bottom = 0
def init(self):
super(convertDataCode_t, self).__init__()
top_desc = idaapi.action_desc_t(
self.select_point_top, # action name uniqure
'Select Top', # show text
SelectTop(), # function exceute
self.top_hotkey,
'Select Top',
198)
bottom_desc = idaapi.action_desc_t(
self.select_point_bottom,
'Select Bottom',
SelectBottom(),
self.bottom_hotkey,
'Select Bottom',
198,
)
convert_data_desc = idaapi.action_desc_t(
self.convert_data,
'Convert Data',
ConvertData(),
self.convertdata_hotkey,
'Convert Data',
198,
)
convert_data_top_desc = idaapi.action_desc_t(
self.convert_data_top,
'Convert Data Top',
ConvertDataTop(),
self.convertdata_hotkey_top,
'Convert Data Top',
198,
)
undefine_data_top_desc = idaapi.action_desc_t(
self.undefine_data,
'undefine Data',
undefineData(),
self.undefinedata_hotkey,
'undefine Data',
198,
)
convert_code_top_desc = idaapi.action_desc_t(
self.convert_code_top,
'convert code',
convertCode(),
self.convert_code_top_hotkey,
'convert code',
198,
)
convert_code_thumb_top_desc = idaapi.action_desc_t(
self.convert_code_thumb_top,
'convert code thumb',
convertCodeThumb(),
self.convert_code_top_thumb_hotkey,
'convert code',
198,
)
convert_struct_data_top_desc = idaapi.action_desc_t(
self.convert_struct_data,
'convert struct data',
convertStructData(),
self.convert_struct_data_hotkey,
'convert struct data',
198,
)
copy_data_to_seg_desc = idaapi.action_desc_t(
self.copy_data_to_segment,
'copy data to seg',
CopyDatatoSeg(),
self.copy_data_to_segment_hotkey,
'copy data to seg',
198,
)
select_point_save_seg_desc = idaapi.action_desc_t(
self.select_point_save_seg,
'select point save',
SelectPointSave(),
self.select_point_save_hotkey,
'select point save',
198,
)
find_string_top_desc = idaapi.action_desc_t(
self.find_string,
'Find String',
findStrlit(),
self.find_string_top_hotkey,
'Find String',
198,
)
idaapi.register_action(top_desc)
idaapi.register_action(bottom_desc)
idaapi.register_action(convert_data_desc)
idaapi.register_action(convert_data_top_desc)
idaapi.register_action(undefine_data_top_desc)
idaapi.register_action(convert_code_top_desc)
idaapi.register_action(find_string_top_desc)
idaapi.register_action(convert_code_thumb_top_desc)
idaapi.register_action(convert_struct_data_top_desc)
idaapi.register_action(copy_data_to_seg_desc)
idaapi.register_action(select_point_save_seg_desc)
idaapi.attach_action_to_menu(self.menu_tab, self.select_point_top, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.select_point_bottom, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.convert_data, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.convert_data_top, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.undefine_data, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.convert_code_top, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.convert_code_thumb_top, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.convert_struct_data, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.copy_data_to_segment, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.select_point_save_seg, idaapi.SETMENU_APP)
idaapi.attach_action_to_menu(self.menu_tab, self.find_string, idaapi.SETMENU_APP)
return idaapi.PLUGIN_KEEP
def term(self):
idaapi.detach_action_from_menu(self.menu_tab, self.select_point_top)
idaapi.detach_action_from_menu(self.menu_tab, self.select_point_bottom)
idaapi.detach_action_from_menu(self.menu_tab, self.convert_data)
idaapi.detach_action_from_menu(self.menu_tab, self.convert_data_top)
idaapi.detach_action_from_menu(self.menu_tab, self.undefine_data)
idaapi.detach_action_from_menu(self.menu_tab, self.convert_code_top)
idaapi.detach_action_from_menu(self.menu_tab, self.convert_code_thumb_top)
idaapi.detach_action_from_menu(self.menu_tab, self.find_string)
idaapi.detach_action_from_menu(self.menu_tab, self.convert_struct_data)
idaapi.detach_action_from_menu(self.menu_tab, self.copy_data_to_segment)
idaapi.detach_action_from_menu(self.menu_tab, self.select_point_save_seg)
return None
def run(self, arg):
pass
def PLUGIN_ENTRY():
return convertDataCode_t()