-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpassmeter.py
More file actions
executable file
·224 lines (187 loc) · 7.85 KB
/
passmeter.py
File metadata and controls
executable file
·224 lines (187 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from getpass import getpass
from rich.console import Console
from rich.table import Table
from rich.text import Text
import os
import re
import math
import argparse
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--password', nargs=1, help='Password')
parser.add_argument('-s', '--score-only',
action='store_true',
help='Only show score number as output'
)
parser.add_argument('-c', '--complexity-only',
action='store_true',
help='Only show complexity text as output'
)
return parser.parse_args()
def getPassword(arguments):
if arguments.password:
return arguments.password[0]
else:
try:
return getpass()
except KeyboardInterrupt:
exit(1)
def calcScore(dict):
for k in dict.keys():
if dict[k]["count"] > 0:
if dict[k]["score"] == 0:
dict[k]["score"] = dict[k]["count"] * dict[k]["mult"]
def printTable(dict):
table = Table()
table.add_column("", style="cyan")
table.add_column("Rate", style="white")
table.add_column("Count", justify="right", style="yellow")
table.add_column("Bonus", justify="right", style="green")
for k in dict.keys():
table.add_row(
dict[k]["text"],
dict[k]["rate"],
str(dict[k]["count"]),
setBonuscolor(dict[k]['score'])
)
console = Console()
console.print(table)
def printResult(dict, option):
score = 0
complexity = "unknown"
for k in dict.keys():
score += dict[k]["score"]
if score < 20:
score = max(0, score)
complexity = "Very Weak"
if score >= 20 and score < 40:
complexity = "Weak"
if score >= 40 and score < 60:
complexity = "Good"
if score >= 60 and score < 80:
complexity = "Strong"
if score >= 80:
score = min(score, 100)
complexity = "Very Strong"
if option.score_only:
print(score)
elif option.complexity_only:
print(complexity)
else:
print("Score: " + str(score) + "%")
print("Complexity: " + complexity)
printTable(dict)
def count(dict, password, key, pattern):
tmp = ""
arrPwd = list(password)
for a in range(0, dict["length"]["count"]):
if re.match(pattern, arrPwd[a]):
if (key == "number" or key == "symbol") \
and a > 0 and a < (dict["length"]["count"] - 1):
dict["midChar"]["count"] += 1
if tmp != "":
if (tmp + 1) == a:
consecKey = "consec" + key[:1].capitalize() + key[1:]
if consecKey in dict.keys():
dict[consecKey]["count"] += 1
tmp = a
dict[key]["count"] += 1
if key == "alphaLC" or key == "alphaUC" or key == "number":
if dict[key]["count"] > 0 \
and dict[key]["count"] < dict["length"]["count"]:
if key == "number":
dict[key]["mult"] = 4
else:
dict[key]["mult"] = 2
dict[key]["score"] = (dict["length"]["count"] - dict[key]["count"]) * dict[key]["mult"]
def countRepChar(dict, password):
repInc = 0
arrPwd = list(password)
arrPwdLen = len(arrPwd)
for a in range(0, arrPwdLen):
charExists = False
for b in range(0, arrPwdLen):
if arrPwd[a] == arrPwd[b] and a != b:
charExists = True
repInc += abs(arrPwdLen/(b-a))
if charExists:
dict["repChar"]["count"] += 1
nUnqChar = arrPwdLen - dict["repChar"]["count"]
repInc = math.ceil(repInc/nUnqChar) if nUnqChar else math.ceil(repInc)
dict["repChar"]["score"] = repInc * dict["repChar"]["mult"]
def countSeq(dict, password, key, pattern):
for s in range(0, len(pattern) - 2):
fwd = pattern[s: s+3]
rev = fwd[:: -1]
if password.lower().find(fwd) != -1 \
or password.lower().find(rev) != -1:
dict[key]["count"] += 1
def countAlphasOnly(dict):
if max(dict["alphaLC"]["count"], dict["alphaUC"]["count"]) > 0 \
and max(dict["symbol"]["count"], dict["number"]["count"]) == 0:
dict["alphasOnly"]["count"] = dict["length"]["count"]
def countNumbersOnly(dict):
if max(dict["alphaLC"]["count"], dict["alphaUC"]["count"], dict["symbol"]["count"]) == 0 \
and dict["number"]["count"] > 0:
dict["numbersOnly"]["count"] = dict["length"]["count"]
def countRequirements(dict):
minReqChars = 4
minPwdLen = 8
arrChars = ["alphaUC", "alphaLC", "number", "symbol"]
for c in arrChars:
if dict[c]["count"] > 0:
dict["requirements"]["count"] += 1
if dict["length"]["count"] >= minPwdLen:
dict["requirements"]["count"] += 1
if dict["requirements"]["count"] >= minReqChars:
dict["requirements"]["mult"] = 2
def setBonuscolor(score):
if score > 0:
return Text("+" + str(score), style="green")
if score < 0:
return Text(str(score), style="red")
return Text("0", style="white")
def checkCommonPassword(password):
passwordlist = "./commonpassword.list"
if os.path.isfile(passwordlist):
if any(password in s for s in open(passwordlist).readlines()):
print("Common password in " + passwordlist)
exit(1)
def main():
pwd = getPassword(parseArgs())
checkCommonPassword(pwd)
dictRule = {
"length": {"count": len(pwd), "mult": 4, "score": 0, "text": "Number of Characters", "rate": "+(n*4)"},
"alphaUC": {"count": 0, "mult": 0, "score": 0, "text": "Uppercase Letters", "rate": "+((len-n)*2)"},
"alphaLC": {"count": 0, "mult": 0, "score": 0, "text": "Lowercase Letters", "rate": "+((len-n)*2)"},
"number": {"count": 0, "mult": 0, "score": 0, "text": "Numbers", "rate": "+(n*4)"},
"symbol": {"count": 0, "mult": 6, "score": 0, "text": "Symbols", "rate": "+(n*6)"},
"midChar": {"count": 0, "mult": 2, "score": 0, "text": "Middle Numbers of Symbols", "rate": "+(n*2)"},
"requirements": {"count": 0, "mult": 0, "score": 0, "text": "Requirements", "rate": "+(n*2)"},
"alphasOnly": {"count": 0, "mult": -1, "score": 0, "text": "Letters Only", "rate": "-n"},
"numbersOnly": {"count": 0, "mult": -1, "score": 0, "text": "Numbers Only", "rate": "-n"},
"repChar": {"count": 0, "mult": -1, "score": 0, "text": "Repeat Characters", "rate": "-?"},
"consecAlphaUC": {"count": 0, "mult": -2, "score": 0, "text": "Consecutive Uppercase Letters", "rate": "-(n*2)"},
"consecAlphaLC": {"count": 0, "mult": -2, "score": 0, "text": "Consecutive Lowercase Letters", "rate": "-(n*2)"},
"consecNumber": {"count": 0, "mult": -2, "score": 0, "text": "Consecutive Numbers", "rate": "-(n*2)"},
"seqAlpha": {"count": 0, "mult": -3, "score": 0, "text": "Sequential Letters (3+)", "rate": "-(n*3)"},
"seqNumber": {"count": 0, "mult": -3, "score": 0, "text": "Sequential Numbers (3+)", "rate": "-(n*3)"},
"seqSymbol": {"count": 0, "mult": -3, "score": 0, "text": "Sequential Symbols (3+)", "rate": "-(n*3)"}
}
count(dictRule, pwd, "alphaUC", r'[A-Z]')
count(dictRule, pwd, "alphaLC", r'[a-z]')
count(dictRule, pwd, "number", r'[0-9]')
count(dictRule, pwd, "symbol", r'[^a-zA-Z0-9_]')
countRequirements(dictRule)
countAlphasOnly(dictRule)
countNumbersOnly(dictRule)
countRepChar(dictRule, pwd)
countSeq(dictRule, pwd, "seqAlpha", "abcdefghijklmnopqrstuvwxyz")
countSeq(dictRule, pwd, "seqNumber", "01234567890")
countSeq(dictRule, pwd, "seqSymbol", ")!@#$%^&*()")
calcScore(dictRule)
printResult(dictRule, parseArgs())
if __name__ == '__main__':
main()