def admin_import_ctf(): backup = request.files['backup'] segments = request.form.get('segments') errors = [] try: if segments: import_ctf(backup, segments=segments.split(',')) else: import_ctf(backup) except Exception as e: print(e) errors.append(type(e).__name__) if errors: return errors[0], 500 else: return redirect(url_for('admin.admin_config'))
def admin_import_ctf(): backup = request.files['backup'] segments = request.form.get('segments') errors = [] try: if segments: import_ctf(backup, segments=segments.split(',')) else: import_ctf(backup) except Exception as e: print(e) errors.append(type(e).__name__) if errors: return errors[0], 500 else: return redirect(url_for('admin.admin_config'))
def admin_import_ctf(): backup = request.files['backup'] segments = request.form.get('segments') errors = [] try: if segments: import_ctf(backup, segments=segments.split(',')) else: import_ctf(backup) except TypeError: errors.append('The backup file is invalid') except IntegrityError as e: errors.append(e.message) except Exception as e: errors.append(type(e).__name__) if errors: return errors[0], 500 else: return redirect(url_for('admin.admin_config'))
def test_import_ctf(): """Test that CTFd can import a CTF""" app = create_ctfd() # TODO: Unrelated to an in-memory database, imports in a test environment are not working with SQLite... if app.config['SQLALCHEMY_DATABASE_URI'].startswith('sqlite') is False: with app.app_context(): base_user = '******' for x in range(10): user = base_user + str(x) user_email = user + "@ctfd.io" gen_team(app.db, name=user, email=user_email) for x in range(10): chal = gen_challenge(app.db, name='chal_name{}'.format(x)) gen_flag(app.db, chal=chal.id, flag='flag') app.db.session.commit() backup = export_ctf() with open('export.zip', 'wb') as f: f.write(backup.read()) destroy_ctfd(app) app = create_ctfd() with app.app_context(): import_ctf('export.zip') app.db.session.commit() print(Teams.query.count()) print(Challenges.query.count()) assert Teams.query.count() == 11 assert Challenges.query.count() == 10 assert Keys.query.count() == 10 destroy_ctfd(app)
from CTFd import create_app from CTFd.utils import import_ctf import zipfile import sys app = create_app() with app.app_context(): import_ctf(sys.argv[1])
""" python import.py export.zip challenges,teams,both,metadata """ from CTFd import create_app from CTFd.utils import import_ctf import sys app = create_app() with app.app_context(): if len(sys.argv) == 3: segments = sys.argv[2].split(',') else: segments = None import_ctf(sys.argv[1], segments=segments)