Files
netsec/week06/hard/client.py
2024-12-10 00:51:04 +09:00

75 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import random
import socket
# Kerckhoffs principle for the win
# here are all the crypto primitives Alice and Bob are using
from insecurelib import KDRV256, HMAC, encrypt, decrypt
# Fill in the right target here
HOST = 'netsec.net.in.tum.de'
PORT1 = 20206
PORT2 = 20306
# note the numbers you encounter may be small for demonstration purposes.
# Anyway, please do NOT brute force.
# debug the data sent over the secure channel
# we don't know the key, there is not much to debug here, ...
def debug_secure_channel(s1, s2, data: str):
data = data.rstrip('\n') # remove trailing newline
if len(data) >= 1024:
print(f"from {s1} to {s2}: '{data[:1024]}...'")
else:
print(f"from {s1} to {s2}: '{data}...'")
iv, ciphertext, mac = data.split(';')
assert len(iv) == 16 * 2 # a hexlified byte is two bytes long, the IV should be 16 bytes
assert (
len(ciphertext) % (16 * 2) == 0
) # a hexlified byte is two bytes long, AES block size is 128 bit (16 byte)
assert (
len(mac) == 16 * 2
) # a quite short MAC. Hint: you still don't want to brute force it!
def main():
# We connect to Alice and Bob and relay their messages.
# They send all their communication over us. How convenient :-)
# Dolev-Yao attacker model without any low-level effort.
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect((HOST, PORT1))
s1f = s1.makefile('rw') # file abstraction for the sockets
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((HOST, PORT2))
s2f = s2.makefile('rw')
# A -> B: p,g,X
data = s1f.readline().rstrip('\n')
print(f"from s1 to s2: '{data}'")
p, g, X = map(int, data.split(','))
p = int(p)
g = int(g)
X = int(X)
print("p: {} g: {} X: {}".format(p, g, X))
# TODO: get the flag
s2f.write(data)
s2f.flush()
# Receive answer (second step of DH)
data = s2f.readline().rstrip('\n')
print("from s2 to s1: `{}'".format(data))
s1f.close()
s2f.close()
s1.close()
s2.close()
if __name__ == '__main__':
main()