BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Storing an EXE within another EXE?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Steve
Guest





PostPosted: Sun Dec 28, 2003 9:38 pm    Post subject: Storing an EXE within another EXE? Reply with quote




Strange question this, but I'd like to know if it's possible to store
one program inside another and pass control over to it from the main
EXE. A bit like a self extracting EXE except it only extracts the EXE
and runs it, not save it to disk.

Any thoughts?.
Back to top
KLinZ
Guest





PostPosted: Sun Dec 28, 2003 9:55 pm    Post subject: Re: Storing an EXE within another EXE? Reply with quote



Steve wrote:

Quote:
Strange question this, but I'd like to know if it's possible to store
one program inside another and pass control over to it from the main
EXE. A bit like a self extracting EXE except it only extracts the EXE
and runs it, not save it to disk.

It's possible if you have enough knowledge of the PE (Portable Executable)
format. You have to find out the start address, which can be found in the PE
header (ImageNtHeaders.OptionalHeader.AddressOfEntryPoint), and JMP to it.

Have look at how executable packers like UPX (upx.sourceforge.net) do it.

--
www.zenobits.com


Back to top
Sonny Grunter
Guest





PostPosted: Mon Dec 29, 2003 9:12 am    Post subject: Re: Storing an EXE within another EXE? Reply with quote



Steve <none (AT) none (DOT) com> wrote

Quote:
Strange question this, but I'd like to know if it's possible to store
one program inside another and pass control over to it from the main
EXE. A bit like a self extracting EXE except it only extracts the EXE
and runs it, not save it to disk.


Yes it's possible. Here's how (and why) I did it:

I once wrote a small pre-installation utility to compensate for some
software that had %WINDIR%TEMP hardcoded as the temporary directory.
The software failed for non-Admin users on Windows 2000 as they no
longer had permissions to %WINDIR%TEMP as they did in previous
versions of Windows. The purpose of my utility was to assign Full
Control permissions to %WINDIR%TEMP for all users of the PC. I did
it by wrapping CACLS.EXE inside of a Delphi program which was included
in the Installshield package and executed before the actual software
installation began. The job of the Delphi program was to extract
CACLS.EXE, script it to make the permission changes, and then delete
it. The installation was done hundreds of times and my Delphi
solution worked like a hot damn.

(I wrote this in a hurry and it was intended as short-term throwaway
code so please excuse any bad coding practices)


File1: CACLS.rc
Notes: - Use BRCC32.exe to compile


- - - start - - -
W2KCACLS RCDATA CACLS.EXE
- - - end - - -



File2: Main.pas
Notes: - The "{$R CACLS.RES}" compiler directive embeds the resource
into the exe
- The ExtractResource function pulls it out at runtime


- - - start - - -
{* This program assigns Full Control folder permissions on
%WINDIR%TEMP
* to BUILTINUsers. It does this by scripting the CACLS command which
* is included as a resource in the EXE.
*}

unit Main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, FileCtrl;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
sWinDir: String;
sTmpDir: String;
ErrMsg: String;
function ExtractResource: Boolean;
function WinExecAndWait(sCmdLine: String): Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
{$R CACLS.RES}

function TForm1.WinExecAndWait(sCmdLine: String): Boolean;
var
StartUpInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartUpInfo,SizeOf(TStartUpInfo),0);
With StartUpInfo Do Begin
cb := SizeOf(TStartUpInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := SW_SHOWNORMAL;
End;

Result := CreateProcess(nil, PChar(sCmdLine), nil, nil, TRUE,
CREATE_NO_WINDOW, nil, nil, StartUpInfo,
ProcessInfo);
If NOT Result Then
ErrMsg := 'CreateProcess Failed' + #13#10 +
SysErrorMessage(GetLastError)
Else Begin
WaitforSingleObject(ProcessInfo.hProcess, 10000);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
End;
end;

function TForm1.ExtractResource: Boolean;
var
MyRes : Integer;
MyResP : Pointer;
MyResS : Integer;
MyMS : TMemoryStream;
begin
MyRes := FindResource(HInstance, 'W2KCACLS', RT_RCDATA);
If MyRes = 0 Then Begin
ErrMsg := 'FindResource failed.' + #13#10 +
SysErrorMessage(GetLastError);
Result := FALSE;
Exit;
End;

MyResS := SizeOfResource(HInstance,MyRes);
MyRes := LoadResource(HInstance,MyRes);
If MyRes = 0 Then Begin
ErrMsg := 'LoadResource failed.' + #13#10 +
SysErrorMessage(GetLastError);
Result := FALSE;
Exit;
End;

MyResP := LockResource(MyRes);
If MyResP = nil Then Begin
ErrMsg := 'LockResource failed.' + #13#10 +
SysErrorMessage(GetLastError);
FreeResource(MyRes);
Result := FALSE;
Exit;
End;

MyMS := TMemoryStream.Create;
With MyMS Do Begin
Write(MyResP^,MyResS);
MyMS.SaveToFile(sTmpDir+'W2KCACLS.exe');
Free;
End;
UnlockResource(MyRes);
FreeResource(MyRes);
Result := TRUE;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
If Win32Platform = VER_PLATFORM_WIN32_NT Then Begin // this utility
only applies to WinNT

// find out what directory Windows is installed in
SetLength(sWinDir, MAX_PATH);
SetLength(sWinDir, GetWindowsDirectory(PChar(sWinDir), MAX_PATH));
sWinDir := sWinDir + '';

// find out what Windows is using for a TEMP directory
SetLength(sTmpDir, MAX_PATH);
SetLength(sTmpDir, GetTempPath(MAX_PATH, PChar(sTmpDir)));

// create %WINDIR%TEMP if it doesn't exist
If NOT DirectoryExists(sWinDir + 'Temp') Then
MkDir(sWinDir + 'Temp');

// extract the Windows 2000 CACLS utility
If NOT ExtractResource Then Begin
MessageDlg(ErrMsg, mtError, [mbOK], 0);
Exit;
End;

// CACLS may not be able to add Full Control permissions if there
are
// already special permissions assigned. The safest way to ensure
// success is to first remove all existing permissions before
attempting
// to assign new permissions
If NOT WinExecAndWait(sTmpDir + 'W2KCACLS.exe ' + sWinDir + 'Temp
/e /r BUILTINUsers') Then
MessageDlg(ErrMsg, mtError, [mbOK], 0);

// run the CACLS utility to assign Full Control to BUILTINUsers
If NOT WinExecAndWait(sTmpDir + 'W2KCACLS.exe ' + sWinDir + 'Temp
/e /g BUILTINUsers:F') Then
MessageDlg(ErrMsg, mtError, [mbOK], 0);

// delete the extracted CACLS utility
DeleteFile(sTmpDir + 'W2KCACLS.exe');
End;

// let's call it a day
Application.Terminate;
end;

end.
- - - end - - -

Back to top
KLinZ
Guest





PostPosted: Mon Dec 29, 2003 2:20 pm    Post subject: Re: Storing an EXE within another EXE? Reply with quote

Sonny Grunter wrote:

Quote:
Steve <none (AT) none (DOT) com> wrote in message
news:<n4juuv47m18mbitsg6pnt0o0h9fver0834 (AT) 4ax (DOT) com>...
Strange question this, but I'd like to know if it's possible to store
one program inside another and pass control over to it from the main
EXE. A bit like a self extracting EXE except it only extracts the EXE
and runs it, not save it to disk.

Yes it's possible. Here's how (and why) I did it:

<extract executable from resource to disk and execute it>

You forgot the 'not save it to disk' part :-)

--
www.zenobits.com

Back to top
Sonny Grunter
Guest





PostPosted: Tue Dec 30, 2003 7:03 am    Post subject: Re: Storing an EXE within another EXE? Reply with quote

"KLinZ" <klinz (AT) NOSPAMzenobitsSPAMMERTSNO (DOT) com.invalid> wrote


Quote:
You forgot the 'not save it to disk' part Smile

Yeah, sort of, but not exactly :-)

I do extract the file to a temp directory but the last few lines of
code delete the file after we're finished with it. I hoped that would
suit Steve's purpose. Frankly, I don't even know if it's possible to
extract and run an EXE directly *without* using a temp file.

Back to top
Steve
Guest





PostPosted: Tue Dec 30, 2003 8:51 am    Post subject: Re: Storing an EXE within another EXE? Reply with quote

On Tue, 30 Dec 2003 09:27:56 +0100, "Bjørge Sæther"
<bjorge (AT) hahaha_itte (DOT) no> wrote:

Quote:
Sonny Grunter wrote:
"KLinZ" <klinz (AT) NOSPAMzenobitsSPAMMERTSNO (DOT) com.invalid> wrote in
message news:<aNWHb.5341$7U1.65171@amstwist00>...

You forgot the 'not save it to disk' part :-)

Yeah, sort of, but not exactly :-)

I do extract the file to a temp directory but the last few lines of
code delete the file after we're finished with it. I hoped that would
suit Steve's purpose. Frankly, I don't even know if it's possible to
extract and run an EXE directly *without* using a temp file.

One could end up discussing terms, but . . . . a RAM disk ?

Is it easy to set up a RAM disk? On all versions of windows?

Back to top
KLinZ
Guest





PostPosted: Tue Dec 30, 2003 11:00 pm    Post subject: Re: Storing an EXE within another EXE? Reply with quote

Sonny Grunter wrote:

Quote:
Frankly, I don't even know if it's possible to
extract and run an EXE directly *without* using a temp file.

Yes you can, see my first reply.

--
www.zenobits.com

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.