-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (106 loc) · 2.41 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"debug/elf"
"fmt"
"log"
"os"
"github.com/fatih/color"
)
var version = "1.3.0"
func main() {
red, green := color.New(color.FgRed).PrintlnFunc(), color.New(color.FgGreen).PrintlnFunc()
yellow := color.New(color.FgYellow).PrintlnFunc()
yellow("Gochecksec Version:", version)
if len(os.Args) != 2 {
_, err := fmt.Fprintln(os.Stderr, "Usage: gochecksec <binary>")
if err != nil {
return
}
os.Exit(1)
}
filename := os.Args[1]
binary, err := elf.Open(filename)
if err != nil {
if _, err := fmt.Fprintln(os.Stderr, err); err != nil {
return
}
os.Exit(1)
}
defer func(f *elf.File) {
err := f.Close()
if err != nil {
log.Fatal(err)
}
}(binary)
// Check binary arch
arch := binary.Machine
switch arch {
case elf.EM_X86_64:
green("Arch: x86-64")
case elf.EM_386:
green("Arch: x86")
default:
green("Arch: %d\n", arch)
}
// Check for RELRO (Relocation Read-Only)
if binary.Section(".data.rel.ro") != nil {
// Check for Full RELRO
if binary.Section(".data.rel.ro").Addr == binary.Section(".data").Addr {
green("RELRO: Full RELRO")
} else {
yellow("RELRO: Partial RELRO")
}
} else {
red("RELRO: No RELRO")
}
// Check for PIE (Position Independent Executable)
if binary.Class == elf.ELFCLASS64 {
eh := binary.Section(".eh_frame_hdr")
if eh.Type == elf.SHT_PROGBITS {
green("PIE: Enabled")
} else {
red("PIE: Disabled")
}
}
// Check for NX (Non-Executable Stack)
for _, p := range binary.Progs {
if p.Type == elf.PT_GNU_STACK && p.Flags&elf.PF_X == 0 {
green("NX: NX Enabled")
break
} else {
red("NX: NX Disabled")
break
}
}
// Check for Stack Canary
for _, s := range binary.Sections {
if s.Name == ".note.gnu.build-id" {
green("Stack: Has Stack Canary")
break
} else {
red("Stack: No Stack Canary")
break
}
}
// Check for RWX (Read-Write-Execute) segments
for _, segment := range binary.Progs {
if segment.Type == elf.PT_LOAD && segment.Flags&elf.PF_X == elf.PF_X && segment.Flags&elf.PF_W == elf.PF_W {
green("RWX: Has RWX segment")
break
} else {
red("RWX: No RWX segment")
break
}
}
// Check for Fortify
for _, s := range binary.Sections {
if s.Name == ".fortify_functions" {
green("Fortify: Enabled")
break
} else {
red("Fortify: Disabled")
break
}
}
green("\nCreated By: Jay Townsend\nPlease report issues to https://github.com/l1ghtn1ng/gochecksec/issues")
}