-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelib-sample.py
77 lines (62 loc) · 2.39 KB
/
gamelib-sample.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
import sys
import requests
from gamelib import *
class SampleServiceInterface(ServiceInterface):
name = 'SampleService'
def check_integrity(self, team, tick):
try:
assert_requests_response(requests.get('http://{}:8000/'.format(team.ip)), 'text/html; charset=utf-8')
except IOError:
raise OfflineException('Could not login')
def store_flags(self, team, tick):
username = usernames.generate_username()
password = usernames.generate_password()
self.store(team, tick, 'credentials', [username, password])
try:
flag = self.get_flag(team, tick, 1)
response = assert_requests_response(
requests.post('http://{}:8000/register'.format(team.ip), data={'username': username, 'password': password, 'flag': flag}),
'text/html; charset=utf-8'
)
assert 'Flag stored!' in response.text
return 1
except IOError:
raise OfflineException('Could not register')
def retrieve_flags(self, team, tick):
username, password = self.load(team, tick, 'credentials')
try:
session = requests.Session()
assert_requests_response(
session.post('http://{}:8000/login'.format(team.ip), data={'username': username, 'password': password}),
'text/html; charset=utf-8'
)
response = assert_requests_response(session.get('http://{}:8000/data'.format(team.ip)), 'text/html; charset=utf-8')
# V1 - easy check for flag
flag = self.get_flag(team, tick, 1)
if flag not in response.text:
raise FlagMissingException("Flag not found")
# V2 - advanced check with payload
flags = self.search_flags(response.text)
assert len(flags) > 0, 'No flags found'
flag = list(flags)[0]
_, _, _, payload = self.check_flag(flag, team.id, tick) # returns None,None,None,None if invalid
if not flag or not payload or payload != 1:
# not the flag we're looking for
raise FlagMissingException("Flag not found")
return 1
except IOError:
raise OfflineException('Could not login')
if __name__ == '__main__':
# TEST CODE
team = Team(12, 'n00bs', '127.0.0.1')
tick = int(sys.argv[1]) if len(sys.argv) > 1 else 2
service = SampleServiceInterface(7)
print('[1] Integrity check...')
service.check_integrity(team, tick)
print('Passed.')
print('[2] Store flags...')
flags = service.store_flags(team, tick)
print('Done ({} flags).'.format(flags))
print('[3] Retrieve the flags in the next tick')
flags = service.retrieve_flags(team, tick)
print('Done ({} flags).'.format(flags))