forked from lakiw/pcfg_cracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_scorer.py
257 lines (214 loc) · 8.14 KB
/
password_scorer.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
################################################################################
#
# Name: PCFG Password Scorer
# -- Will attempt to assign a probability score to an input values
#
# -- Uses previously trained grammars created by trainer.py to assign
# probabilities to different transistions and terminals
#
# -- PLEASE DO NOT USE AS PART OF A PASSWORD CREATION POLICY OR BLACKLIST
# WITHOUT MAJOR MODIFICATIONS. YOUR USERS WILL HATE YOU AND THERE'S
# A LOT OF FUNDAMENTAL WORK THAT STILL NEEDS TO BE DONE TO MAKE THIS
# USEFUL FOR THOSE USE CASES.
#
#
# Written by Matt Weir
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# Contact Info: cweir@vt.edu
#
# password_scorer.py
#
################################################################################
# Including this to print error message if python < 3.0 is used
from __future__ import print_function
import sys
# Check for python3 and error out if not
if sys.version_info[0] < 3:
print("This program requires Python 3.x", file=sys.stderr)
sys.exit(1)
import argparse
import os
import traceback
from collections import Counter
# Local imports
from lib_scorer.banner_info import print_banner
from lib_scorer.pcfg_grammar import PcfgGrammar
from lib_scorer.grammar_io import load_grammar
from lib_scorer.file_output import FileOutput
from lib_scorer.omen_scorer import OmenScorer
from lib_trainer.trainer_file_input import TrainerFileInput
## Parses the command line
#
# Responsible for parsing the command line.
#
# If you have any command line options that you want to add, they go here.
#
# All results are returned as a dictionary in 'program_info'
#
# If successful, returns True, returns False if value error, program exits if
# argparse catches a problem.
#
def parse_command_line(program_info):
# Keeping the title text to be generic to make re-using code easier
parser = argparse.ArgumentParser(
description= program_info['name'] +
', version: ' +
program_info['version']
)
## Standard options
#
# The rule name to use when scoring the inpput.
parser.add_argument(
'--rule',
'-r',
help = 'Name of PCFG ruleset for use in scoring. Default is ' +
program_info['rule_name'],
metavar = 'RULESET_NAME',
required = False,
default = program_info['rule_name']
)
# The input file of items to score
parser.add_argument(
'--input',
'-i',
help = 'The filename of the input set to score. Newline seperated',
metavar = 'INPUT_FILENAME',
required = True
)
# The output file to save results too
parser.add_argument(
'--output',
'-o',
help = 'The output file to save results to. If not specified will output to stdout',
metavar = 'OUTPUT_FILENAME',
required = False,
default = program_info['output_file']
)
# The probability limit used to classify password/not passwords
parser.add_argument(
'--limit',
'-l',
help = 'The probability to use as a cut-off for password/not_password',
metavar = 'LIMIT_PERCENTAGE',
required = False,
default = program_info['limit'],
type = float,
)
# The OMEN limit used to classify password/not password
parser.add_argument(
'--max_omen',
'-m',
help = 'The maximum OMEN level for categorization as a password. Set to "0" to disable OMEN matching',
metavar = 'MAX_OMEN_LEVEL',
required = False,
default = program_info['max_omen_level'],
type = int,
)
# Parse all the args and save them
args=parser.parse_args()
# Standard Options
program_info['rule_name'] = args.rule
program_info['input_file'] = args.input
program_info['output_file']= args.output
program_info['limit'] = args.limit
program_info['max_omen_level'] = args.max_omen
## Sanity checking of values
#
# Check to make sure limit makes sense
if program_info['limit'] < 0 or program_info['limit'] > 1.0:
print("Error, limit must be a value between 1.0 and 0.0")
print("Also, your limit probably should be very close to 0.0, or have a lot of 0's in it")
print("This is because most password guesses have very low probabilities of actually being a target's password.")
return False
return True
## Main function, starts everything off
#
def main():
# Information about this program
program_info = {
# Program and Contact Info
'name':'PCFG Password Scorer',
'version': '4.1',
'author':'Matt Weir',
'contact':'cweir@vt.edu',
# Standard Options
'rule_name':'Default',
'output_file':None,
'limit':0,
# OMEN Options
#
# Note, pickng 9 as the default because the keyspace when training
# on rockyou for level 9 is roughly 600 million which seems reasonable
'max_omen_level':9
}
print_banner()
print("Version: " + str(program_info['version']))
print()
# Parsing the command line
if not parse_command_line(program_info):
# There was a problem with the command line so exit
print("Exiting...")
return
# Load Rules file from the standard storage location
# Making this OS independent
base_directory = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'Rules',
program_info['rule_name'])
## Create the grammar object
#
# Does not load the grammar yet
grammar = PcfgGrammar(limit = program_info['limit'])
# Attempt to load the rules file into the grammar
print("Loading Rule: " + str(program_info['rule_name']))
if not load_grammar(grammar, base_directory):
print("Exiting...")
return
# Initialize the multiword detector
print("Initializing Multi-Word Detector")
grammar.create_multiword_detector()
# Initalize the OMEN scorer
print("Initializing the OMEN scorer")
grammar.create_omen_scorer( base_directory, program_info['max_omen_level'])
# Initialize the file input to read input values from
# Re-using the TrainerFileInput from the trainer
file_input = TrainerFileInput(
program_info['input_file'],
grammar.encoding)
# Open file for output
writer = FileOutput(program_info['output_file'], grammar.encoding)
# Start processing input
print("Processing input wordlist")
print()
false_negative = 0
try:
input_value = file_input.read_password()
while input_value:
result = grammar.parse(input_value)
writer.write_data(result)
if result[1] == 'o':
false_negative += 1
input_value = file_input.read_password()
except Exception as msg:
traceback.print_exc(file=sys.stdout)
print("Exception: " + str(msg))
print("Exiting...")
return
print("False negatives: " + str(false_negative))
if __name__ == "__main__":
main()