#!/usr/bin/env python3importthreadingimportsocketimportrandomimportcodecsimportlzmaaslzwithopen("flag.txt","rb")asf:FLAG=f.read().strip()defnone(v):returnlen(v)defzlib(v):returnlen(codecs.encode(v,"zlib"))defbzip2(v):returnlen(codecs.encode(v,"bz2"))deflzma(v):returnlen(lz.compress(v))COMPRESSION_FUNCS=[none,zlib,bzip2,lzma]defhandle_connection(s,addr):s.sendall(("Please send: seed:string\\n\n""I'll then show you the compression benchmark results!\n""Note: Flag has format DrgnS{[A-Z]+}\n").encode())data=b''whileTrue:idx=data.find(b'\n')ifidx==-1:iflen(data)>128:s.shutdown(socket.SHUT_RDWR)s.close()returnd=s.recv(1024)ifnotd:s.close()returndata+=dcontinueline=data[:idx]data=data[idx+1:]seed,string=line.split(b':',1)flag=bytearray(FLAG)random.seed(int(seed))random.shuffle(flag)print(flag)test_string=string+bytes(flag)response=[]forcfuncinCOMPRESSION_FUNCS:res=cfunc(test_string)response.append(f"{cfunc.__name__:>8}{res:>4}")response.append('')response.append('')s.sendall('\n'.join(response).encode())s.shutdown(socket.SHUT_RDWR)s.close()defmain():withsocket.socket(socket.AF_INET,socket.SOCK_STREAM)ass:s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)s.bind(('0.0.0.0',1337))s.listen(256)whileTrue:conn,addr=s.accept()print(f"Connection from: {addr}")th=threading.Thread(target=handle_connection,args=(conn,addr),daemon=True)th.start()if__name__=="__main__":main()