createCommonPasswordCdb.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Create serialized/commonpasswords.cdb
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to create common password cdb database.
  26. *
  27. * Meant to take a file like
  28. * https://github.com/danielmiessler/SecLists/blob/master/Passwords/rockyou.txt?raw=true
  29. * as input.
  30. * @see serialized/commonpasswords.cdb and PasswordPolicyChecks::checkPopularPasswordBlacklist
  31. * @since 1.27
  32. * @ingroup Maintenance
  33. */
  34. class GenerateCommonPassword extends Maintenance {
  35. public function __construct() {
  36. global $IP;
  37. parent::__construct();
  38. $this->addDescription( 'Generate CDB file of common passwords' );
  39. $this->addOption( 'limit', "Max number of passwords to write", false, true, 'l' );
  40. $this->addArg( 'inputfile', 'List of passwords (one per line) to use or - for stdin', true );
  41. $this->addArg(
  42. 'output',
  43. "Location to write CDB file to (Try $IP/serialized/commonpasswords.cdb)",
  44. true
  45. );
  46. }
  47. public function execute() {
  48. $limit = (int)$this->getOption( 'limit', PHP_INT_MAX );
  49. $langEn = Language::factory( 'en' );
  50. $infile = $this->getArg( 0 );
  51. if ( $infile === '-' ) {
  52. $infile = 'php://stdin';
  53. }
  54. $outfile = $this->getArg( 1 );
  55. if ( !is_readable( $infile ) && $infile !== 'php://stdin' ) {
  56. $this->error( "Cannot open input file $infile for reading", 1 );
  57. }
  58. $file = fopen( $infile, 'r' );
  59. if ( $file === false ) {
  60. $this->error( "Cannot read input file $infile", 1 );
  61. }
  62. try {
  63. $db = \Cdb\Writer::open( $outfile );
  64. $alreadyWritten = [];
  65. $skipped = 0;
  66. for ( $i = 0; ( $i - $skipped ) < $limit; $i++ ) {
  67. if ( feof( $file ) ) {
  68. break;
  69. }
  70. $rawLine = fgets( $file );
  71. if ( $rawLine === false ) {
  72. $this->error( "Error reading input file" );
  73. break;
  74. }
  75. if ( substr( $rawLine, -1 ) !== "\n" && !feof( $file ) ) {
  76. // We're assuming that this just won't happen.
  77. $this->error( "fgets did not return whole line at $i??" );
  78. }
  79. $line = $langEn->lc( trim( $rawLine ) );
  80. if ( $line === '' ) {
  81. $this->error( "Line number " . ( $i + 1 ) . " is blank?" );
  82. $skipped++;
  83. continue;
  84. }
  85. if ( isset( $alreadyWritten[$line] ) ) {
  86. $this->output( "Password '$line' already written (line " . ( $i + 1 ) .")\n" );
  87. $skipped++;
  88. continue;
  89. }
  90. $alreadyWritten[$line] = true;
  91. $db->set( $line, $i + 1 - $skipped );
  92. }
  93. // All caps, so cannot conflict with potential password
  94. $db->set( '_TOTALENTRIES', $i - $skipped );
  95. $db->close();
  96. $this->output( "Successfully wrote " . ( $i - $skipped ) .
  97. " (out of $i) passwords to $outfile\n"
  98. );
  99. } catch ( \Cdb\Exception $e ) {
  100. $this->error( "Error writing cdb file: " . $e->getMessage(), 2 );
  101. }
  102. }
  103. }
  104. $maintClass = "GenerateCommonPassword";
  105. require_once RUN_MAINTENANCE_IF_MAIN;