-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.go
80 lines (67 loc) · 1.52 KB
/
utility.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
package main
import (
"io"
"os"
"fmt"
"bufio"
"os/exec"
"strings"
"crypto/md5"
"github.com/fatih/color"
// "strings"
)
func commandOutput(command string) string {
out, err := exec.Command("sh", "-c", command).Output()
if err != nil {
fmt.Println(`[ERROR] Error running ` + command)
fmt.Println(err)
}
return string(out[:])
}
func runCommand(command string) {
run := exec.Command("sh", "-c", command)
run.Run()
}
func getLocalUsers() {
fle, err := os.Open("/etc/passwd")
if err != nil {
fmt.Println(err.Error())
}
defer fle.Close()
scanner := bufio.NewScanner(fle)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
user := strings.Split(scanner.Text(), ":")
fmt.Println(user[0])
}
}
func getMD5(filePath string) []byte {
fle, err := os.Open(filePath)
if err != nil {
fmt.Println(err.Error())
}
defer fle.Close()
hash := md5.New()
_, err = io.Copy(hash, fle)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("MD5 Hash for %s is %x\n", filePath, hash.Sum(nil))
return hash.Sum(nil)
}
func md5Baselines() {
paths := []string{"/etc/passwd", "/etc/shadow", "/etc/group", "/etc/login.defs", "/etc/shells", "/bin/su", "/etc/hosts.allow", "/etc/hosts.deny", "/etc/hosts", "/etc/fstab"}
for _, file := range paths {
getMD5(file)
}
}
func checkFileExist(filePath string) (bool) {
if _, err := os.Stat(filePath); err == nil {
return true
}
return false
}
func errorPrint(message string) {
red := color.New(color.FgRed, color.Bold).SprintFunc()
fmt.Printf("[%s]%s\n", red("ERROR"), message)
}