1

if I opened an executable in a HEX Editor would I see 1's and 0's in hexadecimal notation?

Also is it theoretically possible to write an executable by hand, for something simple like a "Hello World" program?

Francis
  • 919
  • 3
  • 14
  • 23
  • theres nothing simple about a hello world program. There is no magic to the files, basically any file a program can write you can write too, by hand, if patient enough. Bits is bits... – old_timer Jul 17 '14 at 20:07

2 Answers2

3

If you try to see an executable file in text representation, in any text editor, you will see something like:

^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^B^@>^@^A^@^@^@@^D@^@^@^@^@^@@^@^@^@^@^@^@^@x^Q^@^@ ^@^@^@^@^@^@^@^@@^@8^@ ^@@^@^^^@ES

This is because there bytes, codes which are not text symbols according to ASCII.

But if you open it in a HEX editor, it will be like:

00005f0: 0100 0200 4865 6c6c 6f20 576f 726c 6400  ....Hello World.
0000600: 011b 033b 3400 0000 0500 0000 00fe ffff  ...;4...........
0000610: 8000 0000 40fe ffff 5000 0000 2cff ffff  ....@...P...,...
0000620: a800 0000 50ff ffff c800 0000 e0ff ffff  ....P...........
0000630: f000 0000 0000 0000 1400 0000 0000 0000  ................

Here bytes are represented in HEX.

If you need to modify an executable directly, you will most likely find HEX editing as a better way.

Then you can open it in the HEX editor:

enter image description here

Change a bit:

enter image description here

and here is a result:

ruslan@ruslan-Latitude-E6430:~/Documents$ ./hello 
Hello+World
Ruslan Gerasimov
  • 1,752
  • 1
  • 13
  • 20
2

On question one: yes, you would see a bunch of hex digits. But the same is true of any file format. If you open a text file in a hex editor, you'd still see a bunch of hex digits. However, if you open a text file in a text editor (e. g. Notepad), you'd see a text, and if you open an executable in a text editor, you'd see nonsense, maybe with some legible text fragments in it.

Yes, it's theoretically possible to create an executable file by hand in a hex editor. With some executable formats it's easier, with some - much harder. The very old COM format of MS-DOS is the simplest - it has no header whatsoever, the executable code starts at the beginning of the file. The smallest valid COM file is just a few bytes long (maybe even one, I forgot). Modern executable formats - PE, ELF - have a fairly large header that describes the rest of the file, specifying the target machine/OS, where's code, where's data, what functions does it import from dynamic libraries, etc. While creating a file by hand, you'd have to fill out all those header fields, following a spec.

In general, this is not a fruitful endeavor - a compiler/linker combo will create an executable for you with much less effort.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • I think the question wasn't eyeballing bypassing the compiler/linker process, but more trying to get an understanding of if executable files are just "regular" files or if there's OS magic involved. That's my guess. – Mooing Duck Jul 17 '14 at 19:19
  • 4
    There's no magic to the format itself :) The execute permission on *nix is the closest thing to OS magic there is. – Seva Alekseyev Jul 17 '14 at 19:21
  • https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html makes a tiny ELF executable "by hand", using NASM source code for flat binary output to define every byte of the output file. Fun fact: 3 of the first 4 bytes of an ELF file are actually ASCII text: the 4-byte "magic number" for the file format is `"\x7fELF"` – Peter Cordes Aug 01 '19 at 20:03