forked from lakiw/py_omen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateNG.py
More file actions
354 lines (295 loc) · 15 KB
/
createNG.py
File metadata and controls
354 lines (295 loc) · 15 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
#!/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
#
#
# The MIT License (MIT)
#
# 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
#
# createNG.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 ##--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
####################################################
# Parses the command line
####################################################
def parse_command_line(runtime_options):
parser = argparse.ArgumentParser(description='OMEN Trainer: Creates n-grams for use \
by the OMEN password guess generator')
##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 ' +
'[' + runtime_options['rule_name'] + ']',
metavar='RULESET_NAME', required=False, default=runtime_options['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(runtime_options['ngram']) + ']',
metavar='INT', required=False, type=int, choices=range(2,6), default=runtime_options['ngram'])
try:
args=parser.parse_args()
##Input File options
runtime_options['training_file'] = args.training
runtime_options['encoding'] = args.encoding
##Alphabet options
runtime_options['learn_alphabet'] = args.alphabet
##Sanity check of values
if args.alphabet and args.alphabet < 10:
parser.error("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")
##Output file options
runtime_options['rule_name'] = args.rule
##Markov grammar options
runtime_options['ngram'] = args.ngram
except Exception as msg:
print(msg, file=sys.stderr)
return False
except SystemExit:
return False
return True
###################################################################################
# Prints the startup banner when this tool is run
###################################################################################
def print_banner(program_details):
print('',file=sys.stderr)
print (program_details['program'] + " Version " + program_details['version'], file=sys.stderr)
print ("This version written by " + program_details['author'], file=sys.stderr)
print ("Original version writtem by the Horst Goertz Institute for IT-Security", file=sys.stderr)
print ("Sourcecode available at " + program_details['source'], file=sys.stderr)
print('',file=sys.stderr)
####################################################################################
# ASCII art for displaying an error state before quitting
####################################################################################
def print_error():
print('',file=sys.stderr)
print('An error occured, shutting down',file=sys.stderr)
print('',file=sys.stderr)
print(r' \__/ \__/ \__/ \__/ \__/ \__/ \__/',file=sys.stderr)
print(r' (oo) (o-) (@@) (xx) (--) ( ) (OO)',file=sys.stderr)
print(r'//||\\ //||\\ //||\\ //||\\ //||\\ //||\\ //||\\',file=sys.stderr)
print(r' bug bug bug/w dead bug blind bug after',file=sys.stderr)
print(r' winking hangover bug sleeping bug whatever you did',file=sys.stderr)
print('',file=sys.stderr)
###################################################################################
# ASCII art for more generic failure
###################################################################################
def ascii_fail():
print(" __ ",file=sys.stderr)
print(" _ | |",file=sys.stderr)
print(" Yye |_| |--|",file=sys.stderr)
print(" .---. e AA | | | |",file=sys.stderr)
print(" /.--./\ e A",file=sys.stderr)
print(" // || \/\ e ",file=sys.stderr)
print(" //|/|| |\/\ aa a |\o/ o/--",file=sys.stderr)
print(" ///|\|| | \/\ . ~o \.'\.o'",file=sys.stderr)
print(" //|\|/|| | |\/\ . /.` \o'",file=sys.stderr)
print(" //\|/|\|| | | \/\ ( ( . \o'",file=sys.stderr)
print("___ __ _//|/|\|/|| | | |\/`--' '",file=sys.stderr)
print("__/__/__//|\|/|\|| | | | `--'",file=sys.stderr)
print("|\|/|\|/|\|/|\|/|| | | | |",file=sys.stderr)
print("",file=sys.stderr)
##################################################################
# Main function
##################################################################
def main():
management_vars = {
##--Information about this program--##
'program_details':{
'program':'createNG.py',
'version': '0.1',
'author':'Matt Weir',
'contact':'cweir@vt.edu',
'source':'https://github.com/lakiw/py_omen'
},
##--Runtime specific values, can be overriden via command line options
'runtime_options':{
##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(management_vars['program_details'])
##--Parse the command line ---##
command_line_results = management_vars['runtime_options']
if parse_command_line(command_line_results) != True:
return
##--Set the file encoding for the training set
##--If NOT specified on the command line by the user run an autodetect
if command_line_results['encoding'] == None:
possible_file_encodings = []
if not detect_file_encoding(command_line_results['training_file'], possible_file_encodings):
ascii_fail()
print("Exiting...")
return
command_line_results['encoding'] = possible_file_encodings[0]
##--Learn the alphabet if specified
if command_line_results['learn_alphabet'] != 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(command_line_results['training_file'], command_line_results['encoding'])
##--Error opening the file for reading
except Exception as msg:
print (error,file=sys.stderr)
print ("Error reading file " + self.filename ,file=sys.stderr)
ascii_fail()
print("Exiting...")
return
##--Initialize the alphabet generator
ag = AlphabetGenerator(alphabet_size = command_line_results['learn_alphabet'], ngram = command_line_results['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 != 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
##--Now that we are done, sort and return the alphabet
command_line_results['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',command_line_results['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 = command_line_results['alphabet'],
ngram = command_line_results['ngram'],
max_length = command_line_results['max_length']
)
##--Initialize the trainer file io
try:
input_dataset = TrainerFileIO(command_line_results['training_file'], command_line_results['encoding'])
##--Error opening the file for reading
except Exception as msg:
print (error,file=sys.stderr)
print ("Error reading file " + self.filename ,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 != 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',command_line_results['rule_name'])
##--This will be the config that is actually written to disk
config_info = {
'program_details':management_vars['program_details'],
'training_settings': {
'training_file':command_line_results['training_file'],
'alphabet_encoding':command_line_results['encoding'],
'ngram':command_line_results['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,
}
#print(omen_trainer.grammar)
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 " +command_line_results['rule_name'] + "'" , file=sys.stderr)
#######################################################################
# Standard python stub to call main
#######################################################################
if __name__ == "__main__":
main()