Jump to content
Tuts 4 You
Sign in to follow this  
CodeExplorer

C generate all possible combination of strings - for brute force

Recommended Posts

CodeExplorer

C generate all possible combination of strings - for brute force:

char* ValidChars = "0123456789ABCDEF";
int MinimLen = 1;
int MaximLen = 2;
char SpecialChars[255] = {0};
char GeneratedString[50] = {0};
int Valid_Chars_len = strlen(ValidChars);

SpecialChars[0] = ValidChars[0];  // the first char will be first allowed char

// SpecialChars[i] will point to next char like this:
// SpecialChars['a'] = 'b';
// SpecialChars['b'] = 'c';
// SpecialChars['c'] = 00;  // the end of a loop
for (int i=0;i<Valid_Chars_len-1;i++)
SpecialChars[ValidChars[i]] = ValidChars[i+1];

memset(GeneratedString, ValidChars[0], MinimLen);  // we start with 'aaa' string

char NextChar;
int Pos = 0;
while (1)
{
Pos = 0;
printf("gen = %s\r\n", GeneratedString);

LoopStart:
NextChar = SpecialChars[GeneratedString[Pos]];
if (NextChar!=0)
{
GeneratedString[Pos] = NextChar;
}
else
{

GeneratedString[Pos] = SpecialChars[0];  // we start again
Pos++;

if (Pos>=MaximLen)
break;

goto LoopStart;
}
		

}

The code works 100% ok but it is a bit ugly especially the "goto LoopStart;"
Any other optimizations I could make to the above code or other generation of all combinations possibility?
Obviously should be optimized to the maximum!
 

  • Like 2

Share this post


Link to post
NOP

I could give you bits of my old C# code which does similar if you want to see other examples?

 

If you are going to try different methods then I suggest you add timing functions and analyze the difference between different methods and try to keep as much calculating info out of the loops as much as possible although your example is pretty basic it sounds like you are going to add to it

 

Mine generates all possible combinations off a valid chars string as you have but uses a number loop from 0 to NumOfCombinations(based off password length) with an extra loop with starting / ending password length so can generate all possible combinations of passwords length x to y

 

Share this post


Link to post
CodeExplorer

A@NOP: Will be great to see another example;
Trough brute force in C# is something I rather not do,
even Pelles C for Windows sometimes generates non-optimized asm code!
In the end I think I will code it in MASM.
 

Share this post


Link to post
NOP
41 minutes ago, CodeExplorer said:

Trough brute force in C# is something I rather not do,

I agree, I used it in an old brute forcer I made when I was learning C# and used it more for a coding exercise for brute forcing hashed string combinations in a background task

Coding brute forcers is pretty obsolete now, everything is done for GPU speeds but my old code went something like this (summarized)
 

int StartStrLen = 1;
int EndStrLen = 8;
string ValidCharsStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char[] CharsArr; = ValidCharsStr.ToCharArray();
int CharArrLen = CharsArr.Length;

for (int j = StartStrLen; j <= EndStrLen; j++)
{
    long NumCombinations = (long)Math.Pow(CharArrLen, j);
    
    for (int i = 0; i < NumCombinations; i++)
    {
        GeneratedString = GenerateString(i, j);
        // do something with the string here... display / save to file etc...
    }                
}

public string GenerateString(long Num, int StrLength)
{
    var NewCharArr = new char[StrLength];
    for (int i = 0; i < StrLength; i++)
    {
        double Rem = Num % CharArrLen;
        Num = Num / CharArrLen;
        NewCharArr[i] = CharsArr[(int)Rem];
    }
    return new string(NewCharArr);
}

 

Share this post


Link to post
CodeExplorer

I was able to optimize it by using pointers:

char* ValidChars = "0123456789ABCDEF";
int MinimLen = 8;
int MaximLen = 8;
char SpecialChars[255] = {0};
char GeneratedString[50] = {0};
int Valid_Chars_len = strlen(ValidChars);

SpecialChars[0] = ValidChars[0];  // the first char will be first allowed char

// SpecialChars[i] will point to next char like this:
// SpecialChars['a'] = 'b';
// SpecialChars['b'] = 'c';
// SpecialChars['c'] = 00;  // the end of a loop
for (int i=0;i<Valid_Chars_len-1;i++)
SpecialChars[ValidChars[i]] = ValidChars[i+1];

memset(GeneratedString, ValidChars[0], MinimLen);  // we start with 'aaa' string

char *Pointer = &GeneratedString[0];  // the gen string start
char *PointerEnd = &GeneratedString[0]+MaximLen;  // the gen string end
char NextChar = 0;
while (1)
{
Pointer = &GeneratedString[0];  // place in Pointer address of GeneratedString
printf("gen = %s\r\n", GeneratedString);

LoopStart:
NextChar = SpecialChars[*Pointer];
if (NextChar!=0)
{
*Pointer = NextChar;  // set generated with next char
}
else
{
*Pointer = SpecialChars[0];  // we start again
Pointer++;

if (Pointer>=PointerEnd)  // if we reached the end
break;

goto LoopStart;
}

}


Thanks for your help. If anyone knows any other optimizations that can be made let me know.
 

Share this post


Link to post
Progman

If single threaded then this really is as simple as having a set of counter indexes and the string value can be incremented based on it.  If multi-threaded its a whole different question as synchronizing between them is really inconvenient.  Or you can just check every n-th value in each thread and not worry if some get slightly ahead of others as it avoids the whole synchronization mess.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...