Preparing for the patch

Unfortunately, we cannot simply redirect the call to fgets() instead of j_gets, because we do not have fgets() imported at all (as we are not using it in our C source) as well as due to the fact that gets() takes only one argument (as we see at the address 0x4117EB-cdecl passing of a parameter), while fgets() expects three. Trying to patch the code in place so that it would pass three parameters is not possible without damaging the executable and rendering it unusable. This means that we need to find a place for shim code, which would add the two additional parameters and actually call fgets() (once we add it as an imported function).

Fortunately for us, PE sections in memory (and, in fact, in files too) occupy much more space than their actual content. This is true in our case as well and we need to find where the content of the .text section ends; for this reason we, first of all, look at where the next section begins, as shown in the following screenshot:

As we see in the preceding screenshot, the next section is .rdata and the beginning of its content is highlighted. Once we are there, we begin to scroll up, till we see content other than zeros or 0xcc bytes, as shown in the following screenshot:

We see that as the actual content's last byte is at file offset 0x4196, we have some spare space beginning with the file offset 0x4197; however, it does not seem right to begin a procedure at an unaligned address, so let's begin with the file offset 0x4198. Just to be sure that we are at the right place, let's compare the bytes with what we see in IDA Pro:

Eventually, we see that the bytes are the same and we may use the file offset 0x4198 (virtual address 0x414d98) for our shim code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset