-
Notifications
You must be signed in to change notification settings - Fork 19
/
flag.py
70 lines (57 loc) · 1.63 KB
/
flag.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
import secrets
import string
from typing import Optional
from .base import BaseModel
ALPHABET = string.ascii_uppercase + string.digits
class Flag(BaseModel):
"""
Model representing a flag.
Contains flag round, id, team, task, the value itself
and additional data for the checker.
"""
round: int
id: Optional[int]
team_id: int
task_id: int
flag: str
public_flag_data: Optional[str]
private_flag_data: Optional[str]
vuln_number: Optional[int]
table_name = 'Flags'
__slots__ = (
'id',
'team_id',
'task_id',
'flag',
'round',
'public_flag_data',
'private_flag_data',
'vuln_number',
)
@classmethod
def generate(
cls, service: str, team_id: int, task_id: int, current_round: int
) -> 'Flag':
"""
Generate a new flag.
:param service: service of new flag (to pick the first flag letter)
:param team_id: team id
:param task_id: task id
:param current_round: current round
:return: Flag model instance
"""
service_letter = service[0].upper()
rnd_data = ''.join(secrets.choice(ALPHABET) for _ in range(30))
flag_text = service_letter + rnd_data + '='
return cls(
id=None,
team_id=team_id,
task_id=task_id,
flag=flag_text,
round=current_round,
public_flag_data=None,
private_flag_data=None,
vuln_number=None,
)
def __str__(self) -> str:
return f"Flag({self.id}, task {self.task_id}, team {self.team_id})"