-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreateNG.py
More file actions
360 lines (285 loc) · 12.1 KB
/
createNG.py
File metadata and controls
360 lines (285 loc) · 12.1 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
"""
Name: createNG
--OMEN training program
--(O)rdered (M)arkov (EN)umerator
-- Generates password guesses based on the conditional probabilty of passwords appearing together
Written by Matt Weir
Backend algorithm based on the work done https://github.com/RUB-SysSec/OMEN
Document describing the approach: https://hal.archives-ouvertes.fr/hal-01112124/file/omen.pdf
An even better document describing this: http://mobsec.rub.de/media/mobsec/arbeiten/2014/12/12/2013-ma-angelstorf-omen.pdf
Copyright 2021 Matt Weir
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Contact Info: cweir@vt.edu
"""
##--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 ##--Used for file path information
import uuid ##--Used to uniquely identify the ruleset. Used for saving/restaring cracking sessions
#Custom modules
from omen_trainer.common_file_io import detect_file_encoding
from omen_trainer.alphabet_lookup import AlphabetLookup
from omen_trainer.trainer_file_io import TrainerFileIO
from omen_trainer.output_file_io import save_rules_to_disk
from omen_trainer.alphabet_generator import AlphabetGenerator
from omen_cracker.ascii_art import ascii_fail, print_banner
def parse_command_line(program_info):
"""
Responsible for parsing the command line.
Note: This is a fairly standardized format that I use in many of my programs
Inputs:
program_info: A dictionary that contains the default values of
command line options. Results overwrite the default values and the
dictionary is returned after this function is done.
Returns:
True: If the command line was parsed successfully
False: If an error occured parsing the command line
(Program Exits): If the --help option is specified on the command line
"""
# Keeping the title text to be generic to make re-using code easier
parser = argparse.ArgumentParser(
description= program_info['name'] +
', version: ' +
program_info['version']
)
# Input File options
group = parser.add_argument_group('Input Files')
group.add_argument(
'--training',
'-t',
help='The training set of passwords to train from.',
metavar='FILENAME',
required=True
)
group.add_argument(
'--encoding',
'-e',
help='File encoding used to read the input training set. If not specified autodetect is used',
metavar='ENCODING',
required=False
)
group.add_argument(
'--alphabet',
'-a',
help='Dynamically learn alphabet from training set vs using the default [a-zA-Z0-9!.*@-_$#<?]. ' +
'Note, the size of alphabet will get up to the N most common characters. Higher values can slow down the cracker ' +
'and increase memory requirements',
type=int,
metavar='SIZE_OF_ALPHABET',
required=False
)
# Output file options
group = parser.add_argument_group('Output Options')
group.add_argument(
'--rule',
'-r',
help='Name of generated ruleset. Default is ' +
'[' + program_info['rule_name'] + ']',
metavar='RULESET_NAME',
required=False,
default=program_info['rule_name']
)
# Markov grammar options
group = parser.add_argument_group('nGram Calculation')
group.add_argument(
'--ngram',
'-n',
help='Changes the size of the nGram n ' +
'(possible values="2", "3", "4") Default is [' + str(program_info['ngram']) + ']',
metavar='INT',
required=False,
type=int,
choices=range(2,6),
default=program_info['ngram']
)
# Parse all the args and save them
args=parser.parse_args()
# Input File options
program_info['training_file'] = args.training
program_info['encoding'] = args.encoding
# Alphabet options
program_info['learn_alphabet'] = args.alphabet
# Sanity check of values
if args.alphabet and args.alphabet < 10:
print("Minimum alphabet size is 10 because based on past experience anything less than that is probably a typo. If this is a problem please post on the github site")
return False
# Output file options
program_info['rule_name'] = args.rule
# Markov grammar options
program_info['ngram'] = args.ngram
return True
def main():
"""
Main function, starts everything off
Inputs:
None
Returns:
None
"""
program_info = {
# Program and Contact Info
'name':'Py-OMEN Trainer',
'author':'Matt Weir',
'contact':'cweir@vt.edu',
'source':'https://github.com/lakiw/py_omen',
# Program version info.
'version': '0.2',
# Training set options
'training_file':None,
'encoding':None,
# Output options
'rule_name':'Default',
# nGram Calculation
'ngram':4,
'max_level':10,
'alphabet':'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!.*@-_$#<?',
'learn_alphabet':None,
'smooting':None,
# Options added for this version of OMEN
'max_length':20,
}
# Print out banner
print_banner(program_info)
# Parsing the command line
if not parse_command_line(program_info):
# There was a problem with the command line so exit
print("Exiting...",file=sys.stderr)
return
# Set the file encoding for the training set
# If NOT specified on the command line by the user run an autodetect
if program_info['encoding'] is None:
possible_file_encodings = []
if not detect_file_encoding(program_info['training_file'], possible_file_encodings):
ascii_fail()
print("Exiting...")
return
program_info['encoding'] = possible_file_encodings[0]
# Learn the alphabet if specified
if program_info['learn_alphabet'] is not None:
print('',file=sys.stderr)
print('---Starting first pass through training set to learn the alphabet---',file=sys.stderr)
print('',file=sys.stderr)
# Open the training file IO for the first pass to learn the Alphabet
try:
input_dataset = TrainerFileIO(program_info['training_file'], program_info['encoding'])
# Error opening the file for reading
except Exception as msg:
print(msg,file=sys.stderr)
print("Error reading file " + program_info['training_file'], file=sys.stderr)
ascii_fail()
print("Exiting...")
return
# Initialize the alphabet generator
ag = AlphabetGenerator(alphabet_size = program_info['learn_alphabet'], ngram = program_info['ngram'])
# Now loop through all the passwords to get the character counts for the alphabet
password = input_dataset.read_password()
total_count = 0
while password is not None:
if total_count % 1000000 == 0 and total_count != 0:
print(str(total_count//1000000) +' Million', file=sys.stderr)
ag.process_password(password)
password = input_dataset.read_password()
total_count +=1
# Sort and return the alphabet
program_info['alphabet'] = ag.get_alphabet()
# Saving this only for printing out the location of the alphabet file to console
alphabet_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),'Rules',program_info['rule_name'],'alphabet.txt')
print("Done learning alphabet", file=sys.stderr)
print("Displaying learned alphabet to a console usually ends poorly for non-standard characters.", file=sys.stderr)
print("If you want to review what the alphabet actually is you can view it at: " + alphabet_file, file=sys.stderr)
else:
print("Using Default Alphabet", file=sys.stderr)
print("", file=sys.stderr)
# Initialize lookup tables
omen_trainer = AlphabetLookup(
alphabet = program_info['alphabet'],
ngram = program_info['ngram'],
max_length = program_info['max_length']
)
# Initialize the trainer file io
try:
input_dataset = TrainerFileIO(program_info['training_file'], program_info['encoding'])
# If an error ocurrs when opening the file for reading
except Exception as msg:
print (msg,file=sys.stderr)
print ("Error reading file " + program_info['training_file'] ,file=sys.stderr)
ascii_fail()
print("Exiting...")
return
print("--Starting to parse passwords--",file=sys.stderr)
print("Passwords parsed so far (in millions): ", file=sys.stderr)
# Go through every password
password = input_dataset.read_password()
total_count = 0
while password is not None:
# Print out status info
if total_count % 1000000 == 0 and total_count != 0:
print(str(total_count//1000000) +' Million', file=sys.stderr)
omen_trainer.parse(password)
password = input_dataset.read_password()
total_count +=1
print()
print("Done with intial parsing.", file=sys.stderr)
print("Number of passwords trained on: " + str(total_count), file=sys.stderr)
print("Number of file encoding errors = " + str(input_dataset.num_encoding_errors), file=sys.stderr)
print()
print("--Applying probability smoothing--", file=sys.stderr)
omen_trainer.apply_smoothing()
print("--Saving Results--", file=sys.stderr)
# Save the results
# Get the absolute path in case this program is run from another dirctory
absolute_base_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)),'Rules', program_info['rule_name'])
# This will be the config that is actually written to disk
config_info = {
'program_details':{
'name':program_info['name'],
'author':program_info['author'],
'contact':program_info['contact'],
'version': program_info['version'],
},
'training_settings': {
'training_file':program_info['training_file'],
'alphabet_encoding':program_info['encoding'],
'ngram':program_info['ngram'],
'max_level':10,
'uuid':str(uuid.uuid4()),
},
}
# Bundle everything to send to the "save_rules_to_disk" function
save_info = {
"rule_directory":absolute_base_directory,
"ngrams":omen_trainer,
}
try:
save_rules_to_disk(omen_trainer, save_info, config_info)
except IOError as error:
print ("Error saving rules", file=sys.stderr)
print ("Error is " + str(error), file=sys.stderr)
print ("The OMEN training data likely was not saved to disk", file=sys.stderr)
return
print()
print("Done! Enjoy cracking passwords with OMEN!", file=sys.stderr)
print("To use this training set to crack, make sure you use the following option in enumNG:", file=sys.stderr)
print(" '-r " + program_info['rule_name'] + "'" , file=sys.stderr)
if __name__ == "__main__":
main()