-
Notifications
You must be signed in to change notification settings - Fork 0
/
govm.go
129 lines (106 loc) · 2.81 KB
/
govm.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
120
121
122
123
124
125
126
127
128
129
package govm
import (
"encoding/json"
"errors"
"net"
"runtime"
"strings"
"github.com/pbnjay/memory"
)
// run all checks and return the results as a json
func CheckAll() string {
isCheckMac, _ := CheckMacAdr()
isCheckProcesses, _ := CheckProcesses()
isCheckFiles, _ := CheckFiles()
isCheckCores, _ := CheckCores(0)
isCheckRam, _ := CheckRam(0)
isCheckOnline := CheckOnline()
results := checkAllReturn{
CheckMac: isCheckMac,
CheckProcesses: isCheckProcesses,
CheckFiles: isCheckFiles,
CheckCores: isCheckCores,
CheckRam: isCheckRam,
CheckOnline: isCheckOnline,
}
bResults, _ := json.Marshal(results)
var sResults string = string(bResults)
return sResults
}
// check the machine's mac address for known hypervisor addresses
func CheckMacAdr() (bool, error) {
// get all mac addresses
macAddresses, err := getMacAddresses()
if err != nil {
return false, err
}
// check for known vm mac address patterns
for _, mAdr := range macAddresses {
if strings.Contains(mAdr, "00:50:56") || strings.Contains(mAdr, "00:0C:29") || strings.Contains(mAdr, "00:05:69") || strings.Contains(mAdr, "00:1C:14") || strings.Contains(mAdr, "08:00:27") {
return true, nil
}
}
return false, nil
}
// check running processes for processes containing "vm" or "vbox"
func CheckProcesses() (bool, error) {
// get all processes
processes, err := getAllProcesses()
if err != nil {
return false, err
}
// check if any processes contain "vm" or "vbox"
for _, proc := range processes {
if strings.Contains(strings.ToLower(proc.Executable()), "vm") || strings.Contains(strings.ToLower(proc.Executable()), "vbox") {
return true, nil
}
}
return false, nil
}
// check if files related to running a vm exist on system
func CheckFiles() (bool, error) {
isVm, err := checkFiles()
if err != nil {
return false, err
}
return isVm, nil
}
// check whether the machine has less than or equal to x number of cores (default is 2, leave as 0 for default)
func CheckCores(count int) (bool, error) {
if count < 0 {
return false, errors.New("count must be positive")
}
cores := 2
if count != 0 {
cores = count
}
numOfProcessors := runtime.NumCPU()
if cores <= numOfProcessors {
return false, nil
} else {
return true, nil
}
}
// check whether the machine has less than or equal to x mb of ram (default is 4196, leave as 0 for default)
func CheckRam(mb uint64) (bool, error) {
var ram uint64 = 4196 * 1048576
if mb != 0 {
ram = mb * 1048576
}
amountOfRam := memory.TotalMemory()
// check if ram is less than or equal to machine's ram
if ram <= amountOfRam {
return false, nil
} else {
return true, nil
}
}
// check if machine can access 8.8.8.8
func CheckOnline() bool {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return false
}
defer conn.Close()
return true
}