Advertisement
Vadorequest

BruteForce

Nov 29th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.83 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace BrutForce
  9. {
  10.     class BrutForce
  11.     {
  12.  
  13.         /*
  14.          * Contains all char to check.
  15.          */
  16.         private ArrayList asciiCharactersToCheck;
  17.  
  18.         /*
  19.          * Iterations counter.
  20.          */
  21.         private int counter = 0;
  22.  
  23.         /*
  24.          * Last key used to force the string.
  25.          */
  26.         private String key;
  27.  
  28.         /*
  29.          * Contains matched string.
  30.          */
  31.         private String match;
  32.  
  33.         /*
  34.          * Contains the counter value when the last match was found.
  35.          */
  36.         private int matchCounter;
  37.  
  38.         /*
  39.          * String to force.
  40.          */
  41.         private string strToForce;
  42.  
  43.         /*
  44.          * Min length to the string to force.
  45.          */
  46.         private int minLength;
  47.  
  48.         /*
  49.          * Max length to the string to force.
  50.          */
  51.         private int maxLength;
  52.  
  53.         /*
  54.          * Default constructor.
  55.          * Will generate a default array of characters to check.
  56.          * Check only [a-zA-z0-9]
  57.          */
  58.         public BrutForce()
  59.         {
  60.             // Init.
  61.             ArrayList asciiCharactersToCheck = new ArrayList();
  62.  
  63.             // Fill it - Numbers
  64.             for (int i = 48; i <= 57; i++)
  65.             {
  66.                 asciiCharactersToCheck.Add((char)i);
  67.             }
  68.  
  69.             // Fill it - Uppercase letters
  70.             for (int i = 65; i <= 90; i++)
  71.             {
  72.                 asciiCharactersToCheck.Add((char)i);
  73.             }
  74.             // Fill it - Lowercase letters
  75.             for (int i = 97; i <= 122; i++)
  76.             {
  77.                 asciiCharactersToCheck.Add((char)i);
  78.             }
  79.  
  80.             // Save in instance.
  81.             this.asciiCharactersToCheck = asciiCharactersToCheck;
  82.         }
  83.  
  84.         /*
  85.          * Use a predefined array of ascii characters.
  86.          */
  87.         public BrutForce(ArrayList asciiCharactersToCheck)
  88.         {
  89.             this.asciiCharactersToCheck = asciiCharactersToCheck;
  90.         }
  91.  
  92.         /*
  93.          * Execute the brute force.
  94.          * @Returns >0: Number of iterations used to force the string.
  95.          *          0: No match.
  96.          *          -1: Empty string to force.
  97.          */
  98.         public int Force(String _strToForce, int _minLength, int _maxLength)
  99.         {
  100.             // Init instance vars.
  101.             this.strToForce = _strToForce;
  102.             this.minLength = _minLength <= 0 ? 1: _minLength;// Min value to 1.
  103.             this.maxLength = _maxLength <= 0 ? 1: _maxLength;// Min value to 1.
  104.             this.match = null;
  105.  
  106.             if(strToForce.Length == 0)
  107.             {
  108.                 return -1;
  109.             }
  110.  
  111.             // Define key with the first value in the ascii array. (convert to string)
  112.             this.key = (char)this.asciiCharactersToCheck[0]+"";
  113.  
  114.             // If the size of the key is upper than 1
  115.             if(minLength > 1)
  116.             {
  117.                 // Start to index 1 because the 0 was filled just before.
  118.                 for (int i = 1; i < minLength; i++)
  119.                 {
  120.                     // Concat the string with the first value in the ascii array.
  121.                     this.key += (char) this.asciiCharactersToCheck[0] + "";
  122.                 }
  123.             }
  124.             Console.WriteLine(this.key);// Init key
  125.             // Main loop while the maxLength of the key is not reached or
  126.             for (int i = 0; i < this.asciiCharactersToCheck.Count && key.Length < this.maxLength; i++)
  127.             {
  128.                 if (this._match())
  129.                 {
  130.                     // 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.
  131.                     this.matchCounter = this.counter;
  132.                 }
  133.  
  134.                 // If we have to reset the loop.
  135.                 if(this._incrementKey())
  136.                 {
  137.                     // Reset the counter.
  138.                     i = 0;
  139.                 }
  140.                 Console.WriteLine(this.key);
  141.                 this.counter++;
  142.             }
  143.  
  144.             return this.Counter;
  145.         }
  146.  
  147.         private bool _match()
  148.         {
  149.             /* TODO Change my code here =D (And use your brain {if this.brainIsAvailable() == true})
  150.              * Check if there is a match between the key and the string to force. (XOR)
  151.              * If yes, store the key in the this.match and call your webservice and return true.
  152.              */
  153.             // The following code with only test if the string sent match the key, no XOR.
  154.             if(this.strToForce == this.key)
  155.             {
  156.                 this.match = this.key;// TOO USEFUL GUYS! Do you love it?
  157.                 return true;
  158.             }
  159.             return false;
  160.         }
  161.  
  162.         /*
  163.          * Increment the key, that means increment the last char.
  164.          */
  165.         private bool _incrementKey()
  166.         {
  167.             bool resetLoop = false;
  168.             String key = this.key;
  169.  
  170.             // Get the last char of the key.
  171.             char lastChar = (char)key[key.Length - 1];
  172.  
  173.             // Get its value in the ascii table and increment it.
  174.             int indexAsciiArray = this._getAsciiIndexFromValue(lastChar);
  175.  
  176.             // Check if the index is the last one, in this case we won't increase it but reset it.
  177.             if (indexAsciiArray == this._getAsciiIndexFromValue((char)this.asciiCharactersToCheck[this.asciiCharactersToCheck.Count - 1]))
  178.             {
  179.                 // Reset the main loop in the Public method.
  180.                 resetLoop = true;
  181.  
  182.                 // Reset the last char of the key to the first ascii character.
  183.                 key = this._replaceCharInString(key, (char)this.asciiCharactersToCheck[0]);
  184.  
  185.                 // We have to check the previous character and update it if its value is not the last one on the ascii table.
  186.                 // TODO
  187.                 this._replaceOnResetLoop(key, 1);// Recursive call in itself.
  188.             }else
  189.             {
  190.                 // Just increase the last character.
  191.                 char lastCharInc = (char)this.asciiCharactersToCheck[indexAsciiArray + 1];
  192.  
  193.                 // Remove the last char and add the last char incremented, acts as a replace().
  194.                 key = this._replaceCharInString(key, lastCharInc);
  195.             }
  196.  
  197.             this.key = key;
  198.             return resetLoop;
  199.         }
  200.  
  201.         // I don't like its name but...
  202.         private bool _replaceOnResetLoop(String key, int index)
  203.         {
  204.             bool addCharacter = false;
  205.  
  206.             // Start at index, recursive call.
  207.             for (int i = index; i < key.Length; i++)
  208.             {
  209.                 // TODO JE SUIS ICI !
  210.             }
  211.  
  212.             return addCharacter;
  213.         }
  214.  
  215.         private int _getAsciiIndexFromValue(char value)
  216.         {
  217.             return this.asciiCharactersToCheck.IndexOf(value);
  218.         }
  219.  
  220.         private String _replaceCharInString(String str, char value, int indexFromEnd = 1, int indexFromStart = 0)
  221.         {
  222.             return str.Substring(indexFromStart, str.Length - indexFromEnd) + value;
  223.         }
  224.  
  225.         // Accessors.
  226.         public int Counter
  227.         {
  228.             get { return counter; }
  229.         }
  230.  
  231.         public String Match
  232.         {
  233.             get { return match; }
  234.         }
  235.  
  236.         public string StrToForce
  237.         {
  238.             get { return strToForce; }
  239.         }
  240.  
  241.         public int MinLength
  242.         {
  243.             get { return minLength; }
  244.         }
  245.  
  246.         public int MaxLength
  247.         {
  248.             get { return maxLength; }
  249.         }
  250.  
  251.         public int MatchCounter
  252.         {
  253.             get { return matchCounter; }
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement