📚 OpenRCE is preserved as a read-only archive. Launched at RECon Montreal in 2005. Registration and posting are disabled.








Flag: Tornado! Hurricane!

 Forums >>  IDA Pro  >>  IDAPython: Finding All Callees of a Function

Topic created on: January 7, 2007 15:33 CST by bmazic .

I have written a simple IDAPython script to find all functions called from a specified function (either directly or indirectly). The problem is that I am unable to include imported functions in that list - only functions local to the application are included. Is there a way to get imported functions (with full names, e.g. KERNEL32.CreateFileA) included in that list?

I am using idautils.CodeRefsFrom(head, 0) to get a code reference, and I'm not getting anything for calls to imported functions. For example, idautils.CodeRefsFrom(head, 0) returns empty list for the following instruction:


0x004032A5 call    ds:InterlockedIncrement


Also, is there a way to get the function's start address, provided you specify any address inside the function - something like nonexistent FindFuncStart(ea) function?

Below is the IDAPython script for finding all callees of a function:


def find_all_callees(start_ea):
    '''Return a set of all callees called from the specified function.'''
    
    callees = {}
    visited = set([])
    pending = set([start_ea])
    
    while len(pending) > 0:
        start_ea = pending.pop()

        fname = GetFunctionName(start_ea)
        if not fname: continue

        callees[start_ea] = fname
        visited.add(start_ea)
        
        end_ea = FindFuncEnd(start_ea)
        if end_ea == BADADDR: continue

        all_refs = set([])
        # For each defined element in the function.
        for head in Heads(start_ea, end_ea):
            # We are only interested in code
            if not isCode(GetFlags(head)): continue

            # Get the references made from the current instruction and keep only the ones
            # not local to the function. Assume all such references are function calls.
            refs = CodeRefsFrom(head, 0)
            refs = set(filter(lambda x: not (x>=start_ea and x<=end_ea), refs))
            all_refs |= refs

        all_refs -= visited
        pending |= all_refs

    return callees

# main
ea = ScreenEA()
callees = find_all_callees(ea)
print '0x%08X: %s callees:' % (ea, callees[ea])
for ea in callees:
    print '    0x%08X: %s' % (ea, callees[ea])


Any help would be greatly appreciated.

Boris

  RolfRolles     January 8, 2007 10:43.42 CST
Calls to imported functions are considered data references, not code references.  One way to find them is to iterate over the outgoing references (xref.hpp::first_from and next_from) from the desired address looking for data references (XREF_DATA) which have a type corresponding to a call (fl_CN || fl_CF).  To retrieve the name of the called function, use name.hpp::get_name.  

This will also return the addresses of arbitrary dwords that are called (e.g. call dword ptr [dword_401234]), which you can filter out with bytes.hpp::has_dummy_name.

[EDIT 02/09/07:  According to the whatsnew.txt in IDA 5.1 beta, this situation is no longer the same:  code x-references are created for calls to dwords.]

To find the entry to a function use funcs.hpp::get_func which returns a func_t *f.  f->startEA contains the beginning of the function.

  pedram     January 8, 2007 10:50.31 CST
To get a functions start address given an EA within the function do:

    print "%08x" % get_func(ea).startEA


PaiMei's PIDA format was designed to make life easier with the sort of task you are trying to accomplish. Example:

callees = module.graph_down(function_ea)

for func in callees.nodes.values():
  print func.name


You can control how far "down" you want to look in the call to graph_down as well (see the documentation for more details).

  bmazic   January 8, 2007 14:20.12 CST
Thanks for the answers. I think I should give PaiMei a go, although staying within IDA environment has its benefits, especially during initial code analysis.

Note: Registration is required to post to the forums.

There are 31,328 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06
Question about memor...
Dec/12


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit