Skip to content

Instantly share code, notes, and snippets.

@capnspacehook
Created February 21, 2019 13:37

Revisions

  1. capnspacehook created this gist Feb 21, 2019.
    43 changes: 43 additions & 0 deletions invokeInMemLinux.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@


    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)
    }
    }