Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
How can a x64 compatible application launch a process from memory (for example, loading this process data from a resource), with no existence of any executable (not even a "stub" one)?
Posted
Comments
Eugen Podsypalnikov 7-Mar-13 9:28am    
Hm... why not just the using of a temp file from a temp directory ? :)
Michael Haephrati 7-Mar-13 13:44pm    
A 100% virtual file system requires no temp files.
Sergey Alexandrovich Kryukov 7-Mar-13 14:40pm    
Hm. Really interesting question, my 5.
—SA
Michael Haephrati 7-Mar-13 14:41pm    
Thanks. I try to ask questions that I really can't find an answer for, but believe there should be an answer to...

It looks like you can launch a process from memory, but it's not easy at all.

First of all, I actually already started to explain how x86, x86-64 and Itanium hardware protection can protect population of memory the way it can be executed of code, but later realized that OS should provide a backdoor for such things.
(I don't want to discuss such detail as starting a new child process; the whole topic is so difficult that let's limit our discussions by the problem of putting some code in memory in a custom way, in particular, from some data stored in memory, in other words, without calling CreateProcessEx, and — most difficult part! — actually executing the loaded code.)

This hardware protection in the CPU protected mode is a very interesting and big topic. For some basic information, please see:
http://en.wikipedia.org/wiki/Data_Execution_Prevention[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366553%28v=vs.85%29.aspx[^].

At the same time, there is such thing as the OS loaded working when CreateProcessEx is called. The contradiction is actually resolved in the following way: these CPU have 4 protection rings: http://en.wikipedia.org/wiki/Protection_ring[^].
Windows uses only two rings (0 and 3) and forms so called kernel mode in the ring 0, and user mode in ring 3.

The population of memory marked as executable is only possible in kernel mode. Not only data execution is prevented, but also modification of executable memory pages is prevented. The kernel mode part of the loader writes in memory (translating relative addresses stored in PE files and doing a lot of other things). The method I'm familiar with is making an alias memory descriptor with read-write access pointing to the same physical memory, writing code as data, and them removing all the alias descriptors. One or another way, this is possible, and possible only in the kernel mode. When something invokes the loaded (which can be started from the Shell or by explicit call to CreateProcessEx), eventually the kernel-mode code described above is also invoked through the mechanism described here:
http://en.wikipedia.org/wiki/Protection_ring#Interoperation_between_CPU_and_OS_levels_of_abstraction[^].

However, Windows leave the user-mode backdoor to making executable pages of memory, so the code can actually be executed as data execution prevention won't affect those executable pages. I realized it when I remembered about the operation of .NET and especially Mono (importantly, 3rd-party) JIT-compilers:
http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

Indeed, just look at the Virtual Memory Windows API:

First of all, you can allocate or commit a region of pages in virtual address space with VirtualAlloc, VirtualAllocEx:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366890(v=vs.85).aspx[^].

Now, here is the key point: you can change the access protection of the allocated memory with VirtualProtect or VirtualProtectEx:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366899(v=vs.85).aspx[^].

Pat attention that you can first give a write access to memory, and later give it execution rights, again, in order to avoid data execution protection:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366786%28v=vs.85%29.aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366785%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366912%28v=vs.85%29.aspx[^].

For the whole topic, start here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366779%28v=vs.85%29.aspx[^].

As I say, the problem is really difficult but looks solvable. I could only provide a top-level overview and considerations.

Now, in practice, I cannot imagine that you really want to dig into this. Your question is interesting, but it lacks the explanation of your ultimate goals. I really doubt they require anything that fundamental. In practice, you should not really try to execute anything from memory you populate from your code, unless you are trying to create something really fundamental. If you want it, we can discuss it if you explain your real goals.

—SA
 
Share this answer
 
Comments
Michael Haephrati 8-Mar-13 4:18am    
Thanks for your top level coverage of the question and possible solutions.
My ultimate goals? Well, there are products that aim to create a temporary virtual environment, isolated and protected. For example, see: sandboxie.com and www.boxedapp.com. I was looking for a way to run an executable from memory and noticed that BoxedApp, for example, uses a "stub" process, which is a real file, to which the real contents of the memory to be run, is copied into. The process is created but suspended. Then the memory is replaced and finally the process starts. I was wondering if that is possible without such "stub" process, i.e. without any real executable (even not a fake one). There are working example of implementing such, but these work only on x86 (Win32) environment and not on x64 ones.
See: http://www.rohitab.com/discuss/topic/31681-c-run-program-from-memory-and-not-file/ . Some of the good examples of such questions can be only found at www.rohitab.com, etc. and not in MSDN.
By the way, regarding the discussion about DLLs and executables, you are right about the fact that these are both PEs, and are the same. The difference is the entry point. In the source code level there is very little difference and you can take the source code of an executable and easily compile and build it as a DLL and vice versa. In fact, the purpose of creating DLLs is to make certain functionality available to other applications via interfacing with it.
Michael Haephrati 8-Mar-13 5:19am    
Sergey, how about this solution?
http://www.rohitab.com/discuss/topic/37801-question-memory-execution/page__p__10083457#entry10083457
Sergey Alexandrovich Kryukov 8-Mar-13 11:29am    
Did you try it? It's not clear what kind of image it expects, so I don't know yet how to even test it. I'll take a look, thank you for sharing...
—SA
Michael Haephrati 8-Mar-13 11:34am    
In the example, it looks like the image is loaded form a EXE file... It would have been better proof of concept if the image was loaded from an internal resource. I guess it will not compile with a x64 environment, because the _CONTEXT data structure is different between x86 and x64.
Sergey Alexandrovich Kryukov 8-Mar-13 11:46am    
I'm not sure it's loaded from EXE file, but maybe. Will you try? You can use x86 as it is supported on 64-bit systems via WoW64. Of course you host application should be compiled to x86 target. It's not a great difference, 64- vs 32-bit systems, it's rather the matter of the principle...
—SA
You can use the LoadLibraryEx function[^], although I do not think that will work from managed (e.g. C#) code.
 
Share this answer
 
Comments
Michael Haephrati 7-Mar-13 11:52am    
I am not interested in c#, but nevertheless, to the best of my knowledge it will not work. Launching a process from memory is very tricky, especially when it comes to x64.
For example: http://www.rohitab.com/discuss/topic/31681-c-run-program-from-memory-and-not-file/ (which will only work on Win32 machines)
Richard MacCutchan 7-Mar-13 11:54am    
If you know it will not work then why ask the question?
Michael Haephrati 7-Mar-13 11:56am    
This specific solution will not work, which is why I am asking for a solution that will work. I certainly believe there should be a way
Richard MacCutchan 7-Mar-13 12:16pm    
Without being able to map addresses in the code and link them to DLLs correctly, I think it would be extremely difficult. However, there are probably much simpler ways of solving whatever problem you have.
Michael Haephrati 7-Mar-13 12:28pm    
There is no DLL involved. The question is about executables. The need relates to virtualization and isolation of files or Menory Mapped files. If you create such virtual file and this file is a program, the solution would be how to run it without having a physical file behind the process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900