- 30
- Nov
I response to my Brute Force Calculator post — I would like to take the time to explain the PHP code involved with the program. This tutorial is written assuming you have basic knowledge of HTML.
The extent of the Brute Force Calulator program deals with these specific areas of PHP:
- Comments
- Variables
- Predefined $_GET Variable
- Basic Math
- Exponents
- If Statements
- Else Statements
- Isset
- Echo Function
- Including HTML inside of PHP
NOTE: Source code download included at the end of the post.
First thing that I always do when writing PHP is include a big fat comment with the name, description, email, and license. This is important. After all, it is nice to know what the program does, how to contact the author, and to know whether it can be copied or not. I would assume if something does not have a license I can take credit for it. Not that I would do such a thing, but legally, I could. The /* and */ states a multi-line comment. A // states a single line comment. Also, I always verbosely comment each section of the code with valuable information explaining my philosophy and reasoning.
-
/* Brute Force Calculator
-
Description: Calculates the time taken to brute force any given password
-
Author: <shane -AT- hackosis -DOT- com>
-
This program is free software: you can redistribute it and/or modify
-
it under the terms of the GNU General Public License as published by
-
the Free Software Foundation, either version 3 of the License, or
-
(at your option) any later version.
-
This program is distributed in the hope that it will be useful,
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
GNU General Public License for more details.
-
You should have received a copy of the GNU General Public License
-
along with this program. If not, see <http://www.gnu.org/licenses/>.*/
The next part of the PHP code is the retrieval of our variables through the HTML GET method. This data is provided by the HTML form. PHP variables always start with a $.The $_GET variable is a predefined in PHP and “used to collect values from a form”.
-
//Uppercase
-
$uc = $_GET[‘uc’];
-
-
//Lowercase
-
$lc = $_GET[‘lc’];
-
-
//Numerical
-
$nu = $_GET[‘nu’];
-
-
//Special Characters
-
$sc = $_GET[’sc’];
-
-
//Random Alpha/Numeric
-
$ran = $_GET[‘ran’];
-
-
//Random Alpha/Numeric and special characters
-
$rans = $_GET[‘rans’];
-
-
//Phrase or word subject to a dictionary attack
-
$dict = $_GET[‘dict’];
After we retrieve the variables from the HTML form, using simple addition the total number of characters are calculated and then stored in a variable named length.
-
//Length of password
-
$length = $uc + $lc + $nu + $sc + $ran + $rans + $dict;
Next, the key space needs to be calculated. If no length is entered for a particular character set, then 1 is assumed. The isset function checks to see if the variable is set and if not, performs the command specified for else. All If and else statements enclose the given commands in curly brackets; { and }. This is performed for each variable that is taken from the form and passed to a variable with a prepended “k” for key space.
The PHP pow function is used to calculate the key space using exponents based on the number of characters in the character set (lowercase a through z includes 26 characters). pow(number of characters in the character set(base number), length(exponent)) which is compared to 264 or 26^4, for example.
-
//Keyspace (entropy) of character sets based on length of password – if not set, then it is 1
-
{
-
} else {
-
$kuc = 1;
-
}
-
-
{
-
} else {
-
$klc = 1;
-
}
-
-
{
-
} else {
-
$knu = 1;
-
}
-
-
{
-
} else {
-
$ksc = 1;
-
}
-
-
{
-
}
-
{
-
} else {
-
$krans = 1;
-
}
-
-
{
-
} else {
-
$kdict = 1;
-
}
Next, the ultimate key space is calculated by multiplying the individual key space of each character set and then is stored in a variable named keyspace.
-
$keyspace = $kuc * $klc * $knu * $ksc * $kran * $krans * $kdict;
To reduce the key space by the law of averages, the key space is divided by two. This is included to assume that whatever is brute forcing the password will not try every single combination before it cracks the password.
-
//Reduce in half by law of averages
-
$workload = $keyspace / 2;
The number of possible tries that can be processed by a typical desktop computer (Pentium 4) is passed into a variable named trys. This was 2*2^33 in 2004, so I also multiplied that by 1.5 to account for growth of technology which comes out at 25,769,803,776.
-
//Number of effective trys per hour possible by a typical desktop computer (2*2^33)
-
$trys = 25769803776;
Number of hours needed to crack the password is now calculated. This is achieved by dividing the workload variable by the trys variable. The result is stored in a variable named hours.
-
//Number of hours needed to crack password
-
$hours = $workload / $trys;
Next, the number of days are calculated by dividing the hours by 24. The result is stored in a variable named days.
-
//Calculate number of days to crack password
-
$days = $hours / 24;
You will be happy to know that is all the calculations needed for the Brute Force Calculator. The final step is to apply some formatting to the days, hours, trys, and keyspace variables so that when they are printed they will have the thousands separator. This is achieved with the number_format function. The new variable has a prepended “f” for formatting.
-
//Add formatting for thousands seperator
HTML is then printed after closing the code with PHP tag. This includes the header and body with the HTML form that is used to fill out the information of the length of your password. Notice how we can include PHP code, with the PHP tags anywhere we want. The example here stores the variables back into the form after the submit button has been pressed for reviewing purposes.
I am not going to include the HTML code, you can download the source file at the end of the document.
Finally, the last step in the program is to print the information. This is done with a simple echo function. Whatever is enclosed in quotes will be printed to the browser as is. Anything outside of the quotes will be treated as PHP. Take note of the . before and after the variables to continue the echo function.
-
echo "<p><font size=’5′>Your password is <b>".$length."</b> characters long and has <b>".$fkeyspace."</b> combinations.<br />";
-
echo "It takes <b>".$fhours."</b> hours or <b>".$fdays."</b> days to crack your password on computer that trys <b>".$ftrys."</b> passwords per hour. This is based on a typical PC processor in 2007 and that the processor is under 10% load.</font></p><p>This PHP program is based of off calculation from the <a href=’http://www.mandylionlabs.com/documents/BFTCalc.xls’>spreadsheet</a> from Mandylion Labs. I have not changed any formulas except for multiplying the workload times 1.5 to account for growth of technology (The spreadsheet was created in 2004).";
I hope you enjoyed the walk through and let me know if there is any confusion based on what was explained. This is open source, so if you have anything to add, please contact me. I am especially interested in some Ajax to do automatic loading of the information in the last code snippet without a submit button.
Brute Force Calculator PHP Source Code
Related Posts
Tags: Brute Force Calculation, Featured, PHP, Tutorial, Web development



January 10th, 2008 at 6:12 pm
Not bad. But if you’re going to make something public then it’d be best mixed in with AJAX to load in 1 result at a time. Kudos nonetheless!
January 10th, 2008 at 7:13 pm
Hey Adam.
You acually read my mind. That was my next step and if you would like to contribute I would be happy to credit you.
Unfortunately, I have little to no experience with AJAX so it would take some time and research.