diff --git a/CrewCTF2024-noisy-encryption/description.yml b/CrewCTF2024-noisy-encryption/description.yml new file mode 100644 index 0000000..d807134 --- /dev/null +++ b/CrewCTF2024-noisy-encryption/description.yml @@ -0,0 +1,9 @@ +name: Noisy encryption +authors: + - KLPP +original_ctf: 'CrewCTF 2024' +year: 2024 +description: >- + I can encrypt your messages, however, I will reveal just a little bit of information ... +base64_flag: 'Y3Jld3tEMWN0MW9uNHJ5XzR0dGFja3M/XzR0X3RoMXNfVDFtM18wZl95MzRyX0E3XzdoMTVfdGltM18wZl9kNHlfMW5fN2gxc19QNHJ0XzBmX0MwdW50cnk/fQ==' +flag_format: 'crew{FLAG}' diff --git a/CrewCTF2024-noisy-encryption/release_files/server.py b/CrewCTF2024-noisy-encryption/release_files/server.py new file mode 100644 index 0000000..2d3a3f4 --- /dev/null +++ b/CrewCTF2024-noisy-encryption/release_files/server.py @@ -0,0 +1,47 @@ + +import secrets +import sympy + +FLAG="crew{**REDACTED**}" +BITS=512 +LIM=pow(2,BITS) + +while True: + while (not sympy.isprime(p:=secrets.randbelow(LIM//2)+LIM//2)) or (p-1)%3==0: + pass + + while (not sympy.isprime(q:=secrets.randbelow(LIM//2)+LIM//2)) or (q-1)%3==0: + pass + + n=p*q + if n>pow(2,1023): + break + +phi=(p-1)*(q-1) +e=3 +Secret=secrets.randbelow(LIM) +d=pow(e,-1,phi) +sig=pow(Secret,d,n) + +print("The signature is: "+str(sig)) + +def hamming_weight(x): + return sum([int(y) for y in bin(x)[2:]]) + + +while True: + print("I can encrypt anything for you! But the bits may get messy") + msg=input() + if msg=="guess": + print("Do you know the secret?") + msg=int(input()) + if msg==Secret: + print("You sure do! Here is your prize:") + print(FLAG) + exit(0) + else: + print("Wrong answer!") + exit(0) + msg=int(msg) + enc=pow(msg,3,n) + print(hamming_weight(enc)%2) diff --git a/CrewCTF2024-noisy-encryption/server_files/Dockerfile b/CrewCTF2024-noisy-encryption/server_files/Dockerfile new file mode 100644 index 0000000..31a6530 --- /dev/null +++ b/CrewCTF2024-noisy-encryption/server_files/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.10-slim-buster +RUN apt update -y && apt install -y xinetd && apt clean && rm -rf /var/lib/apt/lists/* + +RUN useradd -d /home/ctf -m ctf +USER ctf + +RUN pip3 install --no-cache-dir sympy==1.13.3 + +COPY xinetd /etc/xinetd.d/xinetd +COPY server.py /home/ctf/server.py + +CMD ["/usr/sbin/xinetd", "-dontfork"] diff --git a/CrewCTF2024-noisy-encryption/server_files/server.py b/CrewCTF2024-noisy-encryption/server_files/server.py new file mode 100644 index 0000000..9a72d3c --- /dev/null +++ b/CrewCTF2024-noisy-encryption/server_files/server.py @@ -0,0 +1,47 @@ + +import secrets +import sympy + +FLAG="crew{D1ct1on4ry_4ttacks?_4t_th1s_T1m3_0f_y34r_A7_7h15_tim3_0f_d4y_1n_7h1s_P4rt_0f_C0untry?}" +BITS=512 +LIM=pow(2,BITS) + +while True: + while (not sympy.isprime(p:=secrets.randbelow(LIM//2)+LIM//2)) or (p-1)%3==0: + pass + + while (not sympy.isprime(q:=secrets.randbelow(LIM//2)+LIM//2)) or (q-1)%3==0: + pass + + n=p*q + if n>pow(2,1023): + break + +phi=(p-1)*(q-1) +e=3 +Secret=secrets.randbelow(LIM) +d=pow(e,-1,phi) +sig=pow(Secret,d,n) + +print("The signature is: "+str(sig)) + +def hamming_weight(x): + return sum([int(y) for y in bin(x)[2:]]) + + +while True: + print("I can encrypt anything for you! But the bits may get messy") + msg=input() + if msg=="guess": + print("Do you know the secret?") + msg=int(input()) + if msg==Secret: + print("You sure do! Here is your prize:") + print(FLAG) + exit(0) + else: + print("Wrong answer!") + exit(0) + msg=int(msg) + enc=pow(msg,3,n) + print(hamming_weight(enc)%2) diff --git a/CrewCTF2024-noisy-encryption/server_files/xinetd b/CrewCTF2024-noisy-encryption/server_files/xinetd new file mode 100644 index 0000000..c52b566 --- /dev/null +++ b/CrewCTF2024-noisy-encryption/server_files/xinetd @@ -0,0 +1,19 @@ +service ctf +{ + disable = no + type = UNLISTED + wait = no + server = /usr/local/bin/python3 + server_args = /home/ctf/server.py + socket_type = stream + protocol = tcp + user = ctf + bind = 0.0.0.0 + port = 1337 + flags = REUSE + per_source = 2 + rlimit_cpu = 30 + log_type = FILE /home/ctf/log + log_on_success = HOST PID EXIT DURATION + log_on_failer = HOST +}