Challenges Crypto esay

211

Baby Time Capsule

description

​ Qubit Enterprises 是一家新成立的公司,吹嘘其独有的量子位稳定方法。他们希望能在未来 10 年内制造出一台能计算 RSA-1024 数字的量子计算机。作为促销活动,他们正在发放 "时间胶囊",里面装着用 1024 位 RSA 加密的未来信息。他们可能是伟大的工程师,但肯定不是密码学家,你能找到不用等待他们的未来机器就能读取信息的方法吗?

solution

class TimeCapsule():

    def __init__(self, msg):
        self.msg = msg
        self.bit_size = 1024
        self.e = 5

    def _get_new_pubkey(self):
        while True:
            p = getPrime(self.bit_size // 2)
            q = getPrime(self.bit_size // 2)
            n = p * q
            phi = (p - 1) * (q - 1)
            try:
                pow(self.e, -1, phi)
                break
            except ValueError:
                pass

        return n, self.e

    def get_new_time_capsule(self):
        n, e = self._get_new_pubkey()
        m = bytes_to_long(self.msg)
        m = pow(m, e, n)

        return {"time_capsule": f"{m:X}", "pubkey": [f"{n:X}", f"{e:X}"]}

​ 观察代码我们可以发现是标准的RSA,其中的me是不变的,那我们就可以考虑使用CRT进行求解

exp

# SAGE
from pwn import *
from Crypto.Util.number import *
from gmpy2 import *

connect = remote(URL, Port)
e = 5
n, c = [], []
for _ in range(5):
    connect.sendlineafter(b'Welcome to Qubit Enterprises. Would you like your own time capsule? (Y/n) ', b'Y')
    info = connect.recvuntil(b']}').decode()
    c.append(int(info[info.find('capsule": "') + 11:info.find('", "pubkey')], 16))
    n.append(int(info[info.find('"pubkey":') + 12:info.find('", "5"]}')], 16))

me = crt(c,n)
m = iroot(me,e)[0]
print(long_to_bytes(m))

The Last Dance

description

​ 要想跻身伯福特帝国的上流社会,你必须参加每年一度在高等法院举行的恰恰舞会。你不知道,在受邀的众多贵族中,你会发现一名被烧死的敌方间谍。你的目标很快变成了抓住他,在他的酒里放了点东西后,你成功地抓住了他。在你的情报机构的审讯室里度过了许多小时,你最终了解到了关于敌方情报机构秘密通讯的重要信息。你能用学到的知识解密其余的信息吗?

solution

​ 可以发现这道题使用了chacha20的一个算法,iv被使用了两次,因此两次加密的明文和密文之间就存在如下关系

plaintext1 ^ plaintext2 = cipertext1 ^ cipertext2  

​ 其中plaintext1message,我们可以通过计算得到plaintext2,也就是FLAG

FLAG = encrypted_message ^ encrypted_flag ^ message

exp

from pwn import *

message = b"Our counter agencies have intercepted your messages and a lot of your agent's identities have been exposed. In a matter of days all of them will be captured"
encrypted_message = bytes.fromhex('7aa34395a258f5893e3db1822139b8c1f04cfab9d757b9b9cca57e1df33d093f07c7f06e06bb6293676f9060a838ea138b6bc9f20b08afeb73120506e2ce7b9b9dcd9e4a421584cfaba2481132dfbdf4216e98e3facec9ba199ca3a97641e9ca9782868d0222a1d7c0d3119b867edaf2e72e2a6f7d344df39a14edc39cb6f960944ddac2aaef324827c36cba67dcb76b22119b43881a3f1262752990')
encrypted_flag = bytes.fromhex('7d8273ceb459e4d4386df4e32e1aecc1aa7aaafda50cb982f6c62623cf6b29693d86b15457aa76ac7e2eef6cf814ae3a8d39c7')

flag = xor(encrypted_message, encrypted_flag, message)
print(flag)

Flippin Bank

description

​ 世界银行受到攻击黑客找到了入侵方法,并将管理员锁在外面。然而,入侵者的 netcat 身份验证并不完全安全。你能帮助管理员登录吗?

solution

​ 观察代码可以发现这道题是AES-CBC,题目需要我们输入账密,代码会对'logged_username=' + user + '&password=' + passwd进行加密并输出,而我们所要做的就是输入一个加密代码让其解密,并得到admin&password=g0d在其解密后的明文里。既然没有keyiv那我们就只能从解密过程上下手了。

CBC_decrypt

​ 在上面的解密过程中,Plaintext1logged_username=而解密是需要第二块Plaintext2admin&password=g,我们可以发现Plaintext2Ciphertext1Text1(Ciphertext2经过block cipher decryption)异或后得到的,其中Text1我们无法控制但Ciphertext1是可以控制的,我们通过对Ciphertext1的控制可以实现Plaintext2符合要求。

username: abcde
abcde's password: g0ld3n_b0y
########################################################################
#                  Welcome to the Bank of the World                    #
#             All connections are monitored and recorded               #
#      Disconnect IMMEDIATELY if you are not an authorized user!       #
########################################################################
Leaked ciphertext: 3a5541e1862d553f9f42e95e187914e6797e8b5f9238331e3a9414b15f693eac57fdbf7b1abeabe790c3cfef2e940ef6

在上面我们加密的信息为logged_username=abcde&password=g0ld3n_b0y,其中Plaintext2abcde&password=g而我们的目的就是让解密后的Plaintext2admin&password=gText1=Ciphertext1\oplus Plaintext2,有了Test1我们就可以更改Ciphertext1

exp

C1_o = 0x3a5541e1862d553f9f42e95e187914e6
P2_o = bytes_to_long(b'abcde&password=g')
P2_t = bytes_to_long(b'admin&password=g')

C1_t = hex(C1 ^ P2_o ^ P2_t)

# C1_t用于替换Ciphertext1
print(C1_t)

**Notes:**但对于这道题有些许疑惑,题目在其中不让我们输入为admin&password=g0ld3n_b0y但可以输入为admin&password=g0ld3n这样不仅可以避免被检测,而且题目加密出来的Leaked ciphertext也是可以直接使用,在本地按照题目的加解密也是可以正确运行的,但事实是无法使用,可能出题人不想让我们这么做。

xorxorxor

description

​ 有了 XOR,谁还需要 AES?

solution

​ 就是XOR

exp

from os import *
from Crypto.Util.number import *

Flag = long_to_bytes(0x134af6e1297bc4a96f6a87fe046684e8047084ee046d84c5282dd7ef292dc9)
flag = b'HTB{'

key = b''
for i in range(4):
    key += long_to_bytes(Flag[i] ^ flag[i])

for i in range(4, len(Flag)):
    flag += long_to_bytes(Flag[i] ^ key[i % len(key)])

print(flag)

RLotto

description

​ 准备好赢彩票了吗?猜随机乐透号码。现在是您成为百万富翁的时候了。

solution

​ 这道题主要是让我们猜测乐透的后五位,看起来是随机的我们并不能一次猜出所有的数字,但有一点就是python中的随机数是伪随机数,当我们有种子后我们就能得到后面的所谓随机数,这道题的种子是时间

exp

from pwn import *
from time import *

extracted = []
next_five = []

connect = remote('167.172.62.51', 32708)

info = connect.recvuntil(b'Put here the next 5 numbers:')
NowTime = int(time())
TrueExtracted = info[len(info) - info[::-1].find(b' :NOITCARTXE'):info.find(b' \r\n')].decode().split(' ')


while TrueExtracted != extracted:
    extracted = []
    NowTime -= 1
    random.seed(NowTime)
    while len(extracted) < 5:
        r = random.randint(1, 90)
        if (r not in extracted):
            extracted.append(str(r))

while len(next_five) < 5:
    r = random.randint(1, 90)
    if (r not in next_five):
        next_five.append(r)

sendinfo = ''
for i in next_five:
    sendinfo += str(i) + ' '

connect.sendline(sendinfo[:-1].encode())

print(connect.recvuntil(b'}'))

Lost Modulus

description

​ 我用 RSA 加密了一条密文,但丢失了模数。你能帮我恢复吗?

solution

​ 我们查看发现c907bit远小于n,所以c=m^{e}

exp

from Crypto.Util.number import *
from gmpy2 import *

c = FLAG
e = 3

print(long_to_bytes(iroot(c,e)[0]))

RSAisEasy

description

​ 我想这是安全的...对吗?

solution

n_{1}*E+n_{2}=(q*p)*E+q*z=q*(p*E+z)

​ 经过化简,我们就可以发现q=\gcd(n_{1},(n1 * E) + n2),知道q我们就开始求z,在p*E+z中我们只需要模p即可得到z

exp

from Crypto.Util.number import *
from gmpy2 import *


n1 = 
c1 = 
c2 = 
# (n1 * E) + n2
n1En2 = 
e = 0x10001

q = gcd(n1En2,n1)
p = n1 //q
z = n1En2//q%p

n2 = q*z
phi1 = (p-1)*(q-1)
phi2 = (q-1)*(z-1)
d1 = invert(e,phi1)
d2 = invert(e,phi2)
print(long_to_bytes(pow(c1,d1,n1)).decode()+long_to_bytes(pow(c2,d2,n2)).decode())

Protein Cookies

description

​ 又是一天对着镜子大展肌肉,却仍然对自己的身材形象不满意。在肾上腺素和肌酸的刺激下,你唯一缺少的就是一个好的锻炼计划。我们听说最好的健身计划来自 Swole Eagle 健身房,但他们已经关闭了注册,因为美国食品和药物管理局(FDA)正在追捕他们,因为他们有一个令健美运动员痛恨的秘密。你有打破常规的胃口,烤箱里满满的蛋白质饼干也准备好成为一天的运动后甜点,你必须进行正确的锻炼,才能不浪费任何宝贵的肌肉增长潜力。渗入健身房会员资格的入口,获得你知道自己应得的锻炼计划!🍪

solution

exp

Quantum-Safe

description

​ 我听说 Shor 的算法会对 RSA 做各种可怕的事情,所以我决定采用超现代的方法,用酷炫的新 maffs 来保护我的旗帜!

solution

exp

Embryonic Plant

description

​ 在一个末世世界里,你是一位有抱负的植物学家,毕生致力于研究植物及其基因操作,是研究植物胚胎阶段的专家。在你环游世界,希望找到人工培育植物的方法,以抵御地球残酷的环境时,你在一个看似荒凉的地方发现了一个新物种。你知道是时候揭开大自然的秘密了。5 棵植物够吗?

solution

exp

Rhome

description

​ 我收到了这封可爱的信,但寄信人在信上贴了封条。她说,只有在我们家罗姆才能打开。

solution

exp

Secure Signing

description

​ 您能破解我们的超级安全签名 Oracle 吗?

solution

exp