From 118621a38d741c81eb3b1422c6cf64fd28541f6d Mon Sep 17 00:00:00 2001 From: Nick Tsai Date: Wed, 31 Jul 2019 18:51:20 +0800 Subject: [PATCH] Prepare for version 1.0.0 --- LICENSE | 21 ++++++ README.md | 133 ++++++++++++++++++++++++++++++++++++- composer.json | 20 ++++++ src/BruteForceAttacker.php | 108 ++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 src/BruteForceAttacker.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f697ab7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Nick Tsai + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index e0bfa1b..fcb776c 100644 --- a/README.md +++ b/README.md @@ -1 +1,132 @@ -# brute-force-attacker-php \ No newline at end of file +

+

Brute Force Attacker for PHP

+
+

+ +Brute-force attack tool for generating all possible string and executing function + +[![Latest Stable Version](https://poser.pugx.org/yidas/google-maps-services/v/stable?format=flat-square)](https://packagist.org/packages/yidas/brute-force-attacker) +[![License](https://poser.pugx.org/yidas/google-maps-services/license?format=flat-square)](https://packagist.org/packages/yidas/brute-force-attacker) + +OUTLINE +------- + +- [Demonstration](#demonstration) +- [Requirements](#requirements) +- [Installation](#installation) +- [Usage](#usage) + - [Options](#options) + +--- + +DEMONSTRATION +------------- + +```php +\yidas\BruteForceAttacker::run([ + 'length' => 2, + 'callback' => function ($string) { + echo "{$string}\n"; + }, +]); +``` + +Generates `0`-`9` string and matches target string: + +```php +\yidas\BruteForceAttacker::run([ + 'length' => 6, + 'charMap' => range('0', '9'), + 'callback' => function ($string, $count) { + if ($string=="264508") { + echo "Matched `{$string}` with {$count} times\n"; + } + }, +]); +``` + +--- + +REQUIREMENTS +------------ +This library requires the following: + +- PHP 5.4.0+\|7.0+ + +--- + +INSTALLATION +------------ + +Run Composer in your project: + + composer require yidas/brute-force-attacker + +Then you could call it after Composer is loaded depended on your PHP framework: + +```php +require __DIR__ . '/vendor/autoload.php'; + +use yidas\BruteForceAttacker; +``` + +--- + +USAGE +----- + +Call the `run()` static method and bring in the options to start: + +```php +\yidas\BruteForceAttacker::run(array $options) +``` + +### Options + +Setting all options including skip mechanism: + +```php +$hash = '5b7476628919d2d57965e25ba8b2588e94723b76'; + +\yidas\BruteForceAttacker::run([ + 'length' => 8, + 'charMap' => array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'), ["+", "/"]), + 'callback' => function ($key, & $count) use ($hash) { + if (sha1($key) == $hash) { + echo "Matched `{$key}` | Hash: {$hash}\n"; + exit; + } + // Display key every some times + if ($count == 0 || $count > 10000000) { + echo "{$key} | " . date("H:i:s") . "\n"; + $count = 0; + } + }, + 'skipLength' => 8, // Select 8st character for skipCount + 'skipCount' => 1, // Start from `B` (Skip 1 arrangement form charMap) +]); +``` + +#### length + +String length for generating + +#### charMap + +Character map used to generate strings + +#### Callback + +Customized function for performing brute-force attack + +```php +function (string $key, integer & $count) +``` + +#### skipLength + +String length for skipping based on `skipCount` setting + +#### skipCount + +Skip count of the `charMap` based on `skipLength` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9b5cfd6 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "yidas/brute-force-attacker", + "description": "Brute-force attack tool for generating all possible string and executing function", + "keywords": ["brute force attack", "tool", "php", "cryptanalytic"], + "homepage": "https://github.com/yidas/brute-force-attacker-php", + "type": "library", + "license": "MIT", + "support": { + "issues": "https://github.com/yidas/brute-force-attacker-php/issues", + "source": "https://github.com/yidas/brute-force-attacker-php" + }, + "require": { + "php": ">=5.4" + }, + "autoload": { + "psr-4": { + "yidas\\": "src/" + } + } +} diff --git a/src/BruteForceAttacker.php b/src/BruteForceAttacker.php new file mode 100644 index 0000000..547f4f9 --- /dev/null +++ b/src/BruteForceAttacker.php @@ -0,0 +1,108 @@ + + * @version 1.0.0 + */ +class BruteForceAttacker +{ + /** + * @var function Customized function for performing brute-force attack + */ + private static $callback; + + /** + * @var array Character map used to generate strings + */ + private static $charMap = []; + + /** + * @var string String for generating + */ + private static $chars = ''; + + /** + * @var integer String length for skipping based on `skipCount` setting + */ + private static $skipLength = 0; + + /** + * @var integer Skip count of the `charMap` based on `skipLength` + */ + private static $skipCount = 0; + + /** + * @var integer Count + */ + private static $count = 0; + + /** + * Run + * + * @param array $options + * @return void + */ + public static function run($options) + { + // Options + $defaultOptions = [ + 'length' => 1, + 'charMap' => array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9')), + 'callback' => function () {}, + 'skipLength' => 0, + 'skipCount' => 0, + ]; + $options = array_merge($defaultOptions, $options); + + // Config + self::$charMap = $options['charMap']; + self::$callback = $options['callback']; + self::$skipLength = $options['skipLength']; + self::$skipCount = $options['skipCount']; + self::$count = 0; + + // Run + $length = ($options['length'] >= 1) ? floor($options['length']) : 1; + self::recur($length); + } + + /** + * Recursive function + * + * @param integer $layer + * @return void + */ + private static function recur($length) { + + // Each charMap + foreach (self::$charMap as $value) { + + // Skip mechanism + if (self::$skipLength==$length && self::$skipCount > 0) { + self::$skipCount--; + continue; + } + + // Assign char + self::$chars[$length-1] = $value; + + if ($length <= 1) { + + // Call user callback + call_user_func_array(self::$callback, [self::$chars, &self::$count]); + // Counter + self::$count ++; + + } else { + // Recur with reducing 1 length + self::recur($length - 1); + } + } + } +}