3 * Create serialized/commonpasswords.cdb
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to create common password cdb database.
29 * Meant to take a file like those from
30 * https://github.com/danielmiessler/SecLists
32 * https://github.com/danielmiessler/SecLists/blob/fe2b40dd84/Passwords/rockyou.txt?raw=true
34 * @see serialized/commonpasswords.cdb and PasswordPolicyChecks::checkPopularPasswordBlacklist
36 * @ingroup Maintenance
38 class GenerateCommonPassword
extends Maintenance
{
39 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Generate CDB file of common passwords' );
43 $this->addOption( 'limit', "Max number of passwords to write", false, true, 'l' );
44 $this->addArg( 'inputfile', 'List of passwords (one per line) to use or - for stdin', true );
47 "Location to write CDB file to (Try $IP/serialized/commonpasswords.cdb)",
52 public function execute() {
53 $limit = (int)$this->getOption( 'limit', PHP_INT_MAX
);
54 $langEn = Language
::factory( 'en' );
56 $infile = $this->getArg( 0 );
57 if ( $infile === '-' ) {
58 $infile = 'php://stdin';
60 $outfile = $this->getArg( 1 );
62 if ( !is_readable( $infile ) && $infile !== 'php://stdin' ) {
63 $this->error( "Cannot open input file $infile for reading", 1 );
66 $file = fopen( $infile, 'r' );
67 if ( $file === false ) {
68 $this->error( "Cannot read input file $infile", 1 );
72 $db = \Cdb\Writer
::open( $outfile );
76 for ( $i = 0; ( $i - $skipped ) < $limit; $i++
) {
77 if ( feof( $file ) ) {
80 $rawLine = fgets( $file );
82 if ( $rawLine === false ) {
83 $this->error( "Error reading input file" );
86 if ( substr( $rawLine, -1 ) !== "\n" && !feof( $file ) ) {
87 // We're assuming that this just won't happen.
88 $this->error( "fgets did not return whole line at $i??" );
90 $line = $langEn->lc( trim( $rawLine ) );
92 $this->error( "Line number " . ( $i +
1 ) . " is blank?" );
96 if ( isset( $alreadyWritten[$line] ) ) {
97 $this->output( "Password '$line' already written (line " . ( $i +
1 ) .")\n" );
101 $alreadyWritten[$line] = true;
102 $db->set( $line, $i +
1 - $skipped );
104 // All caps, so cannot conflict with potential password
105 $db->set( '_TOTALENTRIES', $i - $skipped );
108 $this->output( "Successfully wrote " . ( $i - $skipped ) .
109 " (out of $i) passwords to $outfile\n"
111 } catch ( \Cdb\Exception
$e ) {
112 $this->error( "Error writing cdb file: " . $e->getMessage(), 2 );
117 $maintClass = "GenerateCommonPassword";
118 require_once RUN_MAINTENANCE_IF_MAIN
;