-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumper.py
executable file
·176 lines (122 loc) · 3.8 KB
/
dumper.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
#!/usr/bin/env python3
import json
import sys
from os import listdir, makedirs, path
import requests as req
from utils import *
DEBUG = False
USE_HEADERS_FOR_FILES = False
if '-h' in sys.argv:
print(
f"""~~ universal ctf dumper srcipt ~~
firstly, copy request to task json to /tmp/request
usage: {sys.argv[0]} board_name [range default: 0-200] [http? default depends on board] [-om to get only metadata]
results will be in ./task_name direcrory:
example_task
info.json
file1.zip
file2.png
""")
args = sys.argv[1::]
if len(args) < 2:
args.append('0-200')
if len(args) < 3:
args.append('from board')
if len(args) < 4:
args.append("False")
if len(listdir("./")) != 0:
print("This directory is not empty")
act = input("Do you really want to continue? [y/N] ")
if act != 'y':
exit(0)
def getTask(url, task_id):
global files
j = req.get(url, headers=HEADERS)
if not j.ok:
print('Now at id', task_id, end="\r")
# print('Now at id', url.split('/')[board_info['task_id_in_url']], end="\r")
return None
print(f"[+] Got task with id { task_id }")
# print(j.text)
task = board.parse_task(j.text, id=task_id)
if task is None:
return None
task["id"] = task_id
try:
print(f"[^] Title: { task['Title'] }, Category: { process_category(task) }, Value: { process_value(task['Value']) }")
except KeyError:
print(f"wtf {task=}")
if not path.exists(task['Title']):
makedirs(f'{task["Title"]}')
else:
print('Warning: Task is already downloaded')
# print('rm -r * && !!')
# exit(0)
with open(f'./{task["Title"]}/info.json', 'w') as f:
f.write(json.dumps(task))
for i in task['Files']:
file_headers = HEADERS if USE_HEADERS_FOR_FILES else None
if isinstance(i, str):
name = i
url = i
if isinstance(i, dict):
name = i["name"]
url = i["url"]
f = req.get(files.format(filename=url), headers=file_headers)
while f.status_code != 200:
print('Looks like there is something wrong with files url format string')
print(f'{f.status_code=} {f.text=}')
print(f"{f.history[0].url}")
print(f"{f.url}")
print('You can type it yourself, filename will be placed under {filename}')
print('or type SKIP to skip')
print(f'example filename: {url}')
bs = base.strip("/")
files = bs + input(f"{bs}")
# if "SKIP" in files:
# break
ff = files.format(filename=url)
print(f"getting {ff=}")
f = req.get(ff, headers=file_headers, allow_redirects=True)
with open(f'./{ task["Title"] }/{ name.split("/")[-1] }', 'wb') as file:
file.write(f.content)
if __name__ == '__main__':
board_name = args[0]
Range = list(map(int, args[1].split('-')))
Range[1] += 1
board = __import__(f'board_configs.{ board_name }', fromlist=('wtf'))
board_info = board.getInfo()
http = args[2].lower()
if http == 'from board':
http = board_info['http?']
else:
http = (True if args[2].lower() != 'false' else False)
# parsed args, now parsing /tmp/request
from utils import parse_request
url, HEADERS = parse_request(http)
url = url.split('/')
if DEBUG: print(url)
# /tmp/request parsed
chals = url
if board_info["task_id_in_url"] != None:
chals[board_info["task_id_in_url"]] = "{id}"
chals = '/'.join(chals)
base = '/'.join(url[:3]) + '/'
files = base + board_info["url_to_files"] # there should be {filename} in it
if DEBUG: print(url, base, chals, files)
if not board_info["custom_ids"]:
ids = range(*Range)
else:
ids = board.get_ids(chals)
urls = ((chals.format(id=str(Id)), Id) for Id in ids)
if args[3] != "-om":
for url, task_id in urls:
getTask(url, task_id)
print("getting metadata")
meta = dict( board.get_meta(base, HEADERS) )
for task in listdir("./"):
info = json.load(open(f"./{task}/info.json", 'r'))
for key, val in meta.get(info["id"], {}).items():
info[key] = val
json.dump(info, open(f"./{task}/info.json", 'w'))
print("done.")