Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i execute an executable from memory?

Let's say I have included a binary into my program during compilation so, I keep it in a variable something like var myExec =[]byte{'s','o','m','e',' ','b','y','t','e','s'} So my question is whether there is a way to execute this binary within my program without writing it back to the disc and calling exec or fork on it? I am writing my app in Golang so the method I am seeking for is to do it using Go or C (using CGO).

Basically, I am seeking something like piping the bash script into bash just I don't know where can I pipe the bytes of a native executable to run it and writing it back to disk and then letting os to read it again seems a lot of extra work to be done

like image 585
nikoss Avatar asked Jun 20 '17 13:06

nikoss


1 Answers

In C and assuming Linux, you can change the protection of a memory region by means of the mprotect() system call, so that it can be executed (i.e.: turn a data region into a code region). After that, you could execute that region of memory by jumping into it.

like image 172
ネロク・ゴ Avatar answered Sep 18 '22 15:09

ネロク・ゴ