def handle_client(conn,addr): WORDSAMPLESIZE = 25 CORRECTANSWERS = 50 ELAPSEDTIME = 60 # seed after fork random.seed() #the banner that the players are greeted with BANNER = "\t\tWelcome to the jumbled word server.\n\t\t" BANNER += "="*30 BANNER += "\n[+] Unjumble %d words sequentially within %d seconds.\n" % (CORRECTANSWERS, ELAPSEDTIME) #print the banner conn.send(BANNER) dictList = [] ins = open( "words.txt", "r" ) for line in ins: dictList.append(line.strip()) ins.close() time.sleep(0.2) correctCounter=0 start = int(time.time()) for x in range(0,CORRECTANSWERS): #Send random word list range randomList = random.sample(dictList, WORDSAMPLESIZE) conn.send("Wordlist: %s\n" % repr(randomList)) time.sleep(0.2) #Send random jumbled word serverWord = randomList[random.randint(0,len(randomList)-1)] conn.send("Jumbled word: %s\n" % jumble(serverWord)) time.sleep(0.2) #Receive jumbled word conn.send("Enter unjumbled word: \n") clientWord = conn.recv(1024) if serverWord.strip() == clientWord.strip(): correctCounter+=1 elapsed = (int(time.time()) - start) print elapsed if correctCounter == CORRECTANSWERS and elapsed < ELAPSEDTIME: conn.send(helpers.load_flag()) elif correctCounter == CORRECTANSWERS: conn.send("You unjumbled all the words but took to long") else: conn.send("You got %d / %d words correct" % (correctCounter,CORRECTANSWERS)) conn.close()
def handle_client(connection, addr): reset_key(connection) send_banner(connection) f = conn.makefile() #Get the next command cmd = f.readline() while len(cmd) <> 0: cmd = cmd.strip() if len(cmd) == 0: conn.send("Invalid command. Bye\n") break iv = gen_iv() #Generate a new iv #Process command if cmd[0] == "D": ciphertext = encrypt(iv, helpers.load_flag()) conn.send( iv.encode("hex")[0:iv_size] + ":" + ciphertext.encode("hex") + "\n") #Reset the key after dumping the encrypted flag reset_key(connection) cmd = f.readline() #Get next command continue elif cmd[0] == "E": segs = cmd.split(",") if len(segs) <> 2 or len(segs[1]) < 1: conn.send("Invalid use of encrypt. Usage E,<valuetoencrypt>\n") else: ciphertext = encrypt(iv, segs[1]) conn.send( iv.encode("hex")[0:iv_size] + ":" + ciphertext.encode("hex") + "\n") cmd = f.readline() #Get next command continue else: conn.send("Invalid command. Bye\n") break #clean up sockets f.close() conn.close()
def handle_client(conn, addr): #the actual AES key used 16 bytes AESKey = os.urandom(32) ctr = Counter.new(128) #create a new AES object that uses the above details CTR mode makes it a stream #cipher I believe aes = AES.new(AESKey, AES.MODE_CTR, counter=ctr) #the banner that the players are greeted with BANNER = "\t\tWelcome to the Keygen server.\n\t\t" BANNER += "="*30 BANNER += "\n[+]All access is monitored and any unauthorised access will be treated as CRIME\n" SECRET = 'Key:' + helpers.load_flag() #print the banner conn.send(BANNER) #get input from the client 2048 is arbitrary data = conn.recv(2048).strip() #while the client continues to send us stuff while len(data): data = compress(SECRET + data) #encrypt the data using AES stream mode data = encrypt(aes, data) #send the hex representation to the client conn.send(data) #try to get some more data from the client, if they want to continue, data = conn.recv(2048).strip() #client has gone conn.close()
def handle_client(connection,addr): reset_key(connection) send_banner(connection) f = conn.makefile() #Get the next command cmd = f.readline() while len(cmd) <> 0: cmd = cmd.strip() if len(cmd) == 0: conn.send("Invalid command. Bye\n") break iv = gen_iv() #Generate a new iv #Process command if cmd[0] == "D": ciphertext = encrypt(iv, helpers.load_flag()) conn.send(iv.encode("hex")[0:iv_size]+":"+ciphertext.encode("hex")+"\n") #Reset the key after dumping the encrypted flag reset_key(connection) cmd = f.readline() #Get next command continue elif cmd[0] == "E": segs = cmd.split(",") if len(segs) <> 2 or len(segs[1]) < 1: conn.send("Invalid use of encrypt. Usage E,<valuetoencrypt>\n") else: ciphertext = encrypt(iv, segs[1]) conn.send(iv.encode("hex")[0:iv_size]+":"+ciphertext.encode("hex")+"\n") cmd = f.readline() #Get next command continue else: conn.send("Invalid command. Bye\n") break #clean up sockets f.close() conn.close()
def handle_client(conn,addr): numpy.random.seed() NUM_LEVELS = 10 COMMAND_LIST = ['north', 'south', 'east', 'west', 'up', 'down', 'pickup', 'escape'] conn.send('Please wait while the map loads...\n') levels = {} gr = graph() key_level = rand(NUM_LEVELS-3,NUM_LEVELS-1) door_level = 0 for i in range(NUM_LEVELS): levels[i] = maze(25, 25, 1, 1) add_graph(gr, levels[i], i) if i > 0: (x_d,y_d) = setnode(levels[i], M_DOWN) down = get_id(x_d, y_d, i) gr.add_edge((up, down)) if i < NUM_LEVELS-1: (x_u,y_u) = setnode(levels[i], M_UP) up = get_id(x_u, y_u, i) if i == key_level: (x, y) = setnode(levels[i], M_KEY) key = get_id(x, y, i) if i == door_level: (x, y) = setnode(levels[i], M_DOOR) door = get_id(x, y, i) st,weights = shortest_path(gr, key) distance = weights[door] collapse = distance + 5 start_z = NUM_LEVELS/2 (start_x, start_y) = emptynode(levels[start_z]) c_x = start_x c_y = start_y c_z = start_z player_key = False conn.send('Map loaded.\n') while True: node = levels[c_z][c_y,c_x] if node == M_UP: conn.send('There are stairs heading upward.\n') if node == M_DOWN: conn.send('There are stairs heading downward.\n') if node == M_KEY: conn.send('There is a key here.\n') if node == M_DOOR: conn.send('There is a locked door here.\n') data = conn.recv(1024).strip() commands = data.split('\n') for command in commands: if not command in COMMAND_LIST: conn.send('Invalid command: %s. Possible commands include are: %s\n' % (command, COMMAND_LIST)) continue if command == 'pickup': if player_key: conn.send('You already have the key.\n') else: if checknode(levels[c_z], c_x, c_y, M_KEY): conn.send('You picked up the key.\n') conn.send('The ceiling starts to collapse.\n') player_key = True else: conn.send('No key here.\n') elif command == 'escape': if checknode(levels[c_z], c_x, c_y, M_DOOR): if player_key: conn.send('YOU ESCAPED!\n') conn.send('Key: %s' % helpers.load_flag()) else: conn.send('You need the key to unlock the door.\n') else: conn.send('No door here.\n') elif command == 'north': if checknode(levels[c_z], c_x, c_y-1, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved north.\n') c_y = c_y-1 elif command == 'south': if checknode(levels[c_z], c_x, c_y+1, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved south.\n') c_y = c_y+1 elif command == 'west': if checknode(levels[c_z], c_x-1, c_y, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved west.\n') c_x = c_x-1 elif command == 'east': if checknode(levels[c_z], c_x+1, c_y, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved east.\n') c_x = c_x+1 elif command == 'up': if checknode(levels[c_z], c_x, c_y, M_UP): conn.send('You moved upstairs.\n') c_z = c_z + 1 c_x, c_y = findnode(levels[c_z], M_DOWN) else: conn.send('No stairs upward here.\n') elif command == 'down': if checknode(levels[c_z], c_x, c_y, M_DOWN): conn.send('You moved downstairs.\n') c_z = c_z - 1 c_x, c_y = findnode(levels[c_z], M_UP) else: conn.send('No stairs downward here.\n') if player_key: collapse -= 1 conn.send('Get out of here!\n') if collapse == 10: conn.send('The ceiling is about to collapse!\n') if collapse == 50: conn.send('Large pieces of debris fall from the ceiling.\n') if collapse == 100: conn.send('Hurry!\n') if collapse == 0: conn.send('The ceiling collapses and crushes you. You are dead.\n') conn.send('You are dead.\n') return
def handle_client(conn, addr): numpy.random.seed() NUM_LEVELS = 10 COMMAND_LIST = [ 'north', 'south', 'east', 'west', 'up', 'down', 'pickup', 'escape' ] conn.send('Please wait while the map loads...\n') levels = {} gr = graph() key_level = rand(NUM_LEVELS - 3, NUM_LEVELS - 1) door_level = 0 for i in range(NUM_LEVELS): levels[i] = maze(25, 25, 1, 1) add_graph(gr, levels[i], i) if i > 0: (x_d, y_d) = setnode(levels[i], M_DOWN) down = get_id(x_d, y_d, i) gr.add_edge((up, down)) if i < NUM_LEVELS - 1: (x_u, y_u) = setnode(levels[i], M_UP) up = get_id(x_u, y_u, i) if i == key_level: (x, y) = setnode(levels[i], M_KEY) key = get_id(x, y, i) if i == door_level: (x, y) = setnode(levels[i], M_DOOR) door = get_id(x, y, i) st, weights = shortest_path(gr, key) distance = weights[door] collapse = distance + 5 start_z = NUM_LEVELS / 2 (start_x, start_y) = emptynode(levels[start_z]) c_x = start_x c_y = start_y c_z = start_z player_key = False conn.send('Map loaded.\n') while True: node = levels[c_z][c_y, c_x] if node == M_UP: conn.send('There are stairs heading upward.\n') if node == M_DOWN: conn.send('There are stairs heading downward.\n') if node == M_KEY: conn.send('There is a key here.\n') if node == M_DOOR: conn.send('There is a locked door here.\n') data = conn.recv(1024).strip() commands = data.split('\n') for command in commands: if not command in COMMAND_LIST: conn.send( 'Invalid command: %s. Possible commands include are: %s\n' % (command, COMMAND_LIST)) continue if command == 'pickup': if player_key: conn.send('You already have the key.\n') else: if checknode(levels[c_z], c_x, c_y, M_KEY): conn.send('You picked up the key.\n') conn.send('The ceiling starts to collapse.\n') player_key = True else: conn.send('No key here.\n') elif command == 'escape': if checknode(levels[c_z], c_x, c_y, M_DOOR): if player_key: conn.send('YOU ESCAPED!\n') conn.send('Key: %s' % helpers.load_flag()) else: conn.send('You need the key to unlock the door.\n') else: conn.send('No door here.\n') elif command == 'north': if checknode(levels[c_z], c_x, c_y - 1, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved north.\n') c_y = c_y - 1 elif command == 'south': if checknode(levels[c_z], c_x, c_y + 1, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved south.\n') c_y = c_y + 1 elif command == 'west': if checknode(levels[c_z], c_x - 1, c_y, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved west.\n') c_x = c_x - 1 elif command == 'east': if checknode(levels[c_z], c_x + 1, c_y, M_WALL): conn.send('There is a wall there.\n') else: conn.send('You moved east.\n') c_x = c_x + 1 elif command == 'up': if checknode(levels[c_z], c_x, c_y, M_UP): conn.send('You moved upstairs.\n') c_z = c_z + 1 c_x, c_y = findnode(levels[c_z], M_DOWN) else: conn.send('No stairs upward here.\n') elif command == 'down': if checknode(levels[c_z], c_x, c_y, M_DOWN): conn.send('You moved downstairs.\n') c_z = c_z - 1 c_x, c_y = findnode(levels[c_z], M_UP) else: conn.send('No stairs downward here.\n') if player_key: collapse -= 1 conn.send('Get out of here!\n') if collapse == 10: conn.send('The ceiling is about to collapse!\n') if collapse == 50: conn.send('Large pieces of debris fall from the ceiling.\n') if collapse == 100: conn.send('Hurry!\n') if collapse == 0: conn.send( 'The ceiling collapses and crushes you. You are dead.\n') conn.send('You are dead.\n') return