-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathmkchal
executable file
·193 lines (141 loc) · 7.05 KB
/
mkchal
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
#!/usr/bin/env python3
import os
import sys
import re
import subprocess
import time
import tempfile
import shutil
import string
import glob
from pathlib import Path
def is_valid(line):
try:
int(line.strip())
return False
except:
pass
if line.strip().lower() == 'click':
return False
if re.search(r'https?://', line.lower()) and 'drive.google.com' not in line.lower():
return False
if line.strip().lower() == 'ja / en':
return False
if line.lower() in ['source dockerfile', 'files', 'challenge', 'challenge.py', 'download image', 'download file']:
return False
if len(line.strip()) == 0:
return True
if line.strip().lower() in ['attachment', 'handouts', 'show tags']:
return False
if line.strip().lower().startswith('download link'):
return False
if line.strip().lower().startswith('fastest solvers'):
return False
if re.search(r'^url\s+:', line.lower()):
return False
if re.search(r'^bot\s+:', line.lower()):
return False
if line.strip().lower().startswith('> deploy on'):
return False
if re.search(r'solves\s+\d+', line.lower()):
return False
if re.search(r'score:\s*\d+', line.lower()):
return False
if re.search(r'download (image|file|pcap) \([\d\.]+(kb|mb|b)\)', line.lower()):
return False
if re.search(r'\d+[ \t]+points', line.lower()) or re.search(r'\d+[ \t]*solves', line.lower()) or re.search(r'\d+[ \t]+solve', line.lower()) or re.search(r'\d+[ \t]+score', line.lower()):
return False
if re.fullmatch(r"[0-9a-z-]+\.zip", line.strip().lower()):
return False
if re.fullmatch(r"attachment:\s*[\w\.]+", line.strip().lower()):
return False
if re.search(r'\d+[ \t]*pt', line):
return False
if re.search(r'\d{2}/\d{2}, \d{2}:\d{2}:\d{2}', line):
return False
if line.lower().startswith('points:') or line.lower().startswith('solves:') or line.lower().startswith('topics:') or line.lower().startswith('task points') or line.lower().startswith('my score') or line.lower().startswith('difficulty estimate:'):
return False
if 'points' in line.lower() and 'solves' in line.lower():
return False
is_all_numeric = True
for l in line.strip().split():
is_all_numeric = is_all_numeric and l.strip().isnumeric()
if is_all_numeric:
return False
if line.strip().lower() in [
'welcome', 'expert', 'beginner', 'web', 're', 'rev', 'reverse', 'reversing', 'pwn', 'crypto', 'misc', 'tags', 'description', 'cry', 'pts', 'solves', 'description', 'warmup', 'easy', 'medium', 'hard', 'insane', 'forensics', 'forensic', 'score', 'cryptography', 'submit flag', 'binary', 'steganography', 'unrated', 'reverse engineering', 'task', 'information', 'insights', 'read this carrefully because sometimes the author might leave hints.', 'download files', 'play challenge', 'problem', 'previous submissions', 'download', 'admin bot', 'website', 'pwning',
'misc icon', 'crypto icon', 'pwn icon', 'reversing icon', 'web icon', 'steg', 'stego'
]:
return False
if re.fullmatch("category (forensics|reverse|web|crypto|pwn)", line.strip().lower()):
return False
if re.fullmatch("category: (forensics|reverse|web|crypto|pwn|reverse engineering|game hacking|misc)", line.strip().lower()):
return False
if re.fullmatch("level (easy|medium|hard)", line.strip().lower()):
return False
if re.fullmatch("difficulty: (easy|medium|hard|baby|guessy|elite)", line.strip().lower()):
return False
if re.fullmatch(r"first blood: (\w|\.)+", line.strip().lower()):
return False
if re.fullmatch(r"(http|https)://\d+\.\d+\.\d+\.\d+/", line.strip().lower()):
return False
if re.fullmatch(r"\d+\.\d+\.\d+\.\d+:\d+", line.strip().lower()):
return False
if re.fullmatch(r"nc \d+\.\d+\.\d+\.\d+ \d+", line.strip().lower()):
return False
if re.fullmatch(r"nc [a-z0-9\-_]{1,63}(\.[a-z0-9-_]{1,63})+ \d+", line.strip().lower()):
return False
if re.fullmatch(r"\d{1,2}/\d{1,2}/\d{4}", line.strip().lower()):
return False
if re.fullmatch(r'\d+ pts', line.strip().lower()):
return False
return True
def replace_special_characters(line):
return line.replace(u'\u1d2c', 'A').replace(u'\u1d64', 'U').replace(u'\u00e4', 'a').replace('@', 'a').replace('$', 's').replace('&', 'and').replace('👻', 'Ghost').replace('è', 'e').replace('🏃', 'Ran').replace('é', 'e').replace('ö', 'o').replace('É', 'E').replace('കുട്ടി', 'child').replace('⛳', 'flag').replace('<3', 'heart').replace('🐍', 'snake').replace('å', 'a').replace('Å', 'A').replace('𝑪', 'C').replace('𝑷', 'P').replace('ε', 'e').replace('√', 'sqrt').replace('🅱️', 'B').replace('ś', 's').replace('ę', 'e').replace('Δ', 'A').replace('ς', 'C')
def get_challenge_name(line):
for original_name in reversed(line.strip('|').strip().split('/')):
if original_name not in ['beginner', 'medium', 'hard', 'easy']:
break
args = re.sub(r'(\d+\s+solves|\d+\s+pts)', '', original_name.strip(), flags=re.IGNORECASE).strip()
args = re.sub(r'\(\d+ pts\)', '', args, flags=re.IGNORECASE).strip()
args = re.sub(r'\(solved by \d+ team(s)?\)$', '', args).strip()
args = re.sub('^(misc|crypto|rev|web|pwn)$', '', args).strip()
args = args.split()
tokens = []
for a in args:
tokens.extend(re.sub(r'[^0-9a-zA-Z_+\.]+', ' ', a.replace("'", "")).strip().split())
return '_'.join(tokens)
if __name__ == '__main__':
# create challenge directory
lines = '\n'.join([re.sub(r'(\d+[ \t]+points|\d+pts)', r'\n\1', l.rstrip()) for l in sys.stdin.readlines()]).strip().split('\n')
chal_name = get_challenge_name(replace_special_characters(lines[0]))
lines = '\n'.join(lines[1:]).strip().split('\n')
lines = [re.sub(r"nc [a-z0-9\-_]{1,63}(\.[a-z0-9-_]{1,63})+ \d+", '', l) for l in lines]
lines = [l.strip('|') for l in lines]
lines = [l.strip('\n') for l in lines if is_valid(l)]
shutil.rmtree(chal_name, ignore_errors=True)
os.mkdir(chal_name)
download_dir = os.path.expanduser('~/Downloads')
# move all the files downloaded in the past 60 seconds into the chal directory
attachments = set()
for f in os.listdir(download_dir):
attachments.add(f)
fname = os.path.join(download_dir, f)
if time.time() - os.path.getmtime(fname) <= 300:
subprocess.run(['mv', fname, chal_name])
lines = [l for l in lines if l not in attachments]
# compress big files
for file_ext in ['pcap', 'pcapng', 'wav', 'mp3', 'mov', 'mp4', 'log', 'exe', 'qcow2']:
os.system(f"""find "{chal_name}" -type f -name "*.{file_ext}" | xargs -n 1 xz""")
readme = ''
if os.path.exists('%s/README.md' % chal_name):
readme = open('%s/README.md' % chal_name).read()
readme = re.sub('\n{3,}', '\n\n', '\n'.join(lines[0:]).strip()) + '\n' + '\n\n' + readme
open('%s/README.md' % chal_name, 'w').write(readme.strip() + '\n')
print('\n\n+++++++++++++++++++++++++++++++++++++++++++++++')
print(chal_name)
subprocess.run(['ls', chal_name])
print('+++++++++++++++++++++++++++++++++++++++++++++++\n')
print('\n-----------------------------------------------')
subprocess.run(['cat', chal_name + '/README.md'])
print('-----------------------------------------------\n')