MCPcopy Index your code
hub / github.com/Binject/exec2shell / main

Function main

main.go:28–193  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26)
27
28func main() {
29
30 parser := argparse.NewParser("exec2shell", "Extracts TEXT section of a PE, ELF, or Mach-O executable to shellcode")
31 srcFile := parser.String("i", "in", &argparse.Options{Required: true, Help: "Input PE, ELF, or Mach-o binary"})
32 dstFile := parser.String("o", "out", &argparse.Options{Required: false,
33 Default: "shellcode.bin", Help: "Output file - Shellcode as Binary"})
34 cFile := parser.String("c", "c-outfile", &argparse.Options{Required: false,
35 Help: "Output file - Shellcode as C Array"})
36 cVar := parser.String("n", "c-var", &argparse.Options{Required: false,
37 Default: "SHELLCODE", Help: "Sets variable name for C Array output"})
38 goFile := parser.String("g", "go-outfile", &argparse.Options{Required: false,
39 Help: "Output file - Shellcode as Go Array"})
40 goPkg := parser.String("p", "go-pkg", &argparse.Options{Required: false,
41 Default: "shellcode", Help: "Sets package string for Go Array output"})
42 goVar := parser.String("v", "go-var", &argparse.Options{Required: false,
43 Default: "shellcode", Help: "Sets variable name for Go Array output"})
44
45 if err := parser.Parse(os.Args); err != nil {
46 log.Println(parser.Usage(err))
47 return
48 }
49
50 btype := ERROR
51 buf, err := ioutil.ReadFile(*srcFile)
52 if err != nil {
53 log.Println(err)
54 return
55 }
56
57 if bytes.Equal(buf[:4], []byte{0x7F, 'E', 'L', 'F'}) {
58 btype = ELF
59 }
60 if bytes.Equal(buf[:3], []byte{0xfe, 0xed, 0xfa}) {
61 if buf[3] == 0xce || buf[3] == 0xcf {
62 // FE ED FA CE - Mach-O binary (32-bit)
63 // FE ED FA CF - Mach-O binary (64-bit)
64 btype = MACHO
65 }
66 }
67 if bytes.Equal(buf[1:4], []byte{0xfa, 0xed, 0xfe}) {
68 if buf[0] == 0xce || buf[0] == 0xcf {
69 // CE FA ED FE - Mach-O binary (reverse byte ordering scheme, 32-bit)
70 // CF FA ED FE - Mach-O binary (reverse byte ordering scheme, 64-bit)
71 btype = MACHO
72 }
73 }
74 if bytes.Equal(buf[:2], []byte{0x4d, 0x5a}) {
75 btype = PE
76 }
77
78 if btype == ERROR {
79 log.Println("Unknown Binary Format")
80 return
81 }
82
83 var data []byte
84
85 switch btype {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected