<p>Is there a way to identify, from within a VM, that your code is running inside a VM?</p>

<p>I guess there are more or less easy ways to identify specific VM systems, especially if the VM has the provider's extensions installed (such as for VirtualBox or VMWare). But is there a general way to identify that you are not running directly on the CPU?</p>

[{'answer_id': 39542, 'author': 'wvdschel', 'author_id': 2018, 'author_profile': 'https://Stackoverflow.com/users/2018', 'pm_score': 3, 'selected': False, 'text': "<p>In most cases, you shouldn't try to. You shouldn't care if someone is running your code in a VM, except in a few specific cases.</p>\n\n<p>If you need to, in Linux the most common way is to look at <code>/sys/devices/virtual/dmi/id/product_name</code>, which will list the name of the laptop/mainboard on most real systems, and the hypervisor on most virtual systems. <code>dmidecode | grep Product</code> is another common method, but I think that requires root access.</p>\n"}, {'answer_id': 39551, 'author': 'Mythokia', 'author_id': 4185, 'author_profile': 'https://Stackoverflow.com/users/4185', 'pm_score': 0, 'selected': False, 'text': "<p>If it VM does the job well, it should be invisible to the client that it's being virtualized. However, one can look at other clues.</p>\n\n<p>I would imagine that looking for known drivers or software specific to the VM environment would be the best possible way.</p>\n\n<p>For example, on a VMWare client running Windows, vmxnet.sys would be the network driver, displayed as VMware accelerated AMD PCNet Adapter.</p>\n"}, {'answer_id': 39560, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 0, 'selected': False, 'text': '<p>One good example is that apparently doing a WMI query for the motherboard manufacturer, and if it returns "Microsoft" you\'re in a VM.  Thought I believe this is only for VMWare.  There are likely different ways to tell for each VM host software.</p>\n\n<p>This article here <a href="http://blogs.technet.com/jhoward/archive/2005/07/26/407958.aspx" rel="nofollow noreferrer">http://blogs.technet.com/jhoward/archive/2005/07/26/407958.aspx</a> has some good suggestions and links to a couple of ways to detect if you are in a VM (VMWare and VirtualPC at least).</p>\n'}, {'answer_id': 39563, 'author': 'spoulson', 'author_id': 3347, 'author_profile': 'https://Stackoverflow.com/users/3347', 'pm_score': 3, 'selected': False, 'text': '<p>A more empirical approach is to check for known VM device drivers.  You could write WMI queries to locate, say, the VMware display adapter, disk drive, network adapter, etc.  This would be suitable if you knew you only had to worry about known VM host types in your environment.  Here\'s <a href="http://talhatariq.wordpress.com/2006/05/14/detecting-virtualization-2/" rel="noreferrer">an example of doing this in Perl</a>, which could be ported to the language of your choice.</p>\n'}, {'answer_id': 39565, 'author': 'tonylo', 'author_id': 4071, 'author_profile': 'https://Stackoverflow.com/users/4071', 'pm_score': 0, 'selected': False, 'text': "<p>You might be able to identify whether you're in a virtual machine by looking at the MAC address of your network connection. Xen for example typically recommends using a specific range of addresses 00:16:3e:xx:xx:xx.</p>\n\n<p>This isn't guaranteed as it's up to the administrator of the system to specify what MAC address they like.</p>\n"}, {'answer_id': 39569, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 2, 'selected': False, 'text': '<p>I once ran across an assembly code snippet that told you if you were in a VM....I googled but couldn\'t find the original article.</p>\n\n<p>I did find this though: <a href="http://www.codeproject.com/KB/system/VmDetect.aspx" rel="nofollow noreferrer">Detect if your program is running inside a Virtual Machine</a>.</p>\n\n<p>Hope it helps.</p>\n'}, {'answer_id': 39619, 'author': 'JawnV6', 'author_id': 3869, 'author_profile': 'https://Stackoverflow.com/users/3869', 'pm_score': 6, 'selected': True, 'text': '<p>A lot of the research on this is dedicated to detecting so-called "blue pill" attacks, that is, a malicious hypervisor that is actively attempting to evade detection.  </p>\n\n<p>The classic trick to detect a VM is to populate the ITLB, run an instruction that <strong>must</strong> be virtualized (which necessarily clears out such processor state when it gives control to the hypervisor), then run some more code to detect if the ITLB is still populated.  The first paper on it is located <a href="http://www.symantec.com/avcenter/reference/Virtual_Machine_Threats.pdf" rel="noreferrer">here</a>, and a rather colorful explanation from a <a href="http://web.archive.org/web/20080213212608/http://www.matasano.com/log/930/side-channel-detection-attacks-against-unauthorized-hypervisors/" rel="noreferrer">researcher\'s blog</a> and alternative <a href="http://web.archive.org/web/20080720153608/http://www.matasano.com/log/930/side-channel-detection-attacks-against-unauthorized-hypervisors/" rel="noreferrer">Wayback Machine link to the blog article (images broken)</a>.</p>\n\n<p>Bottom line from discussions on this is that there is always a way to detect a malicious hypervisor, and it\'s much simpler to detect one that isn\'t trying to hide.</p>\n'}, {'answer_id': 108504, 'author': 'Charles Duffy', 'author_id': 14122, 'author_profile': 'https://Stackoverflow.com/users/14122', 'pm_score': 4, 'selected': False, 'text': '<p>Red Hat has a program which detects which (if any) virtualization product it\'s being run under: <a href="https://people.redhat.com/~rjones/virt-what/" rel="noreferrer"><code>virt-what</code></a>.</p>\n\n<p>Using a third-party-maintained tool such is this is a better strategy long-term than trying to roll your own detection logic: more eyes (testing against more virtualization products), etc.</p>\n'}, {'answer_id': 144394, 'author': 'jakobengblom2', 'author_id': 23054, 'author_profile': 'https://Stackoverflow.com/users/23054', 'pm_score': 3, 'selected': False, 'text': '<p>It depends on what you are after:</p>\n\n<ul>\n<li><p>If the VM is not hiding from you on purpose, you can use some known hook.  LIke looking for VmWare drivers or the presence of certain strings in memory or certain other tell-tale signs. </p></li>\n<li><p>If the VM is really wanting you to do special things for it, it will have some obvious hook in place, like modifying the ID of the processor or adding some special registers that you can access to detect it. Or s a special device in a known location in memory (presuming you can get raw access to the physical memory space of your world).  NOte that modern machine designs like the IBM Power6 and Sun UltraSparc T1/T2 are designed to ALWAYS run a hypervisor, and never directly on raw hardware.  The interface to the "hardware" that an OS uses is in fact the interface ot a hypervisor software layer, with no way to get around it. In this case, detection is trivial since it is a constant "yes".  This is the likely future direction for all computer systems that can afford the overhead, look at the support in recent designs like the Freescale QorIQ P4080 chip, for example (www.freescale.com/qoriq). </p></li>\n<li><p>If the VM is intentionally trying to hide, and you are chasing its presence, it is a game of cat-and-mouse where the timing disturbance and different performance profile of a VM is almost always going to give it away.  Obviously, this depends on how the VM is implemented and how much hardware support there is in place in the architecture (I think a zSeries mainframe is much better at hiding the presence of a VM or stack of VMs under your particular OS than a regular x86 machine is, for example).  See <a href="http://jakob.engbloms.se/archives/97" rel="noreferrer">http://jakob.engbloms.se/archives/97</a> for some discussion on this topic.  It is possible to try to hide as a VM, but detection is quite likely to always win if it tries hard enough. </p></li>\n</ul>\n'}, {'answer_id': 3374621, 'author': 'Tim', 'author_id': 10755, 'author_profile': 'https://Stackoverflow.com/users/10755', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.trapkit.de/research/vmm/scoopyng/index.html" rel="nofollow noreferrer">TrapKIT provides ScoopyNG, a tool for VMware identification</a> -- it attempts to work around evasion techniques, but doesn\'t necessarily target any virtualization software other than VMware. Both source and binaries are available.</p>\n'}, {'answer_id': 20227137, 'author': 'm3nda', 'author_id': 2480481, 'author_profile': 'https://Stackoverflow.com/users/2480481', 'pm_score': 0, 'selected': False, 'text': '<p>In Linux systems, you can try to search for common files on /proc.</p>\n\n<p>Example, the existente of /proc/vz/ tell you is a OpenVZ.</p>\n\n<p><a href="http://www.dmo.ca/blog/detecting-virtualization-on-linux/" rel="nofollow">Here\'s a full guide to detect VM\'s environent</a> under Linux without have to "drink pills" :)</p>\n'}, {'answer_id': 50945690, 'author': 'Pritesh Mhatre', 'author_id': 3834496, 'author_profile': 'https://Stackoverflow.com/users/3834496', 'pm_score': 2, 'selected': False, 'text': '<p>Here is a (<strong><em>java + windows</em></strong>) solution to identify whether underlying machine is physical or virtual.</p>\n\n<p><strong>Virtual Machines Examples:</strong></p>\n\n<p><strong>Manufacturer</strong></p>\n\n<ul>\n<li>Xen</li>\n<li>Microsoft Corporation</li>\n<li>innotek GmbH</li>\n<li>Red Hat</li>\n<li>VMware, Inc.</li>\n</ul>\n\n<p><strong>Model</strong></p>\n\n<ul>\n<li>HVM domU</li>\n<li>Virtual Machine</li>\n<li>VirtualBox</li>\n<li>KVM</li>\n<li><p>VMware Virtual Platform</p>\n\n<pre><code>import java.io.BufferedReader;\nimport java.io.InputStreamReader;\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic abstract class OSUtil {\n\npublic static final List&lt;String&gt; readCmdOutput(String command) {\n    List&lt;String&gt; result = new ArrayList&lt;&gt;();\n\n    try {\n        Process p=Runtime.getRuntime().exec("cmd /c " + command);\n        p.waitFor();\n        BufferedReader reader=new BufferedReader(\n                new InputStreamReader(p.getInputStream())\n                );\n        String line;\n        while((line = reader.readLine()) != null) {\n            if(line != null &amp;&amp; !line.trim().isEmpty()) {\n                result.add(line);\n            }\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n\n    return result;\n}\n\npublic static final String readCmdOutput(String command, int lineNumber) {\n    List&lt;String&gt; result = readCmdOutput(command);\n    if(result.size() &lt; lineNumber) {\n        return null;\n    }\n\n    return result.get(lineNumber - 1);\n}\n\npublic static final String getBiosSerial() {\n    return readCmdOutput("WMIC BIOS GET SERIALNUMBER", 2);\n}\n\npublic static final String getHardwareModel() {\n    return readCmdOutput("WMIC COMPUTERSYSTEM GET MODEL", 2);\n}\n\npublic static final String getHardwareManufacturer() {\n    return readCmdOutput("WMIC COMPUTERSYSTEM GET MANUFACTURER", 2);\n}\n\npublic static void main(String[] args) {\n    System.out.println("BIOS Serial: " + getBiosSerial());\n    System.out.println("Hardware Model: " + getHardwareModel());\n    System.out.println("Hardware Manufacturer: " + getHardwareManufacturer());\n}\n}\n</code></pre></li>\n</ul>\n\n<p>You can use the output to decide whether it is a VM or a physical machine:</p>\n\n<p><strong>Physical machine output:</strong></p>\n\n<p>BIOS Serial: 2HC3J12<br>\nHardware Model: Inspiron 7570<br>\nHardware Manufacturer: Dell Inc.     </p>\n\n<p><strong>Virtual machine output:</strong></p>\n\n<p>BIOS Serial: 0<br>\nHardware Model: Innotec GmBH<br>\nHardware Manufacturer: Virtual Box     </p>\n'}]
['https://Stackoverflow.com/questions/39533', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/107/']