Embedding my app in an existing exe.

Hi all,

I'm trying to write a program that injects my program into another existing executable so they are both executed when the original program is ran.

For example:

running notepad.exe would launch notepad and my program which is nested within it.

It's kinda like

Notepad
========
Runs Notepad.exe

MyNotepad
=========
Runs Notepad.exe
and MyApp.exe

The reason for doing this (no it's not to write a virus), i've been asked to write a program to control to some extent the distribution of applications, also to monitor its execution, time running, etc.

I need my application nested within the original exe as thats all I am given to work with, and I can not simply add the exe to the resources of my program as I can not guarentee how it will run.

Bit of a cmplicated post I know, the jist of it is, running my code along side existing code in another exe.

Help help help,

Cheers,

Chris.
[1044 byte] By [chris_gay] at [2007-11-19 7:21:40]
# 1 Re: Embedding my app in an existing exe.
Search CG and google for code injection. It might be what you are looking for.
PadexArt at 2007-11-11 0:27:45 >
# 2 Re: Embedding my app in an existing exe.
I'm trying to write a program that injects my program into another existing executable so they are both executed when the original program is ran.
Say, for a moment, lets assume that you have been successful in writing this program.

Don't you think that a good anti-virus will (and should) block it?
While your intentions are good, the technology in general is open to abuse.
Even a bug in such a software will cause the user inconvenience. :D

However, as far as gaining knowledge goes - I believe that it is nevertheless a good exercise! :thumb:
Share your experience, if you can! :)
Siddhartha at 2007-11-11 0:28:42 >
# 3 Re: Embedding my app in an existing exe.
Do you really need to embedd other apps? Wouldn't it be enough to just launch and control them?! Your app launcher can simply fetch launch instructions from a configuration file or registry. The simplest form is a list of applications (exe-image names or fullpaths) that it read and executed one-by-one with ShellExecute.

It can be vary challenging to embedd applictions fully; i.e. writing your own process loader. Another less challenging way is to store the embedded app to disk and then call CreateProcess/ShellExecute... but this is more or less the same as my first suggestion with a launcher app + config file (ie storing references instead of files).
j0nas at 2007-11-11 0:29:50 >
# 4 Re: Embedding my app in an existing exe.
It can be vary challenging to embedd applictions fully; i.e. writing your own process loader. Another less challenging way is to store the embedded app to disk and then call CreateProcess/ShellExecute... but this is more or less the same as my first suggestion with a launcher app + config file (ie storing references instead of files).Actually, no matter how hard you try, Windows will replace System files if you modify them. :D

1. Open notepad.exe in a binary editor (take a back-up first!) and
2. make changes
3. Save it in %windir%\notepad.exe (original location)

Do a diff between the backed-up version and this one. You may see a diff at the first instance. But, do it again after a few moments (I waited 10 secs), and voila, the file has been restored.

So, as long as re-saving the injected file goes - it is a wasted effort.:wave:
Siddhartha at 2007-11-11 0:30:44 >
# 5 Re: Embedding my app in an existing exe.
Actually, no matter how hard you try, Windows will replace System files if you modify them. :D

Yepp this is correct on XP and on W2K unless WFP is disabled.

I didn't suspect the OP was talking about replacing/changing system files.
j0nas at 2007-11-11 0:31:51 >
# 6 Re: Embedding my app in an existing exe.
I didn't suspect the OP was talking about replacing/changing system files.Yes, WFP protects notepad.exe too. ;)
Siddhartha at 2007-11-11 0:32:50 >
# 7 Re: Embedding my app in an existing exe.
Hi guys,

Sorry for delay in replying, had to go out and about. I would not be using this on system files, they would be apps developed by thrid party developers that I need to add secutiry (in some form to).

I had thought about various solutions, including embedding the exe within my programs resources and launching it from there, I hadn't got far enough into that to see if I could launch the embedded exe without writing it to disk when a collegue started working on this other method.

I'd been distracted some what and he asked for some help. Hence the initial post.

If I can launch the exe from within my application (unecryting and doing checks etc before its launched) then that would be perfect.

However I don't wish to be writing it to disk to launch it as when its there and unprotected it can obviously be copied and redistributed in iunprotected form.

I shall have a think on and post my solution when I finally come up with one, however if anyone has any nice code for launching a in memory exe that would be most helpful.

Thanks guys for your responses,

Cheers,

Chris.
chris_gay at 2007-11-11 0:33:53 >
# 8 Re: Embedding my app in an existing exe.
Yes, WFP protects notepad.exe too. ;)WFP protectes anything located in System32\dllCache although a user is running under admin permissions, WFP can be disabled.

Arjay
Arjay at 2007-11-11 0:34:56 >
# 9 Re: Embedding my app in an existing exe.
WFP protectes anything located in System32\dllCacheWell. Did you correct me there? :rolleyes:
Does there seems to be some confusion in language?

That location mentioned by you is the place where genuine versions of Protected Files are cached (or backed up).

EDIT: The WFP protects files in their original locations, and sources them from this path. So, notepad.exe is protected in %windir%
Siddhartha at 2007-11-11 0:35:49 >
# 10 Re: Embedding my app in an existing exe.
check MapViewOfFile with FILE_MAP_EXECUTE.
neo_the_1 at 2007-11-11 0:36:50 >