test: update files

This commit is contained in:
2023-11-01 11:01:04 +08:00
parent e27570e3fe
commit 81f6dae484
6 changed files with 42 additions and 3 deletions

View File

@@ -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)

View File

@@ -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" ]

View File

@@ -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

1
crypto/easy_dhke/flag.py Normal file
View File

@@ -0,0 +1 @@
flag = b"0xFA{DHKE_is_a_simple_Discrete_logarithm_Problem}"

View File

@@ -0,0 +1 @@
pycryptodome

View File

@@ -0,0 +1,9 @@
FROM python:3.11
COPY easy_pow.py /app/
WORKDIR /app
EXPOSE 10001
CMD [ "python", "easy_pow.py" ]