GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.hex-rays.com — Appcall | Hex-Rays Docs IntroductionAppcall is a mechanism to call functions under a debugger session in the context of the debugged program using IDA's CLI (Command Line Interpreter) or from a script. Such a mechanism can be used for seamless blackbox function calling and use, fuzzing, process instrumentation, DLL injection, testing or extending applications. Appcall mechanism highly depends on the type information of the called function. For that reason, it is necessary to have a correct function prototype before doing an Appcall, otherwise different or incorrect results may be returned. In a nutshell, Appcall works by first hijacking the current thread's stack (please do switch threads explicitly if you want to Appcall in a different context), then pushing the arguments, and then temporarily adjusting the instruction pointer to the beginning of the called function. After the function returns (or an exception occurs), the original stack, instruction pointer, and other registers are restored, and the result is returned to the caller. Please note that while most of the examples in this document are illustrated using a Windows user mode application, Appcall is not limited to Windows and can be used with any platform supported by IDA debuggers. Quick startLet's start explaining the basic concepts of Appcall using the IDC CLI. Let's imagine we have the following It can be called by simply typing the following in the IDC CLI (press "." to jump to the CLI): As you noticed, we invoked an Appcall by simply treating If you have a function with a mangled name or with characters that cannot be used as an identifier name in the IDC language, such as "_my_func@8", then you can use the Or simply directly as: Using AppCall with IDCApart from calling Appcall naturally as shown in the previous section, it is possible to call it explicitly using the The Appcall IDC function requires you to pass a function address, function type information (various forms are accepted) and the parameters (if any): We've seen so far how to call a function if it already has type information, now suppose we have a function that does not: Before calling this function with
This is how we can do it using the first option: As for the second option, we can use Note that we used It is possible to permanently set the prototype of a function programmatically using In the following sections, we are going to cover different scenarios such as calling by reference, working with buffers and complex structures, etc. Passing arguments by referenceTo pass function arguments by reference, it suffices to use the & symbol as in the C language.
We can use this code from IDC:
We need to create a buffer and pass it by reference to the function: __usercall calling conventionIt is possible to Appcall functions with non standard calling conventions, such as routines written in assembler that expect parameters in various registers and so on. One way is to use the __usercall calling convention. Consider this function: And from IDC: Variadic functionsIn C: And in IDC: Calling functions that might cause exceptionsExceptions may occur during an Appcall. To capture them, use the try/catch mechanism: The exception object "e" will be populated with the following fields:
For example, one could get something like this: In some cases, the exception object will contain more information. Functions that accept or return structuresAppcall mechanism also works with functions that accept or return structure types. Consider this C code: We can create a couple of records and link them up together: Because we issued an Appcall, when We can then access the fields naturally (even the linked objects). We can verify that if we just dump the first record through the IDC CLI (or just by calling IDC's Notice how when We can also directly access the fields of the structure from IDC and have those changes reflected in the debugee's memory: Notable observations:
Calling an API that receives a structure and its sizeLet us take another example where we call the GetVersionExA API function: This API requires one of its input fields to be initialized to the size of the structure. Therefore, we need to initialize the structure correctly before passing it to the API to be further populated therein: Now if we dump the ver object contents we observe something like this: Working with opaque typesOpaque types like Let us take for example the And the Let us see how we can get a "FILE *"" and use it as an opaque type and issue an Nothing special about the fopen/fclose Appcalls except that we see the __at__ attribute showing up although it does not belong to the FILE structure definition. This is a special attribute that IDA inserts into all objects, and it contains the memory address from which IDA retrieved the object attribute values. We can use the __at__ to retrieve the C pointer of a given IDC object. Previously, we omitted the __at__ field from displaying when we dumped objects output, but in reality this is what one expects to see as part of the objects attributes used in Appcalls. Let's create a user record again: ..and observe the output: Please note that it is possible to pass as integer (which is a pointer) to a function that expects a pointer to a structure. FindFirst/FindNext APIs exampleIn this example, we call the APIs directly without permanently setting their prototype first. Using LoadLibrary/GetProcAddressIn this example, we are going to initialize the APIs by setting up their prototypes correctly so we can use them later conveniently. Retrieving application's command lineSpecifying Appcall optionsAppcall can be configured with
It is possible to retrieve the Appcall options, change them and then restore them back. To retrieve the options use the Please note that the Appcall options are saved in the database so if you set it once it will retain its value as you save and load the database. Manual AppcallSo far, we've seen how to issue an Appcall and capture the result from the script, but what if we only want to setup the environment and manually step through a function? This can be achieved with manual Appcall. The manual Appcall mechanism can be used to save the current execution context, execute another function in another context and then pop back the previous context and continue debugging from that point. Let us directly illustrate manual Appcall with a real life scenario:
To illustrate, let us take the
Directly after doing that, IDA will switch to the function and from that point on we can debug: Now you are ready to single step that function with all its arguments properly set up for you. When you are done, you can return to the previous context by calling Initiating multiple manual AppcallsIt is possible to initiate multiple manual Appcalls. If manual Appcall is enabled, then issuing an Appcall from an another Appcall will push the current context and switch to the new Appcall context. Such technique is useful if you happen to be tracing a function then you want to debug another function and then resume back from where you were! Manual Appcalls are not designed to be called from a script (because they don't finish), nonetheless if you use them from a script: We observe the following:
Capturing exception debug eventsWe previously illustrated that we can capture exceptions that occur during an Appcall, but that is not enough if we want to learn more about the nature of the exception from the operating system point of view. It would be better if we could somehow get the last debug_event_t that occurred inside the debugger module. This is possible if we use the APPCALL_DEBEV option. Let us repeat the previous example but with the APPCALL_DEBEV option enabled: And in this case, if we dump the exception object's contents, we get these attributes: Appcall related functionsThere are some functions that can be used while working with Appcalls. parse_decl/get_tinfo/sizeofThe The And finally, given a typeinfo string, one can use the Accessing enum members as constantsIn IDC, it is possible to access all the defined enumerations as if they were IDC constants: Then one can type: This syntax makes it even more convenient to use enumerations when calling APIs via Appcall. Storing/Retrieving typed elementsIt is possible to store/retrieve (aka serialize/deserialize) objects to/from the database (or the debugee's memory). To illustrate, let us consider the following memory contents: And we know that this maps to a given type: To retrieve (deserialize) the memory contents into a nice IDC object, we can use the Here is an example: And now if we dump the contents of o: and again we notice the __at__ which holds the address of the retrieved object. To store (serialize) the object back into memory, we can use the Here's an example continuing from the previous one: And finally to verify, we go to the memory address: Using Appcall with IDAPythonThe Appcall concept remains the same between IDC and Python, nonetheless Appcall/Python has a different syntax (using references, unicode strings, etc.) The Appcall mechanism is provided by One can take a reference to an Appcall:
Python Appcall code: If the returned value is also an int64, then you can use the
Things to note:
Passing arguments by reference
We use the following Python code:
First we need to use the Functions that accept or return structuresLike in IDC, we can create objects and pass them with at least two methods. The first method involves using the And finally, if you happen to have your own object instance then just pass your object. The IDAPython object to IDC object conversion routine will skip attributes starting and ending with "__". FindFirst/FindNext exampleUsing GetProcAddressPlease note that we used the Setting the Appcall optionsIn Python, the Appcall options can be set global or locally per Appcall.
Similarly, retrieving the Appcall options is done by either calling To cleanup after a manual Appcall use Calling functions that can cause exceptionsAn Appcall that generates an exception while executing in the current thread will throw a Python Exception object. This is inline with the IDC behavior we described above.
This approach is useful if you want to know whether the Appcall passes or crashes. Now if we want more details about the exception, then we use the APPCALL_DEBEV flag, which will cause an OSError exception to be raised and have its args[0] populated with the last If the Appcall caused a crash, then the debug_event variable will be populated with the last Appcall related functions in PythonStoring/Retrieving objectsStoring/Retrieving objects is also supported in Python:
In this example we show how to:
Let's start with the IDA SDK helper functions first: Now to accomplish similar result using Appcall helper functions: When it comes to storing, instead of using the Appcall's Accessing enum members as constantsLike in IDC, to access the enums, one can use the If the constant was not defined then an attribute error exception will be thrown. To prevent that, use the Please send your comments or questions to [email protected] Last updated Was this helpful? |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |