Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change .exe with hexeditor [closed]

I wrote a little program in C++:

#include <iostream>

int main()
{
    int input;

    std::cin >> input;

    if(input == 5){
        std::cout << "Input == 5";
    }
    else{
        std::cout << "Input != 5";
    }

    return 0;
}

I have already built the program. The working program is in the Release folder. Now I want to change the if statement, without changing the C++ code. I downloaded a hex editor and opened the file. Inside the .exe is a lot. I googled for the problem and found this very nice image:

I searched inside the hex editor for my output Input == 5. I found it. When I change it to something different and execute the file, the program displays the new entered message, not the old one.

But now I want to change the structure of the code (the if statement). I searched for if, but didn't find anything. So, where is the code section (image)?

like image 668
MyNewName Avatar asked Mar 13 '23 11:03

MyNewName


2 Answers

C++ is a high-level language. It is written in "source" (plain text, i.e. if ( ... )), and a compiler translates that to machine code.

Machine code is a very different, low-level language. For one, what C++ does with "if ... else", machine code does with a "conditional branch instruction", which is a (sequence of) specific byte values, i.e. what you see in the hex editor. The "if" no longer exists.

The specific command set, and the byte values representing those commands, differ from CPU family to CPU family.

If you are really interested in machine code, check out The Art of Assembly Language Programming by Randy Hyde. It has a very good introduction to x86 assembly and machine code.

Generally speaking, though, you won't need more than token knowledge of ASM / machine code, since the subject pops up rather seldom even on the professional level, unless you are working on operating systems and / or device drivers (and most of the time not even then).

like image 137
DevSolar Avatar answered Mar 27 '23 15:03

DevSolar


You will not find an if statement in your executable because it will have been translated to machine language, which is the entire purpose of the compilation process -> to translate your C++ code to something that the machine knows how to execute.

If you want to 'hack' your .exe, what you need is a disassembler and the knowledge to use it, and that is a long story, which is why your question is probably too broad for Stackoverflow.

like image 27
dandan78 Avatar answered Mar 27 '23 17:03

dandan78