|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 KeePassXC Team <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 2 or (at your option) |
| 7 | + * version 3 of the License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "Estimate.h" |
| 19 | + |
| 20 | +#include <QCommandLineParser> |
| 21 | +#include <QTextStream> |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <string.h> |
| 26 | +#include <zxcvbn.h> |
| 27 | + |
| 28 | +/* For pre-compiled headers under windows */ |
| 29 | +#ifdef _WIN32 |
| 30 | +#ifndef __MINGW32__ |
| 31 | +#include "stdafx.h" |
| 32 | +#endif |
| 33 | +#endif |
| 34 | + |
| 35 | +Estimate::Estimate() |
| 36 | +{ |
| 37 | + this->name = QString("estimate"); |
| 38 | + this->description = QObject::tr("Estimate the entropy of a password."); |
| 39 | +} |
| 40 | + |
| 41 | +Estimate::~Estimate() |
| 42 | +{ |
| 43 | +} |
| 44 | + |
| 45 | +static void calculate(const char* pwd, bool advanced) |
| 46 | +{ |
| 47 | + double e; |
| 48 | + int len = strlen(pwd); |
| 49 | + if (!advanced) { |
| 50 | + e = ZxcvbnMatch(pwd, 0, 0); |
| 51 | + printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n", pwd, len, e, e * 0.301029996); |
| 52 | + } else { |
| 53 | + int ChkLen; |
| 54 | + ZxcMatch_t *info, *p; |
| 55 | + double m = 0.0; |
| 56 | + e = ZxcvbnMatch(pwd, 0, &info); |
| 57 | + for (p = info; p; p = p->Next) { |
| 58 | + m += p->Entrpy; |
| 59 | + } |
| 60 | + m = e - m; |
| 61 | + printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n Multi-word extra bits %.1f\n", |
| 62 | + pwd, |
| 63 | + len, |
| 64 | + e, |
| 65 | + e * 0.301029996, |
| 66 | + m); |
| 67 | + p = info; |
| 68 | + ChkLen = 0; |
| 69 | + while (p) { |
| 70 | + int n; |
| 71 | + switch (static_cast<int>(p->Type)) { |
| 72 | + case BRUTE_MATCH: |
| 73 | + printf(" Type: Bruteforce "); |
| 74 | + break; |
| 75 | + case DICTIONARY_MATCH: |
| 76 | + printf(" Type: Dictionary "); |
| 77 | + break; |
| 78 | + case DICT_LEET_MATCH: |
| 79 | + printf(" Type: Dict+Leet "); |
| 80 | + break; |
| 81 | + case USER_MATCH: |
| 82 | + printf(" Type: User Words "); |
| 83 | + break; |
| 84 | + case USER_LEET_MATCH: |
| 85 | + printf(" Type: User+Leet "); |
| 86 | + break; |
| 87 | + case REPEATS_MATCH: |
| 88 | + printf(" Type: Repeated "); |
| 89 | + break; |
| 90 | + case SEQUENCE_MATCH: |
| 91 | + printf(" Type: Sequence "); |
| 92 | + break; |
| 93 | + case SPATIAL_MATCH: |
| 94 | + printf(" Type: Spatial "); |
| 95 | + break; |
| 96 | + case DATE_MATCH: |
| 97 | + printf(" Type: Date "); |
| 98 | + break; |
| 99 | + case BRUTE_MATCH + MULTIPLE_MATCH: |
| 100 | + printf(" Type: Bruteforce(Rep)"); |
| 101 | + break; |
| 102 | + case DICTIONARY_MATCH + MULTIPLE_MATCH: |
| 103 | + printf(" Type: Dictionary(Rep)"); |
| 104 | + break; |
| 105 | + case DICT_LEET_MATCH + MULTIPLE_MATCH: |
| 106 | + printf(" Type: Dict+Leet(Rep) "); |
| 107 | + break; |
| 108 | + case USER_MATCH + MULTIPLE_MATCH: |
| 109 | + printf(" Type: User Words(Rep)"); |
| 110 | + break; |
| 111 | + case USER_LEET_MATCH + MULTIPLE_MATCH: |
| 112 | + printf(" Type: User+Leet(Rep) "); |
| 113 | + break; |
| 114 | + case REPEATS_MATCH + MULTIPLE_MATCH: |
| 115 | + printf(" Type: Repeated(Rep) "); |
| 116 | + break; |
| 117 | + case SEQUENCE_MATCH + MULTIPLE_MATCH: |
| 118 | + printf(" Type: Sequence(Rep) "); |
| 119 | + break; |
| 120 | + case SPATIAL_MATCH + MULTIPLE_MATCH: |
| 121 | + printf(" Type: Spatial(Rep) "); |
| 122 | + break; |
| 123 | + case DATE_MATCH + MULTIPLE_MATCH: |
| 124 | + printf(" Type: Date(Rep) "); |
| 125 | + break; |
| 126 | + |
| 127 | + default: |
| 128 | + printf(" Type: Unknown%d ", p->Type); |
| 129 | + break; |
| 130 | + } |
| 131 | + ChkLen += p->Length; |
| 132 | + printf(" Length %d Entropy %6.3f (%.2f) ", p->Length, p->Entrpy, p->Entrpy * 0.301029996); |
| 133 | + for (n = 0; n < p->Length; ++n, ++pwd) { |
| 134 | + printf("%c", *pwd); |
| 135 | + } |
| 136 | + printf("\n"); |
| 137 | + p = p->Next; |
| 138 | + } |
| 139 | + ZxcvbnFreeInfo(info); |
| 140 | + if (ChkLen != len) { |
| 141 | + printf("*** Password length (%d) != sum of length of parts (%d) ***\n", len, ChkLen); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +int Estimate::execute(QStringList arguments) |
| 147 | +{ |
| 148 | + QTextStream inputTextStream(stdin, QIODevice::ReadOnly); |
| 149 | + QTextStream outputTextStream(stdout, QIODevice::WriteOnly); |
| 150 | + |
| 151 | + QCommandLineParser parser; |
| 152 | + parser.setApplicationDescription(this->description); |
| 153 | + parser.addPositionalArgument("password", QObject::tr("Password for which to estimate the entropy."), "[password]"); |
| 154 | + QCommandLineOption advancedOption(QStringList() << "a" |
| 155 | + << "advanced", |
| 156 | + QObject::tr("Perform advanced analysis on the password.")); |
| 157 | + parser.addOption(advancedOption); |
| 158 | + parser.process(arguments); |
| 159 | + |
| 160 | + const QStringList args = parser.positionalArguments(); |
| 161 | + if (args.size() > 1) { |
| 162 | + outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate"); |
| 163 | + return EXIT_FAILURE; |
| 164 | + } |
| 165 | + |
| 166 | + QString password; |
| 167 | + if (args.size() == 1) { |
| 168 | + password = args.at(0); |
| 169 | + } else { |
| 170 | + password = inputTextStream.readLine(); |
| 171 | + } |
| 172 | + |
| 173 | + calculate(password.toLatin1(), parser.isSet(advancedOption)); |
| 174 | + return EXIT_SUCCESS; |
| 175 | +} |
0 commit comments