-
Notifications
You must be signed in to change notification settings - Fork 414
/
wordlist_from_dir1.py
executable file
·218 lines (190 loc) · 8.12 KB
/
wordlist_from_dir1.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import os
import sys
import platform
def generate_wordlist_file(root_dir, dir_path='', word_list=[]):
try:
root_file_list = os.listdir(root_dir)
for file in root_file_list:
if file.startswith("."):
root_file_list.remove(file)
else:
file_path = root_dir + '/' + file
# 如果为文件则记录文件路径
if os.path.isfile(file_path):
word_list.append(dir_path + '/' + file)
# 如果为文件夹则递归
elif os.path.isdir(file_path):
generate_wordlist_file(file_path, dir_path + '/' + file, word_list)
else:
pass
except Exception as e:
print(e)
finally:
return word_list
def generate_wordlist_dir(root_dir, dir_path='', word_list=[]):
try:
root_file_list = os.listdir(root_dir)
for file in root_file_list:
if file.startswith("."):
root_file_list.remove(file)
else:
file_path = root_dir + '/' + file
# 如果为文件则记录文件路径
if os.path.isfile(file_path):
# word_list.append(dir_path + '/' + file)
pass
# 如果为文件夹则递归
elif os.path.isdir(file_path):
generate_wordlist_dir(file_path, dir_path + '/' + file, word_list)
else:
pass
except Exception as e:
print(e)
finally:
word_list.append(dir_path + '/')
return word_list
def generate_file_list():
"""
生成文件列表
:return:
"""
try:
root_dir_path = sys.argv[1]
except:
print('无路径参数,默认为当前文件夹')
root_dir_path = os.getcwd()
finally:
if os.path.isdir(root_dir_path):
# print(platform.system() + platform.release())
if platform.system() == 'Windows':
# windows系统文件路径为 \\
try:
dir_name = root_dir_path.split('\\')
length = len(dir_name)
file_name = dir_name[length - 1].strip()
# 做空值判断
if file_name == '':
file_name = dir_name[length - 2].strip()
if file_name == '':
file_name = 'word_list'
file_name_txt = file_name + '.txt'
file_path_txt = os.getcwd() + '\\' + file_name_txt
file_name_json = file_name + '.json'
file_path_json = os.getcwd() + '\\' + file_name_json
word_list_file = generate_wordlist_file(root_dir_path)
# 增加文件夹的路径列表
# word_list_dir = generate_wordlist_dir(root_dir_path)
# word_list = word_list_dir + word_list_file
# 这里去掉了文件夹路径
word_list = word_list_file
# 写入txt
with open(file_path_txt, 'w') as word_file:
for word in word_list:
word_file.write(word + '\n')
print(file_path_txt)
# 写入json
with open(file_path_json, 'w') as word_file:
word_file.write(json.dumps(word_list))
print(file_path_json)
except:
pass
elif platform.system() == 'Linux':
# linux系统文件路径为 /
try:
dir_name = root_dir_path.split('/')
length = len(dir_name)
file_name = dir_name[length - 1].strip()
# 做空值判断
if file_name == '':
file_name = dir_name[length - 2].strip()
if file_name == '':
file_name = 'word_list'
file_name_txt = file_name + '.txt'
file_path_txt = os.getcwd() + '/' + file_name_txt
file_name_json = file_name + '.json'
file_path_json = os.getcwd() + '/' + file_name_json
word_list_file = generate_wordlist_file(root_dir_path)
# 增加文件夹的路径列表
# word_list_dir = generate_wordlist_dir(root_dir_path)
# word_list = word_list_dir + word_list_file
# 这里去掉了文件夹路径
word_list = word_list_file
with open(file_path_txt, 'w') as word_file:
for word in word_list:
word_file.write(word + '\n')
print(file_path_txt)
# 写入json
with open(file_path_json, 'w') as word_file:
word_file.write(json.dumps(word_list))
print(file_path_json)
except:
pass
else:
# 其他系统不作处理
pass
else:
print('路径参数的指定路径不是文件夹(ps:无路径参数,默认为当前文件夹)')
# def remove_special_symbols_for_filename(file_name, symbles=["*"]):
# pass
def remove_special_symbols_for_filecontent(file_content: str):
"""
文件内容中的特殊符号替换, 使其满足jekyll文档语法,而不报错
:param file_content:
:param symbles: ["{%", "%}", "{{", "}}"]
:return:
"""
file_content = file_content.replace(r"{%", r"\{\%")
file_content = file_content.replace(r"%}", r"\%\}")
file_content = file_content.replace(r"}}", r"\}\}")
file_content = file_content.replace(r"{{", r"\{\{")
################ 解决微信图片引用不显示问题
disable_refer = '<meta name="referrer" content="no-referrer"/>'
if disable_refer not in file_content:
file_content = disable_refer + "\n" + file_content
return file_content
if __name__ == '__main__':
root_path = os.getcwd()
json_file_name = "vulnerability-paper.json"
generate_file_list()
with open(json_file_name) as f:
file_list = f.read()
file_list = json.loads(file_list)
# print(file_list)
for i in file_list:
file_path = os.path.join(root_path, i[1:])
# print(file_path)
######################## 文件名过滤,去掉特殊字符
if os.path.exists(file_path) and str(i).endswith(".md"):
print("文件名过滤:" + str(file_path))
file_path_dst = file_path
#file_path_dst = file_path_dst.replace(" ", "_")
file_path_dst = file_path_dst.replace("&", "and")
file_path_dst = file_path_dst.replace("<=", "小于等于")
file_path_dst = file_path_dst.replace("<", "小于")
file_path_dst = file_path_dst.replace("|", "_")
file_path_dst = file_path_dst.replace("|", "_")
file_path_dst = file_path_dst.replace(r"\\", "_")
if platform.system() == 'Windows':
pass
else:
file_path_dst = file_path_dst.replace(":", "_")
# 重命名文件
os.rename(file_path, file_path_dst)
generate_file_list()
with open(json_file_name) as f:
file_list = f.read()
file_list = json.loads(file_list)
# print(file_list)
for i in file_list:
file_path = os.path.join(root_path, i[1:])
########################## 文件内容过滤,去掉特殊字符
if os.path.exists(file_path) and str(i).endswith(".md"):
print("文件内容过滤:" + str(file_path))
with open(file_path, 'r', encoding="utf8") as fi:
file_content = fi.read()
file_content_results = remove_special_symbols_for_filecontent(file_content)
with open(file_path, 'w', encoding='utf8') as fo:
fo.write(file_content_results)