0

Imagine that following byte array contains all bytes of a executable file (.exe) (that on pratice this not is possible set inside source code because there are much bytes in this file type (.exe) and Visual Studio will crashe)

char buffer[] = { 0x60, 0x33, 0xC0, 0x33, 0xDB, 0xEB, 0x00, 0x03, 0xC3 };

Then i think that better way to achieve this is generate all bytes ( PS: like XTEA Runtime Crypter, but without variable declaration ) from .exe file and insert in a .bin file to set on char buffer[] later, something like this:

vector<char> buffer;

ifstream infile;
infile.open("bytes.bin", ios::binary);
infile.seekg(0, ios::end);
size_t file_size_in_byte = infile.tellg();
buffer.resize(file_size_in_byte);
infile.seekg(0, ios::beg);
infile.read(&buffer[0], file_size_in_byte);
infile.close();

Then i ask:

What's correct format of bytes to generate .bin file?

  1. 0x60, 0x33, 0xC0, 0x33, 0xDB, 0xEB, 0x00, 0x03, 0xC3 - preceded by 0x and separated by comma and space (like the linked C++ code already makes)?
  2. 0x600x330xC00x330xDB0xEB0x000x030xC3 - whitout any space/separation?
  3. 60, 33, C0, 33, DB, EB, 00, 03, C3 - separated by comma and space, but not preceded by 0x?
  4. Like showed in a Hex editor (not preceded by 0x and not separated by comma)?
Coringa
  • 406
  • 2
  • 12
  • 1
    Formatting values is for humans. Your computer is not a human. It will happily read bytes. – IInspectable Oct 08 '20 at 20:07
  • @IInspectable, then i can [load the executable file directly](https://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) to `char buffer[]` (or `vector buffer`) and this will be equivalent to representation > `char buffer[] = { 0x60, 0x33, 0xC0, 0x33, 0xDB, 0xEB, 0x00, 0x03, 0xC3 };` present on *.cpp*? – Coringa Oct 08 '20 at 20:30
  • 1
    Yes. Although that's not an executable. A PE file starts with the bytes `0x4D 0x5A`. – IInspectable Oct 08 '20 at 20:38
  • @IInspectable, thanks for the clarifications. +1 – Coringa Oct 08 '20 at 20:52

1 Answers1

0

You don't format at all, just write raw bytes. You are reading raw bytes using

infile.read(&buffer[0], file_size_in_byte);

Similarly, open your output file with ios::binary and simply call

outfile.write(&buffer[0], some_number_of_bytes);
jkb
  • 2,040
  • 1
  • 5
  • 8
  • `infile.read(&buffer[0], file_size_in_byte);` is same that `char buffer[] = { 0x60, 0x33, 0xC0, 0x33, 0xDB, 0xEB, 0x00, 0x03, 0xC3 };` on IDE (source code)? – Coringa Oct 08 '20 at 19:02
  • I'm supposing that you is considering `infile` == executable file (.exe). – Coringa Oct 08 '20 at 19:03
  • `infile` is not the executable, it is the file you opened, `bytes.bin`. I'm considering `outfile` to also be `bytes.bin`, or any other file you choose to open for output. – jkb Oct 08 '20 at 19:14
  • Then, but how could be the format of content to `bytes.bin`, this not comes in your answer. Read my question one more time please :D. – Coringa Oct 08 '20 at 19:19
  • Sorry, I don't think I understand what you're trying to accomplish, If you are trying to generate C++ source code that contains an array of bytes, then option 1 is probably what you want. What do you mean by "without variable declaration"? – jkb Oct 08 '20 at 19:38