LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2009, 08:45 AM   #1
gulfstream
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
How to add code to binary file?


Hello,

How can I add programming code to existing binary program, is it possible? Like Inno setup installer is additional code over the package inside and when I run the setup.exe (produced by inno) it is not started the program inside, but the additional code over it and then the files are installed/extracted.

I just search for some way to run code before the main linux console binary file (or at the end of this file), but with scripting will be visible to others the programming code.

Like:

script.sh:
#!/bin/bash
./my_code.exe
./standard_linux_code.exe (linux binary)

Can I do it with zip library. What is the easy way?

Thank you.

Gulfstream
 
Old 08-20-2009, 09:32 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Can't be done.

Imagine the security implications of being able to inject 'extra' code into previously compiled binaries! Scary.

I'm guessing you're up to no good, why would you be concerned if the end-user could see you were running one program before another?
 
Old 08-20-2009, 09:39 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by gulfstream View Post
Hello,

How can I add programming code to existing binary program, is it possible? Like Inno setup installer is additional code over the package inside and when I run the setup.exe (produced by inno) it is not started the program inside, but the additional code over it and then the files are installed/extracted.

I just search for some way to run code before the main linux console binary file (or at the end of this file), but with scripting will be visible to others the programming code.

Like:

script.sh:
#!/bin/bash
./my_code.exe
./standard_linux_code.exe (linux binary)

Can I do it with zip library. What is the easy way?

Thank you.

Gulfstream
It may be possible in some cases. Start from here: http://en.wikipedia.org/wiki/Dynamic_linker#LD_PRELOAD .
 
Old 08-20-2009, 09:57 AM   #4
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Safe.
:O
 
Old 08-20-2009, 10:39 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you have the ability to modify file locations (ie root privileges), then you can create a simple compiled and linked binary which can load and run the original runtime binary.

Code:
#include <stdio.h>
#include <stdlib.h>

int childPid;

int main(int argc, char * argv[] ){

    childPid = fork();
    if( childPid ){    /* I am the parent... */
        /* do something or wait for child */
    }
    else{              /* I am the child...  */

        /* Maybe do some processing here; depends on your requirments...
        */
        execvp( "/the/original/program", argv );

        /* We never return from exec...   */
    }
}
Once this is compiled and linked, you still have to arrange that it gets run by default, instead of the original code. That requires moving/renaming files, hence the requirement to be root.

--- rod.
 
Old 08-20-2009, 12:10 PM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
PDV -payload delivery vehicle, can be used to create a binary ELF self-extracting archive which extracts a payload from itself when run and can execute a script at the same time.
 
Old 08-20-2009, 12:54 PM   #7
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by rizhun View Post
Can't be done.

Imagine the security implications of being able to inject 'extra' code into previously compiled binaries! Scary.

I'm guessing you're up to no good, why would you be concerned if the end-user could see you were running one program before another?
Actually, that's how many programs work. Look up Dyninst, or as one responded wrote - LD_PRELOAD. Some unix flavors don't support LD_PRELOAD, or have that functionality disabled for security reasons.

Afa the original question goes, there are many ways of doing what you require. Some would have you simply put a marker at end of the file, write your binary into the file, and then search out the marker for extraction. Many of the SUN / nVidia .bin files work by having a script with the tar embedded (and you can probably have some encryption/password for protection). As one user noted, PDV is a way of accomplishing a 'self-extracting' file.
 
Old 08-20-2009, 01:30 PM   #8
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
gulfstream: what are you trying to accomplish by appending another binary to an existing one. It probably can be done if you're compiling the binary yourself, but otherwise you'd have to append the binary and find a way to jump to the new instructions in this new appendage, not at all easy.

If you goal is to create an installer, it can be accomplished with a shell script. I've done it many times before and it's fairly simple, there are examples all over the internet.
 
Old 08-20-2009, 02:03 PM   #9
gulfstream
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
how to add code to binary file

Hello,

I just want to add binary to binary code (no src available) and I thought that I can do it somehow - to make new binary, like copy-paste of text to text via cat:-) it's like a small program preparing some things before to run other binary. I like the easy simple way like theNbomr wrote in his code. I already had similar idea. With editor can be traced the command that will be started in shellexec/shell, this will be no problem at all.

Thank you for helping me with your ideas!

Gulfstream
 
Old 08-20-2009, 02:19 PM   #10
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Yes, It can be done if you are compiling your own program.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating binary for C code ravish.kanika Linux - Newbie 2 02-27-2008 05:46 AM
How to add some bytes to a binary file? kornerr Linux - General 6 02-28-2006 11:23 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Add precompiled binary to a rpm file doris Linux - Software 1 08-06-2004 07:00 PM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration