-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctfd_scraper.py
300 lines (237 loc) · 11.2 KB
/
ctfd_scraper.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# What do you think you're doing here reading code like that? There ain't any revshell here don't worry
try:
from pathlib import *
import argparse
from datetime import datetime
import re
import os, sys
sys.path.insert(1, str(Path(os.path.realpath(__file__)).parents[1]))
from requests import session
from bs4 import BeautifulSoup
except Exception as e:
raise ImportError("Dude, install the requirements...\n" + str(e))
class MESSAGE(object):
STATE = ("SUCCESS", "DEBUG", "INFO", "WARNING", "ERROR")
def clean_directory_name(name):
clean_ascii = ''.join([i if ord(i) < 128 else ''for i in name])
return ''.join(['_' if i in [' ', '\\', '/', ':', '|', '<', '>', '*', '?'] else i for i in clean_ascii]) # Restricted chars
def log(text, state=1, clean=False): # Append information
if "\n" in text:
lines=[x for x in text.split("\n") if x !="" and x !="\n"]
for text in lines:
logline(text, state, clean)
else:
logline(text, state, clean)
def logline(text, state=1, clean=False):
if not clean:
text = "[" + datetime.now().strftime("%H:%M:%S") + "] [" + MESSAGE.STATE[state] + "] " + text
print(text)
class Challenge:
def __init__(self, CTF=None, args=None, file=None, text=None):
self.solved = False
self.files = []
if CTF:
if CTF.url:
# Newly scraped => Online CTF
self.CTF = CTF
self.id = args['id']
self.name = clean_directory_name(args['name'])
self.category = clean_directory_name(args['category'].lower())
self.directory = self.CTF.path.joinpath(self.category).joinpath(self.name)
self.value = args['value']
self.type = args['type']
elif CTF.path:
# Already scraped => Offline CTF
self.CTF = CTF
self.name = clean_directory_name(args['name'])
self.category = clean_directory_name(args['category'].lower())
self.directory = self.CTF.path.joinpath(self.category).joinpath(self.name)
else:
# Challenge alone
if file:
self.files = [file]
self.desc = str(text) if text else ""
self.directory = Path.cwd().joinpath("ExcaliburOutput-" + str(datetime.now().day) + "-" +
datetime.now().strftime("%H:%M:%S").replace(":", "."))
self.name = "log"
if not os.path.exists(self.directory):
os.makedirs(self.directory)
self.notefile_path = self.directory.joinpath(self.name + '.md')
def __str__(self):
a=""
a+="Challenge has "+str(len(self.files))+" files\n"
a+="Challenge desc: "+str(self.desc)+"\n"
a+="Challenge directory: "+str(self.directory)+"\n"
try:
with open(self.notefile_path,"r") as notefile:
chars=len(notefile.read())
a+="Challenge notefile "+str(self.notefile_path)+", it has "+str(chars)+" characters\n"
except Exception as e:
a+="Could,t process notefile: "+str(e)+"\n"
return a
def scrape(self):
self.get_info()
self.get_files()
self.log("# "+self.name,clean=True)
self.log("##### Challenge's type: "+self.type,clean=True)
if self.files and len(self.files)!=0:
self.log("### Challenge's files:",clean=True)
for file in self.files:
self.log(file.name,clean=True)
self.log("##### Challenge's description: "+self.desc,clean=True)
self.log("### Debug",clean=True)
def get_info(self):
data = self.CTF.session.get(self.CTF.challenges_url+'/'+str(self.id)).json()['data']
self.desc=BeautifulSoup(data['description'],'lxml').text
self.type=data['type']
self.file_list=data['files'] #These are not pathlib paths don't use this variable please
def get_files(self):
for file_url in self.file_list:
filename = file_url.split('/')[-1].split('?')[0]
path=self.directory.joinpath(filename)
if not os.path.exists(path):
try:
resp = self.CTF.session.get(self.CTF.url + file_url, stream=True)
with open(path, 'wb') as handle:
for chunk in resp.iter_content(chunk_size=512):
if chunk:
handle.write(chunk)
except Exception as e:
log(str(e),4)
self.files.append(Path(path))
def load(self):
self.files = [x for x in self.directory.glob('**/*') if '.md' not in str(x) and x.is_file()]
with open(self.notefile_path,'r+') as notefile:
notefile_content=notefile.read()
try:
self.desc=notefile_content.split("##### Challenge's description: ")[1].split("### Debug")[0]
except:
log("Couldn't scrap challenge's description",4)
self.desc=""
try:
self.type=notefile_content.split("##### Challenge's type:")[1].split("\n")[0]
except:
log("Couldn't scrap challenge's type",4)
self.type="None"
def log(self, text, verbose=False, no_verbose_output=None, state=1, clean=False): # Append information
if "\n" in text:
lines=[x for x in text.split("\n") if x !="" and x !="\n"]
for text in lines:
self.logline(text, verbose, no_verbose_output, state, clean)
else:
self.logline(text, verbose, no_verbose_output, state, clean)
def logline(self, text, verbose=False, no_verbose_output=None, state=1, clean=False):
if not clean:
text = "[" + datetime.now().strftime("%H:%M:%S") + "] [" + MESSAGE.STATE[state] + "] " + text
if no_verbose_output:
no_verbose_output = "[" + datetime.now().strftime("%H:%M:%S") + "] [" + MESSAGE.STATE[state] + "] " +\
no_verbose_output
if verbose:
print(text)
elif no_verbose_output:
print(no_verbose_output)
with open(self.notefile_path, "a+") as notefile:
notefile.write(text+" \n")
class CTF():
def __init__(self, args, base_path=None):
self.url=None
self.path=None
if re.match(r"(https?://(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_+.~#?&/=]*)",args.ctf):
log("This is an online CTF, initiating scrapping...",2)
# Online CTF
self.url = args.ctf
# Add trailing slash
if self.url[len(self.url)-1] !="/":
self.url+="/"
self.categories = [] # All lower case
self.challenges = []
self.challenges_id = []
self.auth = dict(name=args.login, password=args.password)
self.challenges_url = self.url + '/api/v1/challenges'
self.hints_url = self.url + '/api/v1/hints'
self.login_url = self.url + '/login'
if self.login():
if not base_path:
base_path = Path.cwd()
self.path = base_path.joinpath(self.title)
if not os.path.exists(self.path):
os.makedirs(self.path)
self.scrape()
else:
log("Couldn't login to the CTF, check credentials, url and availibility",4)
exit()
else:
# Offline CTF
log("This is an offline CTF, initiating loading...",2)
self.path=Path(args.ctf)
self.categories=[x.name for x in self.path.glob('./*/')]
self.challenges = []
for challenge_path in [x for x in self.path.glob('./*/*')]:
chall=Challenge(self,args={"name":challenge_path.name,"category":challenge_path.parents[0].name})
chall.load()
self.challenges.append(chall)
def login(self):
try:
self.session = session()
resp = self.session.get(self.login_url)
soup = BeautifulSoup(resp.text,'lxml')
nonce = soup.find('input', {'name':'nonce'}).get('value')
self.auth['nonce'] = nonce
self.title = soup.title.string
self.session.headers.update({'User-Agent' : "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"})
resp=self.session.post(self.login_url, data=self.auth)
return "Forgot" not in resp.text
except Exception as e:
log(str(e),4)
log("Couldn't login to the CTF, check credentials, url and availibility",4)
exit()
def scrape(self):
log("Scraping the CTF...",2)
resp=self.session.get(self.challenges_url).json()["data"]
#print(resp)
new=0
for chall_info in resp:
#We check if we didn't already scraped the challenge by checking the notefile path of the challenge
challenge=Challenge(self,chall_info)
if not os.path.exists(challenge.notefile_path):
new+=1
self.categories.append(challenge.category)
self.categories=list(set(self.categories))
#Check if category directory exists
if not os.path.exists(self.path.joinpath(challenge.category)):
os.makedirs(self.path.joinpath(challenge.category))
challenge.scrape() # Initiate notefile, scrape files and desc
log("Scraped the challenge "+str(challenge.name),2)
self.challenges.append(challenge)
self.challenges_id.append(challenge.id)
else:
#If already scraped, we only GET for the desc for the info and reuse the same files => create seperate file for others? like notefile, chall files, media/ ?
log("The challenge "+str(challenge.name)+" was already scraped and will be loaded from save.",2)
challenge.solved = True #Temporary
challenge.load() # Load files and desc
self.challenges.append(challenge)
log("I found "+str(new)+" new challenges!",2)
#print(self.challenges,self.categories)
def update(self):
if self.url:
log("Looking for newly unlocked challenges",2)
if self.login():
self.scrape()
else:
log("[!] Couldn't login to the CTF, check credentials, url and availibility. This is not the first login attempt.",4)
exit()
def main():
parser = argparse.ArgumentParser(description='Main function for the scraper')
parser.add_argument('--ctf', type=str,
help='CTFd URL or local dir')
parser.add_argument('--login', type=str,
help='Login username for the CTF')
parser.add_argument('--password', type=str,
help='Login password for the CTF')
args = parser.parse_args()
if not args.ctf or not args.login or not args.password:
print("Invalid usage, try ctfd_scraper.py --help")
exit(0)
ctf = CTF(args)
if __name__ == '__main__':
main()