Information Security Study

[crypto][Hspace CTF 2023] Insecure Mode 1 본문

CTF

[crypto][Hspace CTF 2023] Insecure Mode 1

gayeon_ 2023. 9. 3. 23:34

Insecure Mode 1 문제이다.

 

 

 

 

#!/usr/bin/python3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os
import signal

def timeout(a1, a2):
    print("BYE ~")
    exit(-1)


class InSecureCipher:
    def __init__(self, key):
        self.size = 16
        assert len(key) == self.size
        self.key = key
        self.cipher = AES.new(key, 1)

    def encrypt(self, pt):
        return self.cipher.encrypt(pad(pt, self.size))

    def decrypt(self, ct):
        assert len(ct) % self.size == 0
        return unpad(self.cipher.decrypt(ct), self.size)


def main():
    signal.signal(signal.SIGALRM, timeout)
    signal.alarm(30)

    ISC = InSecureCipher(os.urandom(16))
    secret = os.urandom(64)
    enc_secret = ISC.encrypt(secret)

    print(f"Encrypted secret : {enc_secret.hex()}")
    token = 4
    print(f'You have only 4 tokens to use my oracle.')

    for i in range(token):
        menu = input("inp > ")
        if menu == 'enc':
            pt = bytes.fromhex(input('pt(hex) > '))
            print(ISC.encrypt(pt).hex())
        elif menu == 'dec':
            ct = bytes.fromhex(input('ct(hex) > '))
            if ct == enc_secret:
                print("No. you cant do that.")
            else:
                print(ISC.decrypt(ct).hex())
        
        elif menu == 'secret':
            sec = bytes.fromhex(input('secret(hex) > '))
            if sec == secret:
                print("Congratulation!")
                with open('/home/ctf/flag.txt','r') as f:
                    print(f.read())
                exit(0)
            else:
                print("who are you?")
                exit(-1)
        else:
            print('Unexpected input')
            exit(-1)


if __name__ == "__main__":
    main()

압축 파일에는 파이썬 파일 하나만 존재했다.

import 패키지 중 signal은 윈도우에서 사용이 불가능하다 해서

리눅스에서 파일을 실행했다.

파이썬 파일을 실행하면 토큰 입력 값을 menu에 저장한다.

 

init 함수를 보면 16자리의 plain text를 입력할 수 있는 것 같다.

 

 

이 부분을 보면

menu가 secret인 경우 secret의 hex값을 입력할 수 있는 것 같다.

이 입력 값이 sec와 동일하면 플래그를 얻을 수 있다.

 

 

실행해보니 암호문이 나오고 올바른 토큰을 입력하는 방식으로 동작하는 것 같다.

우선 enc, dec, secret이 아닌 아무 값이나 넣어보았다.

몇 번 실행해보니 Encrypted secret은 계속 바뀌었다.

 

 

 

 

16자리의 숫자를 입력하니 암호화가 되었다.

 

 

 

생성된 암호문을 dec 메뉴에 입력하니 잘 복호화되었다.

 

 

 

 

sec과 secret을 비교하는 부분에서 secret 대신 pt로 바꿔보았다.

 

 

 

pt를 1234567890123456으로 입력하고

secret도 동일하게 입력했지만 "Congratulation!" 문자열과 플래그를 볼 수 없었다.

 

 

 

secret이 코드 상단에 정의되어있어

secret을 출력시키고 붙여넣기 해보려했지만..

print문조차 실행되지 않았다.

 

 

 

 

 

'CTF' 카테고리의 다른 글

[pwn][ASIS CTF 2023] hipwn  (0) 2023.09.23
[crypto][Pointer Overflow CTF 2023] Unquestioned and Unrestrained  (0) 2023.09.23
[Rev][BxMCTF 2023] MGCTF '20 October  (0) 2023.05.30
[Rev][TJCTF 2023] wtmoo  (0) 2023.05.30
[Rev][RITSEC CTF] Cats At Play  (0) 2023.04.01