From 81f6dae48405d9b243addcf39aed0928e9e62b90 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Wed, 1 Nov 2023 11:01:04 +0800 Subject: [PATCH] test: update files --- crypto/easy_dhke/crack.py | 1 + crypto/easy_dhke/dockerfile | 15 +++++++++++++++ crypto/easy_dhke/easy_dhke.py | 18 +++++++++++++++--- crypto/easy_dhke/flag.py | 1 + crypto/easy_dhke/requirements.txt | 1 + crypto/easy_pow/dockerfile | 9 +++++++++ 6 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 crypto/easy_dhke/dockerfile create mode 100644 crypto/easy_dhke/flag.py create mode 100644 crypto/easy_dhke/requirements.txt create mode 100644 crypto/easy_pow/dockerfile diff --git a/crypto/easy_dhke/crack.py b/crypto/easy_dhke/crack.py index 1a6af34..535eceb 100644 --- a/crypto/easy_dhke/crack.py +++ b/crypto/easy_dhke/crack.py @@ -7,6 +7,7 @@ from Crypto.Util.Padding import pad,unpad context.log_level = 'debug' conn = remote("localhost",10001) +# conn = remote("172.20.14.117", 15503) def encrypt(plain_text:bytes, key:bytes)->bytes: cipher = AES.new(key, AES.MODE_ECB) diff --git a/crypto/easy_dhke/dockerfile b/crypto/easy_dhke/dockerfile new file mode 100644 index 0000000..5597e53 --- /dev/null +++ b/crypto/easy_dhke/dockerfile @@ -0,0 +1,15 @@ +FROM python:3.11 + +COPY requirements.txt /app/ + +COPY flag.py /app/ + +COPY easy_dhke.py /app/ + +WORKDIR /app + +RUN pip install -r requirements.txt -i https://mirrors.ustc.edu.cn/pypi/web/simple + +EXPOSE 10001 + +CMD [ "python", "easy_dhke.py" ] \ No newline at end of file diff --git a/crypto/easy_dhke/easy_dhke.py b/crypto/easy_dhke/easy_dhke.py index ae2167f..5bcfa03 100644 --- a/crypto/easy_dhke/easy_dhke.py +++ b/crypto/easy_dhke/easy_dhke.py @@ -6,6 +6,7 @@ import signal import string import random import os +from flag import flag class Task(socketserver.BaseRequestHandler): @@ -32,17 +33,28 @@ class Task(socketserver.BaseRequestHandler): return self._recvall() def dhke(self): + # p is a large prime number used for modulo operations in the Diffie-Hellman key exchange p = 327824197795087630552811243153730025469 + # g is the base used for generating public keys in the Diffie-Hellman key exchange g = 5 + # alice is Alice's private key, an integer chosen by Alice alice = 22751 + # bob is Bob's private key, an integer chosen by Bob bob = 39494 + # Bob calculates his public key as g^bob mod p and assigns it to Bob (uppercase to distinguish from private key) Bob = pow(g, bob, p) + # The shared secret key is calculated by Alice using Bob's public key, raised to the power of Alice's private key mod p key = long_to_bytes(pow(Bob, alice, p)) + # The random seed is initialized with 8 bytes of random data from the OS's cryptographically secure random generator random.seed(os.urandom(8)) + # secret is a random string consisting of 20 alphanumeric characters, used as the secret message secret = ''.join( [random.choice(string.ascii_letters+string.digits) for _ in range(20)]) + self.send(b"[+] Alice said :") - self.send(self.encrypt(secret.encode(),key)) + + self.send(self.encrypt(secret.encode(), key)) + message = self.recv(b"[+] Now tell me what are they talking about: ") if message != secret.encode(): return False @@ -51,8 +63,10 @@ class Task(socketserver.BaseRequestHandler): hacked = self.recv(b"[+] Tell me the cipher:") if self.decrypt(hacked, key) != b"HackedBy0xfa": return False + # If all checks pass, return True indicating the exchange was successful and not intercepted return True + def encrypt(self, plain_text:bytes, key:bytes)->bytes: cipher = AES.new(key, AES.MODE_ECB) cipher_text = cipher.encrypt(pad(plain_text, AES.block_size)) @@ -82,8 +96,6 @@ class ForkedServer(socketserver.ForkingMixIn, socketserver.TCPServer): if __name__ == "__main__": - # flag = bytes(os.getenv("FLAG"),"utf-8") - flag = b"flag{coooloooool}" HOST, PORT = '0.0.0.0', 10001 server = ForkedServer((HOST, PORT), Task) server.allow_reuse_address = True diff --git a/crypto/easy_dhke/flag.py b/crypto/easy_dhke/flag.py new file mode 100644 index 0000000..d981d68 --- /dev/null +++ b/crypto/easy_dhke/flag.py @@ -0,0 +1 @@ +flag = b"0xFA{DHKE_is_a_simple_Discrete_logarithm_Problem}" \ No newline at end of file diff --git a/crypto/easy_dhke/requirements.txt b/crypto/easy_dhke/requirements.txt new file mode 100644 index 0000000..c21b6ec --- /dev/null +++ b/crypto/easy_dhke/requirements.txt @@ -0,0 +1 @@ +pycryptodome \ No newline at end of file diff --git a/crypto/easy_pow/dockerfile b/crypto/easy_pow/dockerfile new file mode 100644 index 0000000..3256a7e --- /dev/null +++ b/crypto/easy_pow/dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11 + +COPY easy_pow.py /app/ + +WORKDIR /app + +EXPOSE 10001 + +CMD [ "python", "easy_pow.py" ] \ No newline at end of file