Added import enumeration support. Check exmples\ex_imports.py

This commit is contained in:
elias.bachaalany 2010-03-05 11:18:25 +00:00
parent b935e24aba
commit 57d37a638a
4 changed files with 87 additions and 4 deletions

View File

@ -38,7 +38,7 @@ Make sure all the needed tools (compiler, swig) are on the PATH.
1, Unpack the IDAPython source and IDA Pro SDK into the following 1, Unpack the IDAPython source and IDA Pro SDK into the following
directory structure: directory structure:
swigsdk-versions/5.6/ - version 5.4 of the IDA Pro SDK swigsdk-versions/5.6/ - version 5.6 of the IDA Pro SDK
idapython/ - IDAPython source code idapython/ - IDAPython source code
2, On Mac OS X copy libida.dylib from the IDA install directory to 2, On Mac OS X copy libida.dylib from the IDA install directory to

View File

@ -79,7 +79,8 @@ BINDIST_MANIFEST = [
"examples/ex_choose2.py", "examples/ex_choose2.py",
"examples/ex_debug_names.py", "examples/ex_debug_names.py",
"examples/ex_graph.py", "examples/ex_graph.py",
"examples/ex_dbg.py" "examples/ex_dbg.py",
"examples/ex_imports.py"
] ]
# List files for the source distribution (appended to binary list) # List files for the source distribution (appended to binary list)

26
examples/ex_imports.py Normal file
View File

@ -0,0 +1,26 @@
# -----------------------------------------------------------------------
# This is an example illustrating how to enumerate imports
# (c) Hex-Rays
#
import idaapi
def imp_cb(ea, name, ord):
print "%08x: %s (ord#%d)" % (ea, name, ord)
# True -> Continue enumeration
# False -> Stop enumeration
return True
nimps = idaapi.get_import_module_qty()
print "Found %d import(s)..." % nimps
for i in xrange(0, nimps):
name = idaapi.get_import_module_name(i)
if not name:
print "Failed to get import module name for #%d" % i
continue
print "Walking-> %s" % name
idaapi.enum_import_names(i, imp_cb)
print "All done..."

View File

@ -1,7 +1,63 @@
%ignore nmSerEA; %ignore nmSerEA;
%ignore nmSerN; %ignore nmSerN;
%ignore maxSerialName; %ignore maxSerialName;
%ignore get_import_module_name;
%rename (get_import_module_name) py_get_import_module_name;
%ignore NALT_EA; %ignore NALT_EA;
%ignore enum_import_names;
%rename (enum_import_names) py_enum_import_names;
%include "nalt.hpp" %include "nalt.hpp"
%{
//<code(py_nalt)>
//-------------------------------------------------------------------------
// callback for enumerating imports
// ea: import address
// name: import name (NULL if imported by ordinal)
// ord: import ordinal (0 for imports by name)
// param: user parameter passed to enum_import_names()
// return: 1-ok, 0-stop enumeration
static int idaapi py_import_enum_cb(
ea_t ea,
const char *name,
uval_t ord,
void *param)
{
PyObject *py_ea = Py_BuildValue(PY_FMT64, pyul_t(ea));
PyObject *py_name = PyString_FromString(name);
PyObject *py_ord = Py_BuildValue(PY_FMT64, pyul_t(ord));
PyObject *py_result = PyObject_CallFunctionObjArgs((PyObject *)param, py_ea, py_name, py_ord, NULL);
int r = py_result != NULL && PyObject_IsTrue(py_result) ? 1 : 0;
Py_DECREF(py_ea);
Py_DECREF(py_name);
Py_DECREF(py_ord);
return r;
}
//</code(py_nalt)>
%}
%inline %{
//<inline(py_nalt)>
//-------------------------------------------------------------------------
PyObject *py_get_import_module_name(int mod_index)
{
char buf[MAXSTR];
if ( !get_import_module_name(mod_index, buf, sizeof(buf)) )
Py_RETURN_NONE;
return PyString_FromString(buf);
}
//-------------------------------------------------------------------------
// enumerate imports from specific module
// return: 1-finished ok, -1 on error, otherwise callback return value (<=0)
int py_enum_import_names(int mod_index, PyObject *py_cb)
{
if ( !PyCallable_Check(py_cb) )
return -1;
return enum_import_names(mod_index, py_import_enum_cb, py_cb);
}
//</inline(py_nalt)>
%}