ColdCore/ctftool

108 lines
3.9 KiB
Plaintext
Raw Normal View History

2015-11-08 06:10:26 +00:00
#!/usr/bin/python3
from database import *
2015-11-08 19:04:02 +00:00
from datetime import datetime, timedelta
2015-11-29 17:10:38 +00:00
import config
import getpass
2015-11-29 17:10:38 +00:00
import hashlib
import os
import os.path
2015-11-29 17:10:38 +00:00
import shutil
2015-11-08 06:10:26 +00:00
import sys
import yaml
import random
import utils
import utils.admin
2015-11-08 06:10:26 +00:00
tables = [Team, TeamAccess, Challenge, ChallengeSolve, ChallengeFailure, NewsItem, TroubleTicket, TicketComment, Notification, ScoreAdjustment, AdminUser]
2015-11-08 06:10:26 +00:00
operation = sys.argv[1]
if operation == "create-tables":
[i.create_table() for i in tables]
2015-11-08 06:10:26 +00:00
print("Tables created")
elif operation == "drop-tables":
if input("Are you sure? Type yes to continue: ") == "yes":
[i.drop_table() for i in tables]
2015-11-08 06:10:26 +00:00
print("Done")
else:
print("Okay, nothing happened.")
elif operation == "add-challenge":
challengefile = sys.argv[2]
with open(challengefile) as f:
chal = Challenge.create(**yaml.load(f))
print("Challenge added with id {}".format(chal.id))
elif operation == "gen-challenge":
n = int(sys.argv[2])
for i in range(n):
name = str(random.randint(0, 999999999))
2015-11-08 19:04:02 +00:00
chal = Challenge.create(name="Challenge".format(name), category="Generated", description="Lorem ipsum, dolor sit amet. The flag is {}".format(name), points=random.randint(50, 400), flag=name)
2015-11-08 06:10:26 +00:00
print("Challenge added with id {}".format(chal.id))
2015-11-08 19:04:02 +00:00
elif operation == "gen-team":
n = int(sys.argv[2])
chals = list(Challenge.select())
ctz = datetime.now()
diff = timedelta(minutes=5)
for i in range(n):
name = "Team {}".format(i + 1)
2015-11-09 02:44:04 +00:00
t = Team.create(name=name, email="none@none.com", affiliation="Autogenerated", eligible=True, key="", email_confirmation_key="autogen", email_confirmed=True)
2015-11-08 19:04:02 +00:00
t.key = "autogen{}".format(t.id)
t.save()
print("Team added with id {}".format(t.id))
2015-11-12 20:47:30 +00:00
for i in range(n * 10):
chal = random.choice(chals)
ctz -= diff
ChallengeSolve.create(team=Team.get(Team.id == (i % n) + 1), challenge=chal, time=ctz)
2015-11-08 19:04:02 +00:00
elif operation == "add-admin":
username = input("Username: ")
password = getpass.getpass().encode()
pwhash = utils.admin.create_password(password)
AdminUser.create(username=username, password=pwhash)
print("AdminUser created")
elif operation == "scan":
path = sys.argv[2]
dirs = [j for j in [os.path.join(path, i) for i in os.listdir(path)] if os.path.isdir(j)]
print(dirs)
n = 0
2015-11-29 17:10:38 +00:00
for d in dirs:
2015-11-29 17:10:38 +00:00
staticpaths = {}
if os.path.exists(os.path.join(d, "static.yml")):
with open(os.path.join(d, "static.yml")) as f:
statics = yaml.load(f)
for static in statics:
h = hashlib.sha256()
with open(os.path.join(d, static), "rb") as staticfile:
while True:
buf = staticfile.read(4096)
h.update(buf)
if not buf:
break
if "." in static:
name, ext = static.split(".", maxsplit=1)
fn = "{}_{}.{}".format(name, h.hexdigest(), ext)
else:
fn = "{}_{}".format(static, h.hexdigest())
staticpaths[static] = fn
shutil.copy(os.path.join(d, static), os.path.join(config.static_dir, fn))
print(fn)
if os.path.exists(os.path.join(d, "problem.yml")):
with open(os.path.join(d, "problem.yml")) as f:
n += 1
2015-11-29 17:10:38 +00:00
data = yaml.load(f)
for i in staticpaths:
print("looking for |{}|".format(i))
data["description"] = data["description"].replace("|{}|".format(i), "{}{}".format(config.static_prefix, staticpaths[i]))
Challenge.create(**data)
print(n, "challenges loaded")
2015-11-08 06:10:26 +00:00
# vim: syntax=python:ft=python