Is it possible to change a string/char/variable in a binary file ?

Hi,


I have a very basic question I was hoping someone could help me with. Let say,and this is just theoretical situation, I compile a program and at compile time set a variable char a = 'A' (or a string, it does not matter). Is it possible, once the binary is created to go into that file and change that character (string) into something else, let say 'B' without messing with a source file. And how difficult would that be ?


Thnx
yes.
it can be anywhere from really easy to near impossible.
easy: open hex editor, find string, and modify it. This works pretty well so long as you do NOT TRY TO INCREASE the size of the string, eg you can't change 'hello' to 'hello world' without great risk. The reason is that adding bytes can shift things that were at known locations, breaking stuff. making it shorter DOES work, because you can just interject a zero into it: change 'hello' to 'wow\0' for example and it will work, and the extra left over chars from 'hello' do no harm.

some programs self-check with a self-crc or the like and refuse to work if they detect a modification. Anti-cheat software of many games will go haywire if you change the program as well. There are a lot of modern things in professional software like this that prevent doing this kind of 1980s hackery, but you can certainly still do it on many other programs including your own.

hard: some better hackers fix the program AND its anti-modification checks so it still works. You can hack on it to bypass the check, or fix the hash value, etc. This is a ROYAL PAIN. Some programs will self repair, esp if internet aware. This can be incidental (update overwrote your change) or active (try modifying a core windows executable, it will be reverted as proactive self defense by the OS, a form of antivirus).
Last edited on
> I compile a program and at compile time set a variable char a = 'A' (or a string, it does not matter).

Instead of hard coding the value to be set in the program, read the value from a data file and set the value of the variable to that. It is trivial to modify the contents of the data file and run the program again.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::string str ;

    {
        // name of file which contains our variable values
        const std::string var_file_name = "my_var_values.txt" ;

        std::ifstream file(var_file_name) ; // open the file for input
        std::getline( file, str ) ; // read the value from the file

        if( str.empty() ) // if input failed
        {
            str = "default value of str" ; // give str a default value
        }
    }

    std::cout << "value of string str: " << str << '\n' ;

    // rest of program
}

echo && echo && g++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors -pthread -march=native main.cpp 
echo && echo 'running the program with nothing in the file' && ./a.out 
echo && echo 'writing the value of the variable into the file and running the program again'
echo 'this is the value of the variable read from the file' >> my_var_values.txt && ./a.out
echo && echo 'modifying the value in the file and running the program once again'
rm  my_var_values.txt && echo 'this is the modified value of the variable' >> my_var_values.txt && ./a.out



running the program with nothing in the file
value of string str: default value of str

writing the value of the variable into the file and running the program again
value of string str: this is the value of the variable read from the file

modifying the value in the file and running the program once again
value of string str: this is the modified value of the variable

http://coliru.stacked-crooked.com/a/1cc1badc68d2ba2b
well, yes lol. If the program is YOURS, there is no sense in hex-editing the executable, fix the source and recompile it. The only reason to do it to your own program would be to learn the process or study something.

there are some screwy things you can do; self modifying programs are possible but it has little practical value. The closest I have come to this is we used to wire our programs so that renaming them to a specific name would run alternate code. You can do the same with runtime args / flags but we did it that way so the customer could NEVER accidentally run the demo or debug modes; renaming the program is a very intentional act while messing up a commandline flag can be done easily.
Last edited on
Topic archived. No new replies allowed.