What I’m trying to do is to sign my compiled executable’s first 32 bytes with a version signature, say “1.2.0” and I need to modify this signature in runtime, keeping in mind that:
- this will be done by the executable itself
- the executable resides on the client side, meaning no recompilation is possible
- using an external file to track the version instead of encoding it in the binary itself is also not an option
- the solution has to be platform-independent; I’m aware that Windows/VC allows you to version an executable using a .rc resource, but I’m unaware of an equivalent for Mac (maybe Info.plist?) and Linux
The solution in my head was to write the version signature in the first or last 32 bytes of the binary (which I didn’t figure out how to do yet) and then I’ll modify those bytes when I need to. Sadly it’s not that simple as I’m trying to modify the same binary that I’m executing.
If you know of how I can do this, or of a cleaner/mainstream solution for this problem, I’d be very grateful. FWIW, the application is a patcher/launcher for a game; I chose to encode the version in the patcher itself instead of the game executable as I’d like it to be self-contained and target-independent.
Update: from your helpful answers and comments, I see that messing with the header/footer of the binary is not the way to go. But regarding the write permission for the running users, the game has to be patched one way or another and the game files need to be modified, there’s no way to circumvent that: to update the game, you’ll need admin privileges.
I would opt for using an external file to hold the signature, and modify that with every update, but I can’t see how I can guard against the user spoofing with that file: if they mess up the version numbers, how can I detect which version I’m running?
Update2: Thanks for all your answers and comments, in truth there are 2 ways to do this: either use an external resource to track the version or embed it in the main application’s binary itself. I could choose only 1 answer on SO so I did the one I’m going with, although it’s not the only one. 🙂
If your application is meant to patch a game, why not embed the version in there while you’re at it? You can use a string like @Juliano shows and modify that from the patcher while the game is not running – which should be the case if you’re currently patching anyways. 😛
Edit: If you’re working with Visual Studio, it’s really easy to embed such a string in the executable with a
#pragma comment, according to this MSDN page:Since the second argument is a simple string literal, it can be concatenated, and I’d have the version in a simple
#define: