feat: build wahrwelt A/D toolkit
This commit is contained in:
1 parent
303438d206
commit
653924d8c6
564 files changed
+36310
-49998
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import socket
|
||||
import logging
|
||||
|
||||
from models import FlagStatus, SubmitResult
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
RESPONSES = {
|
||||
FlagStatus.QUEUED: ['timeout', 'game not started', 'try again later', 'game over', 'is not up',
|
||||
'no such flag'],
|
||||
FlagStatus.ACCEPTED: ['accepted', 'congrat'],
|
||||
FlagStatus.REJECTED: (
|
||||
[
|
||||
'bad',
|
||||
'wrong',
|
||||
'expired',
|
||||
'unknown',
|
||||
'your own',
|
||||
'too old',
|
||||
'not in database',
|
||||
'already submitted',
|
||||
'invalid flag',
|
||||
] + [
|
||||
'self',
|
||||
'invalid',
|
||||
'already_submitted',
|
||||
'team_not_found',
|
||||
'too_old',
|
||||
]
|
||||
),
|
||||
}
|
||||
|
||||
READ_TIMEOUT = 5
|
||||
APPEND_TIMEOUT = 0.05
|
||||
BUFSIZE = 4096
|
||||
|
||||
|
||||
def recvall(sock):
|
||||
sock.settimeout(READ_TIMEOUT)
|
||||
chunks = [sock.recv(BUFSIZE)]
|
||||
|
||||
sock.settimeout(APPEND_TIMEOUT)
|
||||
while True:
|
||||
try:
|
||||
chunk = sock.recv(BUFSIZE)
|
||||
if not chunk:
|
||||
break
|
||||
|
||||
chunks.append(chunk)
|
||||
except socket.timeout:
|
||||
break
|
||||
|
||||
sock.settimeout(READ_TIMEOUT)
|
||||
return b''.join(chunks)
|
||||
|
||||
|
||||
def submit_flags(flags, config):
|
||||
sock = socket.create_connection((config['SYSTEM_HOST'], config['SYSTEM_PORT']),
|
||||
READ_TIMEOUT)
|
||||
greeting = recvall(sock)
|
||||
if b'Please enter flags' not in greeting:
|
||||
raise RuntimeError('Чекер не отправил приветствие: {}'.format(greeting))
|
||||
|
||||
unknown_responses = set()
|
||||
for item in flags:
|
||||
sock.sendall(item.flag.encode() + b'\n')
|
||||
response = recvall(sock).decode().strip()
|
||||
if response:
|
||||
response = response.splitlines()[0]
|
||||
response = response.replace('[{}] '.format(item.flag), '')
|
||||
|
||||
response_lower = response.lower()
|
||||
for status, substrings in RESPONSES.items():
|
||||
if any(s in response_lower for s in substrings):
|
||||
found_status = status
|
||||
break
|
||||
else:
|
||||
found_status = FlagStatus.QUEUED
|
||||
if response not in unknown_responses:
|
||||
unknown_responses.add(response)
|
||||
logger.warning('Неизвестный ответ чекера (флаг будет отправлен повторно): %s', response)
|
||||
|
||||
yield SubmitResult(item.flag, found_status, response)
|
||||
|
||||
sock.close()
|
||||
Reference in new issue
Block a user