-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
80 lines (63 loc) · 3.16 KB
/
models.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
from CTFd.models import db, Teams, Challenges, Flags
from CTFd.utils.user import get_current_team
from . import globals
class RedHerringChallenge(Challenges):
__mapper_args__ = {"polymorphic_identity": "red_herring"}
id = db.Column(
db.Integer, db.ForeignKey("challenges.id", ondelete="CASCADE"), primary_key=True
)
dockerfile = db.Column(db.Text)
def __init__(self, *args, **kwargs):
super(RedHerringChallenge, self).__init__(**kwargs)
self.dockerfile = kwargs["dockerfile"]
def get_container_port(self):
try :
teamid = get_current_team().id
except:
teamid = None
if teamid is None:
return None
container = Containers.query.filter_by(challengeid=self.id, teamid=teamid).first()
return container.port
def get_container_address(self):
return globals.IP_ADDRESS_CONTAINERS
class CheaterTeams(db.Model):
__tablename__ = 'cheater_teams'
id = db.Column(db.Integer, primary_key=True)
challengeid = db.Column(db.Integer, db.ForeignKey('challenges.id', ondelete="CASCADE"))
cheaterid = db.Column(db.Integer, db.ForeignKey('users.id', ondelete="CASCADE"))
cheatteamid = db.Column(db.Integer, db.ForeignKey('teams.id', ondelete="CASCADE"))
sharerteamid = db.Column(db.Integer, db.ForeignKey('teams.id', ondelete="CASCADE"))
flagid = db.Column(db.Integer, db.ForeignKey('flags.id', ondelete="CASCADE"))
date = db.Column(db.DateTime, default=db.func.current_timestamp())
def __init__(self, challengeid, cheaterid, cheatteamid, sharerteamid, flagid):
self.challengeid = challengeid
self.cheaterid = cheaterid
self.cheatteamid = cheatteamid
self.sharerteamid = sharerteamid
self.flagid = flagid
def __repr__(self):
return "<CheaterTeams Team {0} maybe cheated for challenge {1} with the flag {2} belonging to the team {3} at {4} >".format(self.cheatteamid, self.challengeid, self.flagid, self.sharerteamid, self.date)
def cheated_team_name(self):
return Teams.query.filter_by(id=self.cheatteamid).first().name
def shared_team_name(self):
return Teams.query.filter_by(id=self.sharerteamid).first().name
def challenge_name(self):
return Challenges.query.filter_by(id=self.challengeid).first().name
class Containers(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
dockerfile = db.Column(db.Text)
address = db.Column(db.String(80))
port = db.Column(db.Integer)
challengeid = db.Column(db.Integer, db.ForeignKey('challenges.id', ondelete="CASCADE"))
teamid = db.Column(db.Integer, db.ForeignKey('teams.id', ondelete="CASCADE"))
def __init__(self, challengeid, teamid, name, dockerfile, port, address="127.0.0.1"):
self.name = name
self.dockerfile = dockerfile
self.challengeid = challengeid
self.teamid = teamid
self.port = port
self.address = globals.IP_ADDRESS_CONTAINERS
def __repr__(self):
return "<Container ID:(0) {1} for the challenge {2} and the team {3}>".format(self.id, self.name, self.challengeid, self.teamid)