Advertisement
Vadorequest

BrutForce.cs

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