Aistun Aistun - 2 years ago 125
Linux Question

How to inject an executable into another on ubuntu

It's my first post on this forum.
I'm programming on ubuntu.
I'd like to inject, using C++, an executable into another and when I launch the final executable, I want it to execute its code and the injected code.
I don't know if it is possible.
I already tried to inject the binary content of the second into the first but it doesn't seem to work, it execute the first program but not what i injected.
I know that's the principle of a lot of viruses but i don't want to program a virus it's just for learning and for curiosity.
Thanks for your answers.

Answer Source

You cannot inject a random executable into another. If you stop to think about it, you will see that it is not clear that that is even supposed to do. Both executables have an opinion regarding what 100% of the CPU time alloted to the process should be doing, and there is no generic way to merge them.

If the injected executable is meant for injection, than it must also come with the injection methodology. The common means to do that are by using LD_PRELOAD (e.g. fakeroot) or by using ptrace (e.g. fakeroot-ng). Neither one of those techniques changes the actual executable file.

If you want to change the executable file in order to do what you are trying to do, you will need to understand the ELF format. This is out of scope of a stack overflow answer, however.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download