Skip to content

Commit eb85c2d

Browse files
authored
Merge pull request #3 from mengzhuo/master
Enable go mod
2 parents 4fd05cb + 48c36ea commit eb85c2d

File tree

8 files changed

+105
-8
lines changed

8 files changed

+105
-8
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# VM Detection
22

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+
37
This project is a Go implementation of well-known techniques trying to detect if the program is being run in a virtual machine.
48
There are many C programs already doing this, but none written in pure Go.
59

@@ -45,4 +49,4 @@ Thanks to [@hippwn](https://twitter.com/hippwn) for its contribution
4549

4650
Thanks systemd for being [that awesome](https://github.com/systemd/systemd/blob/master/src/basic/virt.c).
4751

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/)

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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=

vmdetect/common.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package vmdetect
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/klauspost/cpuid"
7-
"github.com/shirou/gopsutil/mem"
86
"io"
97
"net"
108
"os"
119
"runtime"
1210
"strings"
11+
12+
"github.com/klauspost/cpuid"
13+
"github.com/shirou/gopsutil/mem"
1314
)
1415

1516
func PrintError(loggee interface{}) {

vmdetect/common_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

vmdetect/linux.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package vmdetect
@@ -7,6 +8,7 @@ import (
78
"io/ioutil"
89
"os"
910
"os/user"
11+
"path/filepath"
1012
"time"
1113
)
1214

@@ -44,7 +46,7 @@ func checkDMITable() bool {
4446
continue
4547
}
4648

47-
dmiContent, err := ioutil.ReadFile(dmiPath + dmiEntry.Name())
49+
dmiContent, err := ioutil.ReadFile(filepath.Join(dmiPath, dmiEntry.Name()))
4850

4951
if err != nil {
5052
PrintError(err)
@@ -127,11 +129,11 @@ Some virtualization technologies can be detected using /proc/device-tree
127129
func checkDeviceTree() bool {
128130
deviceTreeBase := "/proc/device-tree"
129131

130-
if DoesFileExist(deviceTreeBase + "/hypervisor/compatible") {
132+
if DoesFileExist(filepath.Join(deviceTreeBase, "/hypervisor/compatible")) {
131133
return true
132134
}
133135

134-
if DoesFileExist(deviceTreeBase + "/fw-cfg") {
136+
if DoesFileExist(filepath.Join(deviceTreeBase, "/fw-cfg")) {
135137
return true
136138
}
137139

vmdetect/linux_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

vmdetect/windows.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
package vmdetect
@@ -300,8 +301,8 @@ func checkFileSystem() (bool, string) {
300301
}
301302

302303
/*
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.
305306
*/
306307
func IsRunningInVirtualMachine() (bool, string) {
307308
if vmDetected, how := CommonChecks(); vmDetected {

0 commit comments

Comments
 (0)