One of the most powerful aspects of IDA Pro is its ability to recognize functions, label them, and break down the local variables and parameters. Example 5-4 shows an example of a function that has been recognized by IDA Pro.
Example 5-4. Function and stack example
00401020 ; =============== S U B R O U T I N E============================= 00401020 00401020 ; Attributes: ebp-based frame ❶ 00401020 00401020 function proc near ; CODE XREF: _main+1Cp 00401020 00401020 var_C = dword ptr -0Ch ❷ 00401020 var_8 = dword ptr -8 00401020 var_4 = dword ptr -4 00401020 arg_0 = dword ptr 8 00401020 arg_4 = dword ptr 0Ch 00401020 00401020 push ebp 00401021 mov ebp, esp 00401023 sub esp, 0Ch 00401026 mov [ebp+var_8], 5 0040102D mov [ebp+var_C], 3 ❸ 00401034 mov eax, [ebp+var_8] 00401037 add eax, 22h 0040103A mov [ebp+arg_0], eax 0040103D cmp [ebp+arg_0], 64h 00401041 jnz short loc_40104B 00401043 mov ecx, [ebp+arg_4] 00401046 mov [ebp+var_4], ecx 00401049 jmp short loc_401050 0040104B loc_40104B: ; CODE XREF: function+21j 0040104B call sub_401000 00401050 loc_401050: ; CODE XREF: function+29j 00401050 mov eax, [ebp+arg_4] 00401053 mov esp, ebp 00401055 pop ebp 00401056 retn 00401056 function endp
Notice how IDA Pro tells us that this is an EBP-based stack frame used in the function
❶, which means the local variables and parameters will
be referenced via the EBP register throughout the function. IDA Pro has successfully discovered all
local variables and parameters in this function. It has labeled the local variables with the prefix var_ and
parameters with the prefix arg_, and named the local variables
and parameters with a suffix corresponding to their offset relative to EBP. IDA Pro will label only
the local variables and parameters that are used in the code, and there is no way for you to know
automatically if it has found everything from the original source code.
Recall from our discussion in Chapter 4 that local
variables will be at a negative offset relative to EBP and arguments will be at a positive offset.
You can see at ❷ that IDA Pro has supplied the start of
the summary of the stack view. The first line of this summary tells us that var_C corresponds to the value -0xCh. This is IDA
Pro’s way of telling us that it has substituted var_C for
-0xC at ❸; it has
abstracted an instruction. For example, instead of needing to read the instruction as mov [ebp-0Ch], 3, we can simply read it as “var_C is now set to 3” and continue with our analysis. This abstraction makes
reading the disassembly more efficient.
Sometimes IDA Pro will fail to identify a function. If this happens, you can create a function
by pressing P. It may also fail to identify EBP-based stack frames, and the instructions mov [ebp-0Ch], eax and push dword ptr
[ebp-010h] might appear instead of the convenient labeling. In most cases, you can fix
this by pressing ALT-P, selecting BP
Based Frame, and specifying 4 bytes for Saved
Registers.