Skip to content

Commit 5ed8258

Browse files
committed
Add more common checks
1 parent 3f9365a commit 5ed8258

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

vmdetect/common.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"bufio"
55
"fmt"
66
"github.com/klauspost/cpuid"
7+
"github.com/shirou/gopsutil/mem"
78
"io"
9+
"net"
810
"os"
11+
"runtime"
912
"strings"
1013
)
1114

@@ -50,14 +53,67 @@ func DoesFileContain(file *os.File, stringsToBeFound ...string) bool {
5053
}
5154
}
5255

56+
/*
57+
Tries to detect a VM using its network configuration.
58+
*/
59+
func checkNetworking() (bool, string) {
60+
61+
blacklistedMacAddressPrefixes := []string{
62+
"00:1C:42", // Parallels
63+
"08:00:27", // VirtualBox
64+
"00:05:69", // |
65+
"00:0C:29", // | > VMWare
66+
"00:1C:14", // |
67+
"00:50:56", // |
68+
"00:16:E3", // Xen
69+
}
70+
71+
interfaces, err := net.Interfaces()
72+
73+
if err != nil {
74+
return false, err.Error()
75+
}
76+
77+
for _, iface := range interfaces {
78+
79+
macAddr := iface.HardwareAddr.String()
80+
if macAddr != "" {
81+
for _, prefix := range blacklistedMacAddressPrefixes {
82+
if strings.HasPrefix(macAddr, prefix) {
83+
return true, fmt.Sprintf("Known MAC address prefix (%v)", prefix)
84+
}
85+
}
86+
}
87+
88+
if iface.Name == "Vmware" {
89+
return true, "Vmware found as network interface name"
90+
}
91+
}
92+
93+
return false, ""
94+
}
95+
5396
/*
5497
Tries to detect VMs using cross-platform techniques.
5598
*/
5699
func CommonChecks() (bool, string) {
100+
57101
// https://lwn.net/Articles/301888/
58102
if cpuid.CPU.VM() {
59103
return true, "CPU Vendor (cpuid space)"
60104
}
61105

106+
if vmDetected, how := checkNetworking(); vmDetected {
107+
return true, how
108+
}
109+
110+
vmStat, err := mem.VirtualMemory()
111+
112+
if err != nil {
113+
PrintError(err)
114+
} else if runtime.NumCPU() < 3 && vmStat.Total < 2048000 {
115+
return true, fmt.Sprintf("Low resources detected (%v CPU and %v bytes of RAM", runtime.NumCPU(), vmStat.Total)
116+
}
117+
62118
return false, "nothing"
63119
}

0 commit comments

Comments
 (0)