In-memory execution problem

Pages: 12
hello!
i am facing a serious problem when trying to make an in-memory execution program.

1
2
3
4
5
6
7
8
9
char code[] = {
        0x00, 0x11 ...etc
};
void *buf;
buf = mmap (0,sizeof(code),PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANON,-1,0);
	  memcpy (buf, code, sizeof(code));
	 
	  /* run code */
	  ((void (*) (void))buf)();



After compiling this, the pogam with crash and nothingg happens.

Can you please tell me whats wrong in my code ?
Last edited on
What's in code?
a simple executable file i transformed into hex to execute it locally.

it's a simple hello world program to test.
Last edited on
You're treating an ELF binary as an internal function. You do realise they're different memory layout (types) right?
no i didn't realise ... what I have to do ?!
Can I pick up the data from the ELF that can be used in this code ? and the progam will be executed
You'd be reinventing the wheel as well as getting your program added to every virus database ever.

Why don't you want to just ask the operating system to run the executable file for you?
Last edited on
What's your overall objective? Why do you want to load an executable and run it?
I am writing this for educational purpose, to know HOW the In-memory execution really woks I have to make one.

Mr LB, i don't care about virus databases, i'm just wanna make a simple pogram to learn this feature.
Why are you using C++ to learn about such a low-level functionality that has nothing to do with C++? You will have much better luck working in assembly.

In-memory execution is an incredibly complex beast. Different operating systems, different hardware, different instruction sets, even different logic for the same OS. I wouldn't touch it with a 50-foot pole.
Last edited on
I Asked you, came here, made an account and posted the question.
The question was: how to do this in C++
Your answer was: don't use C++

For god's sake, did I ask you tell me which language I should use ?

I am already stuck in C++ and this should have done with C++ because I'm working with C++.

If you have an answer, I will be thankful .
If you don't have an answer, tell me you don't know.
Last edited on
vladimirr wrote:
I Asked you, came here, made an account and posted the question.
The question was: how to do this in C++
Your answer was: don't use C++

For god's sake, did I ask you tell me which language I should use ?

I am already stuck in C++ and this should have done with C++ because I'm working with C++.

If you have an answer, I will be thankful .
If you don't have an answer, tell me you don't know.
It is perfectly fine for me to say that this is the wrong language to use for this task. If every language could be used to do everything we would only ever need one language.

Just because I do not know how to give you the answer you want to hear does not mean I cannot help you in any way at all. Don't be disrespectful because I didn't immediately solve your problem the way you wanted it solved.

Instead, you could have said "I am forced to use C++ for this." and that would have been sufficient.
Last edited on
I disagree that C++ is unsuited to do this. The only real difficulty here is decoding the ELF and loading the binary at the correct location. Standalone executables are generally not built with position-independent code, unlike libraries, so it's not easy to load two executables in the same memory space. If you recompile the embedded program as an .so you'll have a much easier time. After you decode the ELF, you'll be able to obtain a pointer to some exported function. Simply casting this pointer to an appropriate function pointer is enough to execute the function.
We do something like this all the time at my workplace to generate code at run time, only we skip generating proper executables, and JIT engines work like this, too.
Hi,
Sorry it's too late to comment.

LB, I didn't disrespecct you, I showed you honestly what I see in my prespective.
I'm sorry if this meant to you as a disrespect.

helios, yes but I'm not fammiliar to that kind of job.
If you give me hints to follow I will spend the night researching about it.

I hope your hints (and maybe tools) will make it easier for me.
Always at the subject (In-memory execution)

Thank you for giving me your time.
Vladimir.
I'm not clear enough on your goal to recommend any tools. Are you researching how to run any dynamic code (i.e. code loaded after the initial load that's performed by the OS), or are you specifically interested in getting code off ELF files?
Yes, i'm intested of making ELF able to be executed in ((void (*) (void))buf)(); jump

thats my point
This is probably what you're looking for, then: http://directory.fsf.org/wiki/Libelf
Thatnks dude, have any idea of how to use it in my case, my english is terrible I can't read a very long tutorials without a 24h of headache, I'm russian so english isn't my 1st lang.

I hope you tell me how to use it in my case. Thank you very much!

vladimir.
because I got this PDF https://mdsp.googlecode.com/files/libelf-by-example-20100112.pdf

its like a trouble to me
English isn't my first language either, but you don't see me complain. If you want to be in this business, you need to be ready to do lots of reading.
Pages: 12