Import all the things! Solving FlareOn4 Challenge 3 with libPeConv

Recently I started making a small library for loading and converting PE files (libpeconv, available on my GitHub). The library is still on early stages of development, so please don’t judge and don’t use it in any serious projects. The API may change anytime! However, I have so much fun developing and testing it, that I wanted to share some of my experiments and ideas.

Some time ago I solved some of the FlareOn4 challenges, i.e. the challenge 3. That time I didn’t have the libpeconv yet, so I solved it by some other method. Now it came to my mind, that with the help of my new library solving it could be way much faster and easier.  In this post I will describe my alternative solution and some of the related experiments.

Tool used

For the static analysis:

  • IDA (demo version is enough)

For building the projects:

  • Visual Studio + CMake
  • Python27 (optional – for the helper script that I used in “Bonus”)


The challenge named greek_to_me.exe is a 32bit PE file. It has stripped relocations.


When we deploy it, it shows the empty console and waits. It is not reading any data from the standard input, so we can conclude that it is using some another way to read the password from the user.

We will start from some static analysis in IDA. The crackme has a very simple and clean structure, it is not obfuscated. We can see that at the beginning of the execution it creates a socket and waits for the input.

The socket listens at localhost on port 2222:


After getting the connection, it reads 4 bytes from the input into the buffer:


After it read 4 bytes, it starts processing the input and uses it for decoding an encrypted buffer:


If the checksum is valid, it means the encrypted code was decrypted properly, and it is further executed.

As we can see, only 1 byte of the input is used for decoding the buffer, so we can easily brutforce it. The code responsible for decoding the buffer is also pretty simple:

const size_t encrypted_len = 0x79;
for (int i = 0; i < encrypted_len; i++) {
    BYTE val = encrypted_code[i];
    encrypted_code[i] = (unknown_byte ^ val) + 0x22;

The only part of the crackme that may be somehow challenging is the checksum – this function is not that simple to reimplement. However, if we want to make a brutforcer, we need to be able to calculate the checksum after every attempt.

In my previous solution, I just reimplemented the checksum – it worked but it was not so much fun 😉 . I saw also some other approaches such as emulating the checksum function by the Unicorn engine, using angr framework, or making a brutforcer that talks to the original program via socket. Can it be done even faster? Let’s see…

LibPeConv comes into play

With PeConv we can convert any PE file from raw format to virtual and back. It also provides a custom PE loader – it’s goal is to provide a possibility of loading any PE file into the current process (even if it is not a DLL and even if it does not have no relocations table – it will be explained in the further part). This loaded PE can be later used as a fully functional PE file that can run from inside the curent process. We can also use any selected function from its code – all we need to know is the function’s RVA and the API.

In this case, I will use libpeconv to load the crackme and import from it the function calculating checksum. Also, rather than copying the encrypted buffer to my code, I will read it directly from the loaded PE.

Preparing the required information

Let’s take a look at the crackme again in IDA. We need to find the appropriate offsets and understand the API of the function that we are going to import.

The function calculating the checksum starts at RVA 0x11E6:


It takes 2 arguments: pointer to the buffer and it’s size.

It returns a WORD type:


Summing up, we can define the function prototype as:

WORD calc_checksum(BYTE *decoded_buffer, size_t buf_size)

It is also worth to note, that this function is self-contained and does not call any imported libraries – that makes importing it even easier (we are not forced to load any imports for the module or to apply relocations).

Another thing that we need is the encrypted buffer. It starts at RVA 0x107C and is 0x79 (121) bytes long:


That’s all! Let’s start coding.

Solving the crackme with libPeConv

The current version of libpeconv allows to load PE file in two ways. By the function load_pe_module and by the function load_pe_executable. The second one: load_pe_executable is a complete loader, that loads given PE to the current process in the RWX memory, automatically applies relocations and load dependencies. The first one (load_pe_module) does not load the dependencies and also gives more control: we may load the PE file in non-executable memory and applying the relocations is optional. More information (and *very* possible updates on the API) you can find here:

As we saw, the function that we want to import is self-contained, so it will not harm if we load the crackme PE without imports and without relocations (to see it loaded as a fully functional PE see the next part of the article). I will use a function load_pe_module

BYTE* loaded_pe = (BYTE*)load_pe_module(
    v_size, // OUT: size of the loaded module
    true,   // executable
    false   // without relocations

Now, let’s import the function. First let’s make a pointer to it:

WORD (*calc_checksum) (BYTE *buffer, size_t buf_size) = NULL;

Calculate the absolute offset to the function within the loaded module:

ULONGLONG offset = DWORD(0x11e6) + (ULONGLONG) loaded_pe;

And filling the pointer:

calc_checksum = ( WORD (*) (BYTE *, size_t ) ) offset;

That’s it, now we can use the function in our application like any other function.

But before we can start brutforcing, we also need to fill the pointer to the buffer:

g_Buffer = (uint8_t*) (0x107C + (ULONGLONG) loaded_pe);

This is the full brutforcer that I perpared:
And it works  🙂  The value that we got is exactly what it was supposed to be:


But still, the found value is just a part of the solution, not the flag that we are searching for. As we know from the static analysis, if this value is given correct, the chunk of code will be decrypted and executed. Would be cool to see how exactly that chunk of code looks when it is written into it’s place, don’t you think?

And also it is very easy to achieve. The PE file is loaded in the RWX memory inside the current process – so we can easily substitute the encrypted chunk of code with the decoded. Simple memcpy will do the job:

memcpy(g_Buffer, g_Buffer2, g_BufferLen);

Then, libPeConv will help us to convert the PE file back to the raw format, so that we can open it in IDA. We can do it with the help of pe_virtual_to_raw from libpeconv:

size_t out_size = 0;
BYTE* unmapped_module = pe_virtual_to_raw(
    loaded_pe, //pointer to the module
    v_size, //virtual size
    module_base, //in this case we need here
                 //the original module base, because
                 //the loaded PE was not relocated
    out_size //OUT: raw size of the unmapped PE

And this is the complete solution:

Comparing the dumped executable with the original one we can see that the buffer was overwritten:
So let’s see the modified exe in IDA:
And yes! At the known offset there is the flag revealed:

Bonus – loading  and running a PE with stripped relocations

Ok, you may say – it was easy – the loaded function was self contained, so we could as well rip it off from the original file, not using any loaders. But what if the function calls several other functions within the given module and also imported functions? Will the same trick work? And could it work even for PE file without relocations?

To answer those questions I prepared another test case. Now, instead of loading one function, I will load and execute the full crackme from inside the brutforcer.

First we will modify few things. This time, instead of using load_pe_module I will use load_pe_executable – to load full executable with dependencies.

BYTE* loaded_pe = (BYTE*)load_pe_executable(path, v_size);

The function will automatically detect that the PE file has no relocations, and enforce loading it at it’s original module base. Mind the fact, that allocating memory at the specific base may not always work – so, sometimes it takes several runs to execute it properly. You must also make sure that the module base of the loader does not collide with the module base required by the payload (if the loader’s base is random it is good enough).

Once the PE file is loaded, we just need to get it’s Entry Point – and then we can call it like any other function*:

// Deploy the payload:
// read the Entry Point from the headers:
ULONGLONG ep_va = get_entry_point_rva(loaded_pe)
    + (ULONGLONG) loaded_pe;

//make pointer to the entry function:
int (*loaded_pe_entry)(void) = (int (*)(void)) ep_va;

//call the loaded PE's ep:
int ret = loaded_pe_entry();

* – but mind the fact that depending on the payload’s implementation details, once you redirected your execution to it’s entry point, it may just exit after finishing it’s job and never return back to your code.

I am going to modify the brutforcer code in such a way, that this time after finding the value the original crackme will be run. This is the code of the full application:

To make sure that everything works fine (the deployed payload really creates socket and gives response in exactly the same way like the one deployed independently), I wrote a small Python script that will communicate with it and display the response:

And now, let’s see it all in action:

This is all what I prepared for today, I hope you enjoyed it! The lib is now under rapid development so many things will get refactored and improved, stay tuned!
The binaries of all the presented loaders, along with the crackme, are available here: – the password to the zip is: crackme


See other approaches to solve the same crackme:

About hasherezade

Programmer and researcher, interested in InfoSec.
This entry was posted in CrackMe, Programming, Tools and tagged , , , . Bookmark the permalink.

2 Responses to Import all the things! Solving FlareOn4 Challenge 3 with libPeConv

  1. sec_panda says:

    Hi hasherezade, did you first learn to write programs in C before learning to reverse?

  2. hasherezade says:

    hi! yes, I learned C/C++ as a teen and then I worked many years as a programmer, doing reversing just as my hobby.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s