Skip to content

Commit 6b39a1c

Browse files
committed
Code base + Partial Linux support
1 parent cca553b commit 6b39a1c

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"VMDetect/vmdetect"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
fmt.Println("Trying to vmdetect if a VM is running...")
10+
11+
isInsideVM, reason, err := vmdetect.IsRunningInVirtualMachine()
12+
13+
if err != nil {
14+
panic(err)
15+
}
16+
17+
if isInsideVM {
18+
fmt.Printf("VM detected thanks to %v\n", reason)
19+
} else {
20+
fmt.Println("No VM has been detected")
21+
}
22+
23+
}

vmdetect/linux.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// +build linux darwin
2+
3+
package vmdetect
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"os/exec"
9+
"os/user"
10+
)
11+
12+
/*
13+
Checks if the program is being run using root.
14+
*/
15+
func isRunningWithAdminRights() (bool, error) {
16+
if currentUser, err := user.Current(); err != nil {
17+
return false, err
18+
} else {
19+
return currentUser.Uid == "0", nil
20+
}
21+
}
22+
23+
/*
24+
Tries to vmdetect VM using privileged access.
25+
*/
26+
func privilegedChecks() (bool, string, error) {
27+
28+
output, err := exec.Command("dmidecode").Output()
29+
30+
if err == nil &&
31+
(bytes.Contains(output, []byte("innotek")) ||
32+
bytes.Contains(output, []byte("VirtualBox")) ||
33+
bytes.Contains(output, []byte("vbox"))){
34+
return true, "dmidecode", nil
35+
}
36+
37+
output, err = exec.Command("dmesg").Output()
38+
39+
if err == nil && bytes.Contains(output, []byte("Hypervisor detected")) {
40+
return true, "dmesg", nil
41+
}
42+
43+
return false, "", nil
44+
}
45+
46+
/*
47+
Tries to vmdetect VM using unprivileged access.
48+
*/
49+
func unprivilegedChecks() (bool, string, error) {
50+
output, err := exec.Command("hostnamectl").Output()
51+
52+
if err == nil && bytes.Contains(output, []byte(" vm\n")) {
53+
return true, "hostnamectl", nil
54+
}
55+
56+
return false, "", nil
57+
}
58+
59+
/*
60+
Public function returning true if a VM is detected.
61+
If so, a non-empty string is also returned to tell how it was detected.
62+
*/
63+
func IsRunningInVirtualMachine() (bool, string, error) {
64+
isAdmin, err := isRunningWithAdminRights()
65+
66+
if err != nil {
67+
return false, "", err
68+
}
69+
70+
if isAdmin {
71+
vmDetected, reason, err := privilegedChecks()
72+
73+
if err != nil {
74+
return false, "", err
75+
}
76+
77+
if vmDetected {
78+
return true, reason, nil
79+
}
80+
} else {
81+
fmt.Println("[WARNING] Running as unprivileged user")
82+
}
83+
84+
return unprivilegedChecks()
85+
}

vmdetect/windows.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// +build windows
2+
3+
package vmdetect
4+
5+
/*
6+
Checks if the program is being run in a privileged context.
7+
*/
8+
func isRunningWithAdminRights() (bool, error) {
9+
return false, nil
10+
}
11+
12+
/*
13+
Tries to vmdetect VM using privileged access.
14+
*/
15+
func privilegedChecks() (bool, string, error) {
16+
return false, "", nil
17+
}
18+
19+
/*
20+
Tries to vmdetect VM using unprivileged access.
21+
*/
22+
func unprivilegedChecks() (bool, string, error) {
23+
return false, "", nil
24+
}
25+
26+
/*
27+
Public function returning true if a VM is detected.
28+
If so, a non-empty string is also returned to tell how it was detected.
29+
*/
30+
func IsRunningInVirtualMachine() (bool, string, error) {
31+
isAdmin, err := isRunningWithAdminRights()
32+
33+
if err != nil {
34+
return false, "", err
35+
}
36+
37+
if isAdmin {
38+
vmDetected, reason, err := privilegedChecks()
39+
40+
if err != nil {
41+
return false, "", err
42+
}
43+
44+
if vmDetected {
45+
return true, reason, nil
46+
}
47+
} else {
48+
fmt.Println("[WARNING] Running as unprivileged user")
49+
}
50+
51+
return unprivilegedChecks()
52+
}

0 commit comments

Comments
 (0)