Skip to content

Commit 787e9a8

Browse files
committed
Now reading DMI table through /sys/class/dmi instead of running dmidecode
1 parent 80bbbdc commit 787e9a8

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

vmdetect/linux.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,58 @@ package vmdetect
55
import (
66
"bufio"
77
"bytes"
8+
"io/ioutil"
89
"os"
9-
"os/exec"
1010
"time"
1111
)
1212

1313
/*
1414
Checks if the DMI table contains vendor strings of known VMs.
1515
*/
1616
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)
1927

2028
if err != nil {
2129
PrintError(err)
2230
return false
2331
}
2432

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
2855
}
2956

3057
/*
3158
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
3260
*/
3361
func checkKernelRingBuffer() bool {
3462

@@ -51,20 +79,20 @@ func checkKernelRingBuffer() bool {
5179

5280
for {
5381
line, _, err := reader.ReadLine()
82+
5483
if err != nil {
55-
if ! os.IsTimeout(err) {
84+
if !os.IsTimeout(err) {
5685
PrintError(err)
5786
}
5887

5988
return false
6089
}
6190

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")) {
6393
return true
6494
}
6595
}
66-
67-
return false
6896
}
6997

7098
/*

0 commit comments

Comments
 (0)