androidInvoking a native function

Our app displays a message: "Hello from C++".

It does this by calling a native C++ function called stringFromJNI() from a FridaTen library. So let’s examine the library.

After we load this into Ghidra, We find a hidden function named getFlag(int).

This function wasn’t declared in the Java space and isn’t being called from anywhere in the library.

  • c code

  • What does getFlag(int) do?

    • It creates a string "NOT_THE_FLAG" by default.

    • If the input is 123, it:

      • Takes a hardcoded string.

      • Applies XOR with 12 on each character.

      • Returns the decoded result (the real flag).

So, to get the flag, we need to invoke this method. Let's use Frida to call this native function.

  • Line-by-line Explanation

    1. This gets the exact memory address of a function inside libFridaTen.so

    2. Converts the memory address into a pointer object Frida can work with.

    3. Turns the native function pointer into a JavaScript function you can call.

      • NativeFunction — What is it?

        • It lets you call native functions (e.g., C/C++ functions inside .so or .dll libraries) directly from your JavaScript code.

    4. Calls the native function getFlag(123)

      • The function returns a memory address, pointing to the flag string.

      • Example: it might return something like 0x12345678.

    5. Converts the pointer into a readable text string

    6. Prints the flag to the Frida console.

We can’t use Module.getExportByName() to get this function because it is not exported.

So, we will get manual address resolution using offsets

  • But how do you get the offset?

    • Open the .so file using Ghidra.

    • Look for the function you want (getFlag, for example)

    • In the Symbol Tree (left side), expand "Functions".

    • Find and click the function name, e.g. getFlag.

    • Look at the function's address (top bar or in the Listing window).

    • Subtract the base address of the binary (usually 00100000 for .so files in Ghidra):

    • Use that offset in Frida like:


Last updated