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:
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 .*/
-
/* 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'];
-
//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;
-
//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.
Read the rest of this entry …