How to Create a Virtual Machine with Ubuntu 15.04 Vivid and VMBuilder?

The need of a Virtual Machine

I recently decided to migrate my dedicated server from Online to OVH and realized that installing a server is extremely painful.
Then I realized that one way to avoid this mess again could be to setup a Virtual Machine so that I could just move that VM to another host later.

How to do it with the command line?

There are plenty of tutorials which explain how to do so. But it looks like the options change every six months. So after hours of experimentation here is what worked for me:

mkdir /var/vms
vmbuilder kvm ubuntu \
	--cpus 2 --arch amd64 \
	--rootsize 65536 \
	--mem 4096 \
	--hostname vm01 \
	--ip 192.168.122.101 --net 192.168.122.0 --mask 255.255.255.0 --gw 192.168.122.1 --bcast 192.168.122.255 --dns 192.168.122.1 \
	--user [USER] --name [YOUR NAME] --pass [PASSWORD] \
	--suite trusty --flavour virtual \
	--addpkg acpid --addpkg linux-image-generic --addpkg vim \
	--mirror "ftp://ubuntu.mirrors.ovh.net/ftp.ubuntu.com/ubuntu" --components main,universe \
	--libvirt qemu:///system \
	--destdir /var/vms/vm01
virsh start vm01
virsh autostart vm01
  • a VM with Ubuntu Trusty as Guest OS run by KVM and controllable with virsh later
  • a NATed network with static IPs
    (the doc doesn’t mention that you need to specify –net and –mask for –ip to be taken into account)
    Note that 192.168.122.0/24 is the default IP range available so you kind of have to choose an IP in that range
  • An automatic startup

Brute force on a .p12 password with c#

It started with a VPN problem

The other day, I had an issue with my VPN and someone from the IT service remotely connected to my machine to put a certificate on it. While he manipulated the certificate manager, he exported a certificate and typed a password of 4 characters that I couldn’t see (characters were obfuscated). Just after that he requested me to use another wifi connection to test the VPN so we had to stop the chat and the remote session. Unfortunatly for me it didn’t work and the guy went to lunch.

So now, I was stuck with no VPN and a certificate which were not working! I decided to delete all the personal certificates having my name from my computer and to re-import the p12 file that the IT guy left on my desktop. I tried to do so but then I was requested to type the password – remember? it was only 4 chars. I tried all the stupid 4 letters password I got in mind (“test”, “1234”, “0000”, “aaaa”, “azer”, “qwert” …) but none of them worked.

Let’s brute force that weak p12 password

So here is the interesting part. I decided to try that very famous thing called “brute force” a password. I was saying in my mind “With my years of programming experience, it should take me 10 minutes to code it and few second to run”.
But actually it took me up to one hour to make the stuff to actually work without obvious bug in C#:

private IEnumerable<string> EnumerateOptions(IList<char> options, int minLength, int maxLength)
{
    var number = new List<int>(maxLength);
    for (int i = 0; i < minLength; i++)
        number.Add(0);

    while (number.Count <= maxLength)
    {
        yield return Stringify(number, options);

        bool overflowed = true;
        for (int numberPart = number.Count - 1; numberPart >= 0 && overflowed; numberPart--)
        {
            number[numberPart]++;
            if (number[numberPart] >= options.Count)
            {
                number[numberPart] = number[numberPart] % options.Count;
            }
            else
            {
                overflowed = false;
            }
        }
        if (overflowed)
            number.Insert(0, 0);
    }
}

private string Stringify(IList<int> number, IList<char> options)
{
    return string.Join("", number.Select(n => options[n]));
}

The result

The app was testing 2k passwords per seconds and found the password in less than 3 minutes 🙂 !

p12-brute-force

The conclusions are

  • don’t use a 4 letters password, obviously, especially on your p12 files which can used to act on your behalf
  • coding simple things can take more time than we expect (think hiring interview questions)
  • The method I’m using to check the password (X509Certificate2Collection.Import) throws an exception when the password is wrong. This makes the process extremelly slow (15x). When you happen to completely ignore the exception (catch(Exception) with a variable), it becomes reasonably faster if you activate the code optimization (Release mode)

That’s it, the code is on Github.  If you happen to spot obvious bugs in the enumerator of passwords, don’t hesitate to let me know with a comment.