Пост принадлежит автору partoftheworlD, GamehackLab[RU]. (ref)
Подключение к питону происходит с помощью:
import idc
import idautils
import idaapi
У меня есть наработки некоторые, возможно что-то пригодится.
import idc
import idaapi
class Func(object):
def __init__(self):
self.reg_value = re.compile(r'')
self.stack_view = []
self.level = 1
def range_reverse(self, start, stop):
for i in FuncItems(stop):
if start > i >= stop:
yield i
def run(self):
if self.stack_view:
for j in self.stack_view:
current_function = idaapi.get_func(j)
level = "-" * self.level
print("{} Jump into -> 0x{}".format(level, hex(current_function.startEA)[2:].replace("L", "").upper()))
for j in [i for i in self.range_reverse(current_function.endEA, current_function.startEA)]:
print("{} 0x{} -- {}".format(level, hex(j)[2:].replace("L", "").upper(), idc.GetDisasm(j)))
self.level += 1
else:
print "Please input addresses to stack view buffer"
pass
if __name__ == '__main__':
f = Func()
f.stack_view = [0x214FE, 0x20F30, 0x214E0, 0x13F00]
f.run()
В выводе получим
\- Jump into -> 0x214E0
\- 0x214E0 -- push ebp
\- 0x214E1 -- mov ebp, esp
\- 0x214E3 -- lea esp, [esp-10h]
\- 0x214E7 -- mov [ebp+var_C], eax
\- 0x214EA -- mov [ebp+var_4], edx
\- 0x214ED -- mov [ebp+var_8], ecx
\- 0x214F0 -- mov eax, [ebp+arg_0]
\- 0x214F3 -- test eax, eax
\- 0x214F5 -- jnz locret_21589
\- 0x214FB -- mov eax, [ebp+var_8]
\- 0x214FE -- cmp word ptr [eax], 1Bh
\- 0x21503 -- jnz locret_21589
\- 0x21509 -- push 0
\- 0x2150B -- mov eax, [ebp+var_8]
\- 0x2150E -- movzx ecx, word ptr [eax]
\- 0x21511 -- mov eax, [ebp+var_4]
\- 0x21514 -- mov edx, 0B01Eh
\- 0x21519 -- call sub_8E870
\- 0x2151E -- test eax, eax
\- 0x21520 -- jnz short locret_21589
\- 0x21522 -- push 0
\- 0x21524 -- mov eax, [ebp+var_4]
\- 0x21527 -- mov ecx, 0
\- 0x2152C -- mov edx, 87h ; '‡'
\- 0x21531 -- call sub_8E870
\- 0x21536 -- and eax, 4
\- 0x21539 -- jnz short locret_21589
\- 0x2153B -- mov eax, [ebp+var_C]
\- 0x2153E -- test dword ptr [eax+160h], 4
\- 0x21548 -- jz short locret_21589
\- 0x2154A -- mov eax, [ebp+var_4]
\- 0x2154D -- mov dl, 1
\- 0x2154F -- call sub_13F00
\- 0x21554 -- mov [ebp+var_10], eax
\- 0x21557 -- test eax, eax
\- 0x21559 -- jz short locret_21589
\- 0x2155B -- mov eax, [ebp+var_10]
\- 0x2155E -- cmp dword ptr [eax+378h], 0
\- 0x21565 -- jz short locret_21589
\- 0x21567 -- mov eax, [ebp+var_10]
\- 0x2156A -- mov eax, [eax+378h]
\- 0x21570 -- mov edx, [ebp+var_10]
\- 0x21573 -- mov edx, [edx+378h]
\- 0x21579 -- mov edx, [edx]
\- 0x2157B -- call dword ptr [edx+2E8h]
\- 0x21581 -- mov eax, [ebp+var_8]
\- 0x21584 -- mov word ptr [eax], 0
\- 0x21589 -- leave
\- 0x2158A -- retn 4
\-- Jump into -> 0x20F30
\-- 0x20F30 -- push ebp
\-- 0x20F31 -- mov ebp, esp
\-- 0x20F33 -- lea esp, [esp-10h]
\-- 0x20F37 -- mov [ebp+var_C], eax
\-- 0x20F3A -- mov [ebp+var_4], edx
\-- 0x20F3D -- mov [ebp+var_8], ecx
\-- 0x20F40 -- mov eax, [ebp+var_8]
\-- 0x20F43 -- cmp word ptr [eax], 0
\-- 0x20F48 -- jz locret_20FCF
\-- 0x20F4E -- mov edx, [ebp+var_4]
\-- 0x20F51 -- mov eax, 5C29B4h
\-- 0x20F56 -- call sub_C910
\-- 0x20F5B -- test al, al
\-- 0x20F5D -- jz short loc_20FC3
\-- 0x20F5F -- mov eax, [ebp+var_4]
\-- 0x20F62 -- mov [ebp+var_10], eax
\-- 0x20F65 -- mov eax, [ebp+var_C]
\-- 0x20F68 -- cmp word ptr [eax+150h], 0
\-- 0x20F71 -- jz short locret_20FCF
\-- 0x20F73 -- mov eax, [ebp+var_C]
\-- 0x20F76 -- mov edx, [ebp+var_8]
\-- 0x20F79 -- mov ax, [eax+150h]
\-- 0x20F80 -- cmp ax, [edx]
\-- 0x20F83 -- jnz short locret_20FCF
\-- 0x20F85 -- mov eax, [ebp+var_C]
\-- 0x20F88 -- mov eax, [eax+154h]
\-- 0x20F8E -- cmp eax, [ebp+arg_0]
\-- 0x20F91 -- jnz short locret_20FCF
\-- 0x20F93 -- mov eax, [ebp+var_C]
\-- 0x20F96 -- mov eax, [eax+14Ch]
\-- 0x20F9C -- cmp eax, [ebp+var_10]
\-- 0x20F9F -- jnz short locret_20FCF
\-- 0x20FA1 -- push [ebp+arg_0]
\-- 0x20FA4 -- mov ecx, [ebp+var_8]
\-- 0x20FA7 -- mov edx, [ebp+var_10]
\-- 0x20FAA -- mov eax, [ebp+var_C]
\-- 0x20FAD -- call sub_21590
\-- 0x20FB2 -- push [ebp+arg_0]
\-- 0x20FB5 -- mov ecx, [ebp+var_8]
\-- 0x20FB8 -- mov edx, [ebp+var_10]
\-- 0x20FBB -- mov eax, [ebp+var_C]
\-- 0x20FBE -- call sub_214E0
\-- 0x20FC3 -- mov eax, [ebp+var_C]
\-- 0x20FC6 -- mov word ptr [eax+150h], 0
\-- 0x20FCF -- leave
\-- 0x20FD0 -- retn 4
\--- Jump into -> 0x214E0
\--- 0x214E0 -- push ebp
\--- 0x214E1 -- mov ebp, esp
\--- 0x214E3 -- lea esp, [esp-10h]
\--- 0x214E7 -- mov [ebp+var_C], eax
\--- 0x214EA -- mov [ebp+var_4], edx
\--- 0x214ED -- mov [ebp+var_8], ecx
\--- 0x214F0 -- mov eax, [ebp+arg_0]
\--- 0x214F3 -- test eax, eax
\--- 0x214F5 -- jnz locret_21589
\--- 0x214FB -- mov eax, [ebp+var_8]
\--- 0x214FE -- cmp word ptr [eax], 1Bh
\--- 0x21503 -- jnz locret_21589
\--- 0x21509 -- push 0
\--- 0x2150B -- mov eax, [ebp+var_8]
\--- 0x2150E -- movzx ecx, word ptr [eax]
\--- 0x21511 -- mov eax, [ebp+var_4]
\--- 0x21514 -- mov edx, 0B01Eh
\--- 0x21519 -- call sub_8E870
\--- 0x2151E -- test eax, eax
\--- 0x21520 -- jnz short locret_21589
\--- 0x21522 -- push 0
\--- 0x21524 -- mov eax, [ebp+var_4]
\--- 0x21527 -- mov ecx, 0
\--- 0x2152C -- mov edx, 87h ; '‡'
\--- 0x21531 -- call sub_8E870
\--- 0x21536 -- and eax, 4
\--- 0x21539 -- jnz short locret_21589
\--- 0x2153B -- mov eax, [ebp+var_C]
\--- 0x2153E -- test dword ptr [eax+160h], 4
\--- 0x21548 -- jz short locret_21589
\--- 0x2154A -- mov eax, [ebp+var_4]
\--- 0x2154D -- mov dl, 1
\--- 0x2154F -- call sub_13F00
\--- 0x21554 -- mov [ebp+var_10], eax
\--- 0x21557 -- test eax, eax
\--- 0x21559 -- jz short locret_21589
\--- 0x2155B -- mov eax, [ebp+var_10]
\--- 0x2155E -- cmp dword ptr [eax+378h], 0
\--- 0x21565 -- jz short locret_21589
\--- 0x21567 -- mov eax, [ebp+var_10]
\--- 0x2156A -- mov eax, [eax+378h]
\--- 0x21570 -- mov edx, [ebp+var_10]
\--- 0x21573 -- mov edx, [edx+378h]
\--- 0x21579 -- mov edx, [edx]
\--- 0x2157B -- call dword ptr [edx+2E8h]
\--- 0x21581 -- mov eax, [ebp+var_8]
\--- 0x21584 -- mov word ptr [eax], 0
\--- 0x21589 -- leave
\--- 0x2158A -- retn 4
\---- Jump into -> 0x13F00
\---- 0x13F00 -- push ebp
\---- 0x13F01 -- mov ebp, esp
\---- 0x13F03 -- lea esp, [esp-0Ch]
\---- 0x13F07 -- mov [ebp+var_4], eax
\---- 0x13F0A -- mov [ebp+var_8], dl
\---- 0x13F0D -- jmp short loc_13F33
\---- 0x13F10 -- cmp [ebp+var_8], 0
\---- 0x13F14 -- jnz short loc_13F27
\---- 0x13F16 -- mov edx, [ebp+var_4]
\---- 0x13F19 -- mov eax, 59ECF0h
\---- 0x13F1E -- call sub_C910
\---- 0x13F23 -- test al, al
\---- 0x13F25 -- jnz short loc_13F45
\---- 0x13F27 -- mov eax, [ebp+var_4]
\---- 0x13F2A -- mov eax, [eax+1F8h]
\---- 0x13F30 -- mov [ebp+var_4], eax
\---- 0x13F33 -- cmp [ebp+var_4], 0
\---- 0x13F37 -- jz short loc_13F45
\---- 0x13F39 -- mov eax, [ebp+var_4]
\---- 0x13F3C -- cmp dword ptr [eax+1F8h], 0
\---- 0x13F43 -- jnz short loc_13F10
\---- 0x13F45 -- mov edx, [ebp+var_4]
\---- 0x13F48 -- mov eax, 59ECF0h
\---- 0x13F4D -- call sub_C910
\---- 0x13F52 -- test al, al
\---- 0x13F54 -- jz short loc_13F5E
\---- 0x13F56 -- mov eax, [ebp+var_4]
\---- 0x13F59 -- mov [ebp+var_C], eax
\---- 0x13F5C -- jmp short loc_13F65
\---- 0x13F5E -- mov [ebp+var_C], 0
\---- 0x13F65 -- mov eax, [ebp+var_C]
\---- 0x13F68 -- leave
\---- 0x13F69 -- retn
Надо будет поправить пару строк, чтобы выводилась не функция целиком, а как при трассировке:
for j in [i for i in self.range_reverse(current_function.endEA, current_function.startEA)]:
https://raw.githubusercontent.com/EiNSTeiN-/idapython/master/examples/ex_graph.py