2017-01-07 07:44:27 +00:00
|
|
|
from CTFd import create_app
|
2017-03-23 00:00:45 +00:00
|
|
|
from CTFd.models import *
|
2017-01-07 07:44:27 +00:00
|
|
|
from sqlalchemy_utils import database_exists, create_database, drop_database
|
|
|
|
from sqlalchemy.engine.url import make_url
|
2017-09-08 03:29:41 +00:00
|
|
|
import datetime
|
2017-09-09 04:17:48 +00:00
|
|
|
import six
|
2017-10-08 01:29:03 +00:00
|
|
|
import gc
|
2017-09-09 04:17:48 +00:00
|
|
|
|
|
|
|
if six.PY2:
|
|
|
|
text_type = unicode
|
|
|
|
binary_type = str
|
|
|
|
else:
|
|
|
|
text_type = str
|
|
|
|
binary_type = bytes
|
2017-01-07 07:44:27 +00:00
|
|
|
|
2017-01-10 08:35:48 +00:00
|
|
|
|
2017-06-18 22:54:20 +00:00
|
|
|
def create_ctfd(ctf_name="CTFd", name="admin", email="admin@ctfd.io", password="password", setup=True):
|
2017-01-10 08:35:48 +00:00
|
|
|
app = create_app('CTFd.config.TestingConfig')
|
2017-01-07 07:44:27 +00:00
|
|
|
|
2017-06-18 22:54:20 +00:00
|
|
|
if setup:
|
2017-09-30 01:22:10 +00:00
|
|
|
app = setup_ctfd(app, ctf_name, name, email, password)
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
def setup_ctfd(app, ctf_name="CTFd", name="admin", email="admin@ctfd.io", 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)
|
2017-01-07 07:44:27 +00:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
2017-07-08 21:53:14 +00:00
|
|
|
def destroy_ctfd(app):
|
2017-10-06 01:39:28 +00:00
|
|
|
with app.app_context():
|
|
|
|
app.db.session.commit()
|
|
|
|
app.db.session.close_all()
|
2017-10-08 01:29:03 +00:00
|
|
|
gc.collect() # Garbage collect (necessary in the case of dataset freezes to clean database connections)
|
2017-10-06 01:39:28 +00:00
|
|
|
app.db.drop_all()
|
2017-07-08 21:53:14 +00:00
|
|
|
drop_database(app.config['SQLALCHEMY_DATABASE_URI'])
|
|
|
|
|
|
|
|
|
2017-01-07 07:44:27 +00:00
|
|
|
def register_user(app, name="user", email="user@ctfd.io", 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)
|
2017-03-23 00:00:45 +00:00
|
|
|
return client
|
|
|
|
|
|
|
|
|
2017-07-18 02:18:23 +00:00
|
|
|
def get_scores(user):
|
|
|
|
scores = user.get('/scores')
|
2017-10-06 01:39:28 +00:00
|
|
|
print(scores.get_data(as_text=True))
|
2017-07-18 02:18:23 +00:00
|
|
|
scores = json.loads(scores.get_data(as_text=True))
|
2017-10-06 01:39:28 +00:00
|
|
|
print(scores)
|
2017-07-18 02:18:23 +00:00
|
|
|
return scores['standings']
|
|
|
|
|
|
|
|
|
2017-10-28 17:31:34 +00:00
|
|
|
def gen_challenge(db, name='chal_name', description='chal_description', value=100, category='chal_category', type='standard', hidden=False):
|
2017-03-23 00:00:45 +00:00
|
|
|
chal = Challenges(name, description, value, category)
|
2017-10-28 17:31:34 +00:00
|
|
|
if hidden:
|
|
|
|
chal.hidden = hidden
|
2017-03-23 00:00:45 +00:00
|
|
|
db.session.add(chal)
|
|
|
|
db.session.commit()
|
|
|
|
return chal
|
|
|
|
|
|
|
|
|
|
|
|
def gen_award(db, teamid, name="award_name", value=100):
|
|
|
|
award = Awards(teamid, name, value)
|
|
|
|
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():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-10-14 20:37:41 +00:00
|
|
|
def gen_flag(db, chal, flag='flag', key_type='static'):
|
2017-03-23 00:00:45 +00:00
|
|
|
key = Keys(chal, flag, key_type)
|
|
|
|
db.session.add(key)
|
|
|
|
db.session.commit()
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
|
|
|
def gen_team(db, name='name', email='user@ctfd.io', password='password'):
|
|
|
|
team = Teams(name, email, password)
|
|
|
|
db.session.add(team)
|
|
|
|
db.session.commit()
|
|
|
|
return team
|
|
|
|
|
|
|
|
|
2017-08-06 23:33:02 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-07-08 21:53:14 +00:00
|
|
|
def gen_solve(db, teamid, chalid, ip='127.0.0.1', flag='rightkey'):
|
|
|
|
solve = Solves(teamid, chalid, ip, flag)
|
2017-09-08 03:29:41 +00:00
|
|
|
solve.date = datetime.datetime.utcnow()
|
2017-03-23 00:00:45 +00:00
|
|
|
db.session.add(solve)
|
|
|
|
db.session.commit()
|
|
|
|
return solve
|
|
|
|
|
2017-05-12 04:34:20 +00:00
|
|
|
|
2017-07-08 21:53:14 +00:00
|
|
|
def gen_wrongkey(db, teamid, chalid, ip='127.0.0.1', flag='wrongkey'):
|
|
|
|
wrongkey = WrongKeys(teamid, chalid, ip, flag)
|
2017-09-08 03:29:41 +00:00
|
|
|
wrongkey.date = datetime.datetime.utcnow()
|
2017-03-23 00:00:45 +00:00
|
|
|
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()
|
2017-05-12 04:34:20 +00:00
|
|
|
return tracking
|
2017-06-03 18:25:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def gen_page(db, route, html):
|
|
|
|
page = Pages(route, html)
|
|
|
|
db.session.add(page)
|
|
|
|
db.session.commit()
|
|
|
|
return page
|