@@ -5,30 +5,58 @@ package vmdetect
5
5
import (
6
6
"bufio"
7
7
"bytes"
8
+ "io/ioutil"
8
9
"os"
9
- "os/exec"
10
10
"time"
11
11
)
12
12
13
13
/*
14
14
Checks if the DMI table contains vendor strings of known VMs.
15
15
*/
16
16
func checkDMITable () bool {
17
- // TODO : instead of running a command, read files in /sys/class/dmi/id/* and look for vendor strings below
18
- output , err := exec .Command ("dmidecode" ).Output ()
17
+
18
+ // /!\ All lowercase /!\
19
+ blacklistDMI := []string {
20
+ "innotek" ,
21
+ "virtualbox" ,
22
+ "vbox" ,
23
+ }
24
+
25
+ dmiPath := "/sys/class/dmi/id/"
26
+ dmiFiles , err := ioutil .ReadDir (dmiPath )
19
27
20
28
if err != nil {
21
29
PrintError (err )
22
30
return false
23
31
}
24
32
25
- return bytes .Contains (output , []byte ("innotek" )) ||
26
- bytes .Contains (output , []byte ("VirtualBox" )) ||
27
- bytes .Contains (output , []byte ("vbox" ))
33
+ for _ , dmiEntry := range dmiFiles {
34
+ if ! dmiEntry .Mode ().IsRegular () {
35
+ continue
36
+ }
37
+
38
+ dmiContent , err := ioutil .ReadFile (dmiPath + dmiEntry .Name ())
39
+
40
+ if err != nil {
41
+ PrintError (err )
42
+ continue
43
+ }
44
+
45
+ for _ , entry := range blacklistDMI {
46
+ // Lowercase comparison to prevent false negatives
47
+ if bytes .Contains (bytes .ToLower (dmiContent ), []byte (entry )) {
48
+ return true
49
+ }
50
+ }
51
+
52
+ }
53
+
54
+ return false
28
55
}
29
56
30
57
/*
31
58
Checks printk messages to see if Linux detected an hypervisor.
59
+ https://github.com/torvalds/linux/blob/31cc088a4f5d83481c6f5041bd6eb06115b974af/arch/x86/kernel/cpu/hypervisor.c#L79
32
60
*/
33
61
func checkKernelRingBuffer () bool {
34
62
@@ -51,20 +79,20 @@ func checkKernelRingBuffer() bool {
51
79
52
80
for {
53
81
line , _ , err := reader .ReadLine ()
82
+
54
83
if err != nil {
55
- if ! os .IsTimeout (err ) {
84
+ if ! os .IsTimeout (err ) {
56
85
PrintError (err )
57
86
}
58
87
59
88
return false
60
89
}
61
90
62
- if bytes .Contains (line , []byte ("Hypervisor detected" )) {
91
+ // Lowercase comparison to prevent false negatives
92
+ if bytes .Contains (bytes .ToLower (line ), []byte ("hypervisor detected" )) {
63
93
return true
64
94
}
65
95
}
66
-
67
- return false
68
96
}
69
97
70
98
/*
0 commit comments