Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Executes a binary or file in memory on a Linux system. Uses the memfd_create(2) syscall. Credits and idea from: https://magisterquis.github.io/2018/03/31/in-memory-only-elf-execution.html
package main
import (
"io/ioutil"
"os"
"os/exec"
"strconv"
"syscall"
"unsafe"
)
func main() {
newFdName := "test_fd"
fdName, err := syscall.BytePtrFromString(newFdName)
if err != nil {
panic(err)
}
fd, _, _ := syscall.Syscall(319, uintptr(unsafe.Pointer(fdName)), 1, 0)
pid := os.Getpid()
file, err := ioutil.ReadFile("/home/capnspacehook/test.bin")
if err != nil {
panic(err)
}
fdPath := "/proc/" + strconv.Itoa(pid) + "/fd/" + strconv.Itoa(int(fd))
err = ioutil.WriteFile(fdPath, file, 0755)
if err != nil {
panic(err)
}
println(fdPath)
fdCmd := exec.Command(fdPath)
fdCmd.Stdout = os.Stdout
fdCmd.Stderr = os.Stderr
err = fdCmd.Run()
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment