@@ -4,8 +4,11 @@ import (
4
4
"bufio"
5
5
"fmt"
6
6
"github.com/klauspost/cpuid"
7
+ "github.com/shirou/gopsutil/mem"
7
8
"io"
9
+ "net"
8
10
"os"
11
+ "runtime"
9
12
"strings"
10
13
)
11
14
@@ -50,14 +53,67 @@ func DoesFileContain(file *os.File, stringsToBeFound ...string) bool {
50
53
}
51
54
}
52
55
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
+
53
96
/*
54
97
Tries to detect VMs using cross-platform techniques.
55
98
*/
56
99
func CommonChecks () (bool , string ) {
100
+
57
101
// https://lwn.net/Articles/301888/
58
102
if cpuid .CPU .VM () {
59
103
return true , "CPU Vendor (cpuid space)"
60
104
}
61
105
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
+
62
118
return false , "nothing"
63
119
}
0 commit comments