Repository navigation
Expand file tree
/
Copy pathloader.go
More file actions
66 lines (59 loc) · 1.44 KB
/
Copy pathloader.go
File metadata and controls
66 lines (59 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"io/ioutil"
"os"
"golang.org/x/sys/unix"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: loader.py <binary> [arguments]\n")
os.Exit(1)
}
// Create memory reagon to hold ELF
fd, err := unix.MemfdCreate("", 0)
if err != nil {
fmt.Printf("Error calling MemfdCreate %s", err)
os.Exit(1)
}
fp := fmt.Sprintf("/proc/self/fd/%d", fd)
file := os.NewFile(uintptr(fd), fp)
defer file.Close()
// Write ELF data into memory. This reads ELF from
// disk or stdin, but could also be from the internet,
// an encrypted file, hardcoded etc.
data := []byte{}
if os.Args[1] == "-" {
// Read from stdin
read_data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Printf("Error reading from stdin: %s", err)
os.Exit(1)
}
data = append(data, read_data...)
} else {
read_data, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Printf("Error reading file %s: %s", os.Args[1], err)
os.Exit(1)
}
data = append(data, read_data...)
}
// Write patched ELF into memfd memory
_, err = file.Write(data)
if err != nil {
fmt.Printf("Error writing data to memfd file %s", err)
os.Exit(1)
}
// Execute in-memory binary
// To pass in commandline args add them from argv
argv := []string{"from_loader"}
if len(os.Args) > 2 {
argv = append(argv, os.Args[2:]...)
}
err = unix.Exec(fp, argv, os.Environ())
if err != nil {
fmt.Printf("Failed to execute: %s", err)
os.Exit(1)
}
}