flip-flop

AES-CBC加密模式

题目

源码如下:

import os
from Crypto.Cipher import AES
from secret import FLAG
auth_major_key = os.urandom(16)

BANNER = """
Login as admin to get the flag ! 
"""

MENU = """
Enter your choice
[1] Create NewStarCTF Account
[2] Create Admin Account
[3] Login
[4] Exit
"""

print(BANNER)

while True:
    print(MENU)

    option = int(input('> '))
    if option == 1:
        auth_pt = b'NewStarCTFer____'
        user_key = os.urandom(16)
        cipher = AES.new(auth_major_key, AES.MODE_CBC, user_key)
        code = cipher.encrypt(auth_pt)
        print(f'here is your authcode: {user_key.hex() + code.hex()}')
    elif option == 2:
        print('GET OUT !!!!!!')
    elif option == 3:
        authcode = input('Enter your authcode > ')
        user_key = bytes.fromhex(authcode)[:16]
        code = bytes.fromhex(authcode)[16:]
        cipher = AES.new(auth_major_key, AES.MODE_CBC, user_key)
        auth_pt = cipher.decrypt(code)
        if auth_pt == b'AdminAdmin______':
            print(FLAG)
        elif auth_pt == b'NewStarCTFer____':
            print('Have fun!!')
        else:
            print('Who are you?')
    elif option == 4:
        print('ByeBye')
        exit(0)
    else:
        print("WTF")

题目分析

此题是通过注册得到的用户账户的密文,然后输入管理员账户对应的密文,才能得到flag。

对用户账户和管理员账户的加密解密均采用AES-CBC模式,但是本题仅有一组明文,iv仅作为偏移量,也就是本题中的user_key。

这里先来说一下我最一开始的思路,由于user_key每次是随机产生的,但是一次程序的运行当中是不变的。由于密文由两部分组成,前半部分为user_key.hex(),后半部分为code.hex(),所以只要构造出admin对应的code即可,那么就需要AES加密当中的key,所以就需要构造一个key来产生出admin对应的code。查询了很多资料发现都难以实现。于是……

那为什么不换一下思路?不能构造code,能不能构造iv?

先来看一下加密方式:

C = encrypt(M ^ iv)

M = decrypt(C)^iv

对应一下本题当中的NewStarCTFer和admin: b'NewStarCTFer____' = decrypt(code) ^ iv1

b'AdminAdmin______' = decrypt(code) ^ iv2

因此可求出admin对应的iv2,随后拼接上普通用户的后半段code即可。

EXP

from Crypto.Util.number import *
authcode= "4f72b2079005a15e30efb3febea8cda266f4c3b47147e79167c3cde81ca84a4b"
n=bytes_to_long(b'NewStarCTFer____')
a=bytes_to_long(b'AdminAdmin______')
iv1= int(authcode[:len(authcode)//2],16)
code = authcode[len(authcode)//2:]
decode = iv1 ^ n
iv2 = a ^ decode
print((hex(iv2)+code)[2:])
#flag{filp_the_word!!!!!!!!}