1

Currently writing a program that opens a file, moves to the given offset & then change the bytes at the offset.

This is the code:

void write(int offset, int modifiedBytes) {

    fstream Binary("path/to/file");

    if(!Binary) {
        cout << "File not found!";
    }

    Binary.seekg(offset); //Go to given position from the given binary file
    Binary.write((char *)&modifiedBytes,4); //put modified bytes into the given position from above
}

and then I have a separate function to add multiply things to it if I want:

void doInjection() {
    write(0xfe,0x7047); //write 7047 to 0xfe
}

although it does write to the given file, it writes 7047 in reverse (4770).

I've also tried to use put(); instead of write, but that allowed me the only use 2 bytes, but it didn't write in reverse.

Anyone has any idea why it's writing my given value in reverse?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

1 Answers1

0

Everything what you are doing depends of de machine and its architecture. Also it can work as unexpected depending on the size of int, that against depends of the cpu architecture.

Take a look to endianness

Mquinteiro
  • 1,034
  • 1
  • 11
  • 31
  • Ahh, that's intresting, didn't know about that. Thanks a lot! :) –  Jul 06 '18 at 18:19
  • @JoeyJurjens I'd advise to go with the `hton()` / `ntoh()` low-level networing functions. If your OS network bindings aren't flawed, these should work reliablly. – πάντα ῥεῖ Jul 06 '18 at 18:24