-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
170 lines (134 loc) · 4.74 KB
/
helpers.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
from CTFd import create_app
from CTFd.models import *
from CTFd.utils import cache
from sqlalchemy_utils import database_exists, create_database, drop_database
from sqlalchemy.engine.url import make_url
import datetime
import six
import gc
if six.PY2:
text_type = unicode
binary_type = str
else:
text_type = str
binary_type = bytes
def create_ctfd(ctf_name="CTFd", name="admin", email="[email protected]", password="password", setup=True):
app = create_app('CTFd.config.TestingConfig')
if setup:
app = setup_ctfd(app, ctf_name, name, email, password)
return app
def setup_ctfd(app, ctf_name="CTFd", name="admin", email="[email protected]", password="password"):
with app.app_context():
with app.test_client() as client:
data = {}
r = client.get('/setup') # Populate session with nonce
with client.session_transaction() as sess:
data = {
"ctf_name": ctf_name,
"name": name,
"email": email,
"password": password,
"nonce": sess.get('nonce')
}
client.post('/setup', data=data)
return app
def destroy_ctfd(app):
with app.app_context():
app.db.session.commit()
app.db.session.close_all()
gc.collect() # Garbage collect (necessary in the case of dataset freezes to clean database connections)
app.db.drop_all()
cache.clear()
drop_database(app.config['SQLALCHEMY_DATABASE_URI'])
def register_user(app, name="user", email="[email protected]", password="password"):
with app.app_context():
with app.test_client() as client:
r = client.get('/register')
with client.session_transaction() as sess:
data = {
"name": name,
"email": email,
"password": password,
"nonce": sess.get('nonce')
}
client.post('/register', data=data)
def login_as_user(app, name="user", password="password"):
with app.app_context():
with app.test_client() as client:
r = client.get('/login')
with client.session_transaction() as sess:
data = {
"name": name,
"password": password,
"nonce": sess.get('nonce')
}
client.post('/login', data=data)
return client
def get_scores(user):
scores = user.get('/scores')
print(scores.get_data(as_text=True))
scores = json.loads(scores.get_data(as_text=True))
print(scores)
return scores['standings']
def gen_challenge(db, name='chal_name', description='chal_description', value=100, category='chal_category', type='standard', hidden=False):
chal = Challenges(name, description, value, category)
if hidden:
chal.hidden = hidden
db.session.add(chal)
db.session.commit()
return chal
def gen_award(db, teamid, name="award_name", value=100):
award = Awards(teamid, name, value)
award.date = datetime.datetime.utcnow()
db.session.add(award)
db.session.commit()
return award
def gen_tag(db, chal, tag='tag_tag'):
tag = Tags(chal, tag)
db.session.add(tag)
db.session.commit()
return tag
def gen_file(db, chal, location):
f = Files(chal, location)
db.session.add(f)
db.session.commit()
return f
def gen_flag(db, chal, flag='flag', key_type='static', data=None):
key = Keys(chal, flag, key_type)
if data:
key.data = data
db.session.add(key)
db.session.commit()
return key
def gen_team(db, name='name', email='[email protected]', password='password'):
team = Teams(name, email, password)
db.session.add(team)
db.session.commit()
return team
def gen_hint(db, chal, hint="This is a hint", cost=0, type=0):
hint = Hints(chal, hint, cost, type)
db.session.add(hint)
db.session.commit()
return hint
def gen_solve(db, teamid, chalid, ip='127.0.0.1', flag='rightkey'):
solve = Solves(teamid, chalid, ip, flag)
solve.date = datetime.datetime.utcnow()
db.session.add(solve)
db.session.commit()
return solve
def gen_wrongkey(db, teamid, chalid, ip='127.0.0.1', flag='wrongkey'):
wrongkey = WrongKeys(teamid, chalid, ip, flag)
wrongkey.date = datetime.datetime.utcnow()
db.session.add(wrongkey)
db.session.commit()
return wrongkey
def gen_tracking(db, ip, team):
tracking = Tracking(ip, team)
db.session.add(tracking)
db.session.commit()
return tracking
def gen_page(db, title, route, html, draft=False, auth_required=False):
page = Pages(title, route, html, draft, auth_required)
db.session.add(page)
db.session.commit()
return page