A Simple Virtual Machine |
Written by Alexey Lyashko | ||||
Wednesday, 01 February 2012 | ||||
Page 1 of 3 Virtual Machines have more uses than you might imagine. We have a real example of how a VM can be used to increase the security of your code.
In computing a Virtual Machine (VM) is a software implementation of either an existing or a fictional hardware platform. VM's are generally divided into two classes -
and
Anyway, if you are just interested in the definition of the term read the article on Wikipedia. There are tons of articles dedicated to this matter on the Internet, hundreds of tutorials and explanations. I see no reason to just add another "trivial" article or tutorial to the heap. Instead, I think it may be more interesting to see the idea in action, to have an example of a real application. One may say that we are surrounded by examples - Java, .NET, etc. This is correct, however, I would like to touch on a slightly different application of this technology - protecting your software/data from being hacked. Data ProtectionMillions of dollars are being spent by software (or content) vendors in an attempt to protect their products from being stolen or used in any other illegal way. There are numerous protection tools and utilities, starting with simple packers/scramblers and ending with complex packages that implement multilevel encryption and virtual machines as well. However, you may disagree, but you won't convince me, that an out-of-the-box solution is good until it gains popularity. There is enough evidence for this statement. In my opinion, no one can protect your software better than you. It only depends on how well protected you want it to be. Although, there are numerous protection methods and techniques, we are going to concentrate on a virtual machine for data coding/decoding. Nothing special, just a trivial XOR method, but, in my opinion, enough to demonstrate the fundamentals. Design Your VMWhile in real life, hardware design precedes its software counterpart, we can do it in reverse order (it is our own VM, after all). Therefore, we will begin with the pseudo executable file format which will be supported by our VM. A good idea is to put a header in the beginning of the file. In order to do so, we have to think what our file is going to contain. The file may be a raw code (remember DOS com files?), but this would not be interesting enough. So, let our file be divided into three sections:
Let us define the header as a C structure:
typedef struct _VM_HEADER Well, one more thing. Actually the most important one. We need a compiler for our pseudo assembly that would be able to output files of this format. Fortunately, we do not have to write one (although, this may be an interesting task). Tomasz Grysztar has done a wonderful work with his Flat Assembler. Despite the fact, that this compiler is intended to compile Intel assembly code, thanks to the wonderful macro instruction support, we can adapt it to our needs. The skeleton source for our file would look like this: include 'defs.asm' as simple as that.
The export section deserves special attention. I tried to make it as easy to use as possible. It is divided into two parts:
In the above example, the export section would look like this: ; Array of file offsets Save the file as 'something.asm' or whatever name you prefer. Compile it with Fasm.
|
||||
Last Updated ( Wednesday, 01 February 2012 ) |