2010-06-28 14:36:40 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -----------------------------------------------------------------------
|
2011-10-14 16:24:38 +02:00
|
|
|
# IDAPython - Python plugin for Interactive Disassembler
|
2010-06-28 14:36:40 +02:00
|
|
|
#
|
2010-11-10 14:58:08 +01:00
|
|
|
# Copyright (c) The IDAPython Team <idapython@googlegroups.com>
|
2010-06-28 14:36:40 +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
|
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import warnings
|
|
|
|
import _idaapi
|
|
|
|
|
|
|
|
# __EA64__ is set if IDA is running in 64-bit mode
|
|
|
|
__EA64__ = _idaapi.BADADDR == 0xFFFFFFFFFFFFFFFFL
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
# Take over the standard text outputs
|
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
class IDAPythonStdOut:
|
|
|
|
"""
|
|
|
|
Dummy file-like class that receives stout and stderr
|
|
|
|
"""
|
|
|
|
def write(self, text):
|
|
|
|
# Swap out the unprintable characters
|
|
|
|
text = text.decode('ascii', 'replace').encode('ascii', 'replace')
|
|
|
|
# Print to IDA message window
|
2011-04-18 18:07:00 +02:00
|
|
|
_idaapi.msg(text)
|
2010-06-28 14:36:40 +02:00
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return False
|
|
|
|
|
2010-07-16 14:07:49 +02:00
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
def runscript(script):
|
|
|
|
"""
|
|
|
|
Executes a script.
|
|
|
|
This function is present for backward compatiblity. Please use idaapi.IDAPython_ExecScript() instead
|
|
|
|
|
|
|
|
@param script: script path
|
|
|
|
|
|
|
|
@return: Error string or None on success
|
|
|
|
"""
|
|
|
|
|
|
|
|
import idaapi
|
|
|
|
return idaapi.IDAPython_ExecScript(script, globals())
|
|
|
|
|
2010-06-28 14:36:40 +02:00
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
def print_banner():
|
|
|
|
banner = [
|
2010-08-10 13:44:59 +02:00
|
|
|
"Python %s " % sys.version,
|
2010-07-16 14:07:49 +02:00
|
|
|
"IDAPython" + (" 64-bit" if __EA64__ else "") + " v%d.%d.%d %s (serial %d) (c) The IDAPython Team <idapython@googlegroups.com>" % IDAPYTHON_VERSION
|
2010-06-28 14:36:40 +02:00
|
|
|
]
|
2010-07-16 14:07:49 +02:00
|
|
|
sepline = '-' * (max([len(s) for s in banner])+1)
|
2010-06-28 14:36:40 +02:00
|
|
|
|
2011-04-18 18:07:00 +02:00
|
|
|
print(sepline)
|
|
|
|
print("\n".join(banner))
|
|
|
|
print(sepline)
|
2010-06-28 14:36:40 +02:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Redirect stderr and stdout to the IDA message window
|
2012-06-24 22:49:11 +02:00
|
|
|
_orig_stdout = sys.stdout;
|
|
|
|
_orig_stderr = sys.stderr;
|
2010-06-28 14:36:40 +02:00
|
|
|
sys.stdout = sys.stderr = IDAPythonStdOut()
|
|
|
|
|
|
|
|
# Assign a default sys.argv
|
|
|
|
sys.argv = [""]
|
|
|
|
|
|
|
|
# Have to make sure Python finds our modules
|
|
|
|
sys.path.append(_idaapi.idadir("python"))
|
|
|
|
|
2011-04-18 18:07:00 +02:00
|
|
|
# Remove current directory from the top of the patch search
|
|
|
|
if '' in sys.path: # On non Windows, the empty path is added
|
|
|
|
sys.path.remove('')
|
|
|
|
|
|
|
|
if os.getcwd() in sys.path:
|
|
|
|
sys.path.remove(os.getcwd())
|
|
|
|
|
|
|
|
# ...and add it to the end if needed
|
|
|
|
if not IDAPYTHON_REMOVE_CWD_SYS_PATH:
|
|
|
|
sys.path.append(os.getcwd())
|
|
|
|
|
2010-06-28 14:36:40 +02:00
|
|
|
# Import all the required modules
|
2011-04-18 18:07:00 +02:00
|
|
|
from idaapi import Choose, get_user_idadir, cvar, Choose2, Appcall, Form
|
|
|
|
from idc import *
|
2010-06-28 14:36:40 +02:00
|
|
|
from idautils import *
|
|
|
|
import idaapi
|
|
|
|
|
|
|
|
# Load the users personal init file
|
2011-10-14 16:24:38 +02:00
|
|
|
userrc = os.path.join(get_user_idadir(), "idapythonrc.py")
|
2010-06-28 14:36:40 +02:00
|
|
|
if os.path.exists(userrc):
|
|
|
|
idaapi.IDAPython_ExecScript(userrc, globals())
|
|
|
|
|
|
|
|
# All done, ready to rock.
|