-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (85 loc) · 2.09 KB
/
main.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"debug/elf"
"debug/macho"
"debug/pe"
"fmt"
"log"
"os"
"path/filepath"
)
// Based on the example program found here:
// https://www.jvt.me/posts/2023/05/15/go-parse-binary-architecture/
// Docs for debug package can be found here:
// https://pkg.go.dev/debug
func main() {
fullBinary, _ := os.Executable()
baseBinary := filepath.Base(fullBinary)
file, err := os.OpenFile(baseBinary+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Fatal(err)
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatal(err)
}
}(file)
logger := log.New(file, "", log.LstdFlags)
if len(os.Args) != 2 {
log.Fatal("Need more args")
}
command := os.Args[1]
err = parseMac(command)
if err != nil {
logger.Println("Doesn't look like a Mach-O file:", err, command)
}
err = parseMacUniversalBinary(command)
if err != nil {
logger.Println("Doesn't look like a Mach-O Universal Binary:", err, command)
}
err = parseElf(command)
if err != nil {
logger.Println("Doesn't look like an ELF file:", err, command)
}
err = parsePE(command)
if err != nil {
logger.Println("Doesn't look like a PE file:", err, command)
}
}
func parseMac(command string) error {
f, err := macho.Open(command)
if err != nil {
return err
}
fmt.Printf("%s is a Mach-O binary with CPU architecture %v\n", command, f.Cpu.String())
return nil
}
func parseMacUniversalBinary(command string) error {
f, err := macho.OpenFat(command)
if err != nil {
return err
}
fmt.Printf("%s is a Mach-O universal binary with architectures:", command)
for _, fa := range f.Arches {
fmt.Printf(" %v", fa.Cpu.String())
}
fmt.Println()
return nil
}
func parseElf(command string) error {
f, err := elf.Open(command)
if err != nil {
return err
}
fmt.Printf("%s is an Executable and Linked Format (ELF) binary with CPU architecture %v\n", command, f.Machine.String())
return nil
}
func parsePE(command string) error {
f, err := pe.Open(command)
if err != nil {
return err
}
fmt.Printf("%s is a PE binary with CPU architecture 0x%x\n", command, f.Machine)
return nil
}