Algorithm, hashes and fun

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Algorithm, hashes and fun

Post by ayu »

I got a little assignment to try and crack some hashes at work for a project.
So I thought I could write about it here for fun :)
Maybe someone will learn something.

The idea is that we will take the plain text passwords when cracked, and hash them with a more common and standard method, like SHA512.
The current method is a MD5 based one, and since I can't find if this specific method is a common one (thus would be supported by for example oclHashCat), I will port it to another language and write my own little cracker using CUDA.

Now, the function that created the hashes is written in PHP.

Code: Select all

function createPwdHash($pwd='',$salt='')
{
	// $pwd can be any password
	// $salt must be numeric and atleast 3 chars long
	// Parse $salt as numeric
	$salt = intval($salt);

	// Validate critical values
	
	if(strlen($pwd) > 0 && strlen($salt) > 2)
	{
		// First Hash of password
		$pwdHash = md5($pwd);

		// Create a string to steal chars from
		$randStr = md5($salt);

		// Calculate regulations allowed by regulator key
		$run = floor(strlen($salt)/3);

		// Salt and encrypt password as many times as the salt allows
		$i = 0;
		while ($i < $run) {

		// Extract regulator values from parameters
		$saltStart = substr($salt,0+($i*3),1);	
		$saltLength = substr($salt,1+($i*3),1);
		$saltTarget = substr($salt,2+($i*3),1);
		$saltValue = substr($randStr,$saltStart,$saltLength);

		// Concat regulated string and create new hash
		$pwdHash = substr($pwdHash,0,$saltTarget).$saltValue.substr($pwdHash,$saltTarget,-1);
		$pwdHash = md5($pwdHash);

		$i++;
		}
	}
	else
	{
		$pwdHash = null;
	}

	return $pwdHash;
}
So, since we are using C#.Net in this project at work, I decided to not write it in C as I usually would, and just follow the project method as it is, thus writing it in C#.
So, so far I have just made a simple port of the algorithm.

Code: Select all

static String createPwdHash(String pwd, String salt)
        {
            MD5 md5Hash = MD5.Create();

            String pwdHash = GetMd5Hash(md5Hash, pwd);
            String randStr = GetMd5Hash(md5Hash, salt);

            int run = (int)Math.Floor((decimal)(salt.Length/3));

            for (int i = 0; i < run; ++i)
            {
                String saltStart = salt.Substring(0 + (i * 3), 1);
                String saltLength = salt.Substring(1 + (i * 3), 1);
                String saltTarget = salt.Substring(2 + (i * 3), 1);
                String saltValue = randStr.Substring(Convert.ToInt32(saltStart), Convert.ToInt32(saltLength));

                pwdHash = pwdHash.Substring(0, Convert.ToInt32(saltTarget)) + saltValue + pwdHash.Substring(Convert.ToInt32(saltTarget), (pwdHash.Length - Convert.ToInt32(saltTarget) - 1));
                pwdHash = GetMd5Hash(md5Hash, pwdHash);

            }

            return pwdHash;
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
So, the next step now is to just write a simple brute forcer, using the hashes that I have and a dictionary.
I will update this thread as I go :)
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Algorithm, hashes and fun

Post by ayu »

Now, I made a little brute force test now using a dictionary and only the CPU (no CUDA yet, if even possible with .Net, but I found a library for it)

Code: Select all

static void Main(string[] args)
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(User));

            Console.WriteLine("Loading users ...");
            User[] users = engine.ReadFile("user.csv") as User[];

            Console.WriteLine("Loading password file ...");
            string[] lines = System.IO.File.ReadAllLines(@"full_sorted.txt");

            Console.WriteLine("Cracking");
            foreach (User user in users)
            {
                foreach (string password in lines)
                {
                    if (createPwdHash(password, user.c1) == user.c4)
                    {
                        Console.WriteLine(user.c4 + " => " + password);
                    }
                }
            }

            Console.WriteLine("Done");

            Console.WriteLine("Press any key too continue...");
            Console.ReadLine();
        }
So far I have figured out the salt field of the user.csv file (no columns supplied), and a few passwords are cracked.
But I still need to cover all different passwords, so the goal is to brute force with and not using a dictionary based attack, and also to do it using the GPU to make it so much faster.
Next step now is to make a better brute forcing method.
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Algorithm, hashes and fun

Post by ayu »

Ok so now I have written a brute force version of the program, using an older code that I wrote a long time ago in C.

http://code.suck-o.com/42424" onclick="window.open(this.href);return false;

The link above does not contain the final version of that code, but it doesn't matter since I only needed a part of it.

Anyway so here's the current work on the C# program (will use the code page from now on due to the lack of proper code highlight and indentation here).

http://code.suck-o.com/42425" onclick="window.open(this.href);return false;

The next step now, is to implement CUDA functionality, which will be a pretty big and hard steps, since I haven't programmed with CUDA before, so that will be exciting ^^

Anyway, good night for now ... the adventure will continue tomorrow :D
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Algorithm, hashes and fun

Post by bad_brain »

would be interesting to run some comparative benchmarks... :-k
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Algorithm, hashes and fun

Post by ayu »

bad_brain wrote:would be interesting to run some comparative benchmarks... :-k
You mean between the CPU version and the GPU one?

Currently I'm trying to get membership on a site that has documentation on how to deal with CUDA in .NET ... apparently not as easy as I thought as the site is rather inactive at the moment (at least they aren't activating my account, nor are they answering my Emails).

But ... I will call the owner of the site if I have to (in Israel), as I really want this little project going :D
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Algorithm, hashes and fun

Post by bad_brain »

cats wrote:
bad_brain wrote:would be interesting to run some comparative benchmarks... :-k
You mean between the CPU version and the GPU one?
yeup! I would offer my overclocked 6-core one... :)
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Algorithm, hashes and fun

Post by ayu »

bad_brain wrote:
cats wrote:
bad_brain wrote:would be interesting to run some comparative benchmarks... :-k
You mean between the CPU version and the GPU one?
yeup! I would offer my overclocked 6-core one... :)
Cool!

I will see if I can implement the CUDA support :D

Just need to get my hands on that damn documentation :P

(If I can't get it, I will simply take a day to rewrite the program in C, since there are loads of documentation for C for CUDA)
"The best place to hide a tree, is in a forest"

Post Reply