2

This may be a naive question, but I wonder when and why (non-research) developers would directly edit a binary file.

It seems like the only good reason to use one is to create a kludge when you need to fix and redistribute an unencrypted binary, and can't access the original source code or data storage.

But in all other cases, I can't imagine where this would fit in with regards to a development process. All the changes would become moot on the subsequent build, and would have to be reimplemented in source code.

  • Do you mean executable binary or binary data files? – Mawg Oct 16 '18 at 13:42
  • Personally, I have reverse engineered quite a few saved game files which were in binary format. Purely for personal interest ;-) – Mawg Oct 16 '18 at 13:56
5

Why directly edit binary files?

Same reason climbers climb mountains. Because you can.

<Rant about the name "Binary File">

Anytime you edit any kind of computer file you ARE editing a binary file. The text you're reading right now is stored on your computer as a binary file. You don't think of it as a binary file only because it is also encoded text. ASCII files, UTF-8 files, are all binary files.

This binary file is really a number. A long number but just a number. It's called binary because it's stored as 1's and 0's. That means it's expressed as a base 2 number. I could store the number for this answer in DNA and it would have 4 base pairs. It'd be the same number. Just expressed a little shorter and gooier.

As long as it's the same number how it's stored isn't important. However, people want to make a distinction between text files and non-text files.

Text files have a predictable encoding, so they can be presented by most any text editor in the manner they were intended to be seen.

Non-text files can be encoded any crazy way, so they can only be presented as originally intended by either the software they were intended for or software that has reverse engineered the format so they can also present them as originally intended.

These non-text files got named binary files. And they are. But so are text files.

I'm going to presume you mean: why directly edit non-text files.

</Rant about the name "Binary File">

If you know the format (the fields and how they are encoded) of a non-text file, every field has meaning for you. The same way it would in a text file. So you can edit it for all the same reasons you would edit a text file.

Indeed, the only reason not to edit such a file is because you don't know the format. Some companies like it this way and try to keep you from knowing the format so they can control their product. Users who can see past this shake off this control since this is their computer. Indeed even if this post were locked you could control how it looked on your computer by fiddling the file that was downloaded to your computer. You could make me say any silly thing.

That's the basics. Here's the fancy stuff.

It seems like the only good reason to use one is to create a kludge when you need to fix and redistribute an unencrypted binary, and can't access the original source code or data storage.

A good example of this is dll hooks. A dll is compiled set of functions. Your OS likely has a few of them. One of them is called any time you press a key. If I want to install a keylogger, I'd like to edit that 'binary' file to call my function so I can record your keystrokes.

This is why it's not a good idea to let just anyone edit your 'binary' files. But executed text files can play just as much havoc. Don't let just anyone touch your autoexec.bat file or they may just format your hard drive for fun.

But in all other cases, I can't imagine where this would fit in with regards to a development process. All the changes would become moot on the subsequent build, and would have to be reimplemented in source code.

Indeed, if the binary is generated by source code it's actually a very bad idea to make editing it by hand afterwards part of the development process. You should be able to build in one step. But this has nothing to do with it being a 'binary' file. Any generated file shouldn't be manually fiddled with. ANY manual steps in the build process are evil. Work hard to remove them from your development process. Failing to do so means you're failing the second step of the Joel test.

  • 1
    I think designers will regularly edit “binary files” without feeling bad about it. ;-) – 5gon12eder Apr 9 '16 at 3:22
  • 1
    I know of one good reason to do that when you have the source. It's how you teach a compiler what an 'A' is. Once it knows you can change your source for the compiler to be an 'A' and not the ascii code for 'A'. There was a nasty unix trojan in the c++ compiler for years based on this trick. It's part of bootstrapping a compiler so it can be written in it's own language. – candied_orange Apr 9 '16 at 3:52
  • 2
    "Because you can" is indeed a fantastic answer. Here's a guy that did exactly this, not by using a text or hex editor, but with a SNES D-pad. engadget.com/2016/03/29/flappy-bird-super-mario-world-hack – RubberDuck Apr 9 '16 at 12:49
  • 1
    @CandiedOrange That sounds like what Ken Thompson described in an acceptance speech (pdf link). A C compiler that recognized the login program's source code and inserted a backdoor, and that recognized its own source code, and inserted the trojan. Supposedly that compiler was never publicly released. – 8bittree Apr 11 '16 at 17:55
  • @8bittree I think you're right. I got a few details wrong but that is likely the source of the story I was remembering. Still, it goes to show that binary files should not be treated as something you can't touch. Working with them simply demands a little more effort from you. Thank you very much for the link. – candied_orange Apr 11 '16 at 18:23
1

To answer the question as to why someone might want to do such a thing... I'm working on an embedded controller that will receive a binary configuration file via its UART. Another developer is working on the tool that will ultimately build this config file and stream it down. His end isn't finished, so I'd like to manually build a file that I can stream down with a terminal program, just to perform some basic tests on my end. The terminal I'm using will allow me to send single digits in binary, but that's rather painful. It will also allow me to download a text file, so I'd really like to build a binary "text file".

A better option would be a tool that would that would take a hex text file and stream it out as binary. I don't know if such a thing exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.