-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmakeReadmeMD.py
More file actions
executable file
·325 lines (278 loc) · 9.42 KB
/
makeReadmeMD.py
File metadata and controls
executable file
·325 lines (278 loc) · 9.42 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import os
import sys
import os.path
import re
from pprint import pprint
from subprocess import Popen, PIPE
readme = open('README.md', 'w')
readme.write("# Free Hack Quest 2016\n")
def getListOfDirsWithTasks():
result = []
dirs = os.listdir('./');
for d in dirs:
print(d);
if os.path.isdir(d):
subdirs = os.listdir('./' + d)
subdirs.sort()
for sd in subdirs:
path = './' + d + '/' + sd
if os.path.isdir(path):
if os.path.isfile(path + '/main.json'):
result.append(path)
print("Found: " + path);
return result
dirs = getListOfDirsWithTasks();
dirs.sort()
game_name = 'Free Hack Quest 2016'
stat_tasks = []
table_tasks = []
errors = {}
def append_errors(path, text):
if path not in errors:
errors[path] = []
errors[path].append(text)
possible_categories = ["admin", "web", "pwn", "crypto", "forensic", "misc", "ppc", "recon", "reverse", "stego"]
def detectEncoding(path):
p = Popen(['file', '-i', path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
pattern = re.compile('.*charset=(.*).*')
m = pattern.match(output)
if m:
return m.group(1)
return 'unknown'
def parseAuthor(path):
author = ''
with open(path) as f:
content = ''.join(f.readlines())
content = content.replace('\r', '')
content = content.replace('\n', '')
content = content.replace('\t', '')
pattern = re.compile('.*"nick"[ ]*\:[ ]*"([A-Z-a-z@!._]*)".*')
m = pattern.match(content)
if m:
author = m.group(1)
contacts = []
pattern = re.compile('.*"contacts"[ ]*\:[ ]*\[[ ]*"([A-Z-a-z@/!._]*)"[ ]*,[ ]*"([A-Z-a-z@/!._]*)".*')
m = pattern.match(content)
if m:
contacts.append(m.group(1));
contacts.append(m.group(2));
return author + '(' + ', '.join(contacts) + ')'
def appendStatCat(category, value):
for cat in stat_tasks:
if cat['category'] == category:
cat['count'] = cat['count'] + 1
cat['value'] = cat['value'] + value
return
stat_tasks.append({'category': category, 'count': 1, 'value': value})
def checkWriteUpFile(folder):
path = folder + '/WRITEUP.md'
if not os.path.isfile(path):
append_errors(folder, 'Missing file WRITEUP.md')
def getCategoryFromTask(data, folder):
category = 'unknown'
if 'category' not in data:
append_errors(folder, 'main.json: Missing field "category"')
else:
category = data['category']
if category not in possible_categories:
append_errors(folder, 'main.json: Field "category" has wrong value')
return category;
def getStatusFromTask(data, folder):
status = 'need verify'
if 'status' not in data:
append_errors(folder, 'main.json: Missing field "status"')
else:
status = data['status']
return status;
def getValueFromTask(data, folder):
value = 0
if 'value' not in data:
append_errors(folder, 'main.json: Missing field "value"')
else:
value = data['value']
if value == 0:
append_errors(folder, 'main.json: Task has 0 value')
return value
def getDescriptionFromTask(data, folder):
description = {"RU" : "", "EN": ""}
if 'name' not in data:
append_errors(folder, 'main.json: Missing field "name"')
else:
description = data['description']
if 'RU' not in description:
append_errors(folder, 'main.json: Missing subfield description "RU"')
else:
if description["RU"] == "":
append_errors(folder, 'main.json: Empty field in description "RU"')
if 'EN' not in description:
append_errors(folder, 'main.json: Missing subfield description "EN"')
else:
if description["EN"] == "":
append_errors(folder, 'main.json: Empty field in description "EN"')
return description
def getAuthorsFromTask(data, path):
authors = []
if 'authors' not in data:
append_errors(path, 'main.json: Missing field "authors"')
else:
if not isinstance(data['authors'], list):
append_errors(path, 'main.json: Field "authors" must be list')
else:
authors_ = data['authors']
for author in authors_:
name = ""
team = ""
contacts = []
if "name" not in author:
append_errors(path, 'main.json: Missing subfield author "name"')
else:
name = author["name"]
if name == "":
append_errors(path, 'main.json: Subfield author "name" is empty')
if "team" not in author:
append_errors(path, 'main.json: Missing subfield author "team"')
else:
team = author["team"]
if team == "":
append_errors(path, 'main.json: Subfield author "team" is empty')
if "contacts" not in author:
append_errors(path, 'main.json: Missing subfield author "contacts"')
else:
if not isinstance(author['contacts'], list):
append_errors(path, 'main.json: Subfield author "contacts" must be list')
else:
for c in author['contacts']:
if c == "":
append_errors(path, 'main.json: Empty field in author "contacts"')
else:
contacts.append(c);
contacts = ', '.join(contacts)
if contacts == "":
append_errors(path, 'main.json: Missing data in subfield authors "contacts"')
authors.append('[' + team + '] ' + name + ' (' + contacts + ')')
return authors
def getNameFromTask(data, folder):
name = path
if 'name' not in data:
append_errors(folder, 'main.json: Missing field "name"')
else:
name = data['name']
if name == "":
append_errors(folder, 'main.json: Field "name" is empty')
dirname = folder.split("/")[-1];
if name != dirname:
append_errors(folder, 'main.json: Field "name" has wrong value must like dirname "' + dirname + '" be "' + folder + '"')
return name
def getFlagKeyFromTask(data, folder):
flag_key = ''
if 'flag_key' not in data:
append_errors(path, 'main.json: Missing field "flag_key"')
else:
flag_key = data['flag_key']
pattern = re.compile('FHQ\(.*\)')
pattern2 = re.compile('FHQ\{.*\}')
m = pattern.match(flag_key)
m2 = pattern2.match(flag_key)
if flag_key == "":
append_errors(folder, 'main.json: Field "flag_key" is empty')
elif not m and not m2:
append_errors(folder, 'main.json: Wrong value of field "flag_key" must be format "FHQ(`md5`) or FHQ(`sometext`)"')
return flag_key
def getGameFromTask(data, folder):
game = ''
if 'game' not in data:
append_errors(folder, 'main.json: Missing field "game"')
else:
game = data['game']
if game != game_name:
append_errors(folder, 'main.json: Wrong game name "' + game + '" Please change to "' + game_name + '"')
return game
def getHintsFromTask(data, folder):
hints = []
if 'hints' not in data:
append_errors(d, 'main.json: Missing field "hints"')
else:
if not isinstance(data['hints'], list):
append_errors(d, 'main.json: Field "hints" must be list')
else:
hints = data['hints']
for hint in hints:
if 'RU' not in hint:
append_errors(folder, 'main.json: Missing subfield hint "RU"')
else:
if hint["RU"] == "":
append_errors(folder, 'main.json: Empty field in hint "RU"')
if 'EN' not in hint:
append_errors(folder, 'main.json: Missing subfield hint "EN"')
else:
if hint["EN"] == "":
append_errors(folder, 'main.json: Empty field in hint "EN"')
return hints;
for d in dirs:
path = d + '/main.json'
#encoding = detectEncoding(path);
if os.path.isfile(path):
try:
checkWriteUpFile(d);
with open(path) as main_json:
data = json.load(main_json)
category = getCategoryFromTask(data, d)
value = getValueFromTask(data, d)
status = getStatusFromTask(data, d);
authors = getAuthorsFromTask(data, d)
name = getNameFromTask(data, d)
getDescriptionFromTask(data, d)
getFlagKeyFromTask(data, d)
appendStatCat(category, value);
table_tasks.append({
'category': category,
'value': value,
'name': name,
'path': d,
'status': status,
'authors': ', '.join(authors) } )
getGameFromTask(data, d)
getHintsFromTask(data, d)
except Exception:
status = ''
encoding = detectEncoding(path);
if encoding != 'utf-8':
status = encoding
append_errors(path, 'Wrong encoding in "' + path + '", expected "utf-8", got "' + encoding + '"')
author = parseAuthor(path);
# print sys.exc_info()
table_tasks.append({'category': 'unknown', 'value': 0, 'name': d, 'status': 'invalid json', 'authors': author } )
appendStatCat('unknown', 0);
readme.write("\n## Short list of tasks\n\n")
for row in table_tasks:
readme.write(' * ' + row['category'] + ' ' + str(row['value']) + ' "' + row['name'] + '" by ' + row['authors'] + "\n")
if len(errors) > 0:
readme.write("\n\n## Errors\n\n")
for path in errors:
print(' * ' + path)
readme.write(' * ' + path + "\n")
for e in errors[path]:
print("\t * " + e)
readme.write('\t * ' + e + "\n")
readme.write("\n## Statistics by categories\n\n")
readme.write("|Category|Count|Summary value\n")
readme.write("|---|---|---\n")
stat_tasks.sort(key=lambda x: x['category'])
tasks_count_all=0
tasks_value_all=0
for cat in stat_tasks:
readme.write("|" + cat['category'] + "|" + str(cat['count']) + "|" + str(cat['value']) + "\n")
tasks_count_all = tasks_count_all + cat['count'];
tasks_value_all = tasks_value_all + cat['value'];
readme.write("|All|" + str(tasks_count_all) + "|" + str(tasks_value_all) + "\n")
# sort table
table_tasks.sort(key=lambda x: x['category'] + ' ' + str(x['value']).zfill(4))
readme.write("\n\n## Status table\n\n")
readme.write("|Category&Value|Name|Status|Author(s)\n")
readme.write("|---|---|---|---\n")
for row in table_tasks:
readme.write('|' + row['category'] + ' ' + str(row['value']) + '|' + row['name'] + '|' + row['status'] + '|' + row['authors'] + "\n")