sch9_ctf_3/run-offline/sasaaageyooo/server.py

70 lines
1.8 KiB
Python
Raw Normal View History

import asyncio
#import time
import random as rnd
import websockets
import json
flag='ctf{1_l0v3_4774ck_0n_7174n}'
async def game(ws,path):
if('train' in path):
train=True
else:
train=False
if train:
state=[3,5,7]
else:
state=[rnd.randint(1,300) for i in range(300)]
await ws.recv()
await ws.send(json.dumps({'state':state}))
while(state!=[0]*len(state)):
try:
data = json.loads(await ws.recv())
index=data['index']
value=data['value']
if(state[index]<value or value<1):
0/0
state[index]-=value
except Exception as e:
await ws.send(json.dumps({'msg':'not valid request'}))
return
if(state==[0]*len(state)):
if(train):
await ws.send(json.dumps({'msg':'Вы победили. Теперь победите в финальной битве'}))
else:
await ws.send(json.dumps({'msg':flag}))
return
x=0
for i in state:
x^=i
mask=x
while(mask&(mask-1)):
mask=mask&(mask-1)
if(x==0):
for i in range(len(state)):
if(state[i]!=0):
index=i
value=rnd.randint(1,state[i])
break
else:
for i in range(len(state)):
if(mask&state[i]):
index=i
value=state[i]-(state[i]^x)
state[index]-=value
await ws.send(json.dumps({'index':index,'value':value}))
start_server = websockets.serve(game, "0.0.0.0", 8000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()