70 lines
2.3 KiB
Python
Executable File
70 lines
2.3 KiB
Python
Executable File
"""
|
|
(c) Immunity, Inc. 2004-2008
|
|
|
|
|
|
U{Immunity Inc.<http://www.immunityinc.com>}
|
|
|
|
findloop
|
|
|
|
"""
|
|
|
|
|
|
from immlib import *
|
|
from immutils import *
|
|
import getopt
|
|
|
|
DESC=""" Find natural loops given a function start address """
|
|
|
|
def usage(imm):
|
|
imm.Log("!findloop -a <address>")
|
|
imm.Log("-a (function start address)")
|
|
imm.Log("-h This help")
|
|
return "Errror!"
|
|
|
|
|
|
def main(args):
|
|
imm = Debugger()
|
|
try:
|
|
opts,argo = getopt.getopt(args, "a:")
|
|
except:
|
|
return usage(imm)
|
|
for o,a in opts:
|
|
if o == "-a":
|
|
loops = imm.findLoops(int(a,16))
|
|
for loop in loops:
|
|
imm.Log("LOOP! from:0x%08x, to:0x%08x"%(loop[0],loop[1]),loop[0])
|
|
|
|
func = imm.getFunction(int(a,16))
|
|
bbs = func.getBasicBlocks()
|
|
|
|
#find first and last node
|
|
first = 0xffffffff
|
|
last = 0
|
|
for node in loop[2]:
|
|
if node < first: first = node
|
|
if node > last: last = node
|
|
|
|
#mark loop nodes, but NOT change anything if there's any kind of comment
|
|
for node in loop[2]:
|
|
imm.Log(" Loop node:0x%08x"%node,node)
|
|
for bb in bbs:
|
|
if bb.getStart() == node:
|
|
instrs = bb.getInstructions(imm)
|
|
for op in instrs:
|
|
if not imm.getComment(op.getAddress()) and op.getAddress() != node:
|
|
if node == last and op.getAddress() == instrs[-1].getAddress():
|
|
#last instruction of last node
|
|
imm.setComment(op.getAddress(), "/")
|
|
else:
|
|
imm.setComment(op.getAddress(), "|")
|
|
|
|
if not imm.getComment(node):
|
|
if node == first:
|
|
imm.setComment(node, "\ Loop 0x%08X Node"%(loop[0]))
|
|
else:
|
|
imm.setComment(node, "| Loop 0x%08X Node"%(loop[0]))
|
|
|
|
return "Done!"
|
|
if o =="-h":
|
|
return usage(imm)
|