-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.py
210 lines (175 loc) · 7.24 KB
/
generate.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
# A python script used to generate password dictionary with custom settings
# Created by Junhan Duan, Aug. 18, 2020
from pypinyin import *
import itertools, argparse
customListNum = []
customListLet = []
customListRes = []
customListSym = []
extraList = []
resultList = []
symbolList = ['!', '@', '#', '$', '%', '&', '*', '_']
# Do cartesian product between %1(list) and %2(list), and write to %3(list)
def doCartesian(head, tail, dstList):
for x in itertools.product(head, tail):
temp = ''.join(list(x[0]))
temp += ''.join(list(x[1]))
dstList.append(temp)
# Determine whether %1(string) contains Chinese character
def containsChinese(str):
for _char in str:
if '\u4e00' <= _char <= '\u9fa5':
return True
return False
# Read content from %1(file) and write it to %2(list)
def readFile(filename, dstList):
file = open(filename, 'r')
for line in file:
line = line.replace('\n', '').replace('\r', '')
dstList.append(line.strip())
# Read the user-input custom file and do basic list initialization
def readCustom(filename):
file = open(filename, encoding='utf-8')
for line in file:
line = line.replace('\n', '').replace('\r', '')
# 英文字符自动加入Head,数字加入Tail
if not containsChinese(line):
if line.isnumeric():
customListNum.append(line.strip())
else:
customListLet.append(line.strip())
# 中文字符转字母
else:
# 中文字符的拼音整体
customListLet.append(''.join(lazy_pinyin(line)))
# 中文字符的拼音整体 - 首字母大写
customListLet.append(''.join(lazy_pinyin(line)).capitalize())
# 中文字符的拼音整体 - 全部大写
customListLet.append(''.join(lazy_pinyin(line)).upper())
# 中文字符的拼音首字母
tempList = []
combLetters = pinyin(line, style=Style.FIRST_LETTER)
for _char in combLetters:
tempList.append(''.join(_char))
customListLet.append(''.join(tempList))
# 中文字符的拼音首字母 - 首字母大写
tempList = []
combLetters = pinyin(line, style=Style.FIRST_LETTER)
for _char in combLetters:
tempList.append(''.join(_char))
customListLet.append(''.join(tempList).capitalize())
# 中文字符的拼音首字母 - 全部大写
tempList = []
combLetters = pinyin(line, style=Style.FIRST_LETTER)
for _char in combLetters:
tempList.append(''.join(_char).upper())
customListLet.append(''.join(tempList))
# 中文字符的前两个字
if len(line) >= 2:
firstTwo = line[:2]
customListLet.append(''.join(lazy_pinyin(firstTwo)))
# 中文字符的后两个字(通常为名字)
if len(line) > 2:
lastFirst = line[-1]
lastSecond = line[-2]
lastTwo = ''.join([lastSecond, lastFirst])
customListLet.append(''.join(lazy_pinyin(lastTwo)))
# 中文字符的前两个字 - 全部大写
if len(line) > 2:
firstTwo = line[:2]
customListLet.append(''.join(lazy_pinyin(firstTwo)).upper())
# 中文字符的后两个字(通常为名字) - 全部大写
if len(line) > 2:
lastFirst = line[-1]
lastSecond = line[-2]
lastTwo = ''.join([lastSecond, lastFirst])
customListLet.append(''.join(lazy_pinyin(lastTwo)).upper())
# 搭配自定字母 & 自定数字 & 符号
# 字母 + 数字
doCartesian(customListLet, customListNum, customListRes)
# 字母 + 符号 + 数字
doCartesian(customListLet, symbolList, customListSym)
doCartesian(customListSym, customListNum, customListRes)
# 字母 + 数字 + 符号
tempList = []
doCartesian(customListLet, customListNum, tempList)
doCartesian(tempList, symbolList, customListRes)
# Strenghten custom password list by adding postfix from list
def strCustomL(strList):
tempList = []
# 字母 + file内容
for x in itertools.product(customListLet, strList):
temp = ''.join(list(x[0]))
temp += ''.join(list(x[1]))
customListRes.append(temp)
tempList.append(temp)
# 字母 + 符号 + file内容
doCartesian(customListSym, strList, customListRes)
# 字母 + file内容 + 符号
doCartesian(tempList, symbolList, customListRes)
# Strenghten custom passwords generated by custom file by adding postfix from file input
def strCustomF(filename):
fileList = []
readFile(filename, fileList)
strCustomL(fileList)
# Write list elements to file
def writeFile(filename, fromList):
with open(filename, 'w') as file:
for x in fromList:
if len(x) <= 16:
file.write("{}\n".format(x))
# Remove redundant password from a list
def removeRedundant(finalList):
finalList = list(dict.fromkeys(finalList))
# 将部分纯数字密码加入Result
def addNumPswd(srcFile, dstList):
srcLine = []
readFile(srcFile, srcLine)
for x in srcLine:
if 6 <= len(x) <= 16:
dstList.append(x)
# 构造经典密码词典 + 数字
def genClassic():
numLine = []
typicalLine = []
readFile('Typical.txt', typicalLine)
readFile('Numbers.txt', numLine)
readFile('Typical.txt', extraList)
doCartesian(typicalLine, numLine, extraList)
def cmd():
args = argparse.ArgumentParser(description = 'Argument')
args.add_argument('-c', '--complex', type = int, dest = 'complex', help = 'Complexity of password generated')
args.add_argument('-f', '--file', type = str, dest = 'filename', help = 'The name of custom file to be input')
args = args.parse_args()
if args.complex is not None:
if args.complex >= 1:
genClassic()
readFile('Top100.txt', extraList)
if args.complex >= 2 and args.filename is not None:
readCustom(args.filename)
strCustomF('Numbers.txt')
addNumPswd('Numbers.txt', customListRes)
elif args.complex >= 2 and args.filename is None:
print('No custom file detected. Please specify the filepath/filename with [-f]/[--file] option')
if args.complex == 3:
readFile('Top500.txt', extraList)
readFile('Numbers.txt', extraList)
readFile('Extra.txt', extraList)
else:
if args.filename is None:
print('No [-c]/[--complex] defined, using default complexity level 1')
genClassic()
readFile('Top100.txt', extraList)
else:
print('No [-c]/[--complex] defined but file specified, using complexity level 2')
genClassic()
readFile('Top100.txt', extraList)
readCustom(args.filename)
strCustomF('Numbers.txt')
addNumPswd('Numbers.txt', customListRes)
resultList = extraList + customListRes
removeRedundant(resultList)
writeFile('Result.txt', resultList)
print('Password has been written to Result.txt')
if __name__ == '__main__':
cmd()