📚 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  >>  Useful Code Snippets

Topic created on: July 12, 2005 09:21 CDT by peter .

Hey Guys,
I've decided to post some code snippets that someone may find useful. Feel free to message me with questions/comments.

This function will determine if a given operand is a function argument.

//
// is_argument()
// [IN] ea - effective address of the line the user wants to examine
// [IN] operand - the operand either 0 for the first operand or 1 for the second operand
//
// returns 1 if it is an argument and or 0 if it is not
//
int is_argument(ea_t ea, int operand)
{
func_t * func = get_func(ea);
    //          saved regs      return size                 local variables
    int size = func->frregs +  get_frame_retsize(func) + func->frsize;
    int offset = calc_stkvar_struc_offset(func, ea, operand);
    
    if(offset >= size)
        return 1;
    else
        return 0;
}


This function will return the number of arguments in a given function. It takes in ea, which is just a given ea of the function you want to know about.

//
// get_number_of_arguments()
// [IN] ea - an address within the function the users wants argument count preformed on
//
// returns number of arguments or 0 if no arguments
//
int get_number_of_arguments(ea_t ea)
{
    func_t * func = get_func( ea );
    struc_t * struc = get_frame(func);
    int i = 0;
    int size = 0;
    int var_size = 0;
    int num_args = 0;
    ea_t startOffset;
    ea_t endOffset;


    //      saved size of registers     local variables
    size = func->frregs                  + func->frsize + get_frame_retsize(func);
    
    while(i <= struc->memqty)
    {
        startOffset = struc->members[i].soff;
        if(i == struc->memqty - 1)
            endOffset = struc->members[i].eoff;
        else
            endOffset = struc->members[i + 1].soff;
        var_size += (endOffset - startOffset);

        if(var_size >= size )
        {
            
            num_args++;
        }

        i++;
    }  

    return num_args;
}


This is my favorite code snippet, this function will dump the size of each variable and its name. It *could* be modified to calculate the distance between a buffer and r (the return address). I have used it to do some rough auditing and calculatng before, its not an exact science but IDA does an ok job building the stack.


//
// dump_stack_sizes()
// [IN] ea - an address within the function the users wants dump preforme don
//
// returns nothing
//
void dump_stack_sizes(ea_t ea)
{
        func_t * func = get_func( ea );
    struc_t * struc = get_frame(func);
    int i = 0;
    int var_size = 0;
    ea_t startOffset;
    ea_t endOffset;
        char * name;
    
    while(i <= struc->memqty)
    {
        startOffset = struc->members[i].soff;
        if(i == struc->memqty - 1)
            endOffset = struc->members[i].eoff;
        else
            endOffset = struc->members[i + 1].soff;
        
                var_size = (endOffset - startOffset);
                name = get_member_name(struc->members[t].id);
                msg("%s[%d]\n", name, var_size);
        i++;
    }  

}


Let me know if you have problems with these snippets or if you are interested in seeing more code snippets.
Also remeber that if you have a question that doesn't get answered at the datarescue forums, you can always ask here i think there are some diffrent demographics between the two forums, and you might get someone here who doesn't check the datarescue forums.
Later'
Peter~

No posts found under this topic.
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