-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwd_dictionary_gen.py
280 lines (233 loc) · 8.19 KB
/
pwd_dictionary_gen.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from random import randint
import sys
import os
MAX_PWD_LEN = 30
def main():
defaultMaxIterations = 10000
defaultGeneratedResults = 50
inputEnable = False
outputEnable = True
customIterationNumber = False
try:
if len(sys.argv) > 1:
i = 1
while i < len(sys.argv):
if sys.argv[i] == '-i' or sys.argv[i] == '--input':
# input from file
i = i+1
inputfile_name = sys.argv[i]
inputEnable = True
elif sys.argv[i] == '-o' or sys.argv[i] == '--output':
# output file
i = i+1
outputfile_name = sys.argv[i]
print ("---outputfile_name--->" + outputfile_name)
outputEnable = False
elif sys.argv[i] == '-n' or sys.argv[i] == '--iterations':
# number of iterations
i = i+1
customIterationNumber = True
generatedResults = int(sys.argv[i])
elif sys.argv[i] == '-h' or sys.argv[i] == '--help':
_printHelp()
os._exit(1)
else:
print ("Parameters error")
print ("Quitting...")
quit()
i = i+1
outputSet = set()
if customIterationNumber == False:
generatedResults = input("Number of iterations for each word [void to default]: ")
if generatedResults != "":
generatedResults = int(generatedResults)
else:
generatedResults = int(defaultGeneratedResults)
maxIterations = defaultMaxIterations
if generatedResults > defaultMaxIterations:
limitCheck = input("Do want to overwrite max suggested number of iterations fixed to " + str(defaultMaxIterations) + " [y/N]? ")
if limitCheck.lower() == "yes" or limitCheck.lower() == "y":
maxIterations = generatedResults
# input
if inputEnable:
inputSet = _readInputFile(inputfile_name)
else:
inputSet = _readInputKeyboard()
# dictionary generation
for w in inputSet:
i = 0
tmpSet = set()
while len(tmpSet) < generatedResults and i < maxIterations:
word = w
x = 0
if len(inputSet) > 1:
x = randint(0,100)
if x < 60:
# operation on a single word
word = _lowUpCase(word)
word = _randCharMix(word)
elif x >= 60:
# operation on multiple words
word = _randWordMix(inputSet)
tmpSet.add(word)
i = i+1
outputSet |= tmpSet
# output
if outputEnable:
_printResults(outputSet)
else:
_printOutputFile(outputSet, outputfile_name)
print (str(len(outputSet)) + " different combinaitons created")
except KeyboardInterrupt:
# to intercept CRTL+C interrupt
print ("\nQuitting...")
except ValueError:
# conversion exception
print ("Inserted unexpected value")
except OSError as err:
# file error
print("OS error: {0}".format(err))
except:
# unexpected exception
print("Unexpected error:", sys.exc_info()[0])
def _printHelp():
print("\nPasswordDictionaryGen:")
print("\npython pwd_dictionary_gen.py [options]")
print("\t-i\t<inputfile>\tword list from file")
print("\t-o\t<outputfile>\toutput dictionary on file")
print("\t-n\t<max numeber of results for each word>")
print("\t-h\tprint help")
def _readInputFile(inputfile_name):
inputSet = set()
with open(inputfile_name,'r') as f:
for line in f:
for inputword in line.split():
inputSet.add(inputword)
#text = in_file.read()
return inputSet
def _readInputKeyboard():
inputSet = set()
inputword = input("Enter a word: ")
while inputword != "":
inputSet.add(inputword)
inputword = input("Enter next word [void to stop]: ")
return inputSet
def _printResults(outputSet):
# print results on stdout
for w in outputSet:
print (w)
def _printOutputFile(outputSet, outputfile_name):
# print results on file
out_file = open(outputfile_name,"w+")
for word in outputSet:
out_file.write(word + "\n")
out_file.close()
def _lowUpCase(word):
res = []
for c in word:
if randint(0,100) > 50:
if c.isupper():
res.append(c.lower())
elif c.islower():
res.append(c.upper())
else:
res.append(c)
else:
res.append(c)
return ''.join(res)
def _randCharMix(word):
res = ""
for i in range(0,len(word)):
if word[i].lower() == 'a':
if randint(0,100) > 50:
res += '@'
else:
res += word[i]
elif word[i].lower() == 'e':
x = randint(0,2)
if x == 1:
res += '3'
elif x == 2:
res += '&'
else:
res += word[i]
elif word[i].lower() == 'i':
x = randint(0,4)
if x == 0:
res += word[i]
elif x == 1:
res += '1'
elif x == 2:
res += '!'
elif x == 3:
res += _lowUpCase("y")
elif x == 4:
res += _lowUpCase("j")
elif word[i].lower() == 'g':
if randint(0,100) > 50:
res += '6'
else:
res += word[i]
elif word[i].lower() == 'o':
if randint(0,100) > 50:
res += '0'
else:
res += word[i]
elif word[i].lower() == 's':
x = randint(0,2)
if x == 1:
res += '5'
elif x == 2:
res += '$'
else:
res += word[i]
elif word[i].lower() == 'z':
if randint(0,100) > 50:
res += '2'
else:
res += word[i]
else:
res += word[i]
return res
def _randWordMix(inputSet):
setLen = len(inputSet)
inputList = list(inputSet)
# How many words to mix:
wordNumber = randint(1, setLen)
# Which words to mix:
wordArray = []
for i in range(0,wordNumber):
index = randint(0,setLen-1)
wordArray.append(inputList[index])
# How many charactes has the final word:
x = randint(0,100)
pwdLen = 0
if x <= 50: # 50%
# from 4 to 8 chars
pwdLen = randint(4,8)
elif x > 50 and x <= 80: # 30%
# from 9 to 16 chars
pwdLen = randint(9,16)
elif x > 80 and x <= 90: # 10%
# from 0 to 3 chars
pwdLen = randint(0,3)
elif x > 90: # 10%
# from 17 to MAX_PWD_LEN chars
pwdLen = randint(17,MAX_PWD_LEN)
word = ""
# Which character use from each word:
while len(word) < pwdLen:
# -> select a word in the list
aInd = randint(1,wordNumber)
aStr = wordArray[aInd-1]
# -> select a char in the word
bInd = randint(1, len(aStr))
bChar = aStr[bInd-1]
word += bChar
# -> 50%: try to concatenate even the following character
if len(word) < pwdLen and bInd < len(aStr) and randint(0,1) == 1:
bChar = aStr[bInd]
word += bChar
return word
if __name__ == "__main__":
main()