I need to convert into op code bytes the instructions that I have disassembled but I can’t find a function that lets me do it, I’ve tried idc.get_bytes but it doesn’t seem to work.
This is my python script:
JavaScript
x
32
32
1
import sys
2
import idc
3
import idautils
4
5
f = open(idc.ARGV[1], 'w') if len(idc.ARGV) > 1 else sys.stdout
6
log = f.write
7
8
# log current file path
9
log(idc.get_input_file_path() + 'n')
10
11
# wait for auto-analysis to complete
12
idc.auto_wait()
13
14
# count functions
15
log( 'count %dn' % len(list(idautils.Functions())) )
16
17
for func in idautils.Functions():
18
flags = idc.get_func_attr(func, FUNCATTR_FLAGS)
19
if flags & FUNC_LIB or flags & FUNC_THUNK:
20
continue
21
dism_addr = list(idautils.FuncItems(func))
22
for line in dism_addr:
23
#log(idc.print_insn_mnem(line) + 'n' )
24
disass = idc.generate_disasm_line(line, 0)
25
log(disass + 'n' )
26
27
# if logging to a file, close it and exit IDA Pro
28
if f != sys.stdout:
29
f.close()
30
idc.qexit(0)
31
32
I’m using this script with the batch mode of IDA Pro 7.7sp1, can you suggest me a method to do it? Thank you in advance.
Advertisement
Answer
So, something like this?
JavaScript
1
29
29
1
def GetFuncHeads(funcea=None):
2
"""
3
Get all heads in a function
4
5
@param funcea: any address in the function
6
"""
7
func = ida_funcs.get_func(funcea)
8
if not func:
9
return []
10
else:
11
funcea = func.start_ea
12
13
ea = funcea
14
15
heads = []
16
for start, end in idautils.Chunks(funcea):
17
heads.extend([head for head in idautils.Heads(start, end)])
18
19
return heads
20
21
def GetInsnLen(ea):
22
insn = ida_ua.insn_t()
23
inslen = ida_ua.decode_insn(insn, ea)
24
if inslen:
25
return inslen
26
return 0
27
28
opcodes = [idc.get_bytes(ea, GetInsnLen(ea)) for ea in GetFuncHeads(here())]
29