I'm modifying an old abandonware game to have infinite lives.
The Address that has the instruction dec ecx
is not the same as its position in the .exe debugged.
I remembered that an old friend of mine told me once that there was a formula to get the "true" address with the instruction inside the .exe. Cheat engine gives me the Memory Address. I remember that in the math formula, I needed to get the Module, in OllyDbg i get it. But i can't remember the formula. Somebody know how is that math formula? The formula it's very simple! There's another way to get the file position to permanently modify the .exe?
Of course there is a formula.
We just need to reverse what the PE loader do:
These numbers are called file offset.
These addresses are called RVAs (Relative Virtual Addresses)
These addresses are called VAs (Virtual Addresses) and are the final addresses of the file offsets.
To help understand the steps below keep in mind that every PE section has, among others, the following properties:
So for a given memory address X, you have to:
There is a tool called PEEditor 1.7 that can do this for you.
For some reason it is now harder to find it, but it should still be there on the Internet. Remember: It is free.
Found PE editor: Here , use the password tuts4you to unpack the RAR archive.
Load a file (by dragging into the window or using Browse button) and than hit FLC (File Location Calculator). In the new window, enter the address.
1 Mostly this is actually means that the base address set in the PE header is used, thanks to paging.
2 Computed as memory starting address+memory length-1
There's a "formula" but you'll actually need to look inside the executable file (although this formula can be simplified based on some assumptions).
Assumption
Usually (I insist on usually, sometimes it is not the case), [SECRVA] will be 0x1000 for the first section - which happens to be the code section - and its [SECRAWADDR] will be 0x400.
So if you are searching for the offset of an instruction based on its address in memory, you can usually assume that:
SECRVA = 0x1000
SECRAWADDR = 0x400
Example
Example based on cmd.exe.
Let's say I'm searching for this code at
0x1C34B0
when the program is loaded into memory:Notice the instruction opcodes (bytes) are:
0xE8B3040000
VA = 0x1C34B0
MODBASE = 0x1B0000
VA - MODBASE = RVA ;
0x1C34B0 - 0x1B0000 = 0x134B0
;RVA = 0x134B0
Opening binary file in PE editor (I use CFF explorer):
first section is .text, its VirtulAddress is 0x1000 and its VirtualSize is 0x23E4C (so the end of the section is at
0x1000 + 0x23E4C = 0x24E4C
).Is 0x134B0 between 0x1000 and 0x24E4C?
0x1000 >= 0x134B0 < 0x24E4C
-> True: so the address lies in the .text section.Note: repeat the same process for each section until you have found the right one.
SECRVA = 0x1000
(section Virtual Address)RVA - SECRVA = OFFSET ;
0x134B0 - 0x1000 = 0x124B0
SECRAWADDR = 0x400
(section Raw Address)OFFSET + SECRAWADDR = INSDATAOFFSET ;
0x124B0 + 0x400 = 0x128B0
If we look at
0x128B0
in the file we have:So we have found exactly the same bytes in file (
0xE8B3040000
) than in memory.