-
Notifications
You must be signed in to change notification settings - Fork 7
/
checksec.go
81 lines (67 loc) · 1.46 KB
/
checksec.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
package main
import (
"bytes"
"debug/elf"
"fmt"
"io"
"os"
)
const (
INVALID = "Not an ELF binary"
DISABLED = "Disabled"
ENABLED = "Enabled"
PARTIAL = "Partial"
SEP = ","
)
type checker func(file *elf.File) string
var checks = []struct {
name string
run checker
}{
{"NX", nx},
{"CANARY", canary},
{"RELRO", relro},
{"PIE", pie},
{"RPATH", rpath},
{"RUNPATH", runpath},
}
func checksec(file *elf.File) {
for n, check := range checks {
fmt.Print(check.name, "=", check.run(file))
if n < len(checks)-1 {
fmt.Print(SEP)
}
}
fmt.Println()
}
func main() {
// cat <bin> | ./a.out
if len(os.Args) == 1 {
var buf bytes.Buffer
if _, err := io.Copy(&buf, os.Stdin); err != nil {
fmt.Println(err)
os.Exit(1)
}
data := buf.Bytes()
if len(data) > 0 {
file, e := elf.NewFile(bytes.NewReader(data))
if e != nil {
fmt.Println(INVALID)
os.Exit(1)
}
checksec(file)
}
// FILE [FILE]*
} else {
for _, arg := range os.Args[1:] {
file, e := elf.Open(arg)
if e != nil {
fmt.Printf("%s,%s\n", arg, INVALID)
} else {
fmt.Print(arg, SEP)
checksec(file)
file.Close()
}
}
}
}