-1

I am attempting to complete a challenge that states:

We've noticed that the aliens are sending messages between their ships, we think they're using XOR to encrypt the messages, and we've intercepted a key.

Set up a server listening on ("localhost", 10000) to intercept one of the alien messages. When you do perform a bitwise XOR on the message with the key "attackthehumans" and then respond with the encrypted data.

Tip: Read the response to get the flag.

After some research, I was able to come up with the following code. However, when I run it in the challenge's code editor, the only feedback I receive is "Error trying to connect to your server and recieving message back."

import socket

# Function to xor strings
def xor_string(string):
    key = "attackthehumans"

    bit_key = ''.join(format(ord(i), 'b') for i in key)
    bit_data = ''.join(format(ord(i), 'b') for i in string)
    
    xor_string = str(0)

    for i in range(len(bit_key)):
        if bit_data[i] == bit_key[i]:
            xor_string = xor_string + str(0)
        else:
            xor_string = xor_string + str(1)
    
    return xor_string

# Sets up server on localhost and port 10000
print("Setting up server...")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 10000))

print("Server set up. Listening for connections...")
server.listen()

conn, addr = server.accept()
print("Connected by: {}".format(addr))

# Once connection is established, server receives data, XOR's it, sends it back, and
# waits for response to get the flag. 
with conn: 
    print("Receiving data from connection... ")
    data = conn.recv()
    data = data.decode()
    
    print("Data received: {}".format(data.decode()))
    
    xor_data = xor_string(data)
  
    conn.sendall(xor_data.encode())
    
    response = conn.recv()
    response = response.decode()
    
    print(response)

I am not sure what I'm doing wrong. I can't figure out if it's the socket or the xor_string function. Any help would be appreciated.

Benjamin
  • 1
  • 1
  • Just FYI, the string you receive from `socket.recv` is a bytes string. You can treat that as a list of integers. You don't have to `decode` it to Unicode, and you do NOT have to do a conversion to bits. Just something like `[k ^ c for k,c in zip(key,text)]`, although you'll have to handle having the text longer than the key. – Tim Roberts Jan 30 '22 at 02:18

1 Answers1

0

This should be enough to do your XORing:

def xor_string(string):
    key = b"attackthehumans"
    key = key * (len(string)//len(key)+1)
    res = bytes([k^s for (k,s) in zip(key,string)])
    return res

Then your main code becomes:

print("Receiving data from connection... ")
data = conn.recv()
print("Data received:", data)
xor_data = xor_string(data)
conn.sendall(xor_data)
print(conn.recv())
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thank you for your reply, but I'm still getting the same error message. Could I be doing something else wrong? – Benjamin Jan 30 '22 at 03:33
  • Are you actually seeing your "connection" message? Your socket processing is basically correct. If I run it here, it is certainly open for connections. – Tim Roberts Jan 30 '22 at 07:31
  • No. I've noticed that the code editor on the challenge doesn't display any output if it comes up with an error, i.e. the message mentioned earlier. When I comment out the section of code that establishes the connection, it does display the output up until that point. Could I be approaching the challenge incorrectly? When I run it outside of the challenge's code editor, it seems to be working. – Benjamin Jan 30 '22 at 20:53
  • It's impossible to tell. The error doesn't say whether the problem is in connecting, or in receiving the data back. As you say, it worked for me locally. – Tim Roberts Jan 31 '22 at 18:27