forked from matteodellamico/montecarlopwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·56 lines (46 loc) · 1.81 KB
/
example.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
#!/usr/bin/env python3
# Copyright 2016 Symantec Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# standard library
import argparse
import csv
import sys
# internal imports
import backoff
import model
import ngram_chain
import pcfg
parser = argparse.ArgumentParser()
parser.add_argument('passwordfile', help='password training set')
parser.add_argument('--min_ngram', type=int, default=2,
help='minimum n for n-grams')
parser.add_argument('--max_ngram', type=int, default=5,
help='maximum n for n-grams')
parser.add_argument('--backoff_threshold', type=int, default=10,
help='threshold for backoff')
parser.add_argument('--samplesize', type=int, default=10000,
help='sample size for Monte Carlo model')
args = parser.parse_args()
with open(args.passwordfile, 'rt') as f:
training = [w.strip('\r\n') for w in f]
models = {'{}-gram'.format(i): ngram_chain.NGramModel(training, i)
for i in range(args.min_ngram, args.max_ngram + 1)}
models['Backoff'] = backoff.BackoffModel(training, 10)
models['PCFG'] = pcfg.PCFG(training)
samples = {name: list(model.sample(args.samplesize))
for name, model in models.items()}
estimators = {name: model.PosEstimator(sample)
for name, sample in samples.items()}
modelnames = sorted(models)
writer = csv.writer(sys.stdout)
writer.writerow(['password'] + modelnames)
for password in sys.stdin:
password = password.strip('\r\n')
estimations = [estimators[name].position(models[name].logprob(password))
for name in modelnames]
writer.writerow([password] + estimations)