'read exe file in memory and execute it
Is it possible using Windows to read a file into memory (keep data in byte array), delete original file from filesystem and execute it from memory?
EDIT
My goals is to protect my java code from reverse enginering.
I wrote a launcher in C++ that take my encrypted jar file, decrypt it and launch it. The little problem is that i have to write my decrypted jar file somewhere in the filesystem, so it can be easily captured and decompiled... there is no way to prevent this?
Solution 1:[1]
No, it's not possible to do like that. There's no system call that says "take this chunk of my memory and use just that part of it as the image of a new process".
You can load code into memory and jump to it within the current process, but that's an ugly thing to do because you have to handle all of the relocations.
With regards to the Java specific part:
You can embed a Java interpreter within your C++ executable. You can write your own class loader for Java (through the C++ interface to the JVM) that will load classes from your encrypted Jar file. That way you could avoid ever writing the unencrypted Jar file to disk. It will of course be visible in memory to anyone with a debugger...
Solution 2:[2]
While this is an old question, there is a way to read a file into memory and execute it, its called a RAM disk. This can be used for any programming language.
- Create a RAM disk. e.g. by running ImDisk toolkit using java's ProcessBuilder
- Copy/create the exe file to the RAM disk
- Execute the file on the RAM disk
- Delete the RAM disk
This would be slow to set up for a single file execution but would do the job.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | JMax |
