-
Notifications
You must be signed in to change notification settings - Fork 14.3k
/
Copy pathcheckvm.rb
305 lines (243 loc) · 7.63 KB
/
checkvm.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Post
include Msf::Post::Windows::Process
include Msf::Post::Windows::Registry
include Msf::Auxiliary::Report
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Windows Gather Virtual Environment Detection',
'Description' => %q{
This module attempts to determine whether the system is running
inside of a virtual environment and if so, which one. This
module supports detection of Hyper-V, VMWare, VirtualBox, Xen, QEMU,
and Parallels.
},
'License' => MSF_LICENSE,
'Author' => [
'Carlos Perez <carlos_perez[at]darkoperator.com>',
'Aaron Soto <aaron_soto[at]rapid7.com>'
],
'Platform' => [ 'win' ],
'SessionTypes' => %w[meterpreter powershell shell],
'References' => [
['URL', 'https://handlers.sans.org/tliston/ThwartingVMDetection_Liston_Skoudis.pdf'],
['URL', 'https://www.heise.de/security/downloads/07/1/1/8/3/5/5/9/vmde.pdf'],
['URL', 'https://evasions.checkpoint.com/techniques/registry.html']
],
'Notes' => {
'Stability' => [CRASH_SAFE],
'Reliability' => [],
'SideEffects' => []
}
)
)
end
# enumerates through a list of VM signature processes and compares them to
# the processes running, returns true upon a match.
def processes_exist?(vm_processes)
vm_processes.each do |x|
@processes.each do |p|
return true if p['name'].casecmp?(x)
end
end
false
end
# loops over a list of services that are known to be signatures of vm's and
# compares them to the list of running services.
def services_exist?(vm_services)
vm_services.each do |srvc|
return true if service_exists?(srvc)
end
false
end
def service_exists?(service)
@services.include?(service)
end
# registers relevant keys and stores them in a hash
def register_keys(key_list)
@keys = {}
key_list.each do |k|
srvals = get_srval(k)
srvals = [] if srvals.nil?
@keys.store(k, srvals)
end
@keys
end
# checks the values of the keys and compares them to vm_k
def key_present?(vm_k)
@keys.each_value do |v|
return true if v.include?(vm_k)
end
false
end
def get_srval(key)
srvals = registry_enumkeys(key)
srvals = [] if srvals.nil?
srvals
end
# returns true if regval matches a regex
def regval_match?(key, val, rgx)
return true if get_regval_str(key, val) =~ rgx
false
end
# returns true if regval is eql to a string
def regval_eql?(key, val, str)
get_regval_str(key, val) == str
end
def get_regval_str(key, valname)
ret = registry_getvaldata(key, valname)
if ret.is_a?(Array)
ret = ret.join
end
ret
end
def parallels?
@system_bios_version = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System', 'SystemBiosVersion')
@video_bios_version = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System', 'VideoBiosVersion')
if @system_bios_version =~ /parallels/i || @video_bios_version =~ /parallels/i
return true
end
false
end
def hyperv?
physical_host = get_regval_str('HKLM\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters', 'PhysicalHostNameFullyQualified')
if physical_host
report_note(
host: session,
type: 'host.physicalHost',
data: { physicalHost: physical_host },
update: :unique_data
)
print_good("This is a Hyper-V Virtual Machine running on physical host #{physical_host}")
return true
end
sfmsvals = registry_enumkeys('HKLM\\SOFTWARE\\Microsoft')
if sfmsvals
%w[Hyper-V VirtualMachine].each do |vm|
return true if sfmsvals.include?(vm)
end
end
if @system_bios_version =~ /vrtual/i || @system_bios_version == 'Hyper-V'
return true
end
keys = %w[HKLM\\HARDWARE\\ACPI\\FADT HKLM\\HARDWARE\\ACPI\\RSDT HKLM\\HARDWARE\\ACPI\\DSDT]
register_keys(keys)
return true if key_present?('VRTUAL')
hyperv_services = %w[vmicexchange]
return true if services_exist?(hyperv_services)
false
end
def vmware?
vmware_services = %w[
vmdebug vmmouse VMTools VMMEMCTL tpautoconnsvc
tpvcgateway vmware wmci vmx86
]
return true if services_exist?(vmware_services)
@system_manufacturer = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS',
'SystemManufacturer')
return true if @system_manufacturer =~ /vmware/i
@scsi_port_1 = get_regval_str('HKLM\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port 1\\Scsi Bus 0\\Target Id 0\\Logical Unit Id 0',
'Identifier')
return true if @scsi_port_1 =~ /vmware/i
return true if regval_match?(
'HKLM\\SYSTEM\\ControlSet001\\Control\\Class\\{4D36E968-E325-11CE-BFC1-08002BE10318}\\0000',
'DriverDesc',
/cl_vmx_svga|VMWare/i
)
vmwareprocs = [
'vmtoolsd.exe',
'vmwareservice.exe',
'vmwaretray.exe',
'vmwareuser.exe'
]
return true if processes_exist?(vmwareprocs)
false
end
def virtualbox?
vboxprocs = [
'vboxservice.exe',
'vboxtray.exe'
]
vbox_srvcs = %w[VBoxMouse VBoxGuest VBoxService VBoxSF VBoxVideo]
if services_exist?(vbox_srvcs) || processes_exist?(vboxprocs)
return true
end
return true if key_present?('VBOX__')
for i in 0..2 do
return true if regval_match?(
"HKLM\\HARDWARE\\DEVICEMAP\\Scsi\\Scsi Port #{i}0\\Scsi Bus 0\\Target
Id 0\\Logical Unit Id 0",
'Identifier',
/vbox/i
)
end
return true if @system_bios_version =~ /vbox/i || @video_bios_version =~ /virtualbox/i
@system_product_name = get_regval_str('HKLM\\HARDWARE\\DESCRIPTION\\System\\BIOS', 'SystemProductName')
return true if @system_product_name =~ /virtualbox/i
false
end
def xen?
xenprocs = [
'xenservice.exe'
]
xen_srvcs = %w[xenevtchn xennet xennet6 xensvc xenvdb]
if processes_exist?(xenprocs) || services_exist?(xen_srvcs)
return true
end
return true if key_present?('Xen')
return true if @system_product_name =~ /xen/i
false
end
def qemu?
if @system_bios_version =~ /qemu/i || @video_bios_version =~ /qemu/i
return true
end
if @scsi_port_0 =~ /qemu|virtio/i || @system_manufacturer =~ /qemu/i
return true
end
return true if regval_match?(
'HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0',
'ProcessorNameString',
/qemu/i
)
return true if key_present?('BOCHS_')
false
end
def report_vm(hypervisor)
print_good("This is a #{hypervisor} Virtual Machine")
report_note(
host: session,
type: 'host.hypervisor',
data: { hypervisor: hypervisor },
update: :unique_data
)
report_virtualization(hypervisor)
end
def run
print_status('Checking if the target is a Virtual Machine ...')
@processes = get_processes
@processes = [] if @processes.nil?
@services = registry_enumkeys('HKLM\\SYSTEM\\ControlSet001\\Services')
@services = [] if @services.nil?
if parallels?
report_vm('Parallels')
elsif hyperv?
report_vm('Hyper-V')
elsif vmware?
report_vm('VMware')
elsif virtualbox?
report_vm('VirtualBox')
elsif xen?
report_vm('Xen')
elsif qemu?
report_vm('Qemu/KVM')
else
print_status('The target appears to be a Physical Machine')
end
end
end