In-Memory execution

Started by Eros Olmi, December 08, 2014, 10:36:13 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Eros Olmi

I've searched in/out about that but not found a definitive solution.

I've an "application" called "MyApplication.EXE" stored as a binary resource inside another "executable" called "InMemoryExe.EXE".
I need to run the "MyApplication.EXE" from the binary memory buffer stored into "InMemoryExe.EXE" without storing "MyApplication.EXE" into a disk file and then shelling.

I know how to execute a DLL from memory, but not success with Exe.
I've found this but still no success: http://www.powerbasic.com/support/pbforums/showthread.php?t=48658&highlight=load+dll+memory

Does someone have an idea on how to do it?

Attached a Power Basic 10.x example of what I mean.
Here below source code of "MyApplication.EXE" and "InMemoryExe.EXE"

"InMemoryExe.EXE"

  #Compile Exe
  #Register None
  #Option Version4
  #Dim All
  #Optimize Speed
  #Option LARGEMEM32
 
  '----------------------------------------------------------------------------
  ' Resources: here we store the binary representation of MyApplication.exe
  '----------------------------------------------------------------------------
  #Resource RCDATA,   MYAPPLICATION     , ".\MyApplication.exe"


  '----------------------------------------------------------------------------
  ' Main entry point for the application
  '----------------------------------------------------------------------------
  Function WinMain(                                         _
                      ByVal hCurInstance  As Long         , _
                      ByVal hPrevInstance As Long         , _
                      ByVal lpszCmdLine   As Asciiz Ptr   , _
                      ByVal nCmdShow      As Long           _
                   ) As Long
  '----------------------------------------------------------------------------
    Local sEmbedded_MyApplication  As String
                                           
    '---Load binary representation of MyApplication.exe into a string buffer                                           
    sEmbedded_MyApplication  = RESOURCE$(RCDATA, "MYAPPLICATION")


    '---If string buffer is not empty
    If Len(sEmbedded_MyApplication) Then
                     
      '---Here we need to create a new process starting from sEmbedded_MyApplication
      '---without storing it into a disk file
     
      '????????????????????????
     
                     
      '---In the meantime ... Do somthing     
      MsgBox _
              "MYAPPLICATION resource buffer has been loaded." & $CrLf & _
              "MYAPPLICATION resource buffer size is:" & Str$(Len(sEmbedded_MyApplication)) & " bytes" & $CrLf & _
              $CrLf & _
              "And now?" & $CrLf & _
              "How to execute from the string buffer?", _
              %MB_TASKMODAL Or %MB_ICONINFORMATION, "InMemoryExe info"


    Else
   
      Beep
      Function = -1&
     
    End If


  End Function



"MyApplication.EXE"

  #Compile Exe
  #Register None
  #Option Version4
  #Dim All
  #Optimize Speed
  #Option LARGEMEM32
 
  '----------------------------------------------------------------------------
  ' Main entry point for the application
  '----------------------------------------------------------------------------
  Function WinMain(                                         _
                      ByVal hCurInstance  As Long         , _
                      ByVal hPrevInstance As Long         , _
                      ByVal lpszCmdLine   As Asciiz Ptr   , _
                      ByVal nCmdShow      As Long           _
                   ) As Long
  '----------------------------------------------------------------------------


    MsgBox "If you see this messagebox executing from InMemory ... you succeeded!", %MB_TASKMODAL Or %MB_ICONINFORMATION, "MyApplication info"

  End Function

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  

Marc Pons

Hi Eros, here is a link for similar job I've done with freebasic & c lib 

http://www.freebasic.net/forum/viewtopic.php?f=8&t=22915

I think it can be transpose to powerbasic ...  you will have the sources in basic and in c
and some exemples.

Something, I have noticed, it is sometime  treeky with program without any window like in console mode ,  so i created an hiden window to insure it is always 1 existing.

Be free to test, adapt to your needs, please give some feed back
Marc
  •  

Eros Olmi

Thanks, something to study.
If I will go to a conclusion I will feedback here.
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  

Theo Gottwald

That sounds interesting, Eros. If you have something in PB, can you share it?

Eros Olmi

So far I've found BoxedApp SDK: http://boxedapp.com/boxedappsdk/index.html

You can download the demo SDK, it has a popup message box but seems working fine.
There are some source code examples in C++, VB and Delphi.
Help file at http://boxedapp.com/download/BoxedAppGuide_en.pdf

I've tested it creating a virtual file, embedding my Exe into it and shelling the new process, all without writing any file to physical disk.
Seems to work.

Here the functions I've used:
Enum EFileAccess
    GenericRead = &H80000000
    GenericWrite = &H40000000
    GenericExecute = &H20000000
    GenericAll = &H1000000
End Enum

Enum EFileShare
    None = &H0
    Read = &H1
    Write = &H2
    Delete = &H4
End Enum

Enum ECreationDisposition
    CreateNew = 1
    CreateAlways = 2
    OpenExisting = 3
    OpenAlways = 4
    TruncateExisting = 5
End Enum

Enum EFileAttributes
    FileReadonly = &H1
    Hidden = &H2
    System = &H4
    Directory = &H10
    Archive = &H20
    Device = &H40
    Normal = &H80
    Temporary = &H100
    SparseFile = &H200
    ReparsePoint = &H400
    Compressed = &H800
    Offline = &H1000
    NotContentIndexed = &H2000
    Encrypted = &H4000
    Write_Through = &H80000000
    Overlapped = &H40000000
    NoBuffering = &H20000000
    RandomAccess = &H10000000
    SequentialScan = &H8000000
    DeleteOnClose = &H4000000
    BackupSemantics = &H2000000
    PosixSemantics = &H1000000
    OpenReparsePoint = &H200000
    OpenNoRecall = &H100000
    FirstPipeInstance = &H80000
End Enum

Declare Function BoxedAppSDK_Init Lib "ThinBasic_Bundle_UI_bxsdk32.Dll" Alias "BoxedAppSDK_Init" () As Long
Declare Function BoxedAppSDK_Exit Lib "ThinBasic_Bundle_UI_bxsdk32.Dll" Alias "BoxedAppSDK_Exit" () As Long

Declare Function BoxedAppSDK_CreateVirtualFileBasedOnBuffer Lib "ThinBasic_Bundle_UI_bxsdk32.Dll" Alias "BoxedAppSDK_CreateVirtualFileBasedOnBufferA"  ( _
                                                                                            ByVal lpFileName            As Dword , _
                                                                                            ByVal dwDesiredAccess       As Dword , _
                                                                                            ByVal dwShareMode           As Dword , _
                                                                                            ByVal lpSecurityAttributes  As Dword , _
                                                                                            ByVal dwCreationDisposition As Dword , _
                                                                                            ByVal dwFlagsAndAttributes  As Dword , _
                                                                                            ByVal hTemplateFile         As Dword , _
                                                                                            ByVal pData                 As Dword , _
                                                                                            ByVal dwSize                As Dword   _
                                                                                      ) As Long


Declare Function BoxedAppSDK_DeleteFileFromVirtualFileSystem Lib "ThinBasic_Bundle_UI_bxsdk32.Dll" Alias "BoxedAppSDK_DeleteFileFromVirtualFileSystemA"  ( _
                                                                                            ByVal lpFileName            As Dword   _
                                                                                      ) As Long



And here how I've used:

BoxedAppSDK_Init

    '---I embed UPX in my Exe as resource and read back at runtime into sEmbedded_UPX string
    '---Here I create a virual file
    sUPXFullPathName = sOutTempDir & sUPX_Name
    fIDUPX = BoxedAppSDK_CreateVirtualFileBasedOnBuffer( _
                                                              StrPtr(sUPXFullPathName), _
                                                              %EFileAccess.GenericWrite, _
                                                              %EFileShare.Read, _
                                                              0, _
                                                              %ECreationDisposition.CreateNew, _
                                                              0, _
                                                              %Null, _
                                                              StrPtr(sEmbedded_UPX), _
                                                              Len(sEmbedded_UPX) _
                                                            )

    '---After creating a virual file containing the UPX.EXE executable, I execute it shelling with some params
    lUPXProcId = Shell(sUPXFullPathName & " " & $Dq & gBundle_Application_ExeName & $Dq & " > null", 0)

BoxedAppSDK_Exit

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  

Marc Pons

@Eros

this solution is someting like a memory "virtual disk", adding an very hugge dll more than 1 Mo ...
in the side of your application ( yes outside  !!!)

It is funny to add a dll and claiming " all is inside the exe"  and not that enormous dll !!!

my proposal is much smaller , without dll  , all in a static lib ( freebasic / gcc static lib type),

is you want / accept  an extra  dll , no problem to provide one.

But I think with the skilled people on that forum mastering C , (much much better than I)
they could help to transpose the little c files to powerbasic.

in fact only 1 : main.c  ( for exe in memory ) because dll.c is to embedd dlls 
all you need is the fork process.

Marc
  •  

Eros Olmi

#6
Thanks Marc.

I think I've already found what I wanted and integrated very easily in my sources.
DLL dimension is not a problem for me because I'm using it in my application and not in something I distribute.

I also found very responsive and supporting people at http://boxedapp.com/boxedappsdk/ both via mail and skype.
Their SDK is working fine: virtual files, virtual directories and processes injected into stub fake process are working as I need.

Maybe others are interested in your offer.

Ciao
Eros
thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •