I am trying to “wet” a C ++ binary with various bits of information. Including this information during assembly is difficult as I do not control the assembly process. So far, the only idea close to what I have in mind is what I got from here , and this is a script that, when setting the C ++ binary, creates a Bash script, as shown below.
In short, it adds to the original binary a Bash if-else, which checks if the first argument is “--version” (in this case it passes some information) or not (in this case it simply decodes itself into the file “originalBinary” then executed. / originalBinary).
This is obviously not ideal:
- Now I have 2 copies of the binary (which can be problematic for large binary files) and
- process start started with
./originalBinary , which confuses someone who doesn't know what is going on
I am wondering if I can do something like replacing the ./originalBinary call with a special exec $0 , where I can also tell exec not to read the file from the very beginning, but using an offset of, say, 100 characters (or whatever the length of the bits Bash at the beginning was not).
Another idea was to do a Bash script editing, i.e. delete the first 21 lines with sed, call
./$0 to call yourself, and then add if-else back when the
./$0 command
./$0 . This, however, seems fragile (what if the machine works before the call returns?).
Finally, it seems that this will fail if the binary is a shared resource, since the linker will be confused with the Bash material at the beginning when it tries to load the library :(
Alternatively, can you suggest any other way to annotate a C ++ binary post build?
I considered the question of preparing an object file with the necessary information, rather than tying it to a given binary file, but this requires that I somehow convert ELF back to the object files that came into it, add my object file to the list, then re-link (I get the impression from here that this can be done with objcopy , but I have not yet managed to get this to work). In addition, the problem with this approach is that there is no good way to return information, for example, invoke binary code using "--version".
Am I trying to do something impossible? I hope I clearly explained the situation.
Thanks.
#!/bin/bash function PrintInformation() { echo "various bits of information" } if [[ $# -eq 1 && "$1" == "--version" ]]; then PrintInformation exit 0 else uudecode $0 ./originalBinary exit 0 fi begin 755 originalBinary M?T5, 1@ (!`0````````````(`/@`!````X`9```````!``````````'`1```` M`````````$``.``)`$``'@`;``8````%````0`````````!``$```````$`` M0```````^`$```````