execute from memory

BlitzMax Forums/BlitzMax Programming/execute from memory

Sub_Zero(Posted 2011) [#1]
Hi

Is it possible to execute a program directly from a bank in memory?

Ie. if i createbank(size of binary file) and read contents of the binary file into the bank, how can i then execute the binary file directly from the bank?


xlsior(Posted 2011) [#2]
The processor on most computer these days supports DEP (data execution prevention) , which is enabled by default under Windows and would prevent such a thing from running.

It's a good thing, since that is how a lot of viruses would otherwise manage to compromise your computer if they get on there through some buffer overflow.


ziggy(Posted 2011) [#3]
If you have the file, why don't you execute it directly? I mean, if you want to execute a file, there's a way to do it. If you're trying to make something specific, maybe a bit more of information would help us helping you.


Sub_Zero(Posted 2011) [#4]
I see... But I would like to know how anyway, I'm not into making viruses, but I support the other side... This would help me in making an elf encrypter.... anyone?


Sub_Zero(Posted 2011) [#5]
Can anyone contribute to this? Ram streams?


ImaginaryHuman(Posted 2011) [#6]
Surely there's a programmatic way to do this, for like when apps run plugins?


H&K(Posted 2011) [#7]
At the very least could you not save it to disk, then run it from there?


GW(Posted 2011) [#8]
executing from memory is not difficult. just write your code to memory and set the function pointer. You just have to be careful.
Check the post in my sig for an example.


Sub_Zero(Posted 2013) [#9]
I know this topic is old, but I'm trying to achieve this.

I've incbinned an encrypted elf file, I then decrypt it to memory....

Does anyone know how to execute it from memory?


Yasha(Posted 2013) [#10]
...Did you learn anything from GW's "interpreter"? Because that's exactly what that code does.

Other things you might want to take a look at include TCC and GNU lightning, both of which build and execute code directly in memory. There are BlitzMax wrappers for both floating around somewhere.


Sub_Zero(Posted 2013) [#11]
Yes i learned alot there.

Does anyone know at which offset the start address of an elf is?

Oh and I didn't find the wrapper for gnu lightning


xlsior(Posted 2013) [#12]
Does anyone know at which offset the start address of an elf is?


http://wiki.osdev.org/ELF


zzz(Posted 2013) [#13]
There is a world of difference between just executing some code on the fly and executing a proper exe file on the fly.. Im not even sure if that is possible in a >0 ring environment since you (afaik) must be able to set up memory mapping and stuff yourself. Although I havent tried it so I might be wrong :) Id look around for the source/documentation on how other file compressors or encrypters do it.


Sub_Zero(Posted 2013) [#14]
Yeah I know you are right, there's extractions of elements from the elf file, relocating the data (i remember relocating from the old 6510 days), and possibly flag the memory as executeable with write permission.

The relocation is important, as jmp and such instructions has to be relocated properly. Wish i had some of my old code from the 64 and maybe a cody of turbo assembler to have a look at it.
http://docs.oracle.com/cd/E19082-01/819-0690/chapter6-62988/index.html


zzz(Posted 2013) [#15]
Well, the operating system would have to load the executable to ram before doing any funky stuff too, so I guess its a question of if that functionality is exposed to the users or not. For unix stuff I wouldnt be suprised if it is :) I tried a bit of googling but couldnt find anything useful though.