I am doing a security presentation for my Computer and Information Security course in a few weeks time, and in this presentation I will be demonstrating the pros and cons of different attacks (dictionary, rainbow and bruteforce). I am do the dictionary and rainbow attacks fine but I need to generate the bruteforce attack on the fly. I need to find an algorithm that will let me cycle though every combination of letter, symbol and number up to a certain character length.
So as an example, for a character length of 12, the first and last few generations will be:
a
ab
abc
abcd
...
...
zzzzzzzzzzzx
zzzzzzzzzzzy
zzzzzzzzzzzz
But it will also use numbers and symbols, so it's quite hard for me to explain... but I think you get the idea. Using only symbols from the ASCII table is fine.
I can kind of picture using an ASCII function to do this with a counter, but I just can't work it out in my head. If anyone could provide some source code (I'll probably be using C#) or even some pseudo code that I can program a function from that'd be great.
Thank you in advance. :)
A recursive function will let you run through all combinations of ValidChars:
Assign the set of valid characters to ValidChars, the maximum length of string you want to maxlength, then call
Dive("", 0);
and away you go.You need to generate all combinations of characters from a set of valid characters ; let's call this set
validChars
. Basically, each set of combinations of length N is a cartesian product ofvalidChars
with itself, N times. That's pretty easy to do using Linq:Obviously, you don't want to manually write the code for each length, especially if you don't know in advance the maximum length...
Eric Lippert has an article about generating the cartesian product of an arbitrary number of sequences. Using the
CartesianProduct
extension method provided by the article, you can generate all combinations of length N as follows:Since you want all combinations from length 1 to MAX, you can do something like that:
allCombinations
is anIEnumerable<IEnumerable<char>>
, if you want to get the results as a sequence of strings, you just need to add a projection:Note that it's certainly not the most efficient solution, but at least it's short and readable...
Another alternative i did, that return a string.
I did not care about the performance of the thing since it was not for a real world scenario.
You can try this code, that use recursion to print all possible strings of 0 to stringsLenght chars lenght, composed by all combination of chars from firstRangeChar to lastRangeChar.
This look to be faster than the solution of @dthorpe.I've compared the algorthms using this code:
and, on my pc, I get these results: