i want to change the value of an integer declared variable in an executable, by using the hex editor only suppose i know that there's a variable type int declared in the code and the variable is this:
int value = 1337;
i want to edit the executable using a hex editor search for the value 1337 and change it to something else, i tried ghex in ubuntu but i don't know how to search for it i converted it to hexadecimal but i didn't find it, thanks in advance guys.
First, you would use
readelf
to determine the virtual address of the variable (where it lives in memory after the program is loaded).-s
will show you the symbol table, and we'll grep for the name of your variable.This will output a line that looks like:
So here, the 64th symbol in the file is
value
. Its load address is 0x60102c, and it's 4 bytes in size. Now we have the virtual address, but this doesn't tell us where it's at in the file. To do that, we need to do three things:Let's run
readelf
again.-S
will list the sections.Here's a snippet of the output. Remember the address of our variable is at
60102c
, and we're looking for the section where60102c
lies between itsAddress
and itsAddress + Size
. Since this is a read-write variable, we can take a guess that it will be in the.data
section.Sure enough,
.data
lives in memory at601028
to601028+8 = 601030
. Subtractingvalue
's address from this section's address, we get:Thus,
value
is at offset 4 from the start of the.data
section. Now, where in the file is the.data
section? That's what theOffset
column tells us..data
begins at file offset1028
. Knowing this, we can find the file offset ofvalue
:We've got our file offset, now let's make sure we know what to expect. Your variable has the value 1337. In hex, that's 0x539. But, we need to bring up byte order (or "endianness"). Intel x86 systems are little endian. That means when an integer larger than one byte is stored at an address, the least-signifiant byte (or "little" end) of the value is at that address, and the remaining bytes are at subsequent (increasing address).
So your 1337 will be stored (as a 4-byte
int
) in the file like this:On a "big endian" system (e.g. Motorola 68k), the value would be seen in the file in the opposite order:
That all said, if you open your ELF file in a hex editor, an go to offset 102c, you will see your value:
ELF files have no checksum or CRC, so you should be able to simply edit that value in your hex editor, and it will have the new value when your program executes!