-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCTFDumper.py
More file actions
executable file
·224 lines (177 loc) · 5.98 KB
/
Copy pathCTFDumper.py
File metadata and controls
executable file
·224 lines (177 loc) · 5.98 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
#!/usr/bin/env python3
banner = r'''
____ _____ _____ ____
/ ___|_ _| ___| _ \ _ _ _ __ ___ _ __ ___ _ __
| | | | | |_ | | | | | | | '_ ` _ \| '_ \ / _ \ '__|
| |___ | | | _| | |_| | |_| | | | | | | |_) | __/ |
\____| |_| |_| |____/ \__,_|_| |_| |_| .__/ \___|_|
|_|
'''
from argparse import ArgumentParser
from requests import Session
from requests.compat import urljoin, urlparse, urlsplit
from typing import Generator, List, Union
from jinja2 import Template
import logging
import logging.config
import re, os
CONFIG = {
'username': None,
'password': None,
'nonce_regex': 'name="nonce"(?:[^<>]+)?value="([0-9a-f]{64})"',
'base_url': None,
'no_file': None,
'no_login': None,
'template': os.path.join(os.path.dirname(os.path.realpath(__file__)), 'templates/default.md'),
'verbose': logging.INFO,
'blacklist': r'[^a-zA-Z0-9_\-\. ]',
}
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
})
logger = logging.getLogger(__name__)
session = Session()
def welcome() -> None:
logger.info(banner)
def setup() -> None:
parser = ArgumentParser(description='A tool for dumping CTFd challenges')
parser.add_argument(
'url',
help='Platform URL',
)
parser.add_argument(
'-u', '--username',
help='Platfrom username',
)
parser.add_argument(
'-p', '--password',
help='Platform password',
)
parser.add_argument(
'--nonce-regex',
help='Platform nonce regex',
)
parser.add_argument(
'--auth-file',
help='File containing username and password, seperated by newline',
)
parser.add_argument(
'-n', '--no-login',
help='Use this option if the platform does not require authentication',
action='store_true',
)
parser.add_argument(
'--no-file',
help='Don\'t download files',
action='store_true',
)
parser.add_argument(
'--trust-all',
help='Will make directory as the name of the challenge, the slashes(/) character will automatically be replaced with underscores(_)',
action='store_true',
)
parser.add_argument(
'-t', '--template',
help='Custom template path',
)
parser.add_argument(
'-v', '--verbose',
help='Verbose',
action='store_true',
)
args = parser.parse_args()
CONFIG['base_url'] = args.url
CONFIG['no_file'] = args.no_file
CONFIG['no_login'] = args.no_login
if not args.no_login:
CONFIG['username'] = args.username
CONFIG['password'] = args.password
if args.nonce_regex:
CONFIG['nonce_regex'] = args.nonce_regex
if args.auth_file:
with open(args.auth_file, 'r') as f:
CONFIG['username'] = f.readline().strip()
CONFIG['password'] = f.readline().strip()
if args.trust_all:
CONFIG['blacklist'] = '/'
if args.verbose:
CONFIG['verbose'] = logging.DEBUG
if args.template:
CONFIG['template'] = args.template
logging.basicConfig(
level=CONFIG['verbose'],
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logging.addLevelName(logging.ERROR, '[-]')
logging.addLevelName(logging.INFO, '[+]')
logging.addLevelName(logging.DEBUG, '[*]')
def get_nonce() -> str:
res = session.get(urljoin(CONFIG['base_url'], '/login'))
return re.search(CONFIG['nonce_regex'], res.text).group(1)
def login() -> None:
nonce = get_nonce()
logger.debug(f'Nonce: {nonce}')
res = session.post(
urljoin(CONFIG['base_url'], '/login'),
data={
'name': CONFIG['username'],
'password': CONFIG['password'],
'nonce': nonce,
}
)
if 'incorrect' in res.text:
logger.error('Login failed!')
exit(1)
def logout() -> None:
logger.info('Done! Logging you out!')
session.get(urljoin(CONFIG['base_url'], '/logout'))
def fetch(url: str) -> Union[List[dict], dict]:
logger.debug(f'Fetching {url}')
res = session.get(url)
if not res.ok or not res.json()['success']:
logger.error('Failed fetching challenge!')
exit(1)
return res.json()['data']
def fetch_file(filepath: str, filename: str, clean_filename: str) -> None:
logger.info(f'Downloading {clean_filename} into {filepath}')
res = session.get(urljoin(CONFIG['base_url'], filename), stream=True)
with open(os.path.join(filepath, clean_filename), 'wb') as f:
f.write(res.content)
def get_challenges() -> Generator[dict, None, None]:
logger.debug('Getting challenges')
challenges = fetch(urljoin(CONFIG['base_url'], '/api/v1/challenges'))
for challenge in challenges:
yield fetch(urljoin(CONFIG['base_url'], f'/api/v1/challenges/{challenge["id"]}'))
def run() -> None:
hostname = urlparse(CONFIG['base_url']).hostname
template = Template(open(CONFIG['template']).read())
for challenge in get_challenges():
category = re.sub(CONFIG['blacklist'], '', challenge['category']).strip()
name = re.sub(CONFIG['blacklist'], '', challenge['name']).strip()
logger.info(f'[{category}] {name}')
filepath = os.path.join(hostname, category, name)
if not os.path.exists(filepath):
logger.info(f'Creating directory {filepath}')
os.makedirs(filepath)
with open(os.path.join(filepath, 'README.md'), 'w+') as f:
rendered = template.render(challenge=challenge)
f.write(rendered)
if CONFIG['no_file']:
continue
if 'files' in challenge:
for filename in challenge['files']:
clean_filename = os.path.basename(urlsplit(filename).path)
fetch_file(filepath, filename, clean_filename)
def main() -> None:
setup()
welcome()
if CONFIG['no_login']:
run()
else:
login()
run()
logout()
if __name__ == '__main__':
main()