I'm trying to figure out how to explore structs in IDA, and getting stuck on the basics. I wrote a very basic test app;

int main() {
    const int kBits = 1024;
    const int kExp = 3;

    RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0);
    RSA_print_fp(stdout, rsa, 0);

    return 0;

Then I imported the OpenSSL headers, so IDA now understands what RSA* and BIGNUM* are. BIGNUM is actually pointing to bignum_st which is referenced from RSA, which is rsa_st.

Then, dropped a breakpoint in the pseudocode view; enter image description here

Now, if I hover over v3, I see it's parsing the struct. So I want to do something like grab the value of rsa->d->dmax, but there doesn't seem to be a way to do this? I can't seem to find a way to access v3 by name in IDAPython, nor can I seem to 'walk' the structure to get to the int that lives at dmax. Is there some way to do this?

  • 1
    Your code works as usual but it raises exception code in IDA+windbg. Can you share the compiled Windows binary?
    – Biswapriyo
    Dec 7, 2018 at 6:43
  • Can I assume what you want to do is to dump the value of v3->d->dmax upon reaching this breakpoint?
    – Pororo
    Dec 7, 2018 at 9:33
  • @Pororo yes, exactly! Dec 7, 2018 at 14:26
  • @Biswapriyo I can't imagine it would work in windows.. I'm doing this under Linux (CentOS 7 if it matters) Dec 7, 2018 at 14:27
  • for some reason I thought this was the writing stack exchange, anyway - +1 good question! will follow this. Jan 18, 2023 at 9:58

3 Answers 3


My knowledge of the IDAPython API isn't the most complete, but what I would do is

  1. Read the source (or look in IDA) to find the location (byte offsets) of d in rsa_st and dmax in bignum_st. This is easy since the members of the structures above d and dmax are pointers or int types.
  2. Inspect the disassembly just before the call and determine the location of v3 - from the pseudocode it appears to be stored in the stack. If you are lucky, pressing tab while your cursor is on v3 will take you to some mov operation in disassembly.
  3. When the breakpoint is hit, use a combination of idc.get_reg_value and idc.get_bytes commands (see https://www.hex-rays.com/products/ida/support/idadoc/162.shtml), using the result from step 2 as a starting point.

Here are the steps:

  1. Set breakpoint. Run the local debugger with F9. Open Debugger dropdown menu form menu bar and choose "Take memory snapshot". Here is the screenshot:


  1. Double click on the variables that you want to know (here the RSA* pointer). IDA will take you to the stack view (aka. IDA View-RIP). Do not move the cursor otherwise you will get different value. Press N to name the variable in that stack view. "Rename address" window will pop-up. Here is the screenshot:


  1. After naming the variable in that stack view, press Y to add the data type i.e. structure type. For this case it will be RSA, not the pointer because all stack variables placed linearly. Here is the screenshot:


Now you can see all the struct members value.


"Setting a breakpoint" suggests that you are doing dynamic analysis. Assuming that and assuming this happens on a real system, may I suggest an alternative route for Linux, based on my other assumption that you are linking OpenSSL dynamically?

When on Linux (and a number of other unixoid systems) you can guide the dynamic linker/loader to override a function. If you know Windows and DLLs it could probably be compared a little to DLL placement attacks, but it is more flexible and requires less effort.

NB: Obviously this approach doesn't teach you how about dealing with structs in IDA, but I think it is a very powerful technique to keep in mind. Because it is an alternative approach that doesn't satisfy your constraint of wanting to learn more about dealing with structs in IDA, I am making this a CW.

There are some components for this approach:

  1. you need a "wrapper" shared object, let's call it mock_openssl_rsagenkey.so, exporting RSA_generate_key() with a binary compatible signature to that from the original OpenSSL shared object (something like libcrypto.so)
  2. you need to tell ld.so via environment variable to preload mock_openssl_rsagenkey.so (see below)

ld.so will take care -- when resolving symbols -- that the preloaded ones take precedence. This is how one of the modes of operation for eFence is implemented (look for LD_PRELOAD, the man page also lists the exported symbols).

So now your only remaining task would be to turn up the correct prototype for RSA_generate_key from the libcrypto.so that your binary uses (try ldd ./your_binary to list dependencies) and write the wrapper .so, using dlopen() and dlsym() to locate the original RSA_generate_key symbols from the actual libcrypto.so

Full example


This is approximately the program you created and are analyzing.

#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>

int main(int argc, char const ** argv, char const** envp)
    int const kBits = 1024;
    int const kExp = 3;

    RSA *rsa = RSA_generate_key(kBits, kExp, 0, 0);
    RSA_print_fp(stdout, rsa, 0);

    return EXIT_SUCCESS;


This is the wrapper shared object, which we'll load with LD_PRELOAD.

#include <dlfcn.h>
#include <openssl/rsa.h>

typedef RSA* (*RSA_generate_key_t)(int, unsigned long, void (*)(int, int, void *), void *);
static RSA_generate_key_t real_RSA_generate_key = 0;
static void* libcrypto = 0;

static void init() __attribute__((constructor));
static void fini() __attribute__((destructor));

void init()
    if (!libcrypto)
        libcrypto = dlopen("libcrypto.so.3", RTLD_NOW);
        if (!libcrypto)
            libcrypto = dlopen("libcrypto.so", RTLD_NOW);
        fprintf(stderr, "Loaded libcrypto: %p\n", libcrypto);
        if (libcrypto)
            real_RSA_generate_key = (RSA_generate_key_t)dlsym(libcrypto, "RSA_generate_key");
        fprintf(stderr, "\tRSA_generate_key == %p\n", (void*)real_RSA_generate_key);

void fini()
    if (libcrypto)
        libcrypto = 0;
        real_RSA_generate_key = 0;

RSA* RSA_generate_key(int bits, unsigned long e, void (*callback)(int, int, void *), void *cb_arg)
    if (!real_RSA_generate_key)
        fprintf(stderr, "FATAL: Have no function pointer for original RSA_generate_key()\n");
        return 0;
    fprintf(stderr, "Calling real_RSA_generate_key(%i, %lu, %p, %p)\n", bits, e, callback, cb_arg);
    RSA* ret = real_RSA_generate_key(bits, e, callback, cb_arg);
    /* do something with ret here */
    if (ret)
        fprintf(stderr, "Returned RSA key: %p ... I can do what I want with it ... MUHAHAHA!\n", ret);
    return ret;


  • RSA_generate_key_t parrots the prototype of the original function as typedef
  • real_RSA_generate_key is going to hold the function pointer retrieved from the real libcrypto.so
  • libcrypto is going to hold the handle to the loaded libcrypto.so
  • init() -- declared as constructor -- will run when our wrapper .so gets loaded
    • it attempts to load libcrypto.so.3
    • failing that, attempts to load libcrypto.so
    • if successful in either of the preceding steps, it uses dlsym() to fetch the address of the real RSA_generate_key and stores it in real_RSA_generate_key
    • ... also outputs the steps, something like:
      Loaded libcrypto: 0x7f9ad86e3700
              RSA_generate_key == 0x7f9ad8478b50
  • finit() -- declared as destructor -- will run when our wrapper .so gets unloaded
  • RSA_generate_key() is our wrapper to the original function and will be called in place of the one from libcrypto.so by a program that preloads this .so


The GNU make recipes to build our stuff.

CFLAGS:=$(strip -Wno-deprecated-declarations $(CFLAGS))
all: bin so
bin: your_binary
so: mock_openssl_rsagenkey.so

bin: LDLIBS=-lcrypto

your_binary: main.c
        $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

so: CFLAGS=-shared -fPIC
so: LDFLAGS=-Wl,-soname,libcrypto.so
so: LDLIBS=-ldl

mock_openssl_rsagenkey.so: mock_openssl.c
        $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

.PHONY: all so bin


  1. write each of the above with the name I gave in the subsection titles into a file
  2. run make (on Debian/Ubuntu you may want to install build-essential, for other systems consult your documentation)
  3. invoke, e.g. with this env LD_PRELOAD=./mock_openssl_rsagenkey.so ./your_binary
Output (excerpt)
$ LD_PRELOAD=./mock_openssl_rsagenkey.so ./your_binary
Loaded libcrypto: 0x7f9ad86e3700
        RSA_generate_key == 0x7f9ad8478b50
Calling real_RSA_generate_key(1024, 3, (nil), (nil))
Returned RSA key: 0x55d1bfc40c10 ... I can do what I want with it ... MUHAHAHA!
RSA Private-Key: (1024 bit, 2 primes)
publicExponent: 3 (0x3)


  • Lines 1..2 are from init()
  • Lines 3..4 are from RSA_generate_key() in our wrapper .so
    • NB: this shows you have full access to all the arguments! While it may be a little more intricate to do, it would also clearly be possible to wrap the callback mechanism provided by OpenSSL into our own, e.g.:
      • prior to calling real_ RSA_generate_key(), allocate a struct holding original callback pointer and cb_arg
      • pass our own callback function
        • inside our callback function unwrap the struct and pass arguments to the original callback if and as needed
      • after real_ RSA_generate_key() clean up the allocated struct

The above as Base64-encoded .tar.xz file for convenience:


Telling ld.so to preload the wrapper .so

Bash and some other shells will be contend to do it with:

LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so ./your_binary

but the more portable way is:

env LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so ./your_binary

which uses /usr/bin/env -- instead of relying on a shell-builtin facility 00 to set the environment variable for a single invocation.

Obviously you could also at the prompt ($ signifies it!) to:

$ export LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so
$ ./your_binary

or on some less comfortable (or older) shells:

$ LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so
$ export LD_PRELOAD
$ ./your_binary

Last, but not least, you could create a little wrapper script, which we'll name your_binary.sh based on your_binary:

#!/usr/bin/env bash
export LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so
exec "${0%.sh}" "$@"
  • #!/usr/bin/env bash is the most portable hashbang, compared to hardcoding the path to a particular bash binary
  • export LD_PRELOAD=/path/to/your/mock_openssl_rsagenkey.so exports LD_PRELOAD as environment variable with the shown value
  • exec "${0%.sh}" "$@":
    • "${0%.sh}" strips the .sh suffix, yielding the name without it
    • "$@" expanding to the quoted arguments passed to our script (e.g. "$1" "$2" ...)
    • exec isn't strictly necessary here, but I prefer it for these small wrapper scripts; it replaces the current shell with the given command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.