-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathida_shims.py
More file actions
790 lines (565 loc) · 16.9 KB
/
Copy pathida_shims.py
File metadata and controls
790 lines (565 loc) · 16.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
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
# Shim file to support IDA 6.x-7.3 and 7.5+
# Documentation provided by Hex-Rays:
# https://hex-rays.com/products/ida/support/ida74_idapython_no_bc695_porting_guide.html
import idc
import idaapi
try:
import ida_bytes
except ImportError:
ida_bytes = None
try:
import ida_name
except ImportError:
ida_name = None
try:
import ida_kernwin
except ImportError:
ida_kernwin = None
try:
import ida_nalt
except ImportError:
ida_nalt = None
try:
import ida_ua
except ImportError:
ida_ua = None
try:
import ida_funcs
except ImportError:
ida_funcs = None
def _get_fn_by_version(lib, curr_fn, archive_fn, archive_lib=None):
'''
Determine which function should be called based on the version of IDA.
:param curr_fn: 7.X version of the function.
:param archive_fn: 6.X version of the function.
:param archive_lib: If the archive lib is different than the current lib,
set it here.
:return: Function based on the version of IDA.
'''
if idaapi.IDA_SDK_VERSION >= 700:
try:
return getattr(lib, curr_fn)
except AttributeError:
raise Exception('%s is not a valid function in %s' % (curr_fn,
lib))
use_lib = lib if archive_lib is None else archive_lib
try:
return getattr(use_lib, archive_fn)
except AttributeError:
raise Exception('%s is not a valid function in %s' % (archive_fn,
use_lib))
def print_insn_mnem(ea):
'''
Get instruction mnemonics.
:param ea: Linear address of the instruction.
:type ea: int
:return: Instruction mnemonic. "" if not instruction is found.
:note: *Heavy breath* This function may not return exactly the same
mnemonics as you see on the screen.
'''
fn = _get_fn_by_version(idc, 'print_insn_mnem', 'GetMnem')
return fn(ea)
def print_operand(ea, n):
'''
Get operand of an instruction or data.
:param ea: Linear address of the item.
:type ea: int
:param n: Number of operand: 0 - the first operand 1 - the second operand.
:type n: int
:return: The current text representation of operand or "".
'''
fn = _get_fn_by_version(idc, 'print_operand', 'GetOpnd')
return fn(ea, n)
def define_local_var(start, end, location, name):
'''
Create a local variable.
:param start: Start address range for the local variable.
:type start: int
:param end: End of address range for the local variable.
:type end: int
:param location: The variable location in the "[bp+xx]" form where xx is
a number. The location can also be specified as a
register name.
:type location: str
:param name: Name of the local variable.
:type name: str
:return: 1-ok, 0-failure
'''
fn = _get_fn_by_version(idc, 'define_local_var', 'MakeLocal')
return fn(start, end, location, name)
def find_func_end(ea):
'''
Determine a new function boundaries.
:param ea: Start address of the new function.
:type ea: int
:return: If a function already exists, then return its end address. If a
function end cannot be determine, the return BADADDR otherwise return the
end address of the new function.
'''
fn = _get_fn_by_version(idc, 'find_func_end', 'FindFuncEnd')
return fn(ea)
def is_code(flag):
'''
Does flag denote start of an instruction.
:param flag: Flag for an instruction.
:type flag: int
:return: True if flags indicate code, False otherwise.
'''
fn = _get_fn_by_version(ida_bytes, 'is_code', 'isCode', idaapi)
return fn(flag)
def get_full_flags(ea):
'''
Get flags value for address 'ea'
:param ea: Linear address.
:type ea: int
:return: 0 if flags not present in the program
'''
fn = _get_fn_by_version(ida_bytes, 'get_full_flags', 'getFlags', idaapi)
return fn(ea)
def get_name(ea):
'''
Get name at the specified address.
:param ea: Linear address
:type ea: int
:return: "" - byte has no name.
'''
fn = _get_fn_by_version(idc, 'get_name', 'Name')
if idaapi.IDA_SDK_VERSION > 700:
return fn(ea, ida_name.GN_VISIBLE)
return fn(ea)
def get_func_off_str(ea):
'''
Convert address to 'funcname+offset' string.
:param ea: Address to convert.
:type ea: int
:return: If the address belongs to a function then return a string formed as
'name+offset' where 'name' is a function name, 'offset' is offset within
the function else return null string.
'''
fn = _get_fn_by_version(idc, 'get_func_off_str', 'GetFuncOffset')
return fn(ea)
def jumpto(ea, opnum=-1, uijmp_flags=0x0001):
'''
Jump to the specified address.
:param ea: Destination
:type ea: int
:param opnum: -1: don't change the x coord.
:type opnum: int
:param uijmp_flags: Jump flags.
:type uijmp_flags: int
:return: success
'''
fn = _get_fn_by_version(ida_kernwin, 'jumpto', 'Jump', idc)
if idaapi.IDA_SDK_VERSION >= 700:
return fn(ea, opnum, uijmp_flags)
return fn(ea)
def ask_yn(default, format_str):
'''
Display a dialog box and get choice from "Yes", "No", "Cancel".
:param default: Default choice: one of Button IDs
:type default: int
:param format_str: The question in printf() style format.
:type format_str: str
:return: The selected button (one of Button IDs).
'''
fn = _get_fn_by_version(ida_kernwin, 'ask_yn', 'AskYN', idc)
return fn(default, format_str)
def ask_file(for_saving, default, dialog):
'''
Get file from user.
:param for_saving: File is for saving.
:type for_saving: int
:param default: File extension.
:type default: str
:param dialog: Dialog box to display to the user.
:type dialog: str
:return: file path.
'''
fn = _get_fn_by_version(ida_kernwin, 'ask_file', 'AskFile', idc)
return fn(for_saving, default, dialog)
def get_func_attr(ea, attr):
'''
Get a function attribute.
:param ea: Any address belonging to the function.
:type ea: int
:param attr: One of FUNCATTR_... constants
:return: BADADDR - error otherwise returns the attribute value.
'''
fn = _get_fn_by_version(idc, 'get_func_attr', 'GetFunctionAttr')
return fn(ea, attr)
def get_name_ea_simple(name):
'''
Get linear address of a name.
:param name: Name of program byte.
:type name: str
:return: Address of the name or BADADDR - No such name.
'''
fn = _get_fn_by_version(idc, 'get_name_ea_simple', 'LocByName')
return fn(name)
def next_head(ea, maxea=4294967295):
'''
Get next defined item (instruction or data) in the program.
:param ea: Linear address to start search from.
:type ea: int
:param maxea: The search will stop at the address maxea. maxea is not
included in the search range
:type maxea: int
:return: BADADDR - no (more) defined items
'''
fn = _get_fn_by_version(idc, 'next_head', 'NextHead')
return fn(ea, maxea)
def get_screen_ea():
'''
Return the linear address of the current screen location.
:return: Address of screen focus.
'''
fn = _get_fn_by_version(idc, 'get_screen_ea', 'ScreenEA')
return fn()
def choose_func(title):
'''
Ask the user to select a function.
:param title: Title of the dialog box.
:type title: str
:return: -1 user refused to select a function, otherwise function start addr
'''
fn = _get_fn_by_version(idc, 'choose_func', 'ChooseFunction')
return fn(title)
def ask_ident(default, prompt):
'''
Ask for a long text.
:param default: The default value.
:type default: str
:param prompt: The prompt value.
:type prompt: str
:return: None or the entered string.
'''
fn = _get_fn_by_version(ida_kernwin, 'ask_str', 'AskIdent', idc)
if idaapi.IDA_SDK_VERSION >= 700:
return fn(default, ida_kernwin.HIST_IDENT, prompt)
return fn(default, prompt)
def set_name(ea, name):
'''
Rename an address.
:param ea: Linear address.
:type ea: int
:param name: New name of address. If name == "" then delete old name.
:type name: str
:return: 1-ok, 0-failure
'''
fn = _get_fn_by_version(idc, 'set_name', 'MakeName')
if idaapi.IDA_SDK_VERSION >= 700:
return fn(ea, name, ida_name.SN_CHECK)
return fn(ea, name)
def get_wide_dword(ea):
'''
Get one wide word of the program at 'ea'
:param ea: linear address.
:type ea: int
:return: uint64
'''
fn = _get_fn_by_version(idc, 'get_wide_dword', 'Dword')
return fn(ea)
def get_strlit_contents(ea):
'''
Get string contents.
:param ea: Linear address.
:type ea: int
:return: String contents or empty string.
'''
fn = _get_fn_by_version(idc, 'get_strlit_contents', 'GetString')
return fn(ea)
def get_func_name(ea):
'''
Retrieve function name.
:param ea: Any address belonging to the function.
:type ea: int
:return: Null string if not found, otherwise the functions name.
'''
fn = _get_fn_by_version(idc, 'get_func_name', 'GetFunctionName')
return fn(ea)
def get_first_seg():
'''
Get first segment.
:return: Address of the start of the first segment or BADADDR if no
segments found.
'''
fn = _get_fn_by_version(idc, 'get_first_seg', 'FirstSeg')
return fn()
def get_segm_attr(segea, attr):
'''
Get segment attribute.
:param segea: Any address within the segment.
:type segea: int
:param attr: One of SEGATTR_... constants.
:type attr: int
:return: Segment attributes.
'''
fn = _get_fn_by_version(idc, 'get_segm_attr', 'GetSegmentAttr')
return fn(segea, attr)
def get_next_seg(ea):
'''
Get next segment.
:param ea: Linear address.
:type ea: int
:return: Start of the next segment or BADADDR
'''
fn = _get_fn_by_version(idc, 'get_next_seg', 'NextSeg')
return fn(ea)
def is_strlit(flags):
'''
Do flags indicate a string.
:param flags: Flags for address.
:type flags: int
:return: bool
'''
fn = _get_fn_by_version(ida_bytes, 'is_strlit', 'isASCII', idc)
return fn(flags)
def create_strlit(start, lenth):
'''
Convert to string literal and give a meaningful name.
:param start: Start ea.
:type start: int
:param lenth: Length of string, or 0 to determine dynamically.
:type lenth: int
:return: bool
'''
fn = _get_fn_by_version(ida_bytes, 'create_strlit', 'MakeStr', idc)
if idaapi.IDA_SDK_VERSION >= 700:
return fn(start, lenth, ida_nalt.STRTYPE_C)
return fn(start, idc.BADADDR)
def is_unknown(flags):
'''
Do flags indicate an unknown type.
:param flags: Flags for address.
:type flags: int
:return: bool
'''
fn = _get_fn_by_version(ida_bytes, 'is_unknown', 'isUnknown', idc)
return fn(flags)
def is_byte(flags):
'''
Do flags indicate a byte type.
:param flags: Flags for address.
:type flags: int
:return: bool
'''
fn = _get_fn_by_version(ida_bytes, 'is_byte', 'isByte', idc)
return fn(flags)
def create_dword(ea):
'''
Convert to data.
:param ea: Linear address .
:type ea: int
:return: bool
'''
fn = _get_fn_by_version(ida_bytes, 'create_data', 'MakeDword', idc)
if idaapi.IDA_SDK_VERSION >= 700:
return fn(ea, ida_bytes.FF_DWORD, 4, idaapi.BADADDR)
return fn(ea)
def op_plain_offset(ea, n, base):
'''
Convert operand to an offset.
:param ea: Linear address.
:type ea: int
:param n: Number of operands.
:type n: int
:param base: Base of the offset.
:type base: int
:return:
'''
fn = _get_fn_by_version(idc, 'op_plain_offset', 'OpOff')
return fn(ea, n, base)
def next_addr(ea):
'''
Get next address in the program.
:param ea: Linear address.
:type ea: int
:return: Next address or BADADDR
'''
fn = _get_fn_by_version(ida_bytes, 'next_addr', 'NextAddr', idc)
return fn(ea)
def can_decode(ea):
'''
Can the bytes at ea be decoded as an instruction?
:param ea: Linear address
:type ea: int
:return: bool
'''
fn = _get_fn_by_version(ida_ua, 'can_decode', 'decode_insn', idaapi)
return fn(ea)
def get_operands(insn):
'''
Get operands for the current address.
:return:
'''
if idaapi.IDA_SDK_VERSION >= 700:
return insn.ops
return idaapi.cmd.Operands
def get_canon_feature(insn):
'''
Get operands for the provided instruction.
:return:
'''
if idaapi.IDA_SDK_VERSION >= 700:
return insn.get_canon_feature()
return idaapi.cmd.get_canon_feature()
def get_segm_name(ea):
'''
Get name of a segment.
:param ea: Any address within the segment.
:type ea: int
:return: Segement name.
'''
fn = _get_fn_by_version(idc, 'get_segm_name', 'SegName')
return fn(ea)
def add_func(ea):
'''
Add a new function.
:param ea: Start address.
:type ea: int
:return: bool
'''
fn = _get_fn_by_version(ida_funcs, 'add_func', 'MakeFunction', idc)
return fn(ea)
def create_insn(ea):
'''
Create instruction.
:param ea: Linear address
:type ea: int
:return: bool
'''
fn = _get_fn_by_version(idc, 'create_insn', 'MakeCode')
return fn(ea)
def get_segm_end(ea):
'''
Get end address of a segment.
:param ea: Linear address
:type ea: int
:return: Address
'''
fn = _get_fn_by_version(idc, 'get_segm_end', 'SegEnd')
return fn(ea)
def get_segm_start(ea):
'''
Get start address of a segment.
:param ea: Linear address
:type ea: int
:return: Address
'''
fn = _get_fn_by_version(idc, 'get_segm_start', 'SegStart')
return fn(ea)
def decode_insn(ea):
"""
Decode instruction.
:param ea: Linear address.
:type ea: int
:return: Instruction at ea.
"""
fn = _get_fn_by_version(ida_ua, 'decode_insn', 'decode_insn', idaapi)
if idaapi.IDA_SDK_VERSION >= 700:
insn = ida_ua.insn_t()
fn(insn, ea)
return insn
fn(ea)
return idaapi.cmd
def get_bookmark(index):
"""
Get bookmark
:param index: Index of bookmark
:type index: int
:return: Address of bookmark
"""
fn = _get_fn_by_version(idc, 'get_bookmark', 'GetMarkedPos')
return fn(index)
def get_bookmark_desc(index):
"""
Get bookmark description.
:param index: Index of bookmark
:type index: int
:return:
"""
fn = _get_fn_by_version(idc, 'get_bookmark_desc', 'GetMarkComment')
return fn(index)
def set_color(ea, what, color):
"""
Set item color.
:param ea: Linear address.
:type ea: int
:param what: Type of the item, one of CIC_... contstants
:type what: int
:param color: New color code in RGB.
:type color: int
:return: bool
"""
fn = _get_fn_by_version(idc, 'set_color', 'SetColor')
return fn(ea, what, color)
def msg(message):
"""
Display a UTF-8 string in the message window.
:param message: Message to print.
:type message: str
:return: PyObject * (what?)
"""
fn = _get_fn_by_version(ida_kernwin, 'msg', 'Message', idc)
return fn(message)
def get_highlighted_identifier():
"""
Get currently highlighted text.
:return: Highlighted text or ""
"""
fn = _get_fn_by_version(ida_kernwin, 'get_highlight',
'get_highlighted_identifier', idaapi)
if idaapi.IDA_SDK_VERSION >= 700:
viewer = ida_kernwin.get_current_viewer()
highlight = fn(viewer)
if highlight and highlight[1]:
return highlight[0]
return fn()
def start_ea(obj):
"""
Return start ea for supplied object.
:param obj: Object to retrieve start ea.
:return: start ea.
"""
if not obj:
return None
try:
return obj.startEA
except AttributeError:
return obj.start_ea
def end_ea(obj):
"""
Return end ea for supplied object.
:param obj: Object to retrieve end ea.
:return: end ea.
"""
if not obj:
return None
try:
return obj.endEA
except AttributeError:
return obj.end_ea
def set_func_flags(ea, flags):
"""
Change function flags.
:param ea: Any address belonging to the function.
:type ea: int
:param flags: Flags to set.
:type flags: int
:return: 0 - ok
"""
fn = _get_fn_by_version(idc, 'set_func_attr', 'SetFunctionFlags')
if idaapi.IDA_SDK_VERSION >= 700:
return fn(ea, idc.FUNCATTR_FLAGS, flags)
return fn(ea, flags)
def get_func_flags(ea):
"""
Get function flags.
:param ea: Any address belonging to the function.
:type ea: int
:return: Flags
"""
fn = _get_fn_by_version(idc, 'get_func_attr', 'GetFunctionFlags')
if idaapi.IDA_SDK_VERSION >= 700:
return fn(ea, idc.FUNCATTR_FLAGS)
return fn(ea)