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;
- using MiddlewareWCF.Couche_metier.Composant_metier;
- 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; 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.
- Console.WriteLine("JE MATCH");
- this.matchCounter = this.counter;
- }
- // If we have to reset the loop.
- if(this._incrementKey())
- {
- // Reset the counter.
- i = 0;
- }
- byte[] resultat = XOR.Decrypt(this.strToForce, this.key);
- Console.WriteLine("text decrypté : {0}", Encoding.UTF8.GetString(resultat));
- 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.
- StringBuilder sb = new StringBuilder(key);
- sb[key.Length - 1] = (char)this.asciiCharactersToCheck[0];
- key = sb.ToString();
- // We have to check the previous character and update it if its value is not the last one on the ascii table.
- key = this._incrementPreviousKeys(key, key.Length - 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().
- StringBuilder sb = new StringBuilder(key);
- sb[key.Length - 1] = lastCharInc;
- key = sb.ToString();
- }
- this.key = key;
- return resetLoop;
- }
- /*
- * Increment the previous key when a key is reset.
- * xyz => if z reach the max value then this method is called.
- */
- private String _incrementPreviousKeys(String key, int index)
- {
- // Get the char to check from the end of the key depending on the index.
- char charToCheck = key[index - 1];
- // If the charToCheck is the last ascii char then reset it and check also the previous one.
- if (charToCheck == (char)this.asciiCharactersToCheck[this.asciiCharactersToCheck.Count - 1])
- {
- // Replace the charToCheck at the defined index in the key string.
- StringBuilder sb = new StringBuilder(key);
- sb[index - 1] = (char)this.asciiCharactersToCheck[0];
- key = sb.ToString();
- // If the charToCheck is the first one then add a new char at the end of the string and replace the first one.
- if (index - 1 == 0)
- {
- sb = new StringBuilder(key);
- sb[0] = (char)this.asciiCharactersToCheck[0];
- return sb.ToString() + this.asciiCharactersToCheck[0];
- }
- else
- {
- // Else call itself changing the index to check its previous char.
- return _incrementPreviousKeys(key, --index);
- }
- }
- else
- {
- // Update the char to the next value in the ascii table.
- char charToCheckInc = (char)this.asciiCharactersToCheck[this._getAsciiIndexFromValue(charToCheck) + 1];
- StringBuilder sb = new StringBuilder(key);
- sb[(index - 1)] = charToCheckInc;
- key = sb.ToString();
- }
- return key;
- }
- /*
- * Get the index of the value in the ascii ArrayList.
- */
- private int _getAsciiIndexFromValue(char value)
- {
- return this.asciiCharactersToCheck.IndexOf(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