Saturday, March 14, 2009

Battlefield Heroes Beta Key Scraping With WatiN

I am pretty excited about the impending release of Battlefield Heroes. Ive been a huge fan of the BF franchise since BF2 and this free-to-play game looks like it is going to be a lot of fun. I signed up for a beta key some time ago and had all but given up of getting one, until I received it the other day out of the blue. It turns out they are getting ready to roll out the game sometime soon and as such are slowly giving out keys to everyone who has signed up in the order that they signed up. So i fired up the game and was pretty excited by what i saw - not only is it fun but my wife was pretty excited by what she saw too. Now she doesnt usually get into games so i was pretty keen to get my wife involved in one of my favourite activities. So all i needed to do was get her a key - problem is that since i signed up over 100 000 people have since decided its a good thing too. Thats a long queue to wait for a beta key. Then i found out that a site called BFHVerse has 60 keys to give out. Naturally every man and his dog wants one so the problem then becomes being able to grab a key within 2 seconds of it being posted before someone else does. Then i saw a particular post that caught my eye - one of the keys was apparently 'not working' because the way it had been printed on the coupon was all screwed up:



Because of this, the general consensus in the comments of the post was that the image was too badly scrambled and there was no point trying to use it. Bingo! This badly printed coupon was a stroke of luck - it meant i might have enough time and little competition to use this beta key. After studying this for a while and using Paint.NET i was able to make out all of the digits except the last three:



This gave me an idea - ive been wanting to try out the WatiN automated web testing framework for a while and there was currently a need to use it at work. This was the perfect opportunity to try it out and write an application to automatically search through all the possible combinations and try and find the remaining digits!

I made the following assumptions which i felt were pretty sound:


  1. The first 17 digits were correct
  2. The first unknown digit had a flat left edge, leading me to conclude it was either an 'E' or 'B'
  3. The other two unknown digits were indiscernible and could be any number, or any character
  4. The characters 'I', 'O', 'S' are generally not use in keys because they can be confused with '1', '0' and '5' respectively
  5. From analysing the other beta keys, it appeared that only certain characters were actually used, so they were a good starting point

Based on these assumption, there were around 1000 combinations of possible keys. This was a feasible amount so i came up with the following quick code to use WatiN to automatically fill in the beta key registration page with the various combinations until a valid key was found:

namespace BattlefieldHeroesBetaKeyFinder
{
using System;
using System.Globalization;
using WatiN.Core;

public class Program
{
//private const string BetaKeyFormat = "16D57-47H26-E5120-43E4H";
private const string BetaKeyFormat = "16D57-47H26-E5120-43{0}";
private const string AccountName = "TestUser";
private const string AccountPassword = "Password";

private static char[] Slot1Digits = { 'E' };//{ 'B', 'D', 'E', 'F' };
private static char[] Slot2Digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'L', 'R', 'T' };
private static char[] Slot3Digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'L', 'R', 'T' };

private static int slot1Offset;
private static int slot2Offset;
private static int slot3Offset;

[STAThread]
static void Main(string[] args)
{
string betaKey = "Not yet assigned";

using (IE ie = new IE("http://beta.battlefield-heroes.com/activate"))
{
do
{
string digits = Program.GetNextDigits();

if (digits == null)
{
betaKey = "No key found";
break;
}

betaKey = String.Format(CultureInfo.InvariantCulture, Program.BetaKeyFormat, digits);
Console.Write("Trying - " + betaKey);
//Console.WriteLine();

ie.TextField(Find.ByName("key")).TypeText(betaKey);
ie.TextField(Find.ByName("sec_name")).TypeText(Program.AccountName);
ie.TextField(Find.ByName("sec_pass")).TypeText(Program.AccountPassword);
ie.TextField(Find.ByName("sec_pass_confirm")).TypeText(Program.AccountPassword);
ie.CheckBox(Find.ByName("agreement")).Checked = true;

ie.Button(Find.ByValue("Activate My Key")).Click();

if (ie.ContainsText("That key appears to be invalid."))
{
Console.WriteLine(", FAILED");
}
else if (ie.ContainsText("That key has already been activated."))
{
Console.WriteLine(", IN USE");
}
else
{
Console.WriteLine(", SUCCESS!");
break;
}
}
while (true);

Console.ReadKey(true);
}
}

private static string GetNextDigits()
{
if (slot3Offset >= Program.Slot3Digits.Length)
{
slot3Offset = 0;
slot2Offset++;
}

if (slot2Offset >= Program.Slot2Digits.Length)
{
slot2Offset = 0;
slot1Offset++;
}

if (slot1Offset >= Program.Slot1Digits.Length)
{
return null;
}

return new string(new char[] { Program.Slot1Digits[slot1Offset], Program.Slot2Digits[slot2Offset], Program.Slot3Digits[slot3Offset++] });
}
}
}


The program took about 2 seconds per attempt and after about 10 minutes of trying i found the key that worked! All this and it had only taken me about an hour to create it all. Unfortunately it turns out that the key had already been registered, but the exercise had taught me a lot. The point of this post is not really to say that the above code is awesome, more that WatiN is an extremely useful package that can be used to autofill and scrap webforms with ease. I highly recommend you check it out. Im just happy that the BF Heroes beta key administration didnt pick up my multiple attempts and ban my IP.


Oh and btw...for those curious types out there, the correct beta key is included as a comment in the above code :)