Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BrutForce
- {
- class BrutForce
- {
- /*
- * Contains all char to check.
- */
- private ArrayList asciiCharactersToCheck;
- /*
- * Iterations counter.
- */
- private int counter = 0;
- /*
- * Last key used to force the string.
- */
- private String key;
- /*
- * Contains matched string.
- */
- private String match;
- /*
- * Contains the counter value when the last match was found.
- */
- private int matchCounter;
- /*
- * String to force.
- */
- private string strToForce;
- /*
- * Min length to the string to force.
- */
- private int minLength;
- /*
- * Max length to the string to force.
- */
- private int maxLength;
- /*
- * Default constructor.
- * Will generate a default array of characters to check.
- * Check only [a-zA-z0-9]
- */
- public BrutForce()
- {
- // Init.
- ArrayList asciiCharactersToCheck = new ArrayList();
- // Fill it - Numbers
- for (int i = 48; i <= 57; i++)
- {
- asciiCharactersToCheck.Add((char)i);
- }
- // Fill it - Uppercase letters
- for (int i = 65; i <= 90; i++)
- {
- asciiCharactersToCheck.Add((char)i);
- }
- // Fill it - Lowercase letters
- for (int i = 97; i <= 122; i++)
- {
- asciiCharactersToCheck.Add((char)i);
- }
- // Save in instance.
- this.asciiCharactersToCheck = asciiCharactersToCheck;
- }
- /*
- * Use a predefined array of ascii characters.
- */
- public BrutForce(ArrayList asciiCharactersToCheck)
- {
- this.asciiCharactersToCheck = asciiCharactersToCheck;
- }
- /*
- * Execute the brute force.
- * @Returns >0: Number of iterations used to force the string.
- * 0: No match.
- * -1: Empty string to force.
- */
- public int Force(String _strToForce, int _minLength, int _maxLength)
- {
- // Init instance vars.
- this.strToForce = _strToForce;
- this.minLength = _minLength <= 0 ? 1: _minLength;// Min value to 1.
- this.maxLength = _maxLength <= 0 ? 1: _maxLength;// Min value to 1.
- this.match = null;
- if(strToForce.Length == 0)
- {
- return -1;
- }
- // Define key with the first value in the ascii array. (convert to string)
- this.key = (char)this.asciiCharactersToCheck[0]+"";
- // If the size of the key is upper than 1
- if(minLength > 1)
- {
- // Start to index 1 because the 0 was filled just before.
- for (int i = 1; i < minLength; i++)
- {
- // Concat the string with the first value in the ascii array.
- this.key += (char) this.asciiCharactersToCheck[0] + "";
- }
- }
- Console.WriteLine(this.key);// Init key
- // Main loop while the maxLength of the key is not reached or
- for (int i = 0; i < this.asciiCharactersToCheck.Count && key.Length < this.maxLength; i++)
- {
- if (this._match())
- {
- // Do whatever you want when there is a match. Or do it in the function, as you want, I don't care, BITCH. Maybe you'll want to RETURN/BREAK/NTM.
- this.matchCounter = this.counter;
- }
- // If we have to reset the loop.
- if(this._incrementKey())
- {
- // Reset the counter.
- i = 0;
- }
- Console.WriteLine(this.key);
- this.counter++;
- }
- return this.Counter;
- }
- private bool _match()
- {
- /* TODO Change my code here =D (And use your brain {if this.brainIsAvailable() == true})
- * Check if there is a match between the key and the string to force. (XOR)
- * If yes, store the key in the this.match and call your webservice and return true.
- */
- // The following code with only test if the string sent match the key, no XOR.
- if(this.strToForce == this.key)
- {
- this.match = this.key;// TOO USEFUL GUYS! Do you love it?
- return true;
- }
- return false;
- }
- /*
- * Increment the key, that means increment the last char.
- */
- private bool _incrementKey()
- {
- bool resetLoop = false;
- String key = this.key;
- // Get the last char of the key.
- char lastChar = (char)key[key.Length - 1];
- // Get its value in the ascii table and increment it.
- int indexAsciiArray = this._getAsciiIndexFromValue(lastChar);
- // Check if the index is the last one, in this case we won't increase it but reset it.
- if (indexAsciiArray == this._getAsciiIndexFromValue((char)this.asciiCharactersToCheck[this.asciiCharactersToCheck.Count - 1]))
- {
- // Reset the main loop in the Public method.
- resetLoop = true;
- // Reset the last char of the key to the first ascii character.
- key = this._replaceCharInString(key, (char)this.asciiCharactersToCheck[0]);
- // We have to check the previous character and update it if its value is not the last one on the ascii table.
- // TODO
- this._replaceOnResetLoop(key, 1);// Recursive call in itself.
- }else
- {
- // Just increase the last character.
- char lastCharInc = (char)this.asciiCharactersToCheck[indexAsciiArray + 1];
- // Remove the last char and add the last char incremented, acts as a replace().
- key = this._replaceCharInString(key, lastCharInc);
- }
- this.key = key;
- return resetLoop;
- }
- // I don't like its name but...
- private bool _replaceOnResetLoop(String key, int index)
- {
- bool addCharacter = false;
- // Start at index, recursive call.
- for (int i = index; i < key.Length; i++)
- {
- // TODO JE SUIS ICI !
- }
- return addCharacter;
- }
- private int _getAsciiIndexFromValue(char value)
- {
- return this.asciiCharactersToCheck.IndexOf(value);
- }
- private String _replaceCharInString(String str, char value, int indexFromEnd = 1, int indexFromStart = 0)
- {
- return str.Substring(indexFromStart, str.Length - indexFromEnd) + value;
- }
- // Accessors.
- public int Counter
- {
- get { return counter; }
- }
- public String Match
- {
- get { return match; }
- }
- public string StrToForce
- {
- get { return strToForce; }
- }
- public int MinLength
- {
- get { return minLength; }
- }
- public int MaxLength
- {
- get { return maxLength; }
- }
- public int MatchCounter
- {
- get { return matchCounter; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement