2007-10-20 09:03:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#------------------------------------------------------------
|
|
|
|
# IDAPython - Python plugin for Interactive Disassembler Pro
|
|
|
|
#
|
2008-06-15 12:03:53 +02:00
|
|
|
# Copyright (c) 2004-2008 Gergely Erdelyi <dyce@d-dome.net>
|
2007-10-20 09:03:51 +02:00
|
|
|
#
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# For detailed copyright information see the file COPYING in
|
|
|
|
# the root of the distribution archive.
|
|
|
|
#------------------------------------------------------------
|
|
|
|
# init.py - Essential init routines
|
|
|
|
#------------------------------------------------------------
|
2008-07-26 12:18:28 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
import warnings
|
|
|
|
|
2007-10-20 09:03:51 +02:00
|
|
|
import _idaapi
|
|
|
|
|
|
|
|
# FIXME: Should fix the offending constant instead
|
|
|
|
warnings.filterwarnings('ignore', category=FutureWarning)
|
|
|
|
|
|
|
|
|
|
|
|
def addscriptpath(script):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Add the path part of the scriptfile to the system path to
|
|
|
|
allow modules to be loaded from the same place.
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
Each path is added only once.
|
|
|
|
"""
|
|
|
|
pathfound = 0
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
scriptpath = os.path.dirname(script)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
for pathitem in sys.path:
|
|
|
|
if pathitem == scriptpath:
|
|
|
|
pathfound = 1
|
|
|
|
break
|
|
|
|
|
|
|
|
if pathfound == 0:
|
|
|
|
sys.path.append(scriptpath)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
# Add the script to ScriptBox if it's not there yet
|
|
|
|
if not script in ScriptBox_instance.list:
|
|
|
|
ScriptBox_instance.list.insert(0, script)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def runscript(script):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Run the specified script after adding its directory path to
|
|
|
|
system path.
|
|
|
|
|
|
|
|
This function is used by the low-level plugin code.
|
|
|
|
"""
|
|
|
|
addscriptpath(script)
|
2008-08-15 22:42:09 +02:00
|
|
|
watchdog.reset()
|
2008-06-15 16:39:43 +02:00
|
|
|
argv = sys.argv
|
|
|
|
sys.argv = [ script ]
|
|
|
|
execfile(script, globals())
|
|
|
|
sys.argv = argv
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
def print_banner():
|
2008-06-15 16:39:43 +02:00
|
|
|
version1 = "IDAPython version %d.%d.%d %s (serial %d) initialized" % IDAPYTHON_VERSION
|
|
|
|
version2 = "Python interpreter version %d.%d.%d %s (serial %d)" % sys.version_info
|
|
|
|
linelen = max(len(version1), len(version2))
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
print '-' * linelen
|
|
|
|
print version1
|
|
|
|
print version2
|
|
|
|
print '-' * linelen
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# Take over the standard text outputs
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
class MyStdOut:
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Dummy file-like class that receives stout and stderr
|
|
|
|
"""
|
|
|
|
def write(self, text):
|
|
|
|
_idaapi.msg(text.replace("%", "%%"))
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
def flush(self):
|
|
|
|
pass
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-25 23:02:31 +02:00
|
|
|
def isatty(self):
|
|
|
|
return False
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
# Redirect stderr and stdout to the IDA message window
|
|
|
|
sys.stdout = sys.stderr = MyStdOut()
|
|
|
|
|
|
|
|
# Assign a default sys.argv
|
|
|
|
sys.argv = [ "" ]
|
|
|
|
|
|
|
|
# Have to make sure Python finds our modules
|
|
|
|
sys.path.append(_idaapi.idadir("python"))
|
|
|
|
|
|
|
|
print_banner()
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# Import all the required modules
|
|
|
|
#-----------------------------------------------------------
|
2008-04-12 11:08:11 +02:00
|
|
|
from idaapi import Choose, get_user_idadir, cvar
|
2007-10-20 09:03:51 +02:00
|
|
|
from idc import *
|
|
|
|
from idautils import *
|
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# Build up the ScriptBox tool
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
class ScriptBox(Choose):
|
2008-06-15 16:39:43 +02:00
|
|
|
def __init__(self, list=[]):
|
|
|
|
Choose.__init__(self, list, "ScriptBox", 1)
|
|
|
|
self.width = 50
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
def run(self):
|
|
|
|
if len(self.list) == 0:
|
|
|
|
Warning("ScriptBox history is empty.\nRun some script with Alt-9 and try again.")
|
|
|
|
return None
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
n = self.choose()
|
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
return self.list[n-1]
|
|
|
|
else:
|
|
|
|
return None
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
def addscript(self, scriptpath):
|
|
|
|
self.list.append(scriptpath)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
ScriptBox_instance = ScriptBox([])
|
|
|
|
|
2008-07-26 12:18:28 +02:00
|
|
|
#-------------------------------------------------------------
|
|
|
|
# Watchdog to catch runaway scripts after a specified timeout
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# watchdog.install()
|
|
|
|
# watchdog.activate(10) # Use 10-second timeout
|
|
|
|
#
|
|
|
|
# Note: The watchdog only works for code running inside
|
|
|
|
# functions, not in global/module namespace.
|
|
|
|
#-------------------------------------------------------------
|
|
|
|
class WatchDog():
|
|
|
|
"""
|
|
|
|
Python tracer-based watchdog class
|
|
|
|
"""
|
|
|
|
def __init__(self, timeout=10):
|
|
|
|
self.timeout = timeout
|
|
|
|
self.installed = False
|
|
|
|
self.active = False
|
|
|
|
|
|
|
|
def install(self):
|
|
|
|
""" Install the tracer function, required for the watchdog """
|
|
|
|
if not self.installed:
|
|
|
|
sys.settrace(self.tracer)
|
|
|
|
self.installed = True
|
|
|
|
|
|
|
|
def activate(self, timeout=None):
|
|
|
|
""" Activate the watchdog, with optional timeout change """
|
|
|
|
assert self.installed, "WatchDog must be installed before activating"
|
|
|
|
if timeout:
|
|
|
|
self.timeout = timeout
|
|
|
|
self.reset()
|
|
|
|
self.active = True
|
|
|
|
|
|
|
|
def deactivate(self):
|
|
|
|
""" Deactivate the watchdog """
|
|
|
|
self.active = True
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
""" Reset the timer, useful for long-running scripts """
|
|
|
|
self.timestamp = time.clock()
|
|
|
|
|
|
|
|
def tracer(self, frame, event, arg):
|
|
|
|
""" Tracer function that receives the tracing events """
|
|
|
|
if not self.active:
|
|
|
|
return None
|
|
|
|
if event == 'line':
|
|
|
|
if time.clock() - self.timestamp > self.timeout:
|
|
|
|
if AskYN(0, "The script has not finished in %d seconds\nWould you like to stop it now?" % self.timeout) == 1:
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
else:
|
|
|
|
self.timestamp = time.clock()
|
|
|
|
return self.tracer
|
|
|
|
|
|
|
|
watchdog = WatchDog(10)
|
|
|
|
|
2007-10-20 09:03:51 +02:00
|
|
|
# Load the users personal init file
|
|
|
|
userrc = get_user_idadir() + os.sep + "idapythonrc.py"
|
|
|
|
|
|
|
|
if os.path.exists(userrc):
|
2008-06-15 16:39:43 +02:00
|
|
|
runscript(userrc)
|
|
|
|
# Remove the user script from the history
|
|
|
|
del ScriptBox_instance.list[0]
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
# All done, ready to rock.
|