File tree Expand file tree Collapse file tree 8 files changed +105
-8
lines changed Expand file tree Collapse file tree 8 files changed +105
-8
lines changed Original file line number Diff line number Diff line change 1
1
# VM Detection
2
2
3
+ [ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/ShellCode33/VM-Detection.svg )] ( https://pkg.go.dev/github.com/ShellCode33/VM-Detection )
4
+ [ ![ GoReportCard] ( https://goreportcard.com/badge/github.com/ShellCode33/VM-Detection )] ( https://goreportcard.com/report/github.com/ShellCode33/VM-Detection )
5
+ [ ![ Coverage Status] ( https://coveralls.io/repos/github/ShellCode33/VM-Detection/badge.svg?branch=master )] ( https://coveralls.io/github/ShellCode33/VM-Detection?branch=master )
6
+
3
7
This project is a Go implementation of well-known techniques trying to detect if the program is being run in a virtual machine.
4
8
There are many C programs already doing this, but none written in pure Go.
5
9
@@ -45,4 +49,4 @@ Thanks to [@hippwn](https://twitter.com/hippwn) for its contribution
45
49
46
50
Thanks systemd for being [ that awesome] ( https://github.com/systemd/systemd/blob/master/src/basic/virt.c ) .
47
51
48
- Thanks to CheckPoint's researchers for their [ wonderful website] ( https://evasions.checkpoint.com/ )
52
+ Thanks to CheckPoint's researchers for their [ wonderful website] ( https://evasions.checkpoint.com/ )
Original file line number Diff line number Diff line change
1
+ module github.com/ShellCode33/VM-Detection
2
+
3
+ go 1.21
4
+
5
+ require (
6
+ github.com/klauspost/cpuid v1.3.1
7
+ github.com/shirou/gopsutil v3.21.11+incompatible
8
+ golang.org/x/sys v0.21.0
9
+ )
10
+
11
+ require (
12
+ github.com/go-ole/go-ole v1.2.6 // indirect
13
+ github.com/stretchr/testify v1.9.0 // indirect
14
+ github.com/yusufpapurcu/wmi v1.2.4 // indirect
15
+ )
Original file line number Diff line number Diff line change
1
+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c =
2
+ github.com/davecgh/go-spew v1.1.1 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
3
+ github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY =
4
+ github.com/go-ole/go-ole v1.2.6 /go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0 =
5
+ github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s =
6
+ github.com/klauspost/cpuid v1.3.1 /go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4 =
7
+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
8
+ github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
9
+ github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI =
10
+ github.com/shirou/gopsutil v3.21.11+incompatible /go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA =
11
+ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg =
12
+ github.com/stretchr/testify v1.9.0 /go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY =
13
+ github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0 =
14
+ github.com/yusufpapurcu/wmi v1.2.4 /go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0 =
15
+ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
16
+ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws =
17
+ golang.org/x/sys v0.21.0 /go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA =
18
+ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA =
19
+ gopkg.in/yaml.v3 v3.0.1 /go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM =
Original file line number Diff line number Diff line change @@ -3,13 +3,14 @@ package vmdetect
3
3
import (
4
4
"bufio"
5
5
"fmt"
6
- "github.com/klauspost/cpuid"
7
- "github.com/shirou/gopsutil/mem"
8
6
"io"
9
7
"net"
10
8
"os"
11
9
"runtime"
12
10
"strings"
11
+
12
+ "github.com/klauspost/cpuid"
13
+ "github.com/shirou/gopsutil/mem"
13
14
)
14
15
15
16
func PrintError (loggee interface {}) {
Original file line number Diff line number Diff line change
1
+ package vmdetect
2
+
3
+ import "testing"
4
+
5
+ func TestCommonCheck (t * testing.T ) {
6
+ inVM , msg := CommonChecks ()
7
+ if inVM && msg != "nothing" {
8
+ t .Errorf ("inside vm but got %s, expect else" , msg )
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ //go:build linux
1
2
// +build linux
2
3
3
4
package vmdetect
7
8
"io/ioutil"
8
9
"os"
9
10
"os/user"
11
+ "path/filepath"
10
12
"time"
11
13
)
12
14
@@ -44,7 +46,7 @@ func checkDMITable() bool {
44
46
continue
45
47
}
46
48
47
- dmiContent , err := ioutil .ReadFile (dmiPath + dmiEntry .Name ())
49
+ dmiContent , err := ioutil .ReadFile (filepath . Join ( dmiPath , dmiEntry .Name () ))
48
50
49
51
if err != nil {
50
52
PrintError (err )
@@ -127,11 +129,11 @@ Some virtualization technologies can be detected using /proc/device-tree
127
129
func checkDeviceTree () bool {
128
130
deviceTreeBase := "/proc/device-tree"
129
131
130
- if DoesFileExist (deviceTreeBase + "/hypervisor/compatible" ) {
132
+ if DoesFileExist (filepath . Join ( deviceTreeBase , "/hypervisor/compatible" ) ) {
131
133
return true
132
134
}
133
135
134
- if DoesFileExist (deviceTreeBase + "/fw-cfg" ) {
136
+ if DoesFileExist (filepath . Join ( deviceTreeBase , "/fw-cfg" ) ) {
135
137
return true
136
138
}
137
139
Original file line number Diff line number Diff line change
1
+ //go:build linux
2
+ // +build linux
3
+
4
+ package vmdetect
5
+
6
+ import (
7
+ "reflect"
8
+ "runtime"
9
+ "strings"
10
+ "testing"
11
+ )
12
+
13
+ func getFunctionName (f interface {}) string {
14
+ fn := runtime .FuncForPC (reflect .ValueOf (f ).Pointer ()).Name ()
15
+ i := strings .LastIndex (fn , "." )
16
+ if i > 0 {
17
+ return fn [i :]
18
+ }
19
+ return fn
20
+ }
21
+
22
+ func TestCheckDMITable (t * testing.T ) {
23
+ check := - 1
24
+ for i , f := range []func () bool {
25
+ checkDMITable ,
26
+ checkKernelRingBuffer ,
27
+ checkUML ,
28
+ checkSysInfo ,
29
+ checkDeviceTree ,
30
+ checkHypervisorType ,
31
+ checkXenProcFile ,
32
+ checkKernelModules ,
33
+ } {
34
+ inVm := f ()
35
+ t .Logf ("%s:%v" , getFunctionName (f ), inVm )
36
+ if inVm && check == - 1 {
37
+ check = i
38
+ }
39
+ }
40
+ inVM , msg := IsRunningInVirtualMachine ()
41
+ t .Log (msg )
42
+ if check == - 1 == inVM {
43
+ t .Errorf ("check:%d, inVm:%v" , check , inVM )
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ //go:build windows
1
2
// +build windows
2
3
3
4
package vmdetect
@@ -300,8 +301,8 @@ func checkFileSystem() (bool, string) {
300
301
}
301
302
302
303
/*
303
- Public function returning true if a VM is detected.
304
- If so, a non-empty string is also returned to tell how it was detected.
304
+ Public function returning true if a VM is detected.
305
+ If so, a non-empty string is also returned to tell how it was detected.
305
306
*/
306
307
func IsRunningInVirtualMachine () (bool , string ) {
307
308
if vmDetected , how := CommonChecks (); vmDetected {
You can’t perform that action at this time.
0 commit comments